Ejemplo n.º 1
0
def _deal_card(r, k, hans=[]):
    """
    PLAYER:: HAN => UNDECIDED | CAMP

    This will deal another card and push any hans either back to camp
    (death) or back to undecided.

    A single card is moved from a random point in the deck onto the
    right side of the table.
    """
    prior_card_names = [get_card(idx).name for idx in r.lrange(path(k, TABLE), 0, -1)]
    card_idx = r.spop(path(k, DECK))

    r.rpush(path(k, TABLE), card_idx)
    card = get_card(card_idx)
    _save_update(r, k, { CARD : card.name })

    if isinstance(card, Hazard) and card.name in prior_card_names:
        _save_update(r, k, { DEATH : { PLAYERS: sorted([h[NAME] for h in hans]),
                                       CARD: card.name } })
        # save intermediate game state for death
        for han in hans:
            r.hset(path(k, PLAYERS, han[NAME]), STATE, DEATH)

        _save_game_state(r, k, True)
        _publish_update(r, k)

        coro_sleep(DEATH_SLEEP)
        new_state = CAMP
    else:
        new_state = UNDECIDED
        if hans:
            _save_update(r, k, { HAN : { PLAYERS : sorted([h[NAME] for h in hans])}})
        if isinstance(card, Artifact):
            artifacts_seen_count = r.incr(path(k, ARTIFACTS_SEEN_COUNT))
            _save_update(r, k, { ARTIFACTS_SEEN_COUNT: int(artifacts_seen_count) })

    for han in hans:
        r.hset(path(k, PLAYERS, han[NAME]), STATE, new_state)
Ejemplo n.º 2
0
def _advance_game_state(r, k):
    """
    GAME STATE MACHINE

    ALL LOGIZ HURR
    """
    if not r.zscore(GAMES, k):
        _initialize_game(r, k)

    players = _get_players(r, k)
    if len(players) < 2:
        if players[0][STATE] == CAMP:
            _save_update(r, k, {MORE_PLAYERS: True})
    elif r.get(path(k, ROUND)) == DONE:
        pass
    elif any(p[STATE] == JOINED for p in players):
        pass
    elif all(p[STATE] == CAMP for p in players):
        if int(r.scard(path(k, ARTIFACTS_UNSEEN))) > 0:
            _next_round(r, k, players) # CAMP => UNDECIDED
        else:
            _game_over(r, k, players) # CAMP => WON | LOST
    elif not any(p[STATE] == UNDECIDED for p in players):
        landos = filter(lambda p: p[STATE] == LANDO, players)
        hans = filter(lambda p: p[STATE] == HAN, players)
        assert len(landos) + len(hans) > 0

        # let everyone see everyone's moves
        _save_game_state(r, k, True)
        _publish_update(r, k)
        coro_sleep(DEAL_SLEEP)

        if len(landos):
            _take_loot(r, k, landos)  # LANDO => CAMP
        if len(hans):  # HAN => CAMP | UNDECIDED
            _deal_card(r, k, hans)
        _advance_game_state(r, k)