Exemplo n.º 1
0
        protected override void OnHandle(IPacket packet, NetState netState, ServerWorldState worldState)
        {
            ChatStatusPacket packetToHandle  = (ChatStatusPacket)packet;
            ServerCharacter  serverCharacter = (ServerCharacter)worldState.GetCharacter(netState.WorldId);

            if (serverCharacter != null)
            {
                foreach (int mapCharacterId in worldState.Characters)
                {
                    ServerCharacter  characterToUpdate = (ServerCharacter)worldState.GetCharacter(mapCharacterId);
                    NetState         clientSendTo      = characterToUpdate.Owner;
                    ChatStatusPacket packetToSend;

                    if (serverCharacter.IsDead && !characterToUpdate.IsDead)
                    {
                        packetToSend = new ChatStatusPacket(serverCharacter.Id, packetToHandle.State);
                    }
                    else
                    {
                        packetToSend = new ChatStatusPacket(serverCharacter.Id, packetToHandle.State);
                    }

                    clientSendTo.Send(packetToSend);
                }
            }
        }
Exemplo n.º 2
0
    public void ReceiveReplicatedGameObjects(ServerWorldState state)
    {
        receivedGameObjects.Clear();

        // Update objects
        for (int i = 0; i < state.ObjectStatesLength; i++)
        {
            NetworkObjectState objectState = state.GetObjectStates(i);
            INetworkGameObject go          = ReceiveReplicatedGameObject(objectState);
            receivedGameObjects.Add(go);
        }

        // Destroy objects we didn't receive in this update
        foreach (INetworkGameObject go in previouslyReceivedGameObjects)
        {
            if (!receivedGameObjects.Contains(go))
            {
                linkingContext.RemoveGameObject(go);
                go.Destroy();
            }
        }

        // Swap the previous and current sets
        HashSet <INetworkGameObject> temp = previouslyReceivedGameObjects;

        previouslyReceivedGameObjects = receivedGameObjects;
        receivedGameObjects           = temp;
    }
Exemplo n.º 3
0
    public void ReplicateState(ServerWorldState state)
    {
        if (!IsConnected())
        {
            return;
        }

        replicationManager.ReceiveReplicatedGameObjects(state);
    }
Exemplo n.º 4
0
        protected override void OnHandle(IPacket packet, NetState netState, ServerWorldState worldState)
        {
            AimToPacket     packetToHandle  = (AimToPacket)packet;
            ServerCharacter serverCharacter = (ServerCharacter)worldState.GetCharacter(netState.WorldId);

            if (serverCharacter != null)
            {
                serverCharacter.SetAimToPosition(new Point(packetToHandle.X, packetToHandle.Y));
                worldState.SendStatChangeToMapCharacters(serverCharacter);
            }
        }
Exemplo n.º 5
0
    private void ProcessServerState()
    {
        ServerWorldState[] copyServerState = new ServerWorldState[serverStateBuffer.Count];
        serverStateBuffer.CopyTo(copyServerState);
        serverStateBuffer.Clear();
        foreach (ServerWorldState serverState in copyServerState)
        {
            if (GameManager.players.TryGetValue(serverState.clientId, out PlayerManager player))
            {
                CharacterController characterController = player.GetComponent <CharacterController>();
                if (player.id == Client.instance.myId)
                {
                    ClientPlayerState clientStateAtTick = stateBuffer.Find(state => state.tick == serverState.tick);
                    if (clientStateAtTick != null && AreEqualPositionsWithMargin(serverState.position, clientStateAtTick.position))
                    {
                        Debug.Log($"Positions are equal at tick {serverState.tick} - {serverState.position}");
                        continue; // Skip when they're the same
                    }
                    else if (clientStateAtTick != null)
                    {
                        Debug.Log($"Positions at tick {serverState.tick} diverged with server at {serverState.position} and client at {clientStateAtTick.position}");
                    }
                    // Set server position to player
                    ApplyMovementDelta(GetDeltaPosition(serverState.position, player.transform));
                    transform.rotation = serverState.rotation;

                    if (!controller.isGrounded)
                    {
                        // Then take over the yVelocity based on the server
                        yVelocity = serverState.serverYVelocity;
                    }

                    // Remove all states from the buffer these have now been made obsolete by the server
                    stateBuffer.RemoveAll(state => state.tick <= serverState.tick); // fixme clean remove?
                    //PrintBuffer(serverState);
                    for (int i = 0; i < stateBuffer.Count; i++)
                    {
                        // Apply buffered states that haven't been ack
                        ClientPlayerState bufferedState = stateBuffer[i];

                        // Predict based on the new ack state
                        Vector3 predictedDeltaPos = GetMovementVector(bufferedState.inputs);
                        yVelocity = bufferedState.yVelocity;
                        ApplyMovementDelta(predictedDeltaPos);
                    }
                }
                else // This is always a new position packet (because it's form a different player), therefore apply it
                {
                    player.SetPosition(serverState.position);
                }
            }
        }
    }
        private void OnSuccessfulUse(int mapId, int mobileId, int itemId, ServerWorldState worldState)
        {
            foreach (int characterId in worldState.GetMapCharacters(mapId))
            {
                if (characterId == mobileId)
                {
                    continue;
                }

                ServerCharacter characterToUpdate = ((ServerCharacter)worldState.GetCharacter(characterId));
                NetState        clientSendTo      = characterToUpdate.Owner;

                clientSendTo.Send(new UseItemPacket(itemId, mobileId));
            }
        }
