Example #1
0
def new_game():
    current_app.logger.info(
        f"{request.method} Request for new_game from user {current_user.username} IP {request.remote_addr} "
    )
    form = NewGame_form()
    users = User.query.all()
    form.player1.choices = [(user.id, user.username) for user in users]
    form.player2.choices = [(user.id, user.username) for user in users]
    if form.validate_on_submit():
        current_app.logger.debug(
            f"New game form validated from user {current_user.username}")
        if not session.is_active():
            sessionid = game_data.getLast_Index()
            if sessionid:
                session.setSessionId(sessionid)
            game = ttGame(form.player1.data, form.player2.data)
            if form.serving.data == 'p1':
                game.setServe(game.player1)
            else:
                game.setServe(game.player2)
            current_app.logger.info(
                f"Game created: P1:{form.player1.data}, P2{form.player2.data}, first serve:{form.serving.data}"
            )
            session.setSession(game)
            current_app.logger.info(
                f"Redirecting to control.game id{session.getSessionId()}")
            return redirect(
                url_for('control.game', gameid=session.getSessionId()))
        else:
            current_app.logger.debug("Game session already in progress")
            flash('Game session already created', category='error')
    return render_template('/control/newgame.html',
                           title="New Game",
                           form=form)
Example #2
0
def save_game():
    current_app.logger.info(
        f"{request.method} Request for save_game from user { current_user.username } IP {request.remote_addr} "
    )
    if session.is_active():
        game = session.getSession()
        game_data.saveGame(
            session.getSessionId(),
            game.player1.user,
            game.player2.user,
            game.getScore(game.player1),
            game.getScore(game.player2),
        )
        UserStats(User.query.get(game.player1.user)).update_stats()
        UserStats(User.query.get(game.player2.user)).update_stats()
        session.endSession()
        flash('Game saved', category='success')
    return redirect(url_for('main.index'))
Example #3
0
def index():
    current_app.logger.info(
        f"{request.method} Request for index from user { current_user.username } IP {request.remote_addr} "
    )
    if session.is_active():
        currentSession = session.getSessionId()
    else:
        currentSession = None
    games = game_data.getIndex()
    allusers = User.query.all()

    return render_template('main/index.html',
                           allusers=allusers,
                           user=current_user,
                           title="Home",
                           reversed=reversed,
                           currentSession=currentSession,
                           games=games,
                           game_data=game_data,
                           User=User)
Example #4
0
def game(gameid):
    current_app.logger.info(
        f"{request.method} Request for game from user { current_user.username } IP {request.remote_addr} "
    )
    currentId = session.getSessionId()
    if int(gameid) != currentId:
        current_app.logger.info(
            f"Redirecting to main.index, No game session with id {gameid} found"
        )
        return redirect(url_for('main.index'))
    game = session.getSession()
    player1 = User.query.filter_by(
        id=game.player1.user).first_or_404().username
    player2 = User.query.filter_by(
        id=game.player2.user).first_or_404().username
    players = {'player1': player1, 'player2': player2}
    current_app.logger.debug(f"Game view created, players {players}")
    return render_template('control/game.html',
                           title="Game in Progress",
                           players=players)
 def test_session_id(self):
     game = Game(1, 2)
     session.setSessionId(5)
     session.setSession(game)
     self.assertEqual(session.getSessionId(), 6)