def verify(token): user = User.get_by_token(token) if user: user.verify() delete_cache(token) return send_success() return send_error('token not found')
def get(self, token): user = User.get_by_token(token) if user: user.verify() delete_cache(token) return 'verification completed' raise APINotFound('token')
def new_game(cls, white_token, black_token, type_game, time_limit, white_user=None, black_user=None): game = cls(white_token, black_token) game.game = engine.Game() game.model = models.Game.create( white=white_token, black=black_token, player_white=white_user, player_black=black_user, state=str(game.game.board), type_game=type_game, time_limit=time_limit, ) delete_cache('wait_{}'.format(white_token)) delete_cache('wait_{}'.format(black_token)) game.send_ws(game.get_info(consts.WHITE), consts.WS_START, consts.WHITE) game.send_ws(game.get_info(consts.BLACK), consts.WS_START, consts.BLACK) return game
def invited(token): try: enemy_token, game_type, game_limit = get_cache( 'invite_{}'.format(token)) except: return send_error('game not found') enemy_user = None user_id = get_cache('user_{}'.format(enemy_token)) if user_id: try: enemy_user = User.get(pk=user_id) except User.DoesNotExist: # TODO: if user not found game will be created with None as white player pass user_token = generate_token(True) game = Game.new_game(enemy_token, user_token, game_type, game_limit, white_user=enemy_user, black_user=request.user) delete_cache('wait_{}'.format(enemy_token)) result = {'game': user_token} result.update(game.get_info(consts.BLACK)) return send_data(result)
def post(self, token): user = User.get_by_token(token) if not user: raise APINotFound('token') user.set_password(self.data['password']) user.save() delete_cache(token) return 'password changed'
def check_draw(self): if not self.model.ended: name1 = self._get_draw_name(consts.WHITE) name2 = self._get_draw_name(consts.BLACK) if get_cache(name1) and get_cache(name2): self.model.game_over(consts.END_DRAW) delete_cache(name1) delete_cache(name2) msg = self.get_info() self.send_ws(msg, consts.WS_DRAW, consts.WHITE) self.send_ws(msg, consts.WS_DRAW, consts.BLACK) self.onMove() return True return False
def test_use_cache(self): def _func(a, b): invert_color(a) return 'ok' func = use_cache(10, name='test_func')(_func) a, b = Game.create(white='1234', black='qwer'), delete_cache # run first time, no cache with patch('tests.decorators_t.invert_color') as mock: self.assertEqual(func(a, b=b), 'ok') mock.assert_called_once_with(a) # run second time, results from cached with patch('tests.decorators_t.invert_color') as mock: self.assertEqual(func(a, b=b), 'ok') self.assertFalse(mock.called) # delete cache and run again, no cache delete_cache(get_cache_func_name('test_func', a, b=b)) with patch('tests.decorators_t.invert_color') as mock: self.assertEqual(func(a, b=b), 'ok') mock.assert_called_once_with(a)
def post(self, game_id): try: pool = GamePool.get(GamePool.pk == game_id) except GamePool.DoesNotExist: raise errors.APINotFound('game') except Exception as e: raise errors.APIException('wrong format') if pool.user1 and pool.user1 == request.user: raise errors.APIException('you cannot start game with yourself') pool.player2 = generate_token(True) pool.user2 = request.user pool.is_started = True pool.save() game = Game.new_game(pool.player1, pool.player2, pool.type_game, pool.time_limit, white_user=pool.user1, black_user=pool.user2) delete_cache('wait_{}'.format(pool.player1)) result = {'game': pool.player2} result.update(game.get_info(consts.BLACK)) return result
def accept(game_id): try: pool = GamePool.get(GamePool.pk == game_id) except GamePool.DoesNotExist: return send_error('Game not found') except Exception as e: return send_error('Wrong format') if pool.user1 and pool.user1 == request.user: return send_error('You cannot start game with yourself') with config.DB.atomic(): pool.player2 = generate_token(True) pool.user2 = request.user pool.is_started = True pool.save() game = Game.new_game(pool.player1, pool.player2, pool.type_game, pool.time_limit, white_user=pool.user1, black_user=pool.user2) delete_cache('wait_{}'.format(pool.player1)) result = {'game': pool.player2} result.update(game.get_info(consts.BLACK)) return send_data(result)
def get(self): delete_cache(request.auth) response = make_response(jsonify({'message': 'logout successfully'})) response.set_cookie('auth', expires=0) return response
def onMove(self): for name in ('game_info_handler', 'game_moves_handler'): delete_cache(get_cache_func_name(name, token=self.white)) delete_cache(get_cache_func_name(name, token=self.black))
def draw_refuse(self, color=None): if self.model.ended: raise errors.EndGame color = self.get_color delete_cache(self._get_draw_name(color)) delete_cache(self._get_draw_name(invert_color(color)))
def _post(user, data): user.set_password(data['password']) user.save() delete_cache(token) return send_message('password changed')
def logout(): delete_cache(request.auth) response = make_response(send_message('logout successfully')) response.set_cookie('auth', expires=0) return response
def test_cache_1(self): self.assertEqual(get_cache('nokey'), None) set_cache('key', 'data') self.assertEqual(get_cache('key'), 'data') delete_cache('key') self.assertEqual(get_cache('key'), None)