Esempio n. 1
0
def chess():
    html = read_txt('chess.html')
    html = fill_page(html)
    if session:

        html += '<p><a href=/chess/new>Start Game</a></p>'

        html += '<h2>Active Games</h2>'
        active_games = sql_query(
            'SELECT * FROM chessGames WHERE (player1=? OR player2=?) AND start=1',
            session['uid'], session['uid'])

        if len(active_games) > 0:
            for g in active_games:
                html += '<p><a href=/chess/game/' + str(g[0]) + '>' + str(
                    g[0]) + ': ' + get_uname(g[1]) + ' v. ' + get_uname(
                        g[2]) + '</a></p>'
        else:
            html += 'No active games.'

        html += '\n<h2>Pending Games</h2>'
        pending_games = sql_query(
            'SELECT * FROM chessGames WHERE (player1=? OR player2=?) AND start=0',
            session['uid'], session['uid'])
        if len(pending_games) > 0:
            for g in pending_games:
                html += '<p><a href=/chess/accept/' + str(g[0]) + '>' + str(
                    g[0]) + ': ' + get_uname(g[1]) + ' v. ' + get_uname(g[2])
        else:
            html += 'No pending games.'
    else:
        html += '<p>You must log in to play chess.</p>'

    return render_template_string(html, view_name='chess', login=login_text())
Esempio n. 2
0
def player_turn(game_id):
    turn = get_moves(game_id)[0][2]
    player1, player2 = sql_query(
        'SELECT (player1, player2) FROM chessGames WHERE gameId=?', game_id)[0]
    if turn % 2 == 0:
        return player1
    else:
        return player2
Esempio n. 3
0
def chess_accept(game_id):
    if session:
        game_record = sql_query('SELECT * FROM chessGames WHERE id=?',
                                game_id)[0]
        if game_record[2] == session['uid']:
            sql_execute('UPDATE chessGames SET start=1 WHERE id=?', game_id)
            return redirect(url_for('chess'))
        else:
            return redirect(url_for('chess'))
Esempio n. 4
0
def submit_move(move, game_id):
    turn = sql_query(
        'SELECT TOP 1 turn FROM chessMoves WHERE gameId=? ORDER BY turn DESC)',
        game_id).fetchone()[0]
    turn += 1

    startX, startY, endX, endY = unpack_move(move)
    if is_valid_move(startX, startY, endX, endY, game_id):
        sql_execute(
            'INSERT INTO chessMoves (gameId, turn, startX, startY, endX, endY) VALUES (?,?,?,?,?,?',
            game_id, turn, startX, startY, endX, endY)
        return True
    return False
Esempio n. 5
0
def chess_new():
    if session:
        if request.method == 'POST':
            if len(
                    sql_query('SELECT uid users WHERE name=?',
                              request.form['player'])) == 1:
                sql_execute(
                    'INSERT INTO chessGames (player1, player2, start) VALUES(?,?,0)',
                    session['uid'], get_uid(request.form['player']))
            return redirect(url_for('chess'))
        else:
            return render_page('chess_new')
    else:
        abort(403)
Esempio n. 6
0
def chess_game(game_id):
    player1 = ''
    player2 = ''
    board = ''
    html = read_txt('chess_game.html', dir_path='app/views/parts')

    game_record = sql_query('SELECT * FROM chessGames WHERE id=?', game_id)
    if len(game_record) == 1:
        game_record = game_record[0]
        player1 = game_record[1]
        player2 = game_record[2]
        board = board_txt(game_id)
        html += '<code><PRE>' + board.replace('\n', '<br/>') + '</PRE></code>'
        if session:
            if game_record[3]:
                # Game is started.
                if request.method == 'POST':
                    if session['uid'] == player_turn(game_id):
                        # Process submitted move.
                        move = request.form['move']
                        if is_valid_move_syntax(move):
                            if submit_move(move, game_id):
                                return redirect(url_for('chess'))
                            else:
                                html += '<p><font color=red>Invalid move.</font></p>'
                        else:
                            html += '<p><font color=red>Invalid move syntax.</font></p>'
                    else:
                        html += "<p>It's not your turn.</p>"
            else:
                html += '<p>This game has not started yet.</p>'
                if session['uid'] == player2:
                    html += '<p><a href=/chess/accept/' + game_id + '>Accept</a></p>'
        else:
            if game_record[4]:
                pass  # Is public game
            else:
                html += '<p>You do not have access to this game.</p>'
    else:
        html += '<p>Game does not exist.</p>'
    html = fill_page(html)
    return render_template_string(html,
                                  game_id=game_id,
                                  player1=player1,
                                  player2=player2,
                                  board=board)
Esempio n. 7
0
def login():
    if request.method == 'POST':
        # Get user record from SQLite database user.db
        print(request.form['username'])
        udata = sql_query('SELECT * FROM users WHERE name=?',
                          request.form['username'])[0]

        # Check hash from db against hash of salt + provided password
        if udata[3] == hashlib.sha256(
            (udata[2] + request.form['password']).encode()).hexdigest():
            session['uid'] = udata[0]
            return redirect(url_for('index'))
        else:
            return render_page(
                'login',
                foot='<p><font> color=red>Invalid Credentials</font></p>')

    return render_page('login')
Esempio n. 8
0
def get_moves(game_id):
    return sql_query(
        'SELECT * FROM chessMoves WHERE gameId=? ORDER BY turn ASC', game_id)