7
7
8
8
import re
9
9
10
+ from zulip_bots .game_handler import BadMoveException
11
+
10
12
from . import database
11
13
from . import mechanics
12
-
13
14
COMMAND_PATTERN = re .compile (
14
15
"^(\\ w*).*(\\ d,\\ d).*(\\ d,\\ d)|^(\\ w+).*(\\ d,\\ d)" )
15
16
16
-
17
17
def getInfo ():
18
18
""" Gets the info on starting the game
19
19
@@ -22,142 +22,125 @@ def getInfo():
22
22
return "To start a game, mention me and add `create`. A game will start " \
23
23
"in that topic. "
24
24
25
-
26
25
def getHelp ():
27
26
""" Gets the help message
28
27
29
28
:return: Help message
30
29
"""
31
30
32
31
return """Commands:
33
- create: Create a new game if it doesn't exist
34
- reset: Reset a current game
35
32
put (v,h): Put a man into the grid in phase 1
36
33
move (v,h) -> (v,h): Moves a man from one point to -> another point
37
34
take (v,h): Take an opponent's man from the grid in phase 2/3
38
35
39
36
v: vertical position of grid
40
37
h: horizontal position of grid"""
41
38
42
-
43
39
def unknown_command ():
44
40
"""Returns an unknown command info
45
41
46
42
:return: A string containing info about available commands
47
43
"""
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 )
51
47
48
+ # def beat(message, topic_name, merels_storage):
52
49
def beat (message , topic_name , merels_storage ):
53
50
""" This gets triggered every time a user send a message in any topic
54
-
55
51
:param message: User's message
56
52
:param topic_name: User's current topic
57
53
: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.
60
57
"""
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
61
61
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 :
63
66
64
- if "create" in message . lower ():
65
- return mechanics . create_room ( topic_name , merels_storage )
67
+ responses = ""
68
+ command = match . group ( 1 )
66
69
67
- if "help" in message .lower ():
68
- return getHelp ()
70
+ if command .lower () == "move" :
69
71
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 ("," )]
75
74
76
- match = COMMAND_PATTERN . match ( message )
75
+ if mechanics . get_take_status ( topic_name , merels_storage ) == 1 :
77
76
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 )
80
85
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 "
83
87
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" :
87
104
responses = ""
88
105
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 )
104
112
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 "
108
116
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 ],
110
127
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 )
128
132
129
133
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."
160
134
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 ()
161
144
162
145
def check_take_mode (response , topic_name , merels_storage ):
163
146
"""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):
175
158
else :
176
159
mechanics .update_change_turn (topic_name , merels_storage )
177
160
178
-
179
161
def check_any_moves (topic_name , merels_storage ):
180
162
"""Check whether the player can make any moves, if can't switch to another
181
163
player
@@ -191,7 +173,6 @@ def check_any_moves(topic_name, merels_storage):
191
173
192
174
return ""
193
175
194
-
195
176
def after_event_checkup (response , topic_name , merels_storage ):
196
177
"""After doing certain moves in the game, it will check for take mode
197
178
availability and check for any possible moves
@@ -205,7 +186,6 @@ def after_event_checkup(response, topic_name, merels_storage):
205
186
check_take_mode (response , topic_name , merels_storage )
206
187
return check_any_moves (topic_name , merels_storage )
207
188
208
-
209
189
def check_win (topic_name , merels_storage ):
210
190
"""Checks whether the current grid has a winner, if it does, finish the
211
191
game and remove it from the database
@@ -214,7 +194,7 @@ def check_win(topic_name, merels_storage):
214
194
:param merels_storage: Merels' storage
215
195
:return:
216
196
"""
217
- merels = database .MerelsStorage (merels_storage )
197
+ merels = database .MerelsStorage (topic_name , merels_storage )
218
198
219
199
win = mechanics .who_won (topic_name , merels_storage )
220
200
if win != "None" :
0 commit comments