Exemplo n.º 1
0
def defend_commit(request, game_id, player_no, tile_count):
    g = Game.objects.get(id=int(game_id))
    if not g.state.startswith("DEFEND"):
        return False

    board = StandardBoard(g, 1)
    build_board_data(board)
    p = g.__getattribute__("player_" + player_no)
    hand = Hand.objects.filter(player=p, turn_no=1, game=g).get()

    civ = g.state.split("|")[1]

    if not int(player_no) == g.waiting_for:
        return False
    if not hand.batch_remove(civ, tile_count):
        return False

    hand.save()

    defend_info = _get_defend_info(g, board)

    losing_kindom = None
    if len(defend_info["attack_board"]) + int(defend_info["attack_committed"]) > len(defend_info["defend_board"]) + int(
        tile_count
    ):
        losing_kingdom = _get_defender_kingdom(g, board)
        winner_no = _get_attacker(g, board)
    else:
        losing_kingdom = _get_attacker_kingdom(g, board)
        winner_no = player_no

    points = board.external_war_removal(losing_kingdom, civ)
    _give_points(game=g, player_no=winner_no, count=points, type=civ)

    build_board_data(board)

    unification_cell_no = board.find_unification_tile()
    more_war = external_war_tile(board, unification_cell_no)
    if more_war:
        g.state = "CHOOSE_COLOR"
        g.waiting_for = g.current_turn
    else:
        board[unification_cell_no].piece = _convert(board[unification_cell_no].special.piece)
        board[unification_cell_no].special = None
        build_board_data(board)

        treasure = board.treasure_to_claim()
        if treasure:
            g.waiting_for = treasure["player_no"]
            g.state = "TREASURE"
        else:
            g.state = "REGULAR"
            g.increment_action()

    g.save()
    board.save()

    update_browsers(game_id)

    return game_state_json(request, game_id, player_no)
Exemplo n.º 2
0
def external_war(request, game_id, player_no, civ, cell):
    civ = int(civ)
    cell = int(cell)

    g = Game.objects.get(id=int(game_id))
    board = StandardBoard(g, 1)
    build_board_data(board)
    p = g.__getattribute__("player_" + player_no)
    hand = Hand.objects.filter(player=p, turn_no=1, game=g).get()

    moves = []
    civ_obj = _convert(hand.__getattribute__("piece" + str(civ)))

    if civ_obj.name() == "civ-farm":
        moves = [
            cell_no for cell_no, cell_obj in enumerate(board) if external_war_tile(board, cell_no, is_ground=False)
        ]
    else:
        moves = [cell_no for cell_no, cell_obj in enumerate(board) if external_war_tile(board, cell_no, is_ground=True)]

    if cell in moves and int(player_no) == g.current_turn:
        board.place_unification(cell, _convert(hand.__getattribute__("piece" + str(civ))))
        hand.remove(civ)
        g.state = "CHOOSE_COLOR"
        g.waiting_for = player_no
    else:
        return False

    g.current_turn = player_no
    g.save()

    board.save()
    hand.save()

    update_browsers(game_id)

    return game_state_json(request, game_id, player_no)
