Thibault Duplessis commited on
Commit
9ed81f1
·
1 Parent(s): 4aa7217

bootstrap conversation

Browse files
Files changed (3) hide show
  1. conversation.py +21 -0
  2. main.py +4 -5
  3. model.py +1 -0
conversation.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Conversation():
2
+ def __init__(self, game, xhr):
3
+ self.game = game
4
+ self.xhr = xhr
5
+
6
+ def react(self, line):
7
+ print("*** {} [{}] {}: {}".format(self.game.url(), line.room, line.username, line.text))
8
+ if (line.username == self.game.username):
9
+ return
10
+ # add your chat commands here. Silly example: self.parrot(line)
11
+ pass
12
+
13
+ def parrot(self, line):
14
+ self.xhr.chat(self.game.id, line.room, "{} said \"{}\"".format(line.username, line.text))
15
+
16
+
17
+ class ChatLine():
18
+ def __init__(self, json):
19
+ self.room = json.get("room")
20
+ self.username = json.get("username")
21
+ self.text = json.get("text")
main.py CHANGED
@@ -11,6 +11,7 @@ import os
11
  import traceback
12
  import yaml
13
  import logging_pool
 
14
 
15
  CONFIG = {}
16
 
@@ -78,6 +79,7 @@ def play_game(li, game_id, weights, threads, challenge_queue):
78
 
79
  #Initial response of stream will be the full game info. Store it
80
  game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl)
 
81
  board = setup_board(game.state)
82
  engine, info_handler = setup_engine(engine_path, board, weights, threads)
83
 
@@ -88,11 +90,8 @@ def play_game(li, game_id, weights, threads, challenge_queue):
88
  for binary_chunk in updates:
89
  upd = json.loads(binary_chunk.decode('utf-8')) if binary_chunk else None
90
  u_type = upd["type"] if upd else "ping"
91
- if u_type == "chatLine" and upd["username"] != username:
92
- print("*** {} [{}] {}: {}".format(game.url(), upd["room"], upd["username"], upd["text"]))
93
- # uncomment the following lines to demonstrate sending chat message (as a parrot)
94
- # if (upd["username"] != username):
95
- # li.chat(game_id, upd["room"], "{} said \"{}\"".format(upd["username"], upd["text"]))
96
  elif u_type == "gameState":
97
  moves = upd.get("moves").split()
98
  board = update_board(board, moves[-1])
 
11
  import traceback
12
  import yaml
13
  import logging_pool
14
+ from conversation import Conversation, ChatLine
15
 
16
  CONFIG = {}
17
 
 
79
 
80
  #Initial response of stream will be the full game info. Store it
81
  game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl)
82
+ conversation = Conversation(game, li)
83
  board = setup_board(game.state)
84
  engine, info_handler = setup_engine(engine_path, board, weights, threads)
85
 
 
90
  for binary_chunk in updates:
91
  upd = json.loads(binary_chunk.decode('utf-8')) if binary_chunk else None
92
  u_type = upd["type"] if upd else "ping"
93
+ if u_type == "chatLine":
94
+ conversation.react(ChatLine(upd))
 
 
 
95
  elif u_type == "gameState":
96
  moves = upd.get("moves").split()
97
  board = update_board(board, moves[-1])
model.py CHANGED
@@ -27,6 +27,7 @@ class Challenge():
27
 
28
  class Game():
29
  def __init__(self, json, username, base_url):
 
30
  self.id = json.get("id")
31
  self.speed = json.get("speed")
32
  self.perf_name = json.get("perf").get("name")
 
27
 
28
  class Game():
29
  def __init__(self, json, username, base_url):
30
+ self.username = username
31
  self.id = json.get("id")
32
  self.speed = json.get("speed")
33
  self.perf_name = json.get("perf").get("name")