Пример #1
0
def enterGame(msg, db, gameMsg):
    gameId = msg['gameId']
    name = msg['name']

    sameNameExists = False
    players = [parsePlayer(i) for i in db.fetchall('SELECT name, handJSON FROM players WHERE gameId = %d; ' % gameId)]

    for player in players:
        if player['name'] == name:
            sameNameExists = True
            send({
                'error': {
                    'event': 'enterGame',
                    'reason': 'same name exists'
                    }
                }, json=True)
            break
    if not sameNameExists:
        db.execute("INSERT INTO players (gameId, name, handJSON, joined) VALUES (%d, '%s', '%s', 0)" % (gameId, name, '[]'))
        game = getGame(db, gameId)
        gameMsg.buildEnterGame()
        send({
            'event': 'enterGame',
            'message' : gameMsg.message,
            'game': game
            }, json=True, room=gameId)
        join_room(gameId)
        messages = [parseMessage(i) for i in db.fetchall("SELECT name, type, messageJSON, time FROM messages WHERE gameId = %d; " % gameId)]
        gameMsg.message['elements']['messages'] = messages
        send({
            'event': 'enterGame',
            'message' : gameMsg.message,
            'game': game
            }, json=True)
Пример #2
0
def playCard(msg, db, gameMsg):
    gameId = msg['gameId']
    name = msg['name']
    cardIndex = msg['cardIndex']

    gameRes = db.fetchone('SELECT gameJSON, deckJSON FROM games WHERE id=%d; ' % gameId)
    playerRes = db.fetchone("SELECT name, handJSON FROM players WHERE gameId = %d AND name='%s'" % (gameId, name))

    game = json.loads(gameRes[0])
    deck = json.loads(gameRes[1])
    player = parsePlayer(playerRes)

    playedCard = hanabi.playCard(game, deck, player, cardIndex)
    hanabi.endTurn(game)
    
    queries = []
    queries.append("UPDATE games SET gameJSON='%s', deckJSON='%s' WHERE id=%s" % (json.dumps(game), json.dumps(deck), gameId))
    queries.append("UPDATE players SET handJSON='%s' WHERE gameId=%d AND name='%s'" 
            % (json.dumps(player['hand']), gameId, player['name']))
    db.bulkExecute(queries)
        
    game = getGame(db, gameId)
    gameMsg.buildPlay(playedCard)
    send({
        'event': 'playCard',
        'message' : gameMsg.message,
        'game': game
        }, json=True, room=gameId)
Пример #3
0
def giveHint(msg, db, gameMsg):
    gameId = msg['gameId']
    hintType = msg['hintType']
    name = msg['name']
    toName = msg['toName']
    hint = int(msg['hint']) if hintType == 'NUMBER' else msg['hint']

    gameRes = db.fetchone('SELECT gameJSON FROM games WHERE id=%d; ' % gameId)
    toPlayerRes = db.fetchone("SELECT name, handJSON FROM players WHERE gameId = %d AND name='%s'" % (gameId, toName))

    game = json.loads(gameRes[0])
    toPlayer = parsePlayer(toPlayerRes)

    if hanabi.canHint(game, name):
        cardsHinted = hanabi.giveHint(game, toPlayer, hintType, hint)

        db.execute("UPDATE games SET gameJSON='%s' WHERE id=%s" % (json.dumps(game), gameId))
        db.execute("UPDATE players SET handJSON='%s' WHERE name='%s'" % (json.dumps(toPlayer['hand']), toPlayer['name']))

        game = getGame(db, gameId)
        gameMsg.buildHint(toName, hintType, hint, cardsHinted)
        send({ 
            'event': 'giveHint',
            'message' : gameMsg.message,
            'game': game
            }, json=True, room=gameId)
    else:
        send({
            'error': {
                'event': 'giveHint',
                'reason': 'invalid hint'
                }
            }, json=True)
Пример #4
0
def startGame(msg, db, gameMsg):
    gameId = msg['gameId']

    gameResult = db.fetchone('SELECT gameJSON FROM games WHERE id=%d; ' % gameId)
    players = db.fetchall('SELECT name, handJSON FROM players WHERE gameId = %d AND joined=1; ' % gameId)
    game = json.loads(gameResult[0])
    players = [parsePlayer(i) for i in players]

    if (not game['hasStarted']) and len(players) > 1:
        game['hasStarted'] = True

        deck = hanabi.startGameAndGetDeck(game, players)
        queries = []
        for player in players:
            queries.append("UPDATE players SET handJSON='%s' WHERE gameId=%d AND name='%s'" 
                    % (json.dumps(player['hand']), gameId, player['name']))
        queries.append("UPDATE games SET gameJSON='%s', deckJSON='%s' WHERE id=%s" 
                % (json.dumps(game), json.dumps(deck), gameId))
        db.bulkExecute(queries)   

        game = getGame(db, gameId)
        gameMsg.buildStartGame()
        send({
            'event': 'startGame',
            'message' : gameMsg.message,
            'game': game
            }, json=True, room=gameId)
    else:
        send({
            'error': {
                'event': 'startGame',
                'reason': 'game already started'
                }
            }, json=True)