Exemplo n.º 3
0
def game_state_json(request, game_id, player_no, options={}):
    g = Game.objects.get(id=int(game_id))
    board = StandardBoard(g, 1)
    build_board_data(board)

    p = g.__getattribute__("player_" + player_no)
    hand = Hand.objects.filter(player=p, turn_no=1, game=g).get()

    ground_moves = [cell_no for cell_no, cell in enumerate(board) if safe_tile(board, cell_no, is_ground=True)]
    war_ground_moves = [
        cell_no for cell_no, cell in enumerate(board) if external_war_tile(board, cell_no, is_ground=True)
    ]

    river_moves = [cell_no for cell_no, cell in enumerate(board) if safe_tile(board, cell_no, is_ground=False)]

    safe_temples = [
        cell_no for cell_no, cell in enumerate(board) if safe_ruler(board, cell_no, "ruler-temple", player_no)
    ]
    safe_settlements = [
        cell_no for cell_no, cell in enumerate(board) if safe_ruler(board, cell_no, "ruler-settlement", player_no)
    ]
    safe_farms = [cell_no for cell_no, cell in enumerate(board) if safe_ruler(board, cell_no, "ruler-farm", player_no)]
    safe_merchants = [
        cell_no for cell_no, cell in enumerate(board) if safe_ruler(board, cell_no, "ruler-merchant", player_no)
    ]

    safe_reposition_merchants, war_reposition_merchants = _get_reposition_info(board, player_no, "merchant")
    safe_reposition_temples, war_reposition_temples = _get_reposition_info(board, player_no, "temple")
    safe_reposition_settlements, war_reposition_settlements = _get_reposition_info(board, player_no, "settlement")
    safe_reposition_farms, war_reposition_farms = _get_reposition_info(board, player_no, "farm")

    war_temples = _get_internal_war_info(board, player_no, "temple")
    war_settlements = _get_internal_war_info(board, player_no, "settlement")
    war_farms = _get_internal_war_info(board, player_no, "farm")
    war_merchants = _get_internal_war_info(board, player_no, "merchant")

    tiles = [hand.piece0, hand.piece1, hand.piece2, hand.piece3, hand.piece4, hand.piece5]

    player_no_prefix = "player_" + player_no + "_points_"
    points = {}
    for civ in ["temple", "settlement", "farm", "merchant", "treasure"]:
        points[civ] = g.__getattribute__(player_no_prefix + civ)

    attack_info = _get_attack_info(g, board)
    defend_info = _get_defend_info(g, board)

    defend_internal_info = _get_defend_internal_info(g, board)

    treasure_info = board.treasure_to_claim()
    if not treasure_info or player_no != treasure_info["player_no"]:
        treasure_info["must_choose"] = []
        treasure_info["can_choose"] = []
        treasure_info["num_choose"] = 0

    state = (g.waiting_for == int(player_no)) and g.state or "NOTYOU"

    json_obj = {
        "legal_ground_moves": ground_moves,
        "war_ground_moves": war_ground_moves,
        "legal_river_moves": river_moves,
        "legal_ruler_moves": {
            "temple": safe_temples,
            "settlement": safe_settlements,
            "farm": safe_farms,
            "merchant": safe_merchants,
        },
        "legal_ruler_repositions": {
            "temple": safe_reposition_temples,
            "settlement": safe_reposition_settlements,
            "farm": safe_reposition_farms,
            "merchant": safe_reposition_merchants,
        },
        "war_ruler_moves": {
            "temple": war_temples,
            "settlement": war_settlements,
            "farm": war_farms,
            "merchant": war_merchants,
        },
        "war_ruler_repositions": {
            "temple": war_reposition_temples,
            "settlement": war_reposition_settlements,
            "farm": war_reposition_farms,
            "merchant": war_reposition_merchants,
        },
        "temple_count": hand.count("t"),
        "player_hand": tiles,
        "unification": board.get_cell_no_for_unification(),
        "temple_civ": board.get_cell_no_for_civ("t") + board.get_cell_no_for_civ("T") + board.get_cell_no_for_civ("T*"),
        "settlement_civ": board.get_cell_no_for_civ("s"),
        "farm_civ": board.get_cell_no_for_civ("f"),
        "merchant_civ": board.get_cell_no_for_civ("m"),
        "treasure-normal": board.get_cell_no_for_civ("T"),
        "treasure-corner": board.get_cell_no_for_civ("T*"),
        "points": {
            "temple": points["temple"],
            "settlement": points["settlement"],
            "farm": points["farm"],
            "merchant": points["merchant"],
            "treasure": points["treasure"],
        },
        "war_choices": _find_war_choices(board),
        "attack": {
            "tiles_available": attack_info["tiles_available"],
            "attack_board": attack_info["attack_board"],
            "defend_board": attack_info["defend_board"],
        },
        "defend": {
            "tiles_available": defend_info["tiles_available"],
            "defend_board": defend_info["defend_board"],
            "attack_committed": defend_info["attack_committed"],
            "attack_board": defend_info["attack_board"],
        },
        "defend_internal": {
            "tiles_available": defend_internal_info["tiles_available"],
            "defend_board": defend_internal_info["defend_board"],
            "attack_committed": defend_internal_info["attack_committed"],
            "attack_board": defend_internal_info["attack_board"],
        },
        "treasure_info": {
            "must_choose": treasure_info["must_choose"],
            "can_choose": treasure_info["can_choose"],
            "num_choose": treasure_info["num_choose"],
        },
        "stats": {
            "turn_no": g.turn_no,
            "action_no": g.action_no,
            "current_turn_no": g.current_turn,
            "waiting_on": g.__getattribute__("player_" + str(g.waiting_for)).user_name,
        },
        "rulers": dict(
            [
                (
                    player_no,
                    dict(
                        [
                            (ruler_type, board.get_cell_no_for_player_no_and_ruler(player_no, ruler_type[0]))
                            for ruler_type in ["temple", "settlement", "farm", "merchant"]
                        ]
                    ),
                )
                for player_no in xrange(1, g.num_players + 1)
            ]
        ),
        "hand_counts": dict(
            [
                (
                    player_no,
                    Hand.objects.filter(player=g.__getattribute__("player_" + str(player_no)), turn_no=1, game=g)
                    .get()
                    .total_pieces(),
                )
                for player_no in xrange(1, g.num_players + 1)
            ]
        ),
        "state": state,
    }
    json_obj.update(options)

    resp = HttpResponse(simplejson.dumps(json_obj, indent=2))
    resp.headers["Content-Type"] = "text/javascript"

    return resp