Skip to content

Commit 5d17d0c

Browse files
committed
WIP: Adding publish mode and handling to pelita tournament
1 parent f15ee85 commit 5d17d0c

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

pelita/game.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import subprocess
77
import sys
88
import time
9+
import uuid
910
from warnings import warn
1011

1112
from . import layout
@@ -326,6 +327,9 @@ def setup_game(team_specs, *, layout_dict, max_rounds=300, rng=None,
326327
# Initialize the game state.
327328

328329
game_state = dict(
330+
#: UUID
331+
game_uuid=str(uuid.uuid4()),
332+
329333
### The layout attributes
330334
#: Walls. Set of (int, int)
331335
walls=set(layout_dict['walls']),

pelita/scripts/pelita_tournament.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ def setup():
163163
del config["bonusmatch"]
164164
break
165165

166+
res = input_choice("Should the web-viewer be activated? (publishes to tcp://127.0.0.1:5559) (y/n)", [], "yn")
167+
if res == "y":
168+
config['publish'] = "tcp://127.0.0.1:5559"
169+
elif res == "n":
170+
config['publish'] = None
171+
166172
print("Specify the folder where we should look for teams (or none)")
167173
folder = input().strip()
168174
if folder:
@@ -294,6 +300,7 @@ def escape(s):
294300
winner = tournament.play_round2(config, rr_ranking, state, rng)
295301

296302
config.print('The winner of the %s Pelita tournament is...' % config.location, wait=2, end=" ")
303+
config.print()
297304
config.print('{team_group}: {team_name}. Congratulations'.format(
298305
team_group=config.team_group(winner),
299306
team_name=config.team_name(winner)), wait=2)

pelita/tournament/__init__.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def run_and_terminate_process(args, **kwargs):
9393
p.kill()
9494

9595

96-
def call_pelita(team_specs, *, rounds, size, viewer, seed, team_infos=None, write_replay=False, store_output=False):
96+
def call_pelita(team_specs, *, rounds, size, viewer, seed, publish=None,
97+
team_infos=None, write_replay=False, store_output=False):
9798
""" Starts a new process with the given command line arguments and waits until finished.
9899
99100
Returns
@@ -129,6 +130,7 @@ def call_pelita(team_specs, *, rounds, size, viewer, seed, team_infos=None, writ
129130
size = ['--size', size] if size else []
130131
viewer = ['--' + viewer] if viewer else []
131132
seed = ['--seed', seed] if seed else []
133+
publish = ['--publish', publish] if publish else []
132134
write_replay = ['--write-replay', write_replay] if write_replay else []
133135
store_output = ['--store-output', store_output] if store_output else []
134136
append_blue = ['--append-blue', team_infos[0]] if team_infos[0] else []
@@ -137,6 +139,8 @@ def call_pelita(team_specs, *, rounds, size, viewer, seed, team_infos=None, writ
137139
cmd = [sys.executable, '-m', 'pelita.scripts.pelita_main',
138140
team1, team2,
139141
'--reply-to', reply_addr,
142+
'--stop-at', '0',
143+
*publish,
140144
*append_blue,
141145
*append_red,
142146
*rounds,
@@ -265,6 +269,7 @@ def __init__(self, config):
265269
self.size = config.get("size")
266270

267271
self.viewer = config.get("viewer")
272+
self.publish = config.get("publish")
268273
self.interactive = config.get("interactive")
269274
self.statefile = config.get("statefile")
270275

@@ -285,6 +290,13 @@ def __init__(self, config):
285290
self.tournament_log_folder = None
286291
self.tournament_log_file = None
287292

293+
if self.publish:
294+
ctx = zmq.Context()
295+
self.socket = ctx.socket(zmq.PUB)
296+
self.socket.connect(self.publish)
297+
else:
298+
self.socket = None
299+
288300
@property
289301
def team_ids(self):
290302
return self.teams.keys()
@@ -301,6 +313,15 @@ def team_name_group(self, team):
301313
def team_spec(self, team):
302314
return self.teams[team]["spec"]
303315

316+
def send_remote(self, action, data=None):
317+
if not self.socket:
318+
return
319+
if data is None:
320+
publish_string = {"__action__": action}
321+
else:
322+
publish_string = {"__action__": action, "__data__": data}
323+
self.socket.send_json(publish_string)
324+
304325
def _print(self, *args, **kwargs):
305326
print(*args, **kwargs)
306327
if self.tournament_log_file:
@@ -312,12 +333,15 @@ def print(self, *args, **kwargs):
312333
"""Speak while you print. To disable set speak=False.
313334
You need the program %s to be able to speak.
314335
Set wait=X to wait X seconds after speaking."""
336+
315337
if len(args) == 0:
338+
self.send_remote("SPEAK", " ".join(args))
316339
self._print()
317340
return
318341
stream = io.StringIO()
319342
wait = kwargs.pop('wait', 0.5)
320343
want_speak = kwargs.pop('speak', None)
344+
self.send_remote("SPEAK", " ".join(args))
321345
if (want_speak is False) or not self.speak:
322346
self._print(*args, **kwargs)
323347
else:
@@ -377,6 +401,11 @@ def input(self, str, values=None):
377401
except IndexError:
378402
pass
379403

404+
def init_tournament(self):
405+
self.send_remote("INIT")
406+
407+
def clear_page(self):
408+
self.send_remote("CLEAR")
380409

381410
def wait_for_keypress(self):
382411
if self.interactive:
@@ -419,7 +448,9 @@ def load(cls, config, filename):
419448

420449

421450
def present_teams(config):
451+
config.init_tournament()
422452
config.wait_for_keypress()
453+
config.clear_page()
423454
print("\33[H\33[2J") # clear the screen
424455

425456
greeting = config.greeting
@@ -448,8 +479,9 @@ def set_name(team):
448479
print(sys.stderr)
449480
raise
450481

451-
452-
def play_game_with_config(config, teams, rng, *, match_id=None):
482+
# TODO: Log tournament match cmdline
483+
def play_game_with_config(config: Config, teams, rng, *, match_id=None):
484+
config.clear_page()
453485
team1, team2 = teams
454486

455487
if config.tournament_log_folder:
@@ -474,6 +506,7 @@ def play_game_with_config(config, teams, rng, *, match_id=None):
474506
rounds=config.rounds,
475507
size=config.size,
476508
viewer=config.viewer,
509+
publish=config.publish,
477510
team_infos=team_infos,
478511
seed=seed,
479512
**log_kwargs)
@@ -503,6 +536,7 @@ def start_match(config, teams, rng, *, shuffle=False, match_id=None):
503536
config.print('Starting match: '+ config.team_name_group(team1)+' vs ' + config.team_name_group(team2))
504537
config.print()
505538
config.wait_for_keypress()
539+
config.clear_page()
506540

507541
(final_state, stdout, stderr) = play_game_with_config(config, teams, rng=rng, match_id=match_id)
508542
try:
@@ -621,6 +655,7 @@ def play_round1(config, state, rng):
621655
rr_played = state.round1["played"]
622656

623657
config.wait_for_keypress()
658+
config.clear_page()
624659
config.print()
625660
config.print("ROUND 1 (Everybody vs Everybody)")
626661
config.print('================================', speak=False)
@@ -650,6 +685,7 @@ def play_round1(config, state, rng):
650685
winner = start_match_with_replay(config, match, rng=rng, match_id=match_id)
651686
match_id.next_match()
652687
config.wait_for_keypress()
688+
config.clear_page()
653689

654690
if winner is False or winner is None:
655691
rr_played.append({ "match": match, "winner": False })
@@ -692,9 +728,11 @@ def recur_match_winner(match):
692728
def play_round2(config, teams, state, rng):
693729
"""Run the second round and return the name of the winning team.
694730
695-
teams is the list [group0, group1, ...] not the names of the agens, sorted
731+
teams is the list [group0, group1, ...] not the names of the agents, sorted
696732
by the result of the first round.
697733
"""
734+
config.wait_for_keypress()
735+
config.clear_page()
698736
config.print()
699737
config.print('ROUND 2 (K.O.)')
700738
config.print('==============', speak=False)
@@ -724,6 +762,7 @@ def play_round2(config, teams, state, rng):
724762
winner = start_deathmatch(config, t1_id, t2_id, rng=rng, match_id=match_id)
725763
match.winner = winner
726764

765+
config.clear_page()
727766
config.print(knockout_mode.print_knockout(last_match, config.team_name, highlight=[match]), speak=False)
728767

729768
state.round2["tournament"] = tournament
@@ -736,5 +775,6 @@ def play_round2(config, teams, state, rng):
736775
match_id.next_match()
737776

738777
config.wait_for_keypress()
778+
config.clear_page()
739779

740780
return last_match.winner

0 commit comments

Comments
 (0)