77
88import re
99
10+ from zulip_bots .game_handler import BadMoveException
11+
1012from . import database
1113from . import mechanics
12-
1314COMMAND_PATTERN = re .compile (
1415 "^(\\ w*).*(\\ d,\\ d).*(\\ d,\\ d)|^(\\ w+).*(\\ d,\\ d)" )
1516
16-
1717def getInfo ():
1818 """ Gets the info on starting the game
1919
@@ -22,142 +22,125 @@ def getInfo():
2222 return "To start a game, mention me and add `create`. A game will start " \
2323 "in that topic. "
2424
25-
2625def getHelp ():
2726 """ Gets the help message
2827
2928 :return: Help message
3029 """
3130
3231 return """Commands:
33- create: Create a new game if it doesn't exist
34- reset: Reset a current game
3532put (v,h): Put a man into the grid in phase 1
3633move (v,h) -> (v,h): Moves a man from one point to -> another point
3734take (v,h): Take an opponent's man from the grid in phase 2/3
3835
3936v: vertical position of grid
4037h: horizontal position of grid"""
4138
42-
4339def unknown_command ():
4440 """Returns an unknown command info
4541
4642 :return: A string containing info about available commands
4743 """
48- return "Unknown command. Available commands: create, reset, help, " \
49- "put (v,h), take (v,h), move (v,h) -> (v,h)"
50-
44+ message = "Unknown command. Available commands: " \
45+ "put (v,h), take (v,h), move (v,h) -> (v,h)"
46+ raise BadMoveException ( message )
5147
48+ # def beat(message, topic_name, merels_storage):
5249def beat (message , topic_name , merels_storage ):
5350 """ This gets triggered every time a user send a message in any topic
54-
5551 :param message: User's message
5652 :param topic_name: User's current topic
5753 :param merels_storage: Merels' storage
58- :return: A response string to reply to that topic, if any. If not, it
59- returns an empty string
54+ :return: a tuple of response string and message, non-empty string
55+ we want to keep the turn of the same played,
56+ an empty string otherwise.
6057 """
58+ merels = database .MerelsStorage (topic_name , merels_storage )
59+ match = COMMAND_PATTERN .match (message )
60+ same_player_move = "" # message indicating move of the same player
6161
62- merels = database .MerelsStorage (merels_storage )
62+ if match is None :
63+ return unknown_command ()
64+ if match .group (1 ) is not None and match .group (
65+ 2 ) is not None and match .group (3 ) is not None :
6366
64- if "create" in message . lower ():
65- return mechanics . create_room ( topic_name , merels_storage )
67+ responses = ""
68+ command = match . group ( 1 )
6669
67- if "help" in message .lower ():
68- return getHelp ()
70+ if command .lower () == "move" :
6971
70- if "reset" in message .lower ():
71- if merels .get_game_data (topic_name ) is not None :
72- return mechanics .reset_game (topic_name , merels_storage )
73- else :
74- return "No game created yet."
72+ p1 = [int (x ) for x in match .group (2 ).split ("," )]
73+ p2 = [int (x ) for x in match .group (3 ).split ("," )]
7574
76- match = COMMAND_PATTERN . match ( message )
75+ if mechanics . get_take_status ( topic_name , merels_storage ) == 1 :
7776
78- if match is None :
79- return unknown_command ()
77+ raise BadMoveException ("Take is required to proceed."
78+ " Please try again.\n " )
79+
80+ responses += mechanics .move_man (topic_name , p1 , p2 ,
81+ merels_storage ) + "\n "
82+ no_moves = after_event_checkup (responses , topic_name , merels_storage )
83+
84+ mechanics .update_hill_uid (topic_name , merels_storage )
8085
81- # Matches when user types the command in format of: "command v,h -> v,
82- # h" or something similar that has three arguments
86+ responses += mechanics .display_game (topic_name , merels_storage ) + "\n "
8387
84- if merels .get_game_data (topic_name ) is not None :
85- if match .group (1 ) is not None and match .group (
86- 2 ) is not None and match .group (3 ) is not None :
88+ if no_moves != "" :
89+ same_player_move = no_moves
90+
91+ else :
92+ return unknown_command ()
93+
94+ if mechanics .get_take_status (topic_name , merels_storage ) == 1 :
95+ same_player_move = "Take is required to proceed.\n "
96+ return responses , same_player_move
97+
98+ elif match .group (4 ) is not None and match .group (5 ) is not None :
99+ command = match .group (4 )
100+ p1 = [int (x ) for x in match .group (5 ).split ("," )]
101+
102+ # put 1,2
103+ if command == "put" :
87104 responses = ""
88105
89- command = match .group (1 )
90- if command .lower () == "move" :
91- p1 = [int (x ) for x in match .group (2 ).split ("," )]
92- p2 = [int (x ) for x in match .group (3 ).split ("," )]
93-
94- # Note that it doesn't have to be "move 1,1 -> 1,2".
95- # It can also just be "move 1,1 1,2"
96- if mechanics .get_take_status (topic_name , merels_storage ) == 1 :
97- responses += "Take is required to proceed. Please try " \
98- "again.\n "
99- else :
100- responses += mechanics .move_man (topic_name , p1 , p2 ,
101- merels_storage ) + "\n "
102- responses += after_event_checkup (responses , topic_name ,
103- merels_storage )
106+ if mechanics .get_take_status (topic_name , merels_storage ) == 1 :
107+ raise BadMoveException ("Take is required to proceed."
108+ " Please try again.\n " )
109+ responses += mechanics .put_man (topic_name , p1 [0 ], p1 [1 ],
110+ merels_storage ) + "\n "
111+ no_moves = after_event_checkup (responses , topic_name , merels_storage )
104112
105- mechanics .update_hill_uid (topic_name , merels_storage )
106- else :
107- responses += unknown_command ()
113+ mechanics .update_hill_uid (topic_name , merels_storage )
114+
115+ responses += mechanics . display_game ( topic_name , merels_storage ) + " \n "
108116
109- responses += mechanics .display_game (topic_name ,
117+ if no_moves != "" :
118+ same_player_move = no_moves
119+ if mechanics .get_take_status (topic_name , merels_storage ) == 1 :
120+ same_player_move = "Take is required to proceed.\n "
121+ return responses , same_player_move
122+ # take 5,3
123+ elif command == "take" :
124+ responses = ""
125+ if mechanics .get_take_status (topic_name , merels_storage ) == 1 :
126+ responses += mechanics .take_man (topic_name , p1 [0 ], p1 [1 ],
110127 merels_storage ) + "\n "
111- return responses
112- elif match .group (4 ) is not None and match .group (5 ) is not None :
113- command = match .group (4 )
114- p1 = [int (x ) for x in match .group (5 ).split ("," )]
115-
116- # put 1,2
117- if command == "put" :
118- responses = ""
119-
120- if mechanics .get_take_status (topic_name , merels_storage ) == 1 :
121- responses += "Take is required to proceed. Please try " \
122- "again.\n "
123- else :
124- responses += mechanics .put_man (topic_name , p1 [0 ], p1 [1 ],
125- merels_storage ) + "\n "
126- responses += after_event_checkup (responses , topic_name ,
127- merels_storage )
128+ if "Failed" in responses :
129+ raise BadMoveException (responses )
130+ mechanics .update_toggle_take_mode (topic_name , merels_storage )
131+ no_moves = after_event_checkup (responses , topic_name , merels_storage )
128132
129133 mechanics .update_hill_uid (topic_name , merels_storage )
130- responses += mechanics .display_game (topic_name ,
131- merels_storage ) + "\n "
132- return responses
133- # take 5,3
134- elif command == "take" :
135- responses = ""
136- if mechanics .get_take_status (topic_name , merels_storage ) == 1 :
137- responses += mechanics .take_man (topic_name , p1 [0 ], p1 [1 ],
138- merels_storage ) + "\n "
139- if not ("Failed" in responses ):
140- mechanics .update_toggle_take_mode (topic_name ,
141- merels_storage )
142- mechanics .update_change_turn (topic_name ,
143- merels_storage )
144-
145- mechanics .update_hill_uid (topic_name , merels_storage )
146- responses += check_win (topic_name , merels_storage )
147- if not ("win" in responses .lower ()):
148- responses += mechanics .display_game (topic_name ,
149- merels_storage ) \
150- + "\n "
151-
152- return responses
153- else :
154- return "Taking is not possible."
155- else :
156- return unknown_command ()
157- else :
158- return "No game created yet. You cannot do any of the game commands." \
159- " Create the game first."
160134
135+ responses += mechanics .display_game (topic_name , merels_storage ) + "\n "
136+
137+ if no_moves != "" :
138+ same_player_move = no_moves
139+ return responses , same_player_move
140+ else :
141+ raise BadMoveException ("Taking is not possible." )
142+ else :
143+ return unknown_command ()
161144
162145def check_take_mode (response , topic_name , merels_storage ):
163146 """This checks whether the previous action can result in a take mode for
@@ -175,7 +158,6 @@ def check_take_mode(response, topic_name, merels_storage):
175158 else :
176159 mechanics .update_change_turn (topic_name , merels_storage )
177160
178-
179161def check_any_moves (topic_name , merels_storage ):
180162 """Check whether the player can make any moves, if can't switch to another
181163 player
@@ -191,7 +173,6 @@ def check_any_moves(topic_name, merels_storage):
191173
192174 return ""
193175
194-
195176def after_event_checkup (response , topic_name , merels_storage ):
196177 """After doing certain moves in the game, it will check for take mode
197178 availability and check for any possible moves
@@ -205,7 +186,6 @@ def after_event_checkup(response, topic_name, merels_storage):
205186 check_take_mode (response , topic_name , merels_storage )
206187 return check_any_moves (topic_name , merels_storage )
207188
208-
209189def check_win (topic_name , merels_storage ):
210190 """Checks whether the current grid has a winner, if it does, finish the
211191 game and remove it from the database
@@ -214,7 +194,7 @@ def check_win(topic_name, merels_storage):
214194 :param merels_storage: Merels' storage
215195 :return:
216196 """
217- merels = database .MerelsStorage (merels_storage )
197+ merels = database .MerelsStorage (topic_name , merels_storage )
218198
219199 win = mechanics .who_won (topic_name , merels_storage )
220200 if win != "None" :
0 commit comments