Skip to content
This repository was archived by the owner on Jan 17, 2021. It is now read-only.

Commit 18a7d71

Browse files
committed
game analysis csv logging
1 parent ff58be8 commit 18a7d71

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,6 @@ config_local.py
134134

135135
# ignore local todo files
136136
todo.txt
137+
138+
# ignore local analysis data
139+
.data

python-visualizer/config.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@
3434
from config_local import *
3535

3636
# game analysis paths
37-
GAME_ANALYSIS_SIZE_PATH = "../.data/size"
38-
GAME_ANALYSIS_TIME_PATH = "../.data/time"
37+
GAME_ANALYSIS_DIRECTORY_PATH = "../.data/"

python-visualizer/game_analysis.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
import json
33
import time
44
from datetime import datetime, timedelta
5-
5+
import os
66
import websockets
77
import requests
88
from csv import writer
99

1010
import config
1111

12+
13+
MAX_ROUNDS = 100
14+
15+
def append_to_csv(file_name: str, elements):
16+
with open(file_name, 'a+', newline='') as write:
17+
csv_writer = writer(write)
18+
csv_writer.writerow(elements)
19+
20+
1221
client_time = datetime.now()
1322
time_request_result = requests.get(config.TIME_URL)
1423

@@ -28,31 +37,39 @@
2837
def transform_to_server_time(time_value: datetime):
2938
return time_value + time_offset
3039

40+
3141
async def play():
42+
43+
if not os.path.exists(config.GAME_ANALYSIS_DIRECTORY_PATH):
44+
os.makedirs(config.GAME_ANALYSIS_DIRECTORY_PATH)
45+
46+
board_size_file_name = config.GAME_ANALYSIS_DIRECTORY_PATH + "sizes" + ".csv"
47+
time_file_name = config.GAME_ANALYSIS_DIRECTORY_PATH + "times-" \
48+
+ datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".csv"
49+
3250
url = config.URL
3351
key = config.API_KEY
3452

3553
async with websockets.connect(f"{url}?key={key}") as websocket:
36-
started = False
54+
rounds = 0
3755
print("Waiting for initial state...", flush=True)
3856

3957
while True:
4058
state_json = await websocket.recv()
4159
state = json.loads(state_json)
4260

43-
if not started:
61+
if rounds == 0:
4462
print(f"board -- width: %d -- height: %d" % (state["width"], state["height"]))
45-
started = True
63+
append_to_csv(board_size_file_name, [state["width"], state["height"]])
64+
rounds += 1
4665

47-
if not state["running"]:
66+
if not state["running"] or rounds >= MAX_ROUNDS:
4867
break
4968

5069
deadline = datetime.strptime(state["deadline"], '%Y-%m-%dT%H:%M:%SZ')
5170
available_millis = (deadline - transform_to_server_time(datetime.now())) / timedelta(milliseconds=1)
52-
5371
print(f"\tavailable milliseconds: %d" % available_millis)
54-
55-
time.sleep(0.5)
72+
append_to_csv(time_file_name, [available_millis])
5673

5774
action_json = json.dumps({"action": "change_nothing"})
5875
await websocket.send(action_json)
@@ -61,5 +78,6 @@ async def play():
6178
while True:
6279
try:
6380
asyncio.get_event_loop().run_until_complete(play())
64-
except Exception:
81+
except Exception as e:
82+
print(e)
6583
print("Connection was interrupted!")

0 commit comments

Comments
 (0)