Example #1
0
def _get_reposition_info(board, player_no, ruler_type):
    safe, war = [], []
    ruler_cell = board.is_ruler_placed("ruler-" + ruler_type, player_no)

    if ruler_cell:
        piece = board[ruler_cell].piece
        board[ruler_cell].piece = None
        build_board_data(board)

        safe = [
            cell_no
            for cell_no, cell in enumerate(board)
            if safe_ruler(board, cell_no, "ruler-" + ruler_type, player_no)
        ]
        war = _get_internal_war_info(board, player_no, ruler_type)

        board[ruler_cell].piece = piece
        build_board_data(board)

    return safe, war
Example #2
0
def drop_ruler(request, game_id, player_no, ruler, cell):
    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)

    if safe_ruler(board, cell, "ruler-" + ruler, player_no) and int(player_no) == g.current_turn:
        board.add_ruler(cell, ruler, player_no)
    else:
        return False

    g.increment_action()

    g.save()
    board.save()

    update_browsers(game_id)

    return game_state_json(request, game_id, player_no)
Example #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