예제 #1
0
        private void PlayerReloadingMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            bool state = msg.GetBool();

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.Reloading);

            writer.Put(player.Id);
            writer.Put(state);

            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #2
0
        public bool RemovePlayer(int playerId)
        {
            if (_expectedPlayers.ContainsKey(playerId))
            {
                if (!_expectedPlayers[playerId].IsSpectator)
                {
                    PlayerCount -= 1;
                }
                _expectedPlayers.Remove(playerId);
                return(true);
            }

            if (PlayerList.ContainsKey(playerId))
            {
                var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.PlayerQuit);
                writer.Put(playerId);

                _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
                if (PlayerList[playerId].IsAlive && AlivePlayerList != null)
                {
                    AlivePlayerList.Remove(playerId);
                }
                PlayerList.Remove(playerId);
                PlayerCount -= 1;
                return(true);
            }
            else if (SpectatorList.ContainsKey(playerId))
            {
                SpectatorList.Remove(playerId);
                return(true);
            }

            return(false);
        }
예제 #3
0
        private void ConnectionConfirmMessage(GlobalPlayer player, NetworkMessage msg)
        {
            if (msg.AvailableBytes > 0)
            {
                int playerId = msg.GetInt();
                if (AllPlayers.ContainsKey(playerId))
                {
                    player = AllPlayers[playerId];
                }
                else
                {
                    player = new GlobalPlayer(msg.Peer, playerId);
                }
            }
            else
            {
                player = new GlobalPlayer(msg.Peer);
            }

            _server.RegisterPlayer(player);
            if (!AllPlayers.ContainsKey(player.Id))
            {
                AllPlayers.Add(player.Id, player);
            }

            Logger.WriteLine("Confirmation Connection for player {0}", player.Id);

            var writer = new NetworkWriter(GlobalProtocol.ServerToClient.ConnectionConfirm);

            writer.Put(player.Id);

            player.SendWriter(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #4
0
        private void ConnectionConfirmMessage(InGamePlayer p, NetworkMessage msg)
        {
            int playerId = msg.GetInt();

            if (_expectedPlayers.ContainsKey(playerId))
            {
                var oldPlayer = _expectedPlayers[playerId];
                _expectedPlayers.Remove(playerId);
                var player = new InGamePlayer(msg.Peer, playerId);
                player.Name        = oldPlayer.Name;
                player.IsSpectator = oldPlayer.IsSpectator;

                if (!player.IsSpectator)
                {
                    var writerOther = new NetworkWriter(InGameProtocol.TCPServerToClient.ConectedToRoom);
                    writerOther.Put(player.Id);
                    writerOther.Put(player.Name);
                    _server.SendAll(writerOther, DeliveryMethod.ReliableOrdered);
                }

                var writerNew = new NetworkWriter(InGameProtocol.TCPServerToClient.ListConnectedPlayer);
                writerNew.Put(PlayerList.Count);

                foreach (var connectedPlayer in PlayerList)
                {
                    writerNew.Put(connectedPlayer.Value.Id);
                    writerNew.Put(connectedPlayer.Value.Name);
                    writerNew.Put(connectedPlayer.Value.Position);
                    writerNew.Put(connectedPlayer.Value.Rotation);
                }

                player.SendWriter(writerNew, DeliveryMethod.ReliableOrdered);

                writerNew = new NetworkWriter(InGameProtocol.TCPServerToClient.Registered);
                player.SendWriter(writerNew, DeliveryMethod.ReliableOrdered);
                _server.RegisterPlayer(player);

                if (!player.IsSpectator)
                {
                    PlayerList.Add(player.Id, player);
                }
                else
                {
                    SpectatorList.Add(player.Id, player);
                }
            }
        }
예제 #5
0
        /*********************************************************************************/

        private void PlayerMovementMessage(InGamePlayer player, NetworkMessage msg)
        {
            Vector3 position = msg.GetVector3();
            float   speed_h  = msg.GetFloat();
            float   speed_v  = msg.GetFloat();

            player.Position = position;

            var writer = new NetworkWriter(InGameProtocol.UDPServerToClient.Movement);

            writer.Put(player.Id);
            writer.Put(position);
            writer.Put(speed_h);
            writer.Put(speed_v);

            _server.SendAll(writer, DeliveryMethod.Unreliable);
        }
예제 #6
0
        private void SwitchItemMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            int itemID = msg.GetInt();

            player.SwitchCurrentItem(itemID);

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.SwitchItem);

            writer.Put(player.Id);
            writer.Put(itemID);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #7
0
        private void StartingPlasmaZone()
        {
            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.StartPlasma);

            writer.Put(_currentZoneIndex);
            Server.SendAll(writer, DeliveryMethod.ReliableOrdered);

            _plasmaThread = new Thread(PlasmaFunction);
            _plasmaThread.Start();
        }
