Skip to content

Commit 870e5e5

Browse files
committed
game_handler: add widget_content support
1 parent c258f6e commit 870e5e5

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

zulip_bots/zulip_bots/game_handler.py

Lines changed: 31 additions & 15 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
from zulip_bots.test_lib import BotTestCase
88
import operator
99
import random
@@ -155,16 +155,21 @@ def confirm_invitation_declined(self, game_id: str) -> str:
155155
host = self.invites[game_id]['host']
156156
return 'Declined invitation to play **{}** from @**{}**.'.format(self.game_name, self.get_username_by_email(host))
157157

158-
def send_message(self, to: str, content: str, is_private: bool, subject: str='') -> None:
158+
def send_message(self, to: str, content: str, is_private: bool, subject: str='', widget_content: Optional[str]='') -> None:
159+
if widget_content == '':
160+
widget_content = None
159161
self.bot_handler.send_message(dict(
160162
type='private' if is_private else 'stream',
161163
to=to,
162164
content=content,
163-
subject=subject
165+
subject=subject,
166+
widget_content=widget_content
164167
))
165168

166-
def send_reply(self, original_message: Dict[str, Any], content: str) -> None:
167-
self.bot_handler.send_reply(original_message, content)
169+
def send_reply(self, original_message: Dict[str, Any], content: str, widget_content: Optional[str]='') -> None:
170+
if widget_content == '':
171+
widget_content = None
172+
self.bot_handler.send_reply(original_message, content, widget_content)
168173

169174
def usage(self) -> str:
170175
return '''
@@ -713,20 +718,20 @@ def generate_game_id(self) -> str:
713718
id += valid_characters[random.randrange(0, len(valid_characters))]
714719
return id
715720

716-
def broadcast(self, game_id: str, content: str, include_private: bool=True) -> bool:
721+
def broadcast(self, game_id: str, content: str, include_private: bool=True, widget_content: str='') -> bool:
717722
if include_private:
718723
private_recipients = self.get_players(game_id, parameter='p')
719724
if private_recipients is not None:
720725
for user in private_recipients:
721-
self.send_message(user, content, True)
726+
self.send_message(user, content, True, '', widget_content)
722727
if game_id in self.invites.keys():
723728
if self.invites[game_id]['subject'] != '###private###':
724729
self.send_message(
725-
self.invites[game_id]['stream'], content, False, self.invites[game_id]['subject'])
730+
self.invites[game_id]['stream'], content, False, self.invites[game_id]['subject'], widget_content)
726731
return True
727732
if game_id in self.instances.keys():
728733
self.send_message(
729-
self.instances[game_id].stream, content, False, self.instances[game_id].subject)
734+
self.instances[game_id].stream, content, False, self.instances[game_id].subject, widget_content)
730735
return True
731736
return False
732737

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

780785
def start(self) -> None:
@@ -845,8 +850,8 @@ def send_current_turn_message(self) -> None:
845850
self.players[self.turn]),
846851
self.gameAdapter.gameMessageHandler.get_player_color(self.turn)))
847852

848-
def broadcast(self, content: str) -> None:
849-
self.gameAdapter.broadcast(self.game_id, content)
853+
def broadcast(self, content: str, widget_content: str='') -> None:
854+
self.gameAdapter.broadcast(self.game_id, content, widget_content=widget_content)
850855

851856
def check_draw(self) -> bool:
852857
for d in self.current_draw.values():
@@ -870,7 +875,11 @@ def make_move(self, content: str, is_computer: bool) -> None:
870875
return
871876
except BadMoveException as e:
872877
self.broadcast(e.message)
873-
self.broadcast(self.parse_current_board())
878+
current_board_message = self.parse_current_board()
879+
if isinstance(current_board_message, tuple):
880+
self.broadcast(
881+
current_board_message[0], current_board_message[1])
882+
self.broadcast(str(current_board_message))
874883
return
875884
if not is_computer:
876885
self.current_messages.append(self.gameAdapter.gameMessageHandler.alert_move_message(
@@ -918,8 +927,15 @@ def next_turn(self) -> None:
918927
self.make_move('', True)
919928

920929
def broadcast_current_message(self) -> None:
921-
content = '\n\n'.join(self.current_messages)
922-
self.broadcast(content)
930+
# if there are no widgets, send all the messages all at once
931+
if len(list(filter(lambda x: isinstance(x, tuple), self.current_messages))) == 0:
932+
self.broadcast("\n\n".join(str(m) for m in self.current_messages))
933+
else:
934+
for message in self.current_messages:
935+
if isinstance(message, tuple):
936+
self.broadcast(message[0], message[1])
937+
else:
938+
self.broadcast(str(message))
923939
self.current_messages = []
924940

925941
def parse_current_board(self) -> Any:

0 commit comments

Comments
 (0)