Skip to content

Commit

Permalink
Fix printing agent moves to files
Browse files Browse the repository at this point in the history
  • Loading branch information
kestivvi committed May 21, 2024
1 parent 1cca79b commit f7639ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions coderone/dungeon/game_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ class FileRecorder(Recorder):
""" A game recording that saves the game into a file
"""
def __init__(self, file_name:str):
self.file = open(file_name, mode='wt')
self.buffer = []
self.file = file_name

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
if self.file:
self.file.close()
self.file = None
with open(self.file, 'wt') as f:
for msg in self.buffer:
f.write(str(msg))

def record(self, tick:int, event: GameEvent):
self.file.write(f"{tick}: ")

message = f"{tick}: "
if isinstance(event, GameSysAction):
self.file.write(f"{event.action.value} ")
self.file.write(jsonplus.dumps(event.payload))

elif isinstance(event, PlayerMove):
self.file.write(f"{event.pid} {event.action.value}")
message += f"{event.action.value} "
message += jsonplus.dumps(event.payload)

self.file.write("\n")
self.file.flush()
elif isinstance(event, PlayerMove):
message += f"{event.pid} {event.action.value}"

message += "\n"
self.buffer.append(message)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='coderone-challenge-dungeon',
version='0.1.7',
version='0.1.8',
description='Dungeons and data structures: Coder one AI Game Tournament',
url='https://github.com/gocoderone/dungeons-and-data-structures',
author='Ivan Ryabov',
Expand Down

0 comments on commit f7639ef

Please sign in to comment.