예제 #8
0
        /*************************************************************************************/

        private bool PlayerTakeDamage(InGamePlayer player, short damage, bool affectShield)
        {
            player.TakeDamage(damage, affectShield);

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.HitPlayer);

            writer.Put(player.Id);
            writer.Put(damage);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
            if (!player.IsAlive)
            {
                foreach (var item in player.Inventory)
                {
                    writer = new NetworkWriter(InGameProtocol.TCPServerToClient.DropItem);
                    writer.Put(player.Id);
                    writer.Put(item.Value.ID);
                    writer.Put(item.Value.ID);
                    writer.Put(item.Value.Quantity);
                    _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
                    _itemList.Add(item.Value.ID, item.Value);
                }

                player.Inventory.Clear();

                AlivePlayerList.Remove(player.Id);

                if (AlivePlayerList.Count == 1)
                {
                    var winPlayer = AlivePlayerList.First().Value;

                    writer = new NetworkWriter(InGameProtocol.TCPServerToClient.PlayerWin);
                    writer.Put(winPlayer.Id);
                    _server.SendAll(writer, DeliveryMethod.ReliableOrdered);

                    GameEnded = true;
                    Logger.WriteLine("Room {0} : Player {1} win the game", Id, winPlayer.Id);
                }

                return(true);
            }

            return(false);
        }
예제 #9
0
        private void RocketEndMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            int index = msg.GetInt();

            player.RocketEnd(index);

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.RocketEnd);

            writer.Put(player.Id);
            writer.Put(index);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);

            Logger.WriteLine("Room {0} : Rocket {1} launch by Player {2} exploded.", Id, index, player.Id);
        }
예제 #10
0
        private void RocketMoveMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            int        index = msg.GetInt();
            Vector3    pos   = msg.GetVector3();
            Quaternion rot   = msg.GetQuaternion();

            player.RocketMove(index, pos, rot);
            var writer = new NetworkWriter(InGameProtocol.UDPServerToClient.RocketMove);

            writer.Put(player.Id);
            writer.Put(index);
            writer.Put(pos);
            writer.Put(rot);
            _server.SendAll(writer, DeliveryMethod.Unreliable);
        }
예제 #11
0
        private void SwitchKnifeMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.SwitchKnife);

            writer.Put(player.Id);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #12
0
        private void EnigmaOpenedMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            int enigmaID = msg.GetInt();

            Logger.WriteLine("Room {0} : Player {1} open enigma {2}.", Id, player.Id, enigmaID);

            var enigmas = _enigmasList[enigmaID];

            enigmas.EnigmaOpened = true;

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.EnigmaOpened);

            writer.Put(player.Id);
            writer.Put(enigmaID);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #13
0
        private void ThrowingMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            int   itemID = msg.GetInt();
            short type   = msg.GetShort();
            int   index  = msg.GetInt();

            player.Throwing(itemID, type, index);

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.Throwing);

            writer.Put(player.Id);
            writer.Put(itemID);
            writer.Put(type);
            writer.Put(index);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);

            Logger.WriteLine("Room {0} : Player {1} throw a grenade {2}.", Id, player.Id, index);
        }
예제 #14
0
        private void DropItemMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            int itemID   = msg.GetInt();
            int quantity = msg.GetInt();

            var item = player.DropItem(itemID, quantity);

            _itemList.Add(item.ID, item);

            Logger.WriteLine("Room {0} : Player {1} drop item {2}", Id, player.Id, itemID);

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.DropItem);

            writer.Put(player.Id);
            writer.Put(itemID);
            writer.Put(item.ID);
            writer.Put(quantity);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #15
