Skip to content

Commit 22d3d32

Browse files
committed
game_handler: add widget_content support
1 parent 8688e3a commit 22d3d32

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

zulip_bots/zulip_bots/game_handler.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import random
44
import logging
55
from copy import deepcopy
6-
from typing import Any, Dict, Tuple, List
6+
from typing import Any, Dict, Tuple, List, Union, Optional
77

88

99
class BadMoveException(Exception):
@@ -152,16 +152,21 @@ def confirm_invitation_declined(self, game_id: str) -> str:
152152
host = self.invites[game_id]['host']
153153
return 'Declined invitation to play **{}** from @**{}**.'.format(self.game_name, self.get_username_by_email(host))
154154

155-
def send_message(self, to: str, content: str, is_private: bool, subject: str = '') -> None:
155+
def send_message(self, to: str, content: str, is_private: bool, subject: str = '', widget_content: Optional[str] = '') -> None:
156+
if widget_content == '':
157+
widget_content = None
156158
self.bot_handler.send_message(dict(
157159
type='private' if is_private else 'stream',
158160
to=to,
159161
content=content,
160-
subject=subject
162+
subject=subject,
163+
widget_content=widget_content
161164
))
162165

163-
def send_reply(self, original_message: Dict[str, Any], content: str) -> None:
164-
self.bot_handler.send_reply(original_message, content)
166+
def send_reply(self, original_message: Dict[str, Any], content: str, widget_content: Optional[str] = '') -> None:
167+
if widget_content == '':
168+
widget_content = None
169+
self.bot_handler.send_reply(original_message, content, widget_content)
165170

166171
def usage(self) -> str:
167172
return '''
@@ -710,20 +715,20 @@ def generate_game_id(self) -> str:
710715
id += valid_characters[random.randrange(0, len(valid_characters))]
711716
return id
712717

713-
def broadcast(self, game_id: str, content: str, include_private: bool = True) -> bool:
718+
def broadcast(self, game_id: str, content: str, include_private: bool = True, widget_content: str = '') -> bool:
714719
if include_private:
715720
private_recipients = self.get_players(game_id, parameter='p')
716721
if private_recipients is not None:
717722
for user in private_recipients:
718-
self.send_message(user, content, True)
723+
self.send_message(user, content, True, '', widget_content)
719724
if game_id in self.invites.keys():
720725
if self.invites[game_id]['subject'] != '###private###':
721726
self.send_message(
722-
self.invites[game_id]['stream'], content, False, self.invites[game_id]['subject'])
727+
self.invites[game_id]['stream'], content, False, self.invites[game_id]['subject'], widget_content)
723728
return True
724729
if game_id in self.instances.keys():
725730
self.send_message(
726-
self.instances[game_id].stream, content, False, self.instances[game_id].subject)
731+
self.instances[game_id].stream, content, False, self.instances[game_id].subject, widget_content)
727732
return True
728733
return False
729734

@@ -771,7 +776,7 @@ def __init__(self, gameAdapter: GameAdapter, is_private: bool, subject: str, gam
771776
self.board = self.model.current_board
772777
self.turn = random.randrange(0, len(players)) - 1
773778
self.current_draw = {} # type: Dict[str, bool]
774-
self.current_messages = [] # type: List[str]
779+
self.current_messages = [] # type: List[Union[str, Tuple[str, str]]]
775780
self.is_changing_subject = False
776781

777782
def start(self) -> None:
@@ -842,8 +847,8 @@ def send_current_turn_message(self) -> None:
842847
self.players[self.turn]),
843848
self.gameAdapter.gameMessageHandler.get_player_color(self.turn)))
844849

845-
def broadcast(self, content: str) -> None:
846-
self.gameAdapter.broadcast(self.game_id, content)
850+
def broadcast(self, content: str, widget_content: str = '') -> None:
851+
self.gameAdapter.broadcast(self.game_id, content, widget_content=widget_content)
847852

848853
def check_draw(self) -> bool:
849854
for d in self.current_draw.values():
@@ -867,7 +872,11 @@ def make_move(self, content: str, is_computer: bool) -> None:
867872
return
868873
except BadMoveException as e:
869874
self.broadcast(e.message)
870-
self.broadcast(self.parse_current_board())
875+
current_board_message = self.parse_current_board()
876+
if isinstance(current_board_message, tuple):
877+
self.broadcast(
878+
current_board_message[0], current_board_message[1])
879+
self.broadcast(str(current_board_message))
871880
return
872881
if not is_computer:
873882
self.current_messages.append(self.gameAdapter.gameMessageHandler.alert_move_message(
@@ -899,7 +908,6 @@ def same_player_turn(self, content: str, message: str, is_computer: bool) -> Non
899908
game_over = self.players[self.turn]
900909
self.end_game(game_over)
901910
return
902-
user_turn_avatar = "!avatar({})".format(self.players[self.turn])
903911
self.send_current_turn_message()
904912
self.broadcast_current_message()
905913
if self.players[self.turn] == self.gameAdapter.email:
@@ -915,8 +923,15 @@ def next_turn(self) -> None:
915923
self.make_move('', True)
916924

917925
def broadcast_current_message(self) -> None:
918-
content = '\n\n'.join(self.current_messages)
919-
self.broadcast(content)
926+
# if there are no widgets, send all the messages all at once
927+
if len(list(filter(lambda x: isinstance(x, tuple), self.current_messages))) == 0:
928+
self.broadcast("\n\n".join(str(m) for m in self.current_messages))
929+
else:
930+
for message in self.current_messages:
931+
if isinstance(message, tuple):
932+
self.broadcast(message[0], message[1])
933+
else:
934+
self.broadcast(str(message))
920935
self.current_messages = []
921936

922937
def parse_current_board(self) -> Any:

0 commit comments

Comments
 (0)