-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_run.py
82 lines (71 loc) · 3.54 KB
/
client_run.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
client_run.py: run this file (using "python client_run.py") to participate in the TWIMLfest 2020 codenames competition
Dan Hilgart <[email protected]>
This file starts an async event loop and then adds a new task each X seconds to ping the server and get the current
status for the player. If the server is waiting for this player, it will call
TWIML_codenames_API_Client.query_and_respond() which in turn:
asks the server for the necessary inputs
calls the appropriate function from my_model.py
sends the outputs from that function back to the server
"""
import TWIML_codenames_API_Client
import asyncio
import time
import json
async def check_status_loop(active_games):
"""
The main loop that creates a new task every X seconds to find out whether anything is expected from the player
"""
while True:
loop.create_task(check_status(active_games))
await asyncio.sleep(1)
async def check_status(active_games):
"""
Asks the server what the current status is for this player. If the server is waiting for a query from the player,
adds a task to the loop to query and respond which then:
asks the server for the necessary inputs
calls the appropriate function from my_model.py
sends the outputs from that function back to the server
"""
# request the status from the server:
status = None
while status is None:
status = await TWIML_codenames_API_Client.check_status(player_id, player_key)
if status is None:
time.sleep(1) # do not execute any other calls to the server while waiting to retry check_status
if 'ERROR' in status.keys():
print(status)
else:
# Have any active games ended?
active_games = await TWIML_codenames_API_Client.check_for_ended_games(active_games, status['active games'].keys(),
player_id, player_key)
# Does the player have any active games?
if len(status['active games']) > 0:
# For each active game...
for game_data in status['active games'].values():
# is this a new game?
active_games = await TWIML_codenames_API_Client.check_if_new_game(active_games, game_data['game_id'])
# ...check if the server is waiting on this player:
if game_data['waiting on']['player_id'] == player_id:
# if so, call query_and_respond()
loop.create_task(TWIML_codenames_API_Client.query_and_respond(player_id=player_id,
player_key=player_key,
game_id=game_data['game_id'],
role=game_data['waiting on']['role']
))
if __name__ == "__main__":
# Load player_id and player_key from the myPlayerID-Key.txt file
PlayerID_Key = json.load(open('myPlayerID-Key.txt', 'r'))
player_id = int(PlayerID_Key['Player_ID'])
player_key = int(PlayerID_Key['Player_Key'])
active_games = []
# Create the async event loop
loop = asyncio.get_event_loop()
task = loop.create_task(check_status_loop(active_games))
# run the loop
try:
loop.run_until_complete(task)
except asyncio.CancelledError:
pass
finally:
loop.close()