forked from mawoka-myblock/ClassQuiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_players.py
66 lines (54 loc) · 1.75 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
60
61
62
63
64
65
66
# SPDX-FileCopyrightText: 2023 Marlon W (Mawoka)
#
# SPDX-License-Identifier: MPL-2.0
import random
import time
import sys
import socketio
NUMBER_OF_PLAYERS = 200
GAME_PIN = sys.argv[1]
q_index = 0
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!!!!")
def time_sync(data):
print("time sync")
for i in arr_of_sio_instances:
i.emit("echo_time_sync", data)
time.sleep(0.04)
def set_question_number(data):
print(data)
global q_index
for i in arr_of_sio_instances:
# print(int(data))
# print("hi!", data)
random_num = random.randint(0, len(data["question"]["answers"]) - 1)
print(random_num, q_index)
i.emit(
"submit_answer",
{
"question_index": q_index,
"answer": data["question"]["answers"][random_num]["answer"],
},
)
print(i.sid)
time.sleep(0.03)
q_index = +1
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)
arr_of_sio_instances[3].on("time_sync", time_sync)
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:
sys.exit(1)