Skip to content

Commit 243f7bb

Browse files
amanagrshowell
authored andcommitted
Port merels to game_handler.
Fixes #305
1 parent b0b372c commit 243f7bb

File tree

6 files changed

+205
-238
lines changed

6 files changed

+205
-238
lines changed

zulip_bots/zulip_bots/bots/merels/libraries/database.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
class MerelsStorage():
17-
def __init__(self, storage):
17+
def __init__(self, topic_name, storage):
1818
"""Instantiate storage field.
1919
2020
The current database has this form:
@@ -30,29 +30,6 @@ def __init__(self, storage):
3030
"""
3131
self.storage = storage
3232

33-
def create_new_game(self, topic_name):
34-
""" Creates a new merels game if it doesn't exists yet.
35-
36-
:param topic_name: Name of the topic
37-
:return: True, if the game is successfully created, False if otherwise.
38-
"""
39-
40-
parameters = ("X", 0, 0, EMPTY_BOARD, "", 0)
41-
42-
# Checks whether the game exists yet
43-
# If it exists
44-
45-
try:
46-
if not self.storage.contains(topic_name) or self.storage.get(
47-
topic_name) == "":
48-
self.storage.put(topic_name, json.dumps(parameters))
49-
return True
50-
else:
51-
return False
52-
except KeyError:
53-
self.storage.put(topic_name, json.dumps(parameters))
54-
return True
55-
5633
def update_game(self, topic_name, turn, x_taken, o_taken, board, hill_uid,
5734
take_mode):
5835
""" Updates the current status of the game to the database.

zulip_bots/zulip_bots/bots/merels/libraries/game.py

Lines changed: 80 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
import re
99

10+
from zulip_bots.game_handler import BadMoveException
11+
1012
from . import database
1113
from . import mechanics
12-
1314
COMMAND_PATTERN = re.compile(
1415
"^(\\w*).*(\\d,\\d).*(\\d,\\d)|^(\\w+).*(\\d,\\d)")
1516

16-
1717
def 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-
2625
def 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
3532
put (v,h): Put a man into the grid in phase 1
3633
move (v,h) -> (v,h): Moves a man from one point to -> another point
3734
take (v,h): Take an opponent's man from the grid in phase 2/3
3835
3936
v: vertical position of grid
4037
h: horizontal position of grid"""
4138

42-
4339
def 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):
5249
def 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

162145
def 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-
179161
def 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-
195176
def 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-
209189
def 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":

zulip_bots/zulip_bots/bots/merels/libraries/game_data.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class GameData():
1313
def __init__(self, game_data=(
14-
'test', 'X', 0, 0, 'NNNNNNNNNNNNNNNNNNNNNNNN', '', 0)):
14+
'merels', 'X', 0, 0, 'NNNNNNNNNNNNNNNNNNNNNNNN', '', 0)):
1515
self.topic_name = game_data[0]
1616
self.turn = game_data[1]
1717
self.x_taken = game_data[2]
@@ -41,15 +41,15 @@ def grid(self):
4141
"""
4242
return construct_grid(self.board)
4343

44-
def get_x_piece_possesed_not_on_grid(self):
44+
def get_x_piece_possessed_not_on_grid(self):
4545
"""Gets the amount of X pieces that the player X still have, but not
4646
put yet on the grid
4747
4848
:return: Amount of pieces that X has, but not on grid
4949
"""
5050
return 9 - self.x_taken - mechanics.get_piece("X", self.grid())
5151

52-
def get_o_piece_possesed_not_on_grid(self):
52+
def get_o_piece_possessed_not_on_grid(self):
5353
"""Gets the amount of X pieces that the player O still have, but not
5454
put yet on the grid
5555
@@ -64,8 +64,8 @@ def get_phase(self):
6464
:return: A phase number (1, 2, or 3)
6565
"""
6666
return mechanics.get_phase_number(self.grid(), self.turn,
67-
self.get_x_piece_possesed_not_on_grid(),
68-
self.get_o_piece_possesed_not_on_grid())
67+
self.get_x_piece_possessed_not_on_grid(),
68+
self.get_o_piece_possessed_not_on_grid())
6969

7070
def switch_turn(self):
7171
"""Switches turn between X and O

0 commit comments

Comments
 (0)