def move_rematch(game_id): url = url_for('chess') app.logger.info(game_id) error = True try: game = Game.query.filter_by(id=game_id).first() app.logger.info(game) new_game = Game(player_one=game.player_two, player_two=game.player_one, time_limit=game.time_limit) Game.insert(new_game) answer = new_game.id app.logger.info(answer) if session['userId']: player = Player.query.filter_by(id=session['userId']).first() player.location = 'accepted ' + str(answer) Player.update(player) else: error = True db.session.rollback() except: app.logger.info(sys.exc_info()) error = True db.session.rollback() finally: db.session.close() if error: return json.dumps({'error': 'data base error'}) return json.dumps({'status': 'redirect', 'id': answer, 'url': url})
def move_commence(game_privacy, duration): auth = auth_auth('commence') if auth['success']: app.logger.info(auth) player_id = auth['user_id'] else: app.logger.info('player guest') player_id = auth_guest() try: app.logger.info('trying') if game_privacy == 'public': app.logger.info('we are game privacy') offer = Offer.query.filter_by(public=True).filter_by( time_limit=duration).first() if offer: app.logger.info('we are offer') new_game = Game(player_one=offer.player_one, player_two=player_id, time_limit=duration, offer_id=offer.id) Game.insert(new_game) game = new_game.id offer.delete() current_state = State(game_id=new_game.id, move_number=1, move='white', position=calculate_moves(), time_limit=duration, white_timer=duration, black_timer=duration) State.insert(current_state) db.session.close() return json.dumps({'status': 'redirect', 'id': game}) app.logger.info('we are here') offer = Offer(player_one=player_id, time_limit=duration) Offer.insert(offer) offer_id = offer.id db.session.close() return json.dumps({'status': 'waiting', 'offerId': offer_id}) except: app.logger.info(sys.exc_info()) error = True db.session.rollback() finally: db.session.close() if error: return json.dumps({'status': 'error'})