From 109deb59ed4f590ff6c1d24a92c114e00823b965 Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Tue, 16 Aug 2022 18:33:46 +0300 Subject: [PATCH] Improve naming consistency --- app/bot.py | 82 ++++++++++++------------ app/game.py | 176 +++++++++++++++++++++++++++------------------------- 2 files changed, 132 insertions(+), 126 deletions(-) diff --git a/app/bot.py b/app/bot.py index 40e3bc5..bcd60a0 100644 --- a/app/bot.py +++ b/app/bot.py @@ -40,10 +40,10 @@ storage = GameRegistry() init_logging() INITIATOR_OPERATIONS = [ - Game.OPERATION_START, - Game.OPERATION_RESTART, - Game.OPERATION_END, - Game.OPERATION_RE_VOTE, + Game.OPERATION_START_ESTIMATION, + Game.OPERATION_END_ESTIMATION, + Game.OPERATION_CLEAR_VOTES, + Game.OPERATION_RE_ESTIMATE, ] @@ -59,12 +59,12 @@ async def on_help_command(chat: Chat, match): @bot.command("(?s)/poker\s+(.+)$") @bot.command("/(poker)$") -async def on_start_poker_command(chat: Chat, match): +async def on_poker_command(chat: Chat, match): vote_id = str(chat.message["message_id"]) text = match.group(1) - if text in 'poker': - text = '' + if text in "poker": + text = "" game = storage.new_game(chat.id, vote_id, chat.sender, text) resp = await chat.send_text(**game.get_send_kwargs()) @@ -72,21 +72,21 @@ async def on_start_poker_command(chat: Chat, match): await storage.save_game(game) -@bot.callback(r"ready-click-(.*?)-(.*?)$") -async def on_lobby_vote_click(chat: Chat, callback_query: CallbackQuery, match): +@bot.callback(r"discussion-vote-click-(.*?)-(.*?)$") +async def on_discussion_vote_click(chat: Chat, callback_query: CallbackQuery, match): logbook.info("{}", callback_query) vote_id = match.group(1) - status = match.group(2) - result = "Start status `{}` accepted".format(status) + vote = match.group(2) + result = "Vote `{}` accepted".format(vote) game = await storage.get_game(chat.id, vote_id) if not game: return await callback_query.answer(text="No such game") - if game.phase not in Game.PHASE_INITIATING: - return await callback_query.answer(text="Can't change ready status not in initiating phase") + if game.phase not in Game.PHASE_DISCUSSION: + return await callback_query.answer(text="Can't vote not in " + Game.PHASE_DISCUSSION + " phase") - game.add_lobby_vote(callback_query.src["from"], status) + game.add_discussion_vote(callback_query.src["from"], vote) await storage.save_game(game) await edit_message(chat, game) @@ -94,21 +94,21 @@ async def on_lobby_vote_click(chat: Chat, callback_query: CallbackQuery, match): await callback_query.answer(text=result) -@bot.callback(r"vote-click-(.*?)-(.*?)$") -async def on_vote_click(chat: Chat, callback_query: CallbackQuery, match): +@bot.callback(r"estimation-vote-click-(.*?)-(.*?)$") +async def on_estimation_vote_click(chat: Chat, callback_query: CallbackQuery, match): logbook.info("{}", callback_query) vote_id = match.group(1) - point = match.group(2) - result = "Answer `{}` accepted".format(point) + vote = match.group(2) + result = "Vote `{}` accepted".format(vote) game = await storage.get_game(chat.id, vote_id) if not game: return await callback_query.answer(text="No such game") - if game.phase not in Game.PHASE_VOTING: - return await callback_query.answer(text="Can't change vote not in voting phase") + if game.phase not in Game.PHASE_ESTIMATION: + return await callback_query.answer(text="Can't vote not in " + Game.PHASE_ESTIMATION + " phase") - game.add_vote(callback_query.src["from"], point) + game.add_estimation_vote(callback_query.src["from"], vote) await storage.save_game(game) await edit_message(chat, game) @@ -126,46 +126,46 @@ async def on_initiator_operation_click(chat: Chat, callback_query: CallbackQuery return await callback_query.answer(text="No such game") if callback_query.src["from"]["id"] != game.initiator["id"]: - return await callback_query.answer(text="Operation '{}' is available only for initiator".format(operation)) - - if operation in Game.OPERATION_START: - await run_operation_start(chat, game) - elif operation in Game.OPERATION_RESTART: - await run_operation_restart(chat, game) - elif operation in Game.OPERATION_END: - await run_operation_end(chat, game) - elif operation in Game.OPERATION_RE_VOTE: - await run_re_vote(chat, game) + return await callback_query.answer(text="Operation `{}` is available only for initiator".format(operation)) + + if operation in Game.OPERATION_START_ESTIMATION: + await run_operation_start_estimation(chat, game) + elif operation in Game.OPERATION_END_ESTIMATION: + await run_operation_end_estimation(chat, game) + elif operation in Game.OPERATION_CLEAR_VOTES: + await run_operation_clear_votes(chat, game) + elif operation in Game.OPERATION_RE_ESTIMATE: + await run_re_estimate(chat, game) else: raise Exception("Unknown operation `{}`".format(operation)) await callback_query.answer() -async def run_operation_start(chat: Chat, game: Game): - game.start() +async def run_operation_start_estimation(chat: Chat, game: Game): + game.start_estimation() await edit_message(chat, game) await storage.save_game(game) -async def run_operation_restart(chat: Chat, game: Game): - game.restart() +async def run_operation_clear_votes(chat: Chat, game: Game): + game.clear_votes() await edit_message(chat, game) await storage.save_game(game) -async def run_operation_end(chat: Chat, game: Game): - game.end() +async def run_operation_end_estimation(chat: Chat, game: Game): + game.end_estimation() await edit_message(chat, game) await storage.save_game(game) -async def run_re_vote(chat: Chat, game: Game): +async def run_re_estimate(chat: Chat, game: Game): message = { - "text": game.render(), + "text": game.render_message_text(), } - game.re_vote() + game.re_estimate() # TODO: Extract to method try: @@ -192,5 +192,5 @@ def main(): bot.run(reload=False) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/app/game.py b/app/game.py index 1697ec4..6aaf2f3 100644 --- a/app/game.py +++ b/app/game.py @@ -6,7 +6,7 @@ "♥️", "♠️", "♦️", "♣️", ] -POINTS_LAYOUT = [ +CARD_DECK_LAYOUT = [ ["0", "0.5", "1", "2", "3", "4"], ["5", "6", "7", "8", "9", "10"], ["12", "18", "24", "30"], @@ -51,9 +51,9 @@ def set(self, status): @property def icon(self): - if self.status in "ready": + if self.status in "to_estimate": return "👍" - elif self.status in "discuss": + elif self.status in "need_discuss": return "⁉️" def to_dict(self): @@ -70,14 +70,14 @@ def from_dict(cls, dict): class Game: - PHASE_INITIATING = "initiating" - PHASE_VOTING = "voting" - PHASE_ENDED = "ended" + PHASE_DISCUSSION = "discussion" + PHASE_ESTIMATION = "estimation" + PHASE_RESOLUTION = "resolution" - OPERATION_START = "start" - OPERATION_RESTART = "restart" - OPERATION_END = "end" - OPERATION_RE_VOTE = "re-vote" + OPERATION_START_ESTIMATION = "start_estimation" + OPERATION_END_ESTIMATION = "end_estimation" + OPERATION_CLEAR_VOTES = "clear_votes" + OPERATION_RE_ESTIMATE = "re_estimate" def __init__(self, chat_id, vote_id, initiator, text): self.chat_id = chat_id @@ -85,80 +85,86 @@ def __init__(self, chat_id, vote_id, initiator, text): self.initiator = initiator self.text = text self.reply_message_id = 0 - self.votes = collections.defaultdict(Vote) - self.lobby_votes = collections.defaultdict(LobbyVote) + self.estimation_votes = collections.defaultdict(Vote) + self.discussion_votes = collections.defaultdict(LobbyVote) self.revealed = False - self.phase = self.PHASE_INITIATING + self.phase = self.PHASE_DISCUSSION - def add_vote(self, initiator, point): - self.votes[self._initiator_str(initiator)].set(point) + def add_estimation_vote(self, initiator, vote): + self.estimation_votes[self._initiator_str(initiator)].set(vote) - def add_lobby_vote(self, initiator, status): - self.lobby_votes[self._initiator_str(initiator)].set(status) + def add_discussion_vote(self, initiator, vote): + self.discussion_votes[self._initiator_str(initiator)].set(vote) - def render(self): + def render_message_text(self): result = "" - result += self.render_summary() + result += self.render_initiator_text() result += "\n" - result += self.render_initiator() + result += self.render_topic_text() result += "\n" result += "\n" - if self.phase in self.PHASE_INITIATING: - result += self.render_lobby_votes() - else: - result += self.render_votes() + result += self.render_votes_text() return result - def render_summary(self): + def render_topic_text(self): result = "" - if self.text in '': + if self.text in "": return result - if self.phase in self.PHASE_INITIATING: - result += "Discuss for:" - elif self.phase in self.PHASE_VOTING: - result += "Vote for:" - else: - result += "Results for:" + if self.phase in self.PHASE_DISCUSSION: + result += "Discussion for: " + elif self.phase in self.PHASE_ESTIMATION: + result += "Estimation for: " + elif self.phase in self.PHASE_RESOLUTION: + result += "Resolution for: " - result += "\n" result += self.text return result - def render_initiator(self): + def render_initiator_text(self): return "Initiator: {}".format(self._initiator_str(self.initiator)) - def render_lobby_votes(self): - lobby_votes_count = len(self.lobby_votes) + def render_votes_text(self): + if self.phase in self.PHASE_DISCUSSION: + return self.render_discussion_votes_text() + elif self.phase in self.PHASE_ESTIMATION: + return self.render_estimation_votes_text() + elif self.phase in self.PHASE_RESOLUTION: + return self.render_estimation_votes_text() + else: + return "" + + def render_discussion_votes_text(self): + votes_count = len(self.discussion_votes) result = "" - if self.lobby_votes: - lobby_votes_string = "\n".join( + if self.discussion_votes: + votes_string = "\n".join( "{:3s} {}".format( vote.icon, user_id ) - for user_id, vote in sorted(self.lobby_votes.items()) + for user_id, vote in sorted(self.discussion_votes.items()) ) - result += "Ready status ({}):\n{}".format(lobby_votes_count, lobby_votes_string) + result += "Votes ({}):\n{}".format(votes_count, votes_string) return result - def render_votes(self): - votes_count = len(self.votes) + def render_estimation_votes_text(self): + votes_count = len(self.estimation_votes) result = "" - if self.votes: + if self.estimation_votes: votes_string = "\n".join( "{:3s} {}".format( vote.point if self.revealed else vote.masked, user_id ) - for user_id, vote in sorted(self.votes.items()) + for user_id, vote in sorted(self.estimation_votes.items()) ) result += "Votes ({}):\n{}".format(votes_count, votes_string) @@ -166,7 +172,7 @@ def render_votes(self): def get_send_kwargs(self): return { - "text": self.render(), + "text": self.render_message_text(), "reply_markup": json.dumps(self.get_markup()), } @@ -174,68 +180,68 @@ def get_point_button(self, point): return { "type": "InlineKeyboardButton", "text": point, - "callback_data": "vote-click-{}-{}".format(self.vote_id, point), + "callback_data": "estimation-vote-click-{}-{}".format(self.vote_id, point), } - def get_ready_button(self): + def get_to_estimate_button(self): return { "type": "InlineKeyboardButton", - "text": "👍 Ready", - "callback_data": "ready-click-{}-{}".format(self.vote_id, 'ready'), + "text": "👍 To estimate", + "callback_data": "discussion-vote-click-{}-{}".format(self.vote_id, "to_estimate"), } - def get_discuss_button(self): + def get_need_discuss_button(self): return { "type": "InlineKeyboardButton", "text": "⁉️ Discuss", - "callback_data": "ready-click-{}-{}".format(self.vote_id, 'discuss'), + "callback_data": "discussion-vote-click-{}-{}".format(self.vote_id, "need_discuss"), } - def get_start_button(self): + def get_start_estimation_button(self): return { "type": "InlineKeyboardButton", - "text": "Start", - "callback_data": "{}-click-{}".format(self.OPERATION_START, self.vote_id), + "text": "Start estimation", + "callback_data": "{}-click-{}".format(self.OPERATION_START_ESTIMATION, self.vote_id), } - def get_restart_button(self): + def get_clear_votes_button(self): return { "type": "InlineKeyboardButton", - "text": "Restart", - "callback_data": "{}-click-{}".format(self.OPERATION_RESTART, self.vote_id), + "text": "Clear votes", + "callback_data": "{}-click-{}".format(self.OPERATION_CLEAR_VOTES, self.vote_id), } def get_re_vote_button(self): return { "type": "InlineKeyboardButton", - "text": "Re-vote", - "callback_data": "{}-click-{}".format(self.OPERATION_RE_VOTE, self.vote_id), + "text": "Re-estimate", + "callback_data": "{}-click-{}".format(self.OPERATION_RE_ESTIMATE, self.vote_id), } def get_end_game_button(self): return { "type": "InlineKeyboardButton", - "text": "End game", - "callback_data": "{}-click-{}".format(self.OPERATION_END, self.vote_id), + "text": "End estimation", + "callback_data": "{}-click-{}".format(self.OPERATION_END_ESTIMATION, self.vote_id), } def get_markup(self): layout_rows = [] - if self.phase in self.PHASE_INITIATING: + if self.phase in self.PHASE_DISCUSSION: layout_rows.append( [ - self.get_ready_button(), - self.get_discuss_button(), + self.get_to_estimate_button(), + self.get_need_discuss_button(), ] ) layout_rows.append( [ - self.get_start_button(), + self.get_start_estimation_button(), ] ) - elif self.phase in self.PHASE_VOTING: - for points_layout_row in POINTS_LAYOUT: + elif self.phase in self.PHASE_ESTIMATION: + for points_layout_row in CARD_DECK_LAYOUT: points_buttons_row = [] for point in points_layout_row: points_buttons_row.append(self.get_point_button(point)) @@ -243,11 +249,11 @@ def get_markup(self): layout_rows.append( [ - self.get_restart_button(), + self.get_clear_votes_button(), self.get_end_game_button(), ] ) - elif self.phase in self.PHASE_ENDED: + elif self.phase in self.PHASE_RESOLUTION: layout_rows.append( [ self.get_re_vote_button(), @@ -259,21 +265,21 @@ def get_markup(self): "inline_keyboard": layout_rows, } - def start(self): - self.phase = self.PHASE_VOTING + def start_estimation(self): + self.phase = self.PHASE_ESTIMATION - def restart(self): - self.votes.clear() - self.phase = self.PHASE_VOTING - - def end(self): + def end_estimation(self): self.revealed = True - self.phase = self.PHASE_ENDED + self.phase = self.PHASE_RESOLUTION + + def clear_votes(self): + self.estimation_votes.clear() + self.phase = self.PHASE_ESTIMATION - def re_vote(self): - self.votes.clear() + def re_estimate(self): + self.estimation_votes.clear() self.revealed = False - self.phase = self.PHASE_VOTING + self.phase = self.PHASE_ESTIMATION @staticmethod def _initiator_str(initiator: dict) -> str: @@ -289,8 +295,8 @@ def to_dict(self): "reply_message_id": self.reply_message_id, "phase": self.phase, "revealed": self.revealed, - "votes": {user_id: vote.to_dict() for user_id, vote in self.votes.items()}, - "lobby_votes": {user_id: lobby_vote.to_dict() for user_id, lobby_vote in self.lobby_votes.items()}, + "votes": {user_id: vote.to_dict() for user_id, vote in self.estimation_votes.items()}, + "lobby_votes": {user_id: lobby_vote.to_dict() for user_id, lobby_vote in self.discussion_votes.items()}, } @classmethod @@ -301,10 +307,10 @@ def from_dict(cls, chat_id, vote_id, dict): result.phase = dict["phase"] for user_id, lobby_vote in dict["lobby_votes"].items(): - result.lobby_votes[user_id] = LobbyVote.from_dict(lobby_vote) + result.discussion_votes[user_id] = LobbyVote.from_dict(lobby_vote) for user_id, vote in dict["votes"].items(): - result.votes[user_id] = Vote.from_dict(vote) + result.estimation_votes[user_id] = Vote.from_dict(vote) return result @@ -329,7 +335,7 @@ def new_game(self, chat_id, incoming_message_id: str, initiator: dict, text: str return Game(chat_id, incoming_message_id, initiator, text) async def get_game(self, chat_id, incoming_message_id: str) -> Game: - query = 'SELECT json_data FROM games WHERE chat_id = ? AND game_id = ?' + query = "SELECT json_data FROM games WHERE chat_id = ? AND game_id = ?" async with self._db.execute(query, (chat_id, incoming_message_id)) as cursor: result = await cursor.fetchone() if not result: