Toadofsky commited on
Commit
aaa3dd0
·
1 Parent(s): a6e662c

Add wait command

Browse files
Files changed (3) hide show
  1. conversation.py +6 -3
  2. main.py +4 -4
  3. model.py +2 -1
conversation.py CHANGED
@@ -6,13 +6,16 @@ class Conversation():
6
 
7
  command_prefix = "!"
8
 
9
- def react(self, line):
10
  print("*** {} [{}] {}: {}".format(self.game.url(), line.room, line.username, line.text.encode("utf-8")))
11
  if (line.text[0] == self.command_prefix):
12
- self.command(line, line.text[1:].lower())
13
  pass
14
 
15
- def command(self, line, cmd):
 
 
 
16
  if cmd == "name" or cmd == "engine":
17
  self.send_reply(line, self.engine.name())
18
  if cmd == "howto" or cmd == "help":
 
6
 
7
  command_prefix = "!"
8
 
9
+ def react(self, line, game):
10
  print("*** {} [{}] {}: {}".format(self.game.url(), line.room, line.username, line.text.encode("utf-8")))
11
  if (line.text[0] == self.command_prefix):
12
+ self.command(line, game, line.text[1:].lower())
13
  pass
14
 
15
+ def command(self, line, game, cmd):
16
+ if cmd == "wait":
17
+ game.abort_at = game.abort_at + 60
18
+ self.send_reply(line, "Waiting 60 seconds...")
19
  if cmd == "name" or cmd == "engine":
20
  self.send_reply(line, self.engine.name())
21
  if cmd == "howto" or cmd == "help":
main.py CHANGED
@@ -100,11 +100,11 @@ def play_game(li, game_id, control_queue, engine_factory):
100
  updates = li.get_game_stream(game_id).iter_lines()
101
 
102
  #Initial response of stream will be the full game info. Store it
103
- game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl)
 
104
  board = setup_board(game)
105
  engine = engine_factory(board)
106
  conversation = Conversation(game, engine, li)
107
- abort_at = time.time() + 20
108
 
109
  print("+++ {}".format(game))
110
 
@@ -117,7 +117,7 @@ def play_game(li, game_id, control_queue, engine_factory):
117
  upd = json.loads(binary_chunk.decode('utf-8')) if binary_chunk else None
118
  u_type = upd["type"] if upd else "ping"
119
  if u_type == "chatLine":
120
- conversation.react(ChatLine(upd))
121
  elif u_type == "gameState":
122
  game.state = upd
123
  moves = upd["moves"].split()
@@ -128,7 +128,7 @@ def play_game(li, game_id, control_queue, engine_factory):
128
  li.make_move(game.id, best_move)
129
  abort_at = time.time() + 20 # give opponent some time before aborting
130
  elif u_type == "ping":
131
- if time.time() > abort_at and len(game.state["moves"]) < 6:
132
  print(" Aborting {} by lack of activity".format(game.url()))
133
  li.abort(game.id)
134
  except (RemoteDisconnected, ConnectionError, ProtocolError, HTTPError) as exception:
 
100
  updates = li.get_game_stream(game_id).iter_lines()
101
 
102
  #Initial response of stream will be the full game info. Store it
103
+ abort_at = time.time() + 20
104
+ game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl, abort_at)
105
  board = setup_board(game)
106
  engine = engine_factory(board)
107
  conversation = Conversation(game, engine, li)
 
108
 
109
  print("+++ {}".format(game))
110
 
 
117
  upd = json.loads(binary_chunk.decode('utf-8')) if binary_chunk else None
118
  u_type = upd["type"] if upd else "ping"
119
  if u_type == "chatLine":
120
+ conversation.react(ChatLine(upd), game)
121
  elif u_type == "gameState":
122
  game.state = upd
123
  moves = upd["moves"].split()
 
128
  li.make_move(game.id, best_move)
129
  abort_at = time.time() + 20 # give opponent some time before aborting
130
  elif u_type == "ping":
131
+ if time.time() > game.abort_at and len(game.state["moves"]) < 6:
132
  print(" Aborting {} by lack of activity".format(game.url()))
133
  li.abort(game.id)
134
  except (RemoteDisconnected, ConnectionError, ProtocolError, HTTPError) as exception:
model.py CHANGED
@@ -32,7 +32,7 @@ class Challenge():
32
  return self.__str__()
33
 
34
  class Game():
35
- def __init__(self, json, username, base_url):
36
  self.username = username
37
  self.id = json.get("id")
38
  self.speed = json.get("speed")
@@ -51,6 +51,7 @@ class Game():
51
  self.opponent = self.black if self.is_white else self.white
52
  self.base_url = base_url
53
  self.white_starts = self.initial_fen == "startpos" or self.initial_fen.split()[1] == "w"
 
54
 
55
  def url(self):
56
  return "{}/{}/{}".format(self.base_url, self.id, self.my_color)
 
32
  return self.__str__()
33
 
34
  class Game():
35
+ def __init__(self, json, username, base_url, abort_at):
36
  self.username = username
37
  self.id = json.get("id")
38
  self.speed = json.get("speed")
 
51
  self.opponent = self.black if self.is_white else self.white
52
  self.base_url = base_url
53
  self.white_starts = self.initial_fen == "startpos" or self.initial_fen.split()[1] == "w"
54
+ self.abort_at = abort_at
55
 
56
  def url(self):
57
  return "{}/{}/{}".format(self.base_url, self.id, self.my_color)