예제 #1
0
파일: Player.cs 프로젝트: Arekva/winecrash
        public Player(string nickname) : base(nickname)
        {
            this.Nickname    = nickname;
            this.GUID        = EGuid.UniqueFromString(nickname);
            this.Client      = null;
            this.SkinAddress = null;
            this.Entity      = null;

            lock (PlayersLocker)
                Players.Add(this);
        }
예제 #2
0
        public void ThreatData()
        {
            NetObject[] pending = null;

            lock (this.PendingDataLocker)
            {
                pending = this.PendingData.ToArray();
                this.PendingData.Clear();
            }

            Parallel.For(0, pending.Length, i => //for(int i = 0; i < pending.Length; i++)
            {
                NetObject nobj = pending[i];

                if (nobj.GetType() == typeof(NetPing))
                {
                    if (!FirstPingReceived)
                    {
                        FirstPingReceived = true;

                        //send auth on first data received.

                        new NetPlayer(Player.LocalPlayer).Send(Client.Client);
                    }
                }

                else if (nobj is NetChunk nchunk)
                {
                    World.GetOrCreateChunk(nchunk);
                }

                else if (nobj is NetPlayer nplay)
                {
                    Player existingP = Player.Find(nplay.Nickname);

                    if (existingP == null)
                    {
                        PlayerEntity existingE = (PlayerEntity)Entity.Get(EGuid.UniqueFromString(nplay.Nickname));
                        existingP = new Player(nplay.Nickname);

                        if (existingE != null)
                        {
                            existingP.Entity = existingE;
                        }
                        else
                        {
                            existingP.CreateEntity(new WObject(nplay.Nickname));
                        }

                        //existingP.CreateNonLocalElements();
                    }
                }

                else if (nobj is NetEntity nent)
                {
                    Entity ent = nent.Parse();

                    //todo if not netplayer....
                }

                else if (nobj is NetCamera ncam)
                {
                    Player.LocalPlayer.CameraAngles = ncam.Angles;
                }

                pending[i].Delete();
            });
        }
예제 #3
0
        public override void Tick()
        {
            PlayerSelfSyncCD -= Time.Delta;


            PendingData[] data;
            lock (this.PendingDataLocker)
            {
                data = this.PendingData.ToArray();
                this.PendingData.Clear();
            }


            for (int i = 0; i < data.Length; i++)
            {
                //Debug.Log("DATA");
                if (data[i].Deleted)
                {
                    continue;
                }

                if (data[i].NObject is NetPlayer nplayer)
                {
                    RequiredAuth auth = null;
                    lock (AuthLocker)
                        auth = AuthsRequired.FirstOrDefault(a => a.Client == data[i].Client);

                    if (auth != null)
                    {
                        Guid playerGuid = EGuid.UniqueFromString(nplayer.Nickname);
                        Debug.Log(nplayer.Nickname + " : " + playerGuid);

                        WObject playerWobj = new WObject(nplayer.Nickname);
                        Player  player     = new Player(nplayer.Nickname, auth.Client, playerGuid, null);
                        player.CreateEntity(playerWobj);

                        lock (ConnectedPlayersLocker)
                        {
                            foreach (Player p in ConnectedPlayers)
                            {
                                //send all curent players to the new one
                                new NetPlayer(p).Send(player.Client.Client);

                                //send the new player to the connected ones
                                new NetPlayer(player).Send(p.Client.Client);
                            }

                            ConnectedPlayers.Add(player);
                        }

                        this.OnPlayerConnect?.Invoke(player);

                        lock (AuthLocker)
                            AuthsRequired.Remove(auth);
                        auth.Delete();
                    }
                }

                else if (data[i].NObject is NetEntity nentity)
                {
                    nentity.Parse();
                }

                else if (data[i].NObject is NetInput ninput)
                {
                    Player p = this.FindPlayer(data[i].Client);
                    if (p != null)
                    {
                        p.ParseNetInput(ninput);
                    }
                }

                data[i].Delete();
            }

            RequiredAuth[] rauths = null;
            lock (AuthLocker)
                rauths = AuthsRequired.ToArray();

            for (int i = 0; i < rauths.Length; i++)
            {
                rauths[i].CooldownTimeout -= Time.Delta;
                if (rauths[i].CooldownTimeout < 0.0)
                {
                    DisconnectClient(rauths[i].Client, "Failed to auth to the server");

                    lock (AuthLocker)
                        AuthsRequired.Remove(rauths[i]);
                    rauths[i].Delete();
                }
            }

            Entity[] entities;
            lock (Entity.EntitiesLocker)
            {
                entities = Entity.Entities.ToArray();
            }

            for (int i = 0; i < entities.Length; i++)
            {
                if (entities[i].Deleted)
                {
                    continue;
                }
                SyncEntity(entities[i]);
            }


            if (PlayerSelfSyncCD <= 0.0D)
            {
                PlayerSelfSyncCD = PlayerSelfSyncDelay;

                Player[] players;
                lock (ConnectedPlayersLocker)
                    players = ConnectedPlayers.ToArray();

                for (int i = 0; i < players.Length; i++)
                {
                    new NetCamera(players[i].CameraAngles).Send(players[i].Client.Client);
                }
            }

            Engine.ForceUpdate();
        }