forked from mawoka-myblock/ClassQuiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate_players.py
59 lines (48 loc) · 1.69 KB
/
simulate_players.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import json
import random
import time
import socketio
NUMBER_OF_PLAYERS = 20
GAME_PIN = 35490014
quiz = [None]
def __main__():
arr_of_sio_instances = []
for i in range(NUMBER_OF_PLAYERS):
arr_of_sio_instances.append(socketio.Client())
def joined_game(data):
print("JOINED!!!!")
print(data)
quiz[0] = json.loads(data)
print(type(quiz[0]))
def set_question_number(data):
for i in arr_of_sio_instances:
# print(int(data))
# print("hi!", data)
random_num = random.randint(0, len(quiz[0]["questions"][int(data)]["answers"]) - 1)
print(random_num)
i.emit(
"submit_answer",
{
"question_index": int(data),
"answer": quiz[0]["questions"][int(data)]["answers"][random_num]["answer"],
},
)
print(i.sid)
time.sleep(0.03)
print("Everyone answered!")
for i in arr_of_sio_instances:
i.connect("http://localhost:8080/socket.io/")
arr_of_sio_instances[3].on("joined_game", joined_game)
arr_of_sio_instances[3].on("set_question_number", set_question_number)
for index, i in enumerate(arr_of_sio_instances):
i.emit("join_game", {"username": str(index), "game_pin": GAME_PIN, "captcha": None})
time.sleep(0.03)
print("Everyone joined!")
if __name__ == "__main__":
try:
__main__()
except KeyboardInterrupt:
exit(1)