0
        private void OpenAccessZone()
        {
            foreach (var e in EnigmasList)
            {
                var enigma = EnigmasList[e.Key];
                if (enigma.Zone == _currentZoneIndex)
                {
                    enigma.EnigmaAccessOpened = true;
                }
            }

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.EnigmaAccessOpened);

            writer.Put(_currentZoneIndex + 1);
            Server.SendAll(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #16
0
        private void GenerateMap()
        {
            /*********** Generate Items ***********/
            Logger.WriteLine("Room {0} : Generating items", Id);
            ItemGenerator itemGenerator = new ItemGenerator();

            itemGenerator.Generate();
            _itemList = itemGenerator.ItemList;

            NetworkWriter writer = new NetworkWriter(InGameProtocol.TCPServerToClient.ItemList);

            writer.Put(itemGenerator.ItemList.Count);

            foreach (var item in itemGenerator.ItemList)
            {
                writer.Put(item.Value.SpawnIndex);
                writer.Put(item.Value.ID);
                writer.Put(item.Value.Type);
                writer.Put(item.Value.Rarity);
                writer.Put(item.Value.Quantity);
                Logger.WriteLine("Room {0} : Item generated -> id = {1}\tquantity = {2}\ttype = {3}",
                                 Id,
                                 item.Value.ID,
                                 item.Value.Quantity,
                                 (ItemTypes.ItemEnum)item.Value.Type);
            }

            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
            Logger.WriteLine("Room {0} : Generating items finish", Id);

            /*********** Generate Enigmas ***********/
            Logger.WriteLine("Room {0} : Generating enigmas", Id);
            EnigmasGenerator enigmasGenerator = new EnigmasGenerator();

            enigmasGenerator.Generate();
            _enigmasList = enigmasGenerator.EnigmasList;

            writer = new NetworkWriter(InGameProtocol.TCPServerToClient.EnigmasList);
            writer.Put(enigmasGenerator.EnigmasList.Count);

            foreach (var enimga in enigmasGenerator.EnigmasList)
            {
                writer.Put(enimga.Value.SpawnIndex);
                writer.Put(enimga.Value.EnigmaID);
                writer.Put(enimga.Value.EnigmaType);
                Logger.WriteLine("Room {0} : Enigma generated -> id = {1}\ttype = {2}",
                                 Id,
                                 enimga.Value.EnigmaID,
                                 enimga.Value.EnigmaType);
            }

            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
            Logger.WriteLine("Room {0} : Generating enigmas finish", Id);
        }
예제 #17
0
        private void ConnectToRoomMessage(GlobalPlayer player, NetworkMessage msg)
        {
            bool spectator = msg.GetBool();

            GameInstance tojoin = null;

            if (spectator)
            {
                int roomID = msg.GetInt();
                if (!Rooms.ContainsKey(roomID))
                {
                    return;
                }
                tojoin = Rooms[roomID];
            }
            else
            {
                foreach (var room in Rooms)
                {
                    if (room.Value.Joinable)
                    {
                        tojoin = room.Value;
                        break;
                    }
                }

                if (tojoin == null)
                {
                    tojoin = new GameInstance(this);
                    Rooms.Add(tojoin.Id, tojoin);
                }
            }

            player.IsSpectator = spectator;
            tojoin.AddExpectedPlayer(player);

            NetworkWriter writer = new NetworkWriter(GlobalProtocol.ServerToClient.ConnectToRoom);

            writer.Put(tojoin.ListenPort);
            player.SendWriter(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #18
0
        private void OpenCrateMessage(InGamePlayer player, NetworkMessage msg)
        {
            if (!GameStarted || GameEnded)
            {
                return;
            }

            int crateID = msg.GetInt();

            Logger.WriteLine("Room {0} : Player {1} open crate {2}.", Id, player.Id, crateID);

            var itemList = ItemGenerator.GenerateCrateItem();

            var writer = new NetworkWriter(InGameProtocol.TCPServerToClient.OpenCrate);

            writer.Put(player.Id);
            writer.Put(crateID);
            writer.Put(itemList.Count);

            foreach (var item in itemList)
            {
                writer.Put(item.ID);
                writer.Put(item.Type);
                writer.Put(item.Rarity);
                writer.Put(item.Quantity);
                _itemList.Add(item.ID, item);

                Logger.WriteLine("Room {0} : Item generated on crate {1} -> id = {2}\tquantity = {3}\ttype = {4}",
                                 Id,
                                 crateID,
                                 item.ID,
                                 item.Quantity,
                                 (ItemTypes.ItemEnum)item.Type);
            }

            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
        }
예제 #19
0
        private void RoomLoop()
        {
            int           counter = TimeBeforeStart;
            NetworkWriter writer  = null;

            while (!GameStarted && PlayerCount < MinPlayerToStart)
            {
                Thread.Sleep(1000);
                if (_stopRoom)
                {
                    return;
                }
            }

            Logger.WriteLine("Room {0} : Enough player as join, start counter.", Id);

            while (!GameStarted && counter > 0)
            {
                Thread.Sleep(1000);
                if (_stopRoom)
                {
                    return;
                }
                if (PlayerCount == 32 && counter > 10)
                {
                    counter = 10;
                }
                else
                {
                    counter -= 1;
                }

                if (counter <= 10)
                {
                    Joinable = false;
                }

                writer = new NetworkWriter(InGameProtocol.TCPServerToClient.Counter);
                writer.Put(counter);
                _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
            }

            Logger.WriteLine("Room {0} : Game started", Id);
            GameStarted = true;
            Joinable    = false;
            writer      = new NetworkWriter(InGameProtocol.TCPServerToClient.GameStart);
            _server.SendAll(writer, DeliveryMethod.ReliableOrdered);
            AlivePlayerList = new Dictionary <int, InGamePlayer>(PlayerList);

            GenerateMap();

            MapEvent mapEvent = new MapEvent();

            mapEvent.ItemList    = _itemList;
            mapEvent.EnigmasList = _enigmasList;
            mapEvent.Server      = _server;

            while (!GameEnded)
            {
                if (_stopRoom)
                {
                    break;
                }
                mapEvent.Update();
                foreach (var player in PlayerList)
                {
                    if (player.Value.IsAlive && mapEvent.CheckPlayerInPlasma(player.Value.Position))
                    {
                        PlayerTakeDamage(player.Value, 1, false);
                    }
                }

                Thread.Sleep(1000);
            }

            Logger.WriteLine("Room {0} : Game ended", Id);

            while (!_stopRoom)
            {
                Thread.Sleep(1000);
            }
            mapEvent.Stop();
        }