Пример #5
0
def enterGame(msg, db, gameMsg):
    gameId = msg['gameId']
    name = msg['name']

    sameNameExists = False
    players = [
        parsePlayer(i) for i in db.fetchall(
            'SELECT name, handJSON FROM players WHERE gameId = %d; ' % gameId)
    ]

    for player in players:
        if player['name'] == name:
            sameNameExists = True
            send(
                {
                    'error': {
                        'event': 'enterGame',
                        'reason': 'same name exists'
                    }
                },
                json=True)
            break
    if not sameNameExists:
        db.execute(
            "INSERT INTO players (gameId, name, handJSON, joined) VALUES (%d, '%s', '%s', 0)"
            % (gameId, name, '[]'))
        game = getGame(db, gameId)
        gameMsg.buildEnterGame()
        send({
            'event': 'enterGame',
            'message': gameMsg.message,
            'game': game
        },
             json=True,
             room=gameId)
        join_room(gameId)
        messages = [
            parseMessage(i) for i in db.fetchall(
                "SELECT name, type, messageJSON, time FROM messages WHERE gameId = %d; "
                % gameId)
        ]
        gameMsg.message['elements']['messages'] = messages
        send({
            'event': 'enterGame',
            'message': gameMsg.message,
            'game': game
        },
             json=True)
Пример #6
0
def startGame(msg, db, gameMsg):
    gameId = msg['gameId']

    gameResult = db.fetchone('SELECT gameJSON FROM games WHERE id=%d; ' %
                             gameId)
    players = db.fetchall(
        'SELECT name, handJSON FROM players WHERE gameId = %d AND joined=1; ' %
        gameId)
    game = json.loads(gameResult[0])
    players = [parsePlayer(i) for i in players]

    if (not game['hasStarted']) and len(players) > 1:
        game['hasStarted'] = True

        deck = hanabi.startGameAndGetDeck(game, players)
        queries = []
        for player in players:
            queries.append(
                "UPDATE players SET handJSON='%s' WHERE gameId=%d AND name='%s'"
                % (json.dumps(player['hand']), gameId, player['name']))
        queries.append(
            "UPDATE games SET gameJSON='%s', deckJSON='%s' WHERE id=%s" %
            (json.dumps(game), json.dumps(deck), gameId))
        db.bulkExecute(queries)

        game = getGame(db, gameId)
        gameMsg.buildStartGame()
        send({
            'event': 'startGame',
            'message': gameMsg.message,
            'game': game
        },
             json=True,
             room=gameId)
    else:
        send(
            {
                'error': {
                    'event': 'startGame',
                    'reason': 'game already started'
                }
            },
            json=True)
Пример #7
0
def giveHint(msg, db, gameMsg):
    gameId = msg['gameId']
    hintType = msg['hintType']
    name = msg['name']
    toName = msg['toName']
    hint = int(msg['hint']) if hintType == 'NUMBER' else msg['hint']

    gameRes = db.fetchone('SELECT gameJSON FROM games WHERE id=%d; ' % gameId)
    toPlayerRes = db.fetchone(
        "SELECT name, handJSON FROM players WHERE gameId = %d AND name='%s'" %
        (gameId, toName))

    game = json.loads(gameRes[0])
    toPlayer = parsePlayer(toPlayerRes)

    if hanabi.canHint(game, name):
        cardsHinted = hanabi.giveHint(game, toPlayer, hintType, hint)

        db.execute("UPDATE games SET gameJSON='%s' WHERE id=%s" %
                   (json.dumps(game), gameId))
        db.execute("UPDATE players SET handJSON='%s' WHERE name='%s'" %
                   (json.dumps(toPlayer['hand']), toPlayer['name']))

        game = getGame(db, gameId)
        gameMsg.buildHint(toName, hintType, hint, cardsHinted)
        send({
            'event': 'giveHint',
            'message': gameMsg.message,
            'game': game
        },
             json=True,
             room=gameId)
    else:
        send({'error': {
            'event': 'giveHint',
            'reason': 'invalid hint'
        }},
             json=True)
Пример #8
0
def playCard(msg, db, gameMsg):
    gameId = msg['gameId']
    name = msg['name']
    cardIndex = msg['cardIndex']

    gameRes = db.fetchone(
        'SELECT gameJSON, deckJSON FROM games WHERE id=%d; ' % gameId)
    playerRes = db.fetchone(
        "SELECT name, handJSON FROM players WHERE gameId = %d AND name='%s'" %
        (gameId, name))

    game = json.loads(gameRes[0])
    deck = json.loads(gameRes[1])
    player = parsePlayer(playerRes)

    playedCard = hanabi.playCard(game, deck, player, cardIndex)
    hanabi.endTurn(game)

    queries = []
    queries.append(
        "UPDATE games SET gameJSON='%s', deckJSON='%s' WHERE id=%s" %
        (json.dumps(game), json.dumps(deck), gameId))
    queries.append(
        "UPDATE players SET handJSON='%s' WHERE gameId=%d AND name='%s'" %
        (json.dumps(player['hand']), gameId, player['name']))
    db.bulkExecute(queries)

    game = getGame(db, gameId)
    gameMsg.buildPlay(playedCard)
    send({
        'event': 'playCard',
        'message': gameMsg.message,
        'game': game
    },
         json=True,
         room=gameId)