bots: Switch TicTacToe to use state_handler.state() contextmanager.

This commit is contained in:
neiljp (Neil Pilgrim) 2017-07-23 13:22:54 -07:00 committed by Tim Abbott
parent 5ca9ec0771
commit 45527c4cf4

View file

@ -281,37 +281,31 @@ class ticTacToeHandler(object):
command += val command += val
original_sender = message['sender_email'] original_sender = message['sender_email']
mydict = state_handler.get_state() with state_handler.state({}) as mydict:
if not mydict: user_game = mydict.get(original_sender)
state_handler.set_state({}) if (not user_game) and command == "new":
mydict = state_handler.get_state() user_game = TicTacToeGame(copy.deepcopy(initial_board))
mydict[original_sender] = user_game
user_game = mydict.get(original_sender) if command == 'new':
if (not user_game) and command == "new": if user_game and not first_time(user_game.board):
user_game = TicTacToeGame(copy.deepcopy(initial_board)) return_content = "You're already playing a game! Type **@tictactoe help** or **@ttt help** to see valid inputs."
mydict[original_sender] = user_game else:
return_content = "Welcome to tic-tac-toe! You'll be x's and I'll be o's. Your move first!\n"
if command == 'new': return_content += TicTacToeGame.positions
if user_game and not first_time(user_game.board): elif command == 'help':
return_content = "You're already playing a game! Type **@tictactoe help** or **@ttt help** to see valid inputs." 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."
else: 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 = "Hmm, I didn't understand your input. Type **@tictactoe help** or **@ttt help** to see valid inputs."
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."
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: if "Game over" in return_content or "draw" in return_content:
del mydict[original_sender] del mydict[original_sender]
state_handler.set_state(mydict)
bot_handler.send_message(dict( bot_handler.send_message(dict(
type = 'private', type = 'private',