Exemple #1
0
def currentTurn():
    """broadcasts information about current player and his tokens on hand"""
    player = cv.getCurrentPlayer()
    tokens = []
    for i in player["tokens"]:
        tokens.append(cv.getTokenHand(i, player["color"]))
        tokens[-1]["color"] = tokens[-1]["color"].real
    broadcast({"action": "turn", "player": player["name"], "tokens": tokens, "stackSize": player["stack_size"]})
Exemple #2
0
def nextTurn():
    """changes turn for next player"""
    player = cv.getNextPlayer()
    game["current_player"] = player["name"]
    tokens = []
    for i in player["tokens"]:
        tokens.append(cv.getTokenHand(i, player["color"]))
        tokens[-1]["color"] = tokens[-1]["color"].real
    if tokens.__len__() == 0:
        # bitwa koncowa
        game["state"] = 2
        performBattle()
    elif isBoardFull():
        performBattle()
    elif game["players"][player["name"]]["life"] < 1:
        nextTurn()
    else:
        broadcast({"action": "turn", "player": player["name"], "tokens": tokens, "stackSize": player["stack_size"]})
Exemple #3
0
def messageFromPlayer(request, socket, context, message):
    """handles messages from players"""
    global game
    print ">>>> ", message
    act = message["action"]

    # oczekiwanie na graczy: join, getColor, getPlayers, ready
    if act == "join":
        if game["state"] != 0:
            # gra juz trwa
            socket.send({"action": "joinState", "joined": False, "reason": "gameInProgress"})
        elif game["players"].__len__() > 3:
            # za duzo graczy
            socket.send({"action": "joinState", "joined": False, "reason": "playersCount"})
        elif message["name"] in game["players"]:
            # gracz o podanym imieniu istnieje
            socket.send({"action": "joinState", "joined": False, "reason": "nameNotAvailable"})
        else:
            game["players"][message["name"]] = {
                "ready": False,
                "socket": socket.session.session_id,
                "color": Color.NONE.real,
                "life": 20,
            }
            socket.send({"action": "joinState", "joined": True, "playerName": message["name"]})
            sendPlayersList()
    elif act == "getColor":
        socket.send({"action": "setColor", "color": game["players"][getNameBySocket(socket)]["color"]})
    elif act == "getPlayers":
        socket.send({"action": "playersList", "list": game["players"]})
    elif act == "ready":
        all_ready = True
        for p in game["players"].itervalues():
            if p["socket"] == socket.session.session_id:
                p["ready"] = message["is_ready"]
            if not p["ready"]:
                all_ready = False
        sendPlayersList()
        if all_ready:
            if game["players"].__len__() > 1:
                # colors = (Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN)
                i = 1
                for name in game["players"].iterkeys():
                    game["players"][name]["color"] = i
                    cv.addPlayer(name)
                    i = i + 1
                game["state"] = 1
                broadcast({"action": "gameState", "state": 1})
                player = cv.getCurrentPlayer()
                tokens = []
                game["current_player"] = player["name"]
                for i in player["tokens"]:
                    tokens.append(cv.getTokenHand(i, player["color"]))
                    tokens[-1]["color"] = tokens[-1]["color"].real
                broadcast(
                    {"action": "turn", "player": player["name"], "tokens": tokens, "stackSize": player["stack_size"]}
                )

                # wlasciwa rozgrywka
    elif act == "nextTurn":
        if getNameBySocket(socket) == game["current_player"]:
            nextTurn()
        else:
            sendError(socket, "notYourTurn")
    elif act == "putToken":
        if getNameBySocket(socket) == game["current_player"]:
            color = Color.values[game["players"][getNameBySocket(socket)]["color"]]
            added = cv.addToken(message["id"], color, message["q"], message["r"], message["angle"])
            if added:
                token = cv.getTokenBoard(message["q"], message["r"])
                broadcast(
                    {
                        "action": "tokenAdded",
                        "color": color.real,
                        "id": message["id"],
                        "name": token["name"],
                        "q": message["q"],
                        "r": message["r"],
                        "angle": message["angle"],
                    }
                )
                currentTurn()
            else:
                sendError(socket, "addFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "throw":
        if getNameBySocket(socket) == game["current_player"]:
            thrown = cv.throwToken(message["id"], Color.values[game["players"][getNameBySocket(socket)]["color"]])
            if thrown:
                currentTurn()
            else:
                sendError(socket, "throwFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "currentPlayer":
        currentTurn()
    elif act == "move":
        if getNameBySocket(socket) == game["current_player"]:
            player = cv.getCurrentPlayer()
            token = cv.getTokenBoard(message["src_q"], message["src_r"])
            if token.__len__() == 0:
                sendError(socket, "moveFailed")
                return
            moved = cv.actionTokenMove(
                message["token"],
                player["color"],
                message["src_q"],
                message["src_r"],
                message["dst_q"],
                message["dst_r"],
            )
            if moved:
                # broadcast: info o przesunietym tokenie
                broadcast(
                    {
                        "action": "tokenMoved",
                        "id": token["id"],
                        "color": token["color"].real,
                        "src_q": message["src_q"],
                        "src_r": message["src_r"],
                        "dst_q": message["dst_q"],
                        "dst_r": message["dst_r"],
                        "name": token["name"],
                    }
                )
                currentTurn()
            else:
                sendError(socket, "moveFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "push":
        if getNameBySocket(socket) == game["current_player"]:
            player = cv.getCurrentPlayer()
            token = cv.getTokenBoard(int(message["src_q"]), int(message["src_r"]))
            if token.__len__() == 0:
                sendError(socket, "pushFailed")
                return
            pushed = cv.getTokenBoard(int(message["dst_q"]), int(message["dst_r"]))
            if pushed.__len__() == 0:
                sendError(socket, "pushFailed")
                return
            moved = cv.actionTokenPush(
                int(message["token"]),
                player["color"],
                int(message["src_q"]),
                int(message["src_r"]),
                int(message["dst_q"]),
                int(message["dst_r"]),
            )
            if moved:
                pushed_q = 2 * int(message["dst_q"]) - int(message["src_q"])
                pushed_r = 2 * int(message["dst_r"]) - int(message["src_r"])
                broadcast(
                    {
                        "action": "tokenMoved",
                        "id": pushed["id"],
                        "color": pushed["color"].real,
                        "src_q": message["dst_q"],
                        "src_r": message["dst_r"],
                        "dst_q": pushed_q,
                        "dst_r": pushed_r,
                        "name": pushed["name"],
                    }
                )
                currentTurn()
            else:
                sendError(socket, "pushFailed")
        else:
            sendError(socket, "notYourTurn")
    elif act == "battle":
        if getNameBySocket(socket) == game["current_player"]:
            player = cv.getCurrentPlayer()
            performBattle(int(message["token"]), player["color"])
        else:
            sendError(socket, "notYourTurn")
    elif act == "getPushes":
        socket.send({"action": "pushes", "pushes": cv.getPushes(int(message["q"]), int(message["r"]))})
    elif act == "getMoves":
        socket.send({"action": "moves", "moves": cv.getMoves(int(message["q"]), int(message["r"]))})
    elif act == "getBoard":
        socket.send({"action": "board", "board": getBoard()})
        print cv.getBoard()