コード例 #1
0
ファイル: fill_zobrist.py プロジェクト: qihua/katagui
def main():
    if len(sys.argv) != 2:
        usage(True)

    n = 0
    total = db.select('select count(*) from v_games_no_zobrist')[0]['count']

    while (1):
        rows = db.select('select * from v_games_no_zobrist limit 10')
        #rows = db.select( "select * from t_game where game_hash = 'a7161d79bb484e77'")
        #BP()
        if len(rows) == 0: break
        n += len(rows)
        print('Updating zobrist hashes %d/%d' % (n, total))
        for row in rows:
            try:
                game_hash = row['game_hash']
                game = dbmodel.Game(game_hash)
                if not game.data['game_record']:
                    print('No game record for game hash %s; ignoring' %
                          game_hash)
                    continue
                rec = game.data['game_record']['record']
                if not rec:
                    print('No moves for game hash %s; erasing game' %
                          game_hash)
                    db.update_row('t_game', 'game_hash', game_hash,
                                  {'game_record': ''})
                    continue
                if len(game.data['game_record']['var_record']) > len(rec):
                    rec = game.data['game_record']['var_record']
                moves = [x['mv'] for x in rec]
                rc0s = []
                # Convert to 0 based 0-18 (row,col) pairs
                for move in moves:
                    coord = move
                    if coord in ('pass', 'resign'):
                        rc0s.append(coord)
                        continue
                    point = go_utils.point_from_coords(coord)
                    rc0 = (point.row - 1, point.col - 1)
                    rc0s.append(rc0)
                zobrist = go_utils.game_zobrist(rc0s, ZOBRIST_MOVES)
                db.update_row('t_game', 'game_hash', game_hash,
                              {'zobrist': str(zobrist)})
                db.tstamp('t_game', 'game_hash', game_hash, 'ts_zobrist')
                print('updated %s' % game_hash)
            except Exception as e:
                print(
                    'Exception updating zobrist for game hash %s; erasing game'
                    % game_hash)
                db.update_row('t_game', 'game_hash', game_hash,
                              {'game_record': ''})

    print('Done')
コード例 #2
0
def update_game():
    """ Update a game in the database """
    try:
        data = request.json
        game_hash = current_user.data['game_hash']
        if not game_hash:
            msg = 'error: user %s is not in a game.' % current_user.data[
                'username']
            app.logger.info('>>>> ' + msg)
            return jsonify({'result': msg})
        game = dbmodel.Game(game_hash)
        game.update_db(data)

        # Tell all the watchers about the change.
        # This will wake up the other dynos and hit their WatcherSockets.send() in routes_watch.py
        nmoves = 0
        try:
            gr = game.data['game_record']
            nmoves = gr['n_visible']
            # Get a flask response and extract the json.
            # This deals with dates, which json.dumps does not.
            jsonstr = jsonify({
                'action':
                'update_game',
                'game_hash':
                game_hash,
                'game_data':
                game.data,
                'nmoves':
                nmoves,
                'client_timestamp':
                data.get('client_timestamp', 0)
            }).data
            redis.publish(REDIS_CHAN, jsonstr)
        except:
            app.logger.info('EXCEPTION in update_game(), ignored')

        color = 'B' if nmoves % 2 else 'W'
        app.logger.info('>>>>>>>>>>>>>>>>> update game %s %s %d' %
                        (str(game_hash), color, nmoves))
        return jsonify({'result': 'ok'})
    except:
        app.logger.info('ERROR: Exception in update_game()')
        return jsonify({'result': 'error: update_game() failed'})
コード例 #3
0
def create_game():
    """ Create a new game in the database """
    try:
        data = request.json
        data.update({'username': current_user.data['username']})
        game = dbmodel.Game()

        # Store user IP with the game
        if request.headers.getlist("X-Forwarded-For"):
            ip = request.headers.getlist("X-Forwarded-For")[0]
        else:
            ip = request.remote_addr
        data.update({'ip_addr': ip})

        game.update_db(data)
        current_user.data['game_hash'] = game.id
        current_user.update_db()
        return jsonify({'game_hash': game.id})
    except:
        app.logger.info('ERROR: Exception in create_game()')
        return redirect(url_for('index'))
コード例 #4
0
def load_game():
    """ Load a game from the database """
    game_hash = request.json['game_hash']
    game = dbmodel.Game(game_hash)
    res = jsonify(game.data)
    return res