public static void CheckCallbacks(int identity)
                {
                    List <VariableCallback> callbacks = Callbacks.FindAll(x => x.identity == identity);

                    Network.Identity Identity = Network.Identity.List.Find(x => x.Id == identity);
                    foreach (VariableCallback callback in callbacks)
                    {
                        callback.Check(Identity.Variables.GetVariableAsString(callback.Id));
                    }
                }
Exemple #2
0
        /// <summary>
        /// Call this to get actions.
        /// </summary>
        public static void Update()
        {
            if (!client.Connected)
            {
                return;
            }

            string Msg = Read();

            if (!string.IsNullOrEmpty(Msg))
            {
                var list = JsonExtensions.FromDelimitedJson <Client.MessagesIncoming.NetworkMessage>(new StringReader(Msg)).ToList();

                foreach (Client.MessagesIncoming.NetworkMessage message in list)
                {
                    switch (message.t)
                    {
                    case Client.MessagesIncoming.MessageType.JoinLobby:
                        Client.MessagesIncoming.OnLobbyJoined?.Invoke(message.jl.Success, message.jl.Id);
                        Client.NetworkVariables.ConnectionId = message.jl.Id;
                        break;

                    case Client.MessagesIncoming.MessageType.P2P:
                        string p2pmsg = Encoding.UTF8.GetString(CLZF2.Decompress(message.m.Msg));
                        Client.MessagesIncoming.OnP2P?.Invoke(p2pmsg, message.m.s);     // Available for custom methods.

                        OnNetworkMessage?.Invoke(p2pmsg);

                        Network.Identity identity = JsonConvert.DeserializeObject <Network.Identity>(p2pmsg);

                        if (identity.netType == Network.Identity.NetworkType.Request)
                        {         // Host assigns an id here.
                            if (Client.NetworkVariables.IsHost)
                            {
                                identity.Id      = Network.Identity.IdCounter++;
                                identity.netType = Network.Identity.NetworkType.Post;
                                identity.Spawn();
                            }
                        }
                        else
                        {
                            //This is a post.
                            identity.OnSpawned();                               // Because this may remove the identity.
                            Network.Actions.OnIdentityUpdate?.Invoke(identity); // Ordering is importent here. OnIdentityUpdate should be called first.
                        }

                        break;

                    case Client.MessagesIncoming.MessageType.LobbyUpdate:
                        Client.MessagesIncoming.OnLobbyUpdate?.Invoke(message.lu.IsHost, message.lu.DC, message.lu.C);

                        // Lobby update
                        Client.NetworkVariables.IsHost = message.lu.IsHost;

                        if (message.lu.DC != 0)
                        {
                            // Disconnected connection, remove spawned player if exist.
                            Network.Identity _identity = Network.Identity.List.Find(x => x.Id == message.lu.DC);
                            if (_identity != null)
                            {
                                _identity.Variables.SetVariable("Destroy", 2);
                            }
                        }

                        break;

                    case Client.MessagesIncoming.MessageType.LobbyLeave:
                        Client.MessagesIncoming.OnLobbyLeave?.Invoke();
                        break;
                    }
                }
            }
        }