public void HandleDefendRequest(DefendRequestMessage message, int connectID)
    {
        PlayerData playerData  = serverDataHolder.players.Find(x => x.playerIndex == connectID);
        RoomData   currentRoom = serverDataHolder.rooms[playerData.roomID[0], playerData.roomID[1]];

        //monster adds player to target list
        Monster monster = serverDataHolder.activeMonsters.Find(x => x.roomID == playerData.roomID);

        if (!monster.targetPlayers.Contains(playerData.playerIndex))
        {
            monster.targetPlayers.Add(playerData.playerIndex);
        }

        //check treasure even has ammount
        if (currentRoom.containsMonster == false)
        {
            //if not then send request denied
            RequestDenied(message, connectID);
            return;
        }

        //then update data in player
        playerData.hp += 4;

        //send  message back to player
        PlayerDefendsMessage defendMessage = new PlayerDefendsMessage()
        {
            PlayerID = connectID,
            newHP    = (ushort)playerData.hp
        };

        SendMessage(defendMessage, connections[connectID]);

        NextPlayerTurn();
    }
Esempio n. 2
0
    /// <summary>
    /// Let the player defend in the game showing the shield.
    /// </summary>
    public void SendDefendRequest()
    {
        DefendRequestMessage defendRequest = new DefendRequestMessage();

        clientBehaviour.SendRequest(defendRequest);
        PlayerManager.Instance.CurrentPlayer.Shield.SetActive(true);
    }
    void Update()
    {
        networkJobHandle.Complete();

        for (int i = 0; i < connections.Length; ++i)
        {
            if (!connections[i].IsCreated)
            {
                connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        NetworkConnection newConnection;

        while ((newConnection = networkDriver.Accept()) != default)
        {
            if (connections.Length >= 4)
            {
                return;
            }
            connections.Add(newConnection);
            //Debug.Log("Accepted new connection");


            //new player data is set
            //Color col = Random.ColorHSV();
            Color col = ColorExtensions.colors[(ColorExtensions.RandomStartIndex + newConnection.InternalId) % ColorExtensions.colors.Length];
            col.a = 1;
            var colour         = (Color32)col;
            var playerID       = newConnection.InternalId;
            var welcomeMessage = new WelcomeMessage
            {
                PlayerID = playerID,
                Colour   = ((uint)colour.r << 24) | ((uint)colour.g << 16) | ((uint)colour.b << 8) | colour.a
            };
            SendMessage(welcomeMessage, newConnection);

            //save it to list
            PlayerData newData = new PlayerData();
            newData.color       = colour;
            newData.playerIndex = playerID;
            if (serverDataHolder.players == null)
            {
                serverDataHolder.players = new List <PlayerData>();
            }
            serverDataHolder.players.Add(newData);

            //Debug.Log("server data holder players count: " + serverDataHolder.players.Count);
        }

        DataStreamReader reader;

        for (int i = 0; i < connections.Length; ++i)
        {
            if (!connections[i].IsCreated)
            {
                continue;
            }

            NetworkEvent.Type cmd;
            while ((cmd = networkDriver.PopEventForConnection(connections[i], out reader)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    var messageType = (MessageHeader.MessageType)reader.ReadUShort();
                    switch (messageType)
                    {
                    case MessageHeader.MessageType.none:
                        StayAlive(i);
                        break;

                    case MessageHeader.MessageType.newPlayer:
                        break;

                    case MessageHeader.MessageType.welcome:
                        break;

                    case MessageHeader.MessageType.setName:
                        var message = new SetNameMessage();
                        message.DeserializeObject(ref reader);
                        messagesQueue.Enqueue(message);

                        PlayerData newPlayerData = GetPlayerData(connections[i]);
                        newPlayerData.name = message.Name;

                        NewPlayerJoined(connections[i]);

                        break;

                    case MessageHeader.MessageType.playerLeft:
                        break;

                    case MessageHeader.MessageType.moveRequest:
                        var moveRequest = new MoverequestMessage();
                        moveRequest.DeserializeObject(ref reader);
                        //messagesQueue.Enqueue(moveRequest);
                        bool canmove = HandleMoveRequest(moveRequest, i);
                        if (canmove)
                        {
                            NextPlayerTurn();
                        }
                        break;

                    case MessageHeader.MessageType.claimTreasureRequest:

                        var treasureRquest = new ClaimTreasureRequestMessage();
                        treasureRquest.DeserializeObject(ref reader);
                        HandleTreasureClaim(treasureRquest, i);
                        break;

                    case MessageHeader.MessageType.leaveDungeonRequest:
                        var leaveDungeonRequest = new LeavesDungeonRequestMessage();
                        leaveDungeonRequest.DeserializeObject(ref reader);
                        HandleLeaveDungeonRequest(leaveDungeonRequest, i);
                        break;

                    case MessageHeader.MessageType.defendRequest:
                        var defendRequest = new DefendRequestMessage();
                        defendRequest.DeserializeObject(ref reader);
                        HandleDefendRequest(defendRequest, i);
                        break;

                    case MessageHeader.MessageType.attackRequest:
                        var attackRequest = new AttackRequestMessage();
                        attackRequest.DeserializeObject(ref reader);
                        HandleAttackRequest(attackRequest, i);
                        break;

                    default:
                        break;
                    }
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    PlayerLeftMessage playerLeftMessage = new PlayerLeftMessage
                    {
                        PlayerLeftID = i
                    };

                    SendMessageToAll(playerLeftMessage);
                    Debug.Log("Client disconnected");
                    connections[i] = default;
                }
            }
        }

        networkJobHandle = networkDriver.ScheduleUpdate();

        ProcessMessagesQueue();
    }
Esempio n. 4
0
    public void RequestDefendAgainstMonster()
    {
        DefendRequestMessage message = new DefendRequestMessage();

        dataHolder.client.SendMessage(message);
    }
    public void Update()
    {
        networkJobHandle.Complete();

        for (int i = 0; i < connections.Length; ++i)
        {
            if (!connections[i].IsCreated)
            {
                connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        NetworkConnection c;

        while (!serverManager.game.started && serverManager.lobby.players.Count < ServerManager.maxPlayers && (c = networkDriver.Accept()) != default)
        {
            connections.Add(c);
            Debug.Log("Accepted connection");
            serverManager.HandleNewConnection(c.InternalId);
        }

        DataStreamReader reader;

        for (int i = 0; i < connections.Length; ++i)
        {
            if (!connections[i].IsCreated)
            {
                continue;
            }

            NetworkEvent.Type cmd;
            while ((cmd = networkDriver.PopEventForConnection(connections[i], out reader)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    var messageType = (Message.MessageType)reader.ReadUShort();
                    Debug.Log("Host Received: " + messageType + " from " + connections[i].InternalId);

                    Message message = null;
                    switch (messageType)
                    {
                    case Message.MessageType.None: break;     //stay alive

                    case Message.MessageType.SetName: message = new SetNameMessage(); break;

                    case Message.MessageType.MoveRequest: message = new MoveRequestMessage(); break;

                    case Message.MessageType.AttackRequest: message = new AttackRequestMessage(); break;

                    case Message.MessageType.DefendRequest: message = new DefendRequestMessage(); break;

                    case Message.MessageType.ClaimTreasureRequest: message = new ClaimTreasureRequestMessage(); break;

                    case Message.MessageType.LeaveDungeonRequest: message = new LeaveDungeonRequestMessage(); break;

                    default: break;
                    }
                    if (message != null)
                    {
                        message.DeserializeObject(ref reader);
                        receivedMessagesQueue.Enqueue(new AdressedMessage(message, connections[i].InternalId));
                    }
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    Debug.Log("Client disconnected");
                    serverManager.HandleDisconnect(connections[i].InternalId);
                    connections[i] = default;
                }
            }
        }

        ProcessMessagesQueue();

        //order slightly wrong but will be sent next cycle
        foreach (KeyValuePair <int, float> lastSendTime in lastSendTimes)
        {
            if (Time.time - lastSendTime.Value > STAY_ALIVE_AFTER_SECONDS)
            {
                QeueMessage(new AdressedMessage(new NoneMessage(), lastSendTime.Key));
            }
        }
        networkJobHandle = networkDriver.ScheduleUpdate();
    }