Exemplo n.º 7
0
    void ProcessMessage(ServerMessage msg)
    {
        ServerMessageType msgType = msg.MsgType;

        switch (msgType)
        {
        case ServerMessageType.ServerHello:
            ServerHello serverHello = msg.GetMsg(new ServerHello());
            ProcessServerHello(serverHello);
            break;

        case ServerMessageType.ServerWorldState:
            ServerWorldState serverWorldState = msg.GetMsg(new ServerWorldState());
            ReplicateState(serverWorldState);
            break;
        }
    }
        protected override void OnHandle(IPacket packet, NetState netState, ServerWorldState worldState)
        {
            UseItemPacket incomingPacket = (UseItemPacket)packet;
            //if (incomingPacket.MobileId != netState.WorldId)
            //{
            //    netState.SendError(0, string.Format("Invalid CMD: '{0},{1},{2}'.", incomingPacket.PacketId, incomingPacket.MobileId, incomingPacket.ItemId));
            //    return;
            //}

            ServerCharacter serverCharacter = (ServerCharacter)worldState.GetCharacter(netState.WorldId);
            Item            itemToUse       = worldState.GetItem(incomingPacket.ItemId);

            if (serverCharacter != null && itemToUse != null)
            {
                if (itemToUse.IsWorldItem)
                {
                    if (serverCharacter.PickupItem(itemToUse))
                    {
                        worldState.SaveItemPosition(itemToUse);
                        this.OnSuccessfulUse(serverCharacter.CurrentMapId, incomingPacket.MobileId, incomingPacket.ItemId, worldState);
                    }
                }
                else
                {
                    if (itemToUse.Category == ItemCategory.Consumable)
                    {
                        ConsumableItem consumable = (ConsumableItem)itemToUse;
                        if (consumable.Use(serverCharacter, worldState))
                        {
                            worldState.SaveItemUse(itemToUse);
                            this.OnSuccessfulUse(serverCharacter.CurrentMapId, incomingPacket.MobileId, incomingPacket.ItemId, worldState);
                        }
                    }
                    if (itemToUse.Category == ItemCategory.Armor || itemToUse.Category == ItemCategory.Weapon)
                    {
                        if (serverCharacter.Equip(itemToUse))
                        {
                            worldState.SaveCharacterItems(serverCharacter);
                            this.OnSuccessfulUse(serverCharacter.CurrentMapId, incomingPacket.MobileId, incomingPacket.ItemId, worldState);
                        }
                    }
                }
            }
        }
        protected override void OnHandle(IPacket packet, NetState netState, ServerWorldState worldState)
        {
            AuthPacket         incomingPacket = (AuthPacket)packet;
            PlayerSessionModel session        = worldState.GetPlayerSession(incomingPacket.SessionId);

            //worldState.AuthenticateClient(netState, incomingPacket.SessionId, incomingPacket.CharacterId);

            if (session != null)
            {
                IPAddress sessionIPAddress = IPAddress.Parse(session.ClientAddress);

                if (netState.Address == sessionIPAddress || sessionIPAddress.Equals(IPAddress.IPv6Loopback) || sessionIPAddress.Equals(IPAddress.Loopback))
                {
                    netState.Id = session.Id;
                    ServerCharacter selectedChar = worldState.LoadCharacter(session.CharacterID);
                    if (selectedChar != null)
                    {
                        selectedChar.Owner         = netState;
                        selectedChar.Owner.WorldId = selectedChar.Id;
                        worldState.AddCharacter(selectedChar);
                        netState.WriteConsole("Session confirmed, character: {0} selected.", selectedChar.Id);
                    }
                    else
                    {
                        netState.WriteConsole("AUTHERROR: Invalid characterID: {0}.", session.CharacterID);
                        netState.Dispose();
                    }
                }
                else
                {
                    netState.WriteConsole("AUTHERROR: Session failed, adress '{0}' did not match session address '{1}'.", netState.Address.ToString(), sessionIPAddress.ToString());
                    netState.Dispose();
                }
            }
            else
            {
                netState.WriteConsole("AUTHERROR: Invalid sessionID '{0}'.", incomingPacket.SessionId);
                netState.Dispose();
            }
        }
        protected override void OnHandle(IPacket packet, NetState netState, ServerWorldState worldState)
        {
            MoveItemPacket incomingPacket = (MoveItemPacket)packet;
            //if (incomingPacket.MobileId != netState.WorldId)
            //{
            //    netState.SendError(0, string.Format("Invalid CMD: '{0},{1},{2}'.", incomingPacket.PacketId, incomingPacket.MobileId, incomingPacket.ItemId));
            //    return;
            //}

            ServerCharacter serverCharacter = (ServerCharacter)worldState.GetCharacter(netState.WorldId);
            Item            item            = (Item)worldState.GetItem(incomingPacket.ItemID);

            if (serverCharacter != null && item != null)
            {
                if (incomingPacket.MoveToID.HasValue) //item.Data.IsWorldItem &&
                {
                    int           containerID = incomingPacket.MoveToID.Value;
                    ContainerItem moveTo      = (ContainerItem)worldState.GetItem(containerID);
                    if (moveTo != null)
                    {
                        if (serverCharacter.MoveItem(item, moveTo))
                        {
                            worldState.SaveItemPosition(item);
                            this.OnSuccessfulChange(serverCharacter.CurrentMapId, serverCharacter.Id, containerID, incomingPacket.ItemID, worldState);
                        }
                    }
                }
                else if (incomingPacket.MoveToPosition.HasValue)
                {
                    Point moveToPostion = incomingPacket.MoveToPosition.Value;
                    if (serverCharacter.MoveItem(item, moveToPostion))
                    {
                        worldState.SaveItemPosition(item);
                        this.OnSuccessfulChange(serverCharacter.CurrentMapId, serverCharacter.Id, moveToPostion, incomingPacket.ItemID, worldState);
                    }
                }
            }
        }
        protected override void OnHandle(IPacket packet, NetState netState, ServerWorldState worldState)
        {
            PerformAbilityPacket incomingPacket = (PerformAbilityPacket)packet;
            ServerCharacter      mobileToUpdate = (ServerCharacter)worldState.GetCharacter(netState.WorldId);

            if (mobileToUpdate != null)
            {
                if (worldState.PerformAbility(incomingPacket.AbilityUsed, mobileToUpdate))
                {
                    foreach (int characterId in worldState.GetMapCharacters(mobileToUpdate.CurrentMapId))
                    {
                        if (characterId == mobileToUpdate.Id)
                        {
                            continue;
                        }

                        ServerCharacter characterToUpdate = ((ServerCharacter)worldState.GetCharacter(characterId));
                        NetState        clientSendTo      = characterToUpdate.Owner;

                        clientSendTo.Send(incomingPacket);
                    }
                }
            }
        }
Exemplo n.º 12
0
 private static void PrintBuffer(ServerWorldState serverState)
 {
     Debug.Log($"buffer size at server tick {serverState.tick}: {stateBuffer.Count}");
     stateBuffer.ForEach((el) => { Debug.Log($"\t {el.tick}"); });
 }
 public void Handle(IPacket packet, NetState netState, ServerWorldState worldState)
 {
     this.OnHandle(packet, netState, worldState);
 }
 protected abstract void OnHandle(IPacket packet, NetState netState, ServerWorldState worldState);
        private void OnSuccessfulChange(int currentMapId, int mobileId, Point moveToPostion, int itemID, ServerWorldState worldState)
        {
            foreach (int characterId in worldState.GetMapCharacters(currentMapId))
            {
                if (characterId == mobileId)
                {
                    continue;
                }

                ServerCharacter characterToUpdate = ((ServerCharacter)worldState.GetCharacter(characterId));
                NetState        clientSendTo      = characterToUpdate.Owner;

                clientSendTo.Send(new MoveItemServerPacket(itemID, mobileId, moveToPostion));
            }
        }