Esempio n. 1
0
        public void DisconnectPlayer(int playerId)
        {
            if (!clients.ContainsKey(playerId))
            {
                Logger.LogNotice(LoggerSection.Connection, $"Skipping player {playerId} disconnect, because it is already disconnected");
                return;
            }

            Logger.LogEvent(LoggerSection.Connection, $"{clients[playerId].GetTcpEndPoint()} has disconnected (player {playerId})");

            if (clients[playerId].serverPlayer.remoteControllable.isLobbyHost)
            {
                packetsSender.SendKickedPacket(playerId);
                foreach (int playerIdToRemove in clients.Keys.ToList())
                {
                    RemovePlayerFromGame(playerIdToRemove);
                }

                Logger.LogEvent(LoggerSection.Connection, $"Removed every player, because the host player {playerId} has disconnected");

                // Get ready to accept fresh new players
                if (scenesManager.GetActiveScene() != Scene.Lobby)
                {
                    scenesManager.SwitchScene(Scene.Lobby);
                }
            }
            else
            {
                RemovePlayerFromGame(playerId);
            }
        }
        public void SendUdpPacket(ClientPacketType clientPacketType, Packet packet)
        {
            if (udpConnection == null)
            {
                Logger.LogNotice(LoggerSection.Network, "Unable to send udp packet, because udp connection is down");
                return;
            }

            packet.WriteLength();
            udpConnection.SendPacket(packet);

            Logger.LogEvent(LoggerSection.Network, $"Sent «{Helpers.GetEnumName(clientPacketType)}» UDP packet to the server");
        }
Esempio n. 3
0
        public void Interact()
        {
            if (chosen == null)
            {
                Logger.LogNotice(LoggerSection.Interactions, "Unable to interact without any chosen interactable");
                return;
            }

            if (IsInteractionDisabled())
            {
                Logger.LogNotice(LoggerSection.Interactions, "Not calling interaction, because interaction is disabled");
                return;
            }

            chosen.Interact();
        }
Esempio n. 4
0
        private void OnConnection(IAsyncResult result)
        {
            try
            {
                IPEndPoint clientIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     data             = udpClient.EndReceive(result, ref clientIpEndPoint);

                // Start listening for the next client connection
                udpClient.BeginReceive(OnConnection, null);

                if (data.Length < sizeof(int))
                {
                    return;
                }

                Packet packet   = new Packet(data);
                int    playerId = packet.ReadInt();

                if (!playersManager.clients.ContainsKey(playerId))
                {
                    Logger.LogNotice(LoggerSection.Network, $"Skipping tcp packed from player {playerId}, because it is already disconnected");
                    return;
                }

                if (!playersManager.clients[playerId].IsConnectedViaUdp())
                {
                    playersManager.clients[playerId].ConnectUdp(clientIpEndPoint);
                    return;
                }

                if (!playersManager.clients[playerId].IsCorrectUdpIpEndPoint(clientIpEndPoint))
                {
                    Logger.LogError(LoggerSection.Network, "Hacking attempt, client ids doesn't match");
                    return;
                }

                HandlePacketData(playerId, packet);
            }
            catch (ObjectDisposedException objectDisposedException)
            {
                Logger.LogNotice(LoggerSection.Network, $"Error receiving UDP data because udpClient is already disposed: {objectDisposedException}");
            }
            catch (Exception exception)
            {
                Logger.LogError(LoggerSection.Network, $"Error receiving UDP data: {exception}");
            }
        }
Esempio n. 5
0
        public void LateUpdate()
        {
            if (target == null)
            {
                Logger.LogNotice(LoggerSection.PlayerCamera, "Not updating, because the target is null");
                return;
            }

            Vector3 desiredPosition = target.transform.position + offset;

            if (shaking)
            {
                Random random  = new Random();
                int    randomX = random.Next(-50, 50);
                int    randomY = random.Next(-50, 50);
                desiredPosition += new Vector3(randomX * shakeFactor, randomY * shakeFactor, 0);
            }

            Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, 1 - smoothFactor);

            transform.position = smoothedPosition;
        }