Example #1
0
def scoreboard():
    current_app.logger.info(
        f"{request.method} Request for scoreboard from user { current_user.username } IP {request.remote_addr} "
    )
    if not session.is_active():
        flash("That game no longer exists", 'error')
        return "nogame"
    game = session.getSession()
    if request.method == 'POST':
        data = request.json
        if data['command'] == 'p1':
            game.Score(game.player1)
        elif data['command'] == 'p2':
            game.Score(game.player2)
        elif data['command'] == 'subp1':
            game.subtract(game.player1)
        elif data['command'] == 'subp2':
            game.subtract(game.player2)

    if game.getWinner():
        winner = WinnerData()
        winner.name = User.query.filter_by(
            id=game.getWinner().user).first_or_404().username
        winner.score = game.getScore(game.winner)
        if game.getWinner() == game.player1:
            winner.against = User.query.filter_by(
                id=game.player2.user).first_or_404().username
            winner.against_score = game.getScore(game.player2)
        else:
            winner.against = User.query.filter_by(
                id=game.player1.user).first_or_404().username
            winner.against_score = game.getScore(game.player1)
        if IS_RPI:
            winAnim = threading.Thread(target=WinAnimation,
                                       args=(
                                           matrix_obj,
                                           winner.name,
                                       ))
            winAnim.start()
        return render_template('control/win.html',
                               title="Game Won",
                               winner=winner)

    else:
        data = ScoreboardData()
        data.player1_score = game.getScore(game.player1)
        data.player2_score = game.getScore(game.player2)
        data.serving = game.getServe()
        player1_User = User.query.get(game.player1.user)
        player2_User = User.query.get(game.player2.user)
        if IS_RPI:
            Scores(matrix_obj, data.player1_score, data.player2_score,
                   data.serving, player1_User.initial, player2_User.initial)
        return render_template('control/scoreboard.html',
                               data=data,
                               player1_User=player1_User,
                               player2_User=player2_User)
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 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_end(self):
     game = Game(1, 2)
     session.setSession(game)
     session.endSession()
     self.assertFalse(session.getSession())
 def test_session_create(self):
     game = Game(1, 2)
     session.setSession(game)
     self.assertEqual(session.getSession(), game)