zulip_bots: Update StateHandler API to behave dict-like.

This matches the external StateHandler API with the embedded
StateHandler API.
This commit is contained in:
derAnfaenger 2017-10-24 13:15:51 +02:00
parent 32df4e097d
commit 59f81845dd
5 changed files with 65 additions and 57 deletions

View file

@ -280,33 +280,33 @@ class ticTacToeHandler(object):
for val in command_list:
command += val
original_sender = message['sender_email']
storage = bot_handler.storage
if not storage.contains(original_sender):
storage.put(original_sender, None)
user_board = storage.get(original_sender)
if (not user_board) and command == "new":
user_board = copy.deepcopy(initial_board)
storage.put(original_sender, user_board)
user_game = TicTacToeGame(user_board) if user_board else None
with bot_handler.storage.state({}) as mydict:
user_board = mydict.get(original_sender)
if (not user_board) and command == "new":
user_board = copy.deepcopy(initial_board)
mydict[original_sender] = user_board
user_game = TicTacToeGame(user_board) if user_board else None
if command == 'new':
if user_game and not first_time(user_game.board):
return_content = "You're already playing a game! Type **@tictactoe help** or **@ttt help** to see valid inputs."
else:
return_content = "Welcome to tic-tac-toe! You'll be x's and I'll be o's. Your move first!\n"
return_content += TicTacToeGame.positions
elif command == 'help':
return_content = TicTacToeGame.detailed_help_message
elif (user_game) and TicTacToeGame.check_validity(user_game, TicTacToeGame.sanitize_move(user_game, command)):
user_board = user_game.board
return_content = TicTacToeGame.tictactoe(user_game, user_board, command)
elif (user_game) and command == 'quit':
del mydict[original_sender]
return_content = "You've successfully quit the game."
if command == 'new':
if user_game and not first_time(user_game.board):
return_content = "You're already playing a game! Type **@tictactoe help** or **@ttt help** to see valid inputs."
else:
return_content = "Hmm, I didn't understand your input. Type **@tictactoe help** or **@ttt help** to see valid inputs."
return_content = "Welcome to tic-tac-toe! You'll be x's and I'll be o's. Your move first!\n"
return_content += TicTacToeGame.positions
elif command == 'help':
return_content = TicTacToeGame.detailed_help_message
elif (user_game) and TicTacToeGame.check_validity(user_game, TicTacToeGame.sanitize_move(user_game, command)):
return_content = TicTacToeGame.tictactoe(user_game, user_board, command)
elif (user_game) and command == 'quit':
storage.put(original_sender, None)
return_content = "You've successfully quit the game."
else:
return_content = "Hmm, I didn't understand your input. Type **@tictactoe help** or **@ttt help** to see valid inputs."
if "Game over" in return_content or "draw" in return_content:
del mydict[original_sender]
if "Game over" in return_content or "draw" in return_content:
storage.put(original_sender, None)
bot_handler.send_message(dict(
type = 'private',