Example #1
0
    private void FixedUpdate()
    {
        if (!_shouldTakeInput)
        {
            return;
        }

        if (Input.GetMouseButton(0))
        {
            if (!_firsInput)
            {
                if (FirstInputEvent != null)
                {
                    FirstInputEvent.Invoke();
                }
                _firsInput = true;

                // OH NO !!
                PlayerObject.GetComponent <CustomCubeTrail>().StartTrail();
            }

            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, float.MaxValue, LayerMask.GetMask("Ground")))
            {
                _playerRb.MovePosition(hit.point + Vector3.up * 0.5f);

                if (PlayerPositionEvent != null)
                {
                    PlayerPositionEvent.Invoke(PlayerObject.transform.position);
                }
            }
        }
    }
Example #2
0
        // Simple collision check
        private bool player_InRegion(PlayerPositionEvent p, ushort[] region)
        {
            int x = p.MapPositionX;
            int y = p.MapPositionY;

            return(x >= region[0] && x <= region[2] && y >= region[1] && y <= region[3]);
        }
Example #3
0
        /// <summary>
        /// <para>Sends back ssplayer using the PlayerPositionEvent.</para>
        /// </summary>
        /// <param name="e">Player Name</param>
        /// <returns>SSPlayer with all updated info</returns>
        public SSPlayer GetPlayer(PlayerPositionEvent e)
        {
            // Grab player info
            SSPlayer ssp = GetPlayer(e.PlayerName);

            ssp.ModLevel = e.ModLevel;
            // Update PlayerPosition
            ssp.Position = e;
            //return info
            return(ssp);
        }
Example #4
0
    public NetworkEvent Handle(Server server, NetworkEvent e, NetworkData player)
    {
        // TODO: implement
        PlayerPositionEvent ppe = (PlayerPositionEvent)e;

        server._ExecuteOnMainThread.RunOnMainThread.Enqueue(() => {
            GameObject.FindGameObjectWithTag("PlayerManager").GetComponent <PlayerManager>().Control(player.NetworkID, ppe.x, ppe.y);
        });

        return(null);
    }
Example #5
0
        public void MonitorPlayerPositionEvent(object sender, PlayerPositionEvent e)
        {
            if (!m_Initialized)
            {
                return;
            }

            SSPlayer ssp = m_Players.GetPlayer(e);

            m_BaseDuel.Event_PlayerPosition(ssp);
            m_BaseRace.Event_PlayerPosition(ssp);
        }
Example #6
0
    void ReadPosition()
    {
        string positionPath = Application.dataPath + "/CSV/" + "PlayerPositionEvent.csv";

        string fileData = System.IO.File.ReadAllText(positionPath.ToString());

        string[] lines = fileData.Split("\n"[0]);

        for (int i = 1; i < lines.Length - 1; i++) //i = 1 instead of 0 because we want to skip the headers
        {
            lineData = (lines[i].Trim()).Split(","[0]);
            PlayerPositionEvent PositionDataReturn = new PlayerPositionEvent();
            PositionDataReturn.x = float.Parse(lineData[0], CultureInfo.InvariantCulture);
            PositionDataReturn.y = float.Parse(lineData[1], CultureInfo.InvariantCulture);
            PositionDataReturn.z = float.Parse(lineData[2], CultureInfo.InvariantCulture);

            arrPosition.Add(PositionDataReturn);
        }
    }
Example #7
0
 /// <summary>
 /// Handle the player positon and check if the player is
 /// within a registered region.
 /// </summary>
 /// <param name="e"></param>
 private void HandlePlayerPosition(PlayerPositionEvent e)
 {
     // Check all defined regions
     foreach (RegionInfo r in m_regionList.Values)
     {
         // Check if the player is within the region
         if (r.region.Contains(e.MapPositionX, e.MapPositionY))
         {
             try
             {
                 // Call the region handler
                 r.handler(e);
             }
             catch (Exception ex)
             {
                 ChatEvent msg = new ChatEvent();
                 msg.ChatType  = ChatTypes.Arena;
                 msg.SoundCode = SoundCodes.Fart2;
                 msg.Message   = this.GetType().ToString() + " Region handler: " + ex.Message;
                 SendGameEvent(msg);
             }
         }
     }
 }
Example #8
0
        private async void ParsePacket(Packet packet)
        {
            if (packet.payload == null)
            {
                Game.Log("Packet Received (" + packet.commandType + ").");
            }
            else if (packet.commandType == Packets.PingRequest && packet.commandType == Packets.PingReply && packet.commandType == Packets.PlayerPing &&
                     packet.commandType != Packets.Position && packet.commandType != Packets.PlayerPosition)
            {
                Game.Log("Packet Received (" + packet.commandType + ") " + ByteArray2String(packet.payload) + ".");
            }

            if (packet.commandType == 0 && host)
            {
                // <Host>
                // Ping request from player
                Game.Log("Ping test request received.");

                Tuple <short, byte[]> pingReplyPacket = CreatePacket(Packets.PingTestReply, null);
                await SendPacket(pingReplyPacket);

                this.Close();
            }
            else if (packet.commandType == 1)
            {
                // <Player>
                // Ping reply from host

                PingTimer.Stop();
                connected = false;

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PingTestReplyReceivedEvent?.Invoke(null, new PingTestReplyArguments(
                                                           StreamSocket.Information.RemoteAddress.ToString(),
                                                           Int32.Parse(StreamSocket.Information.RemotePort),
                                                           PingTimer.ElapsedMilliseconds
                                                           ));
                });
            }
            else if (packet.commandType == 2 && host)
            {
                // <Host>
                // Join request from player

                byte[] playerIdBytes = new byte[packet.payload.Length];
                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, packet.payload.Length);
                PlayerId = Encoding.UTF8.GetString(playerIdBytes);

                Game.Log("Join request from player (" + PlayerId + ").");

                // Code
                ushort code = 0;

                // Check if player id
                // is used
                if (Game.PlayerId == PlayerId)
                {
                    code = 1;
                }
                else
                {
                    for (int i = 0; i < server.Connections.Count; i++)
                    {
                        Connection connection = server.Connections[i];

                        if (connection.PlayerId == PlayerId)
                        {
                            code = 1;
                        }
                    }
                }

                if (code == 1)
                {
                    Game.Log("Join request from player (" + PlayerId + ") rejected, player id used.");
                }

                // Check if maximum number of players
                // is exceeded
                if (server.Connections.Count == 16)
                {
                    code = 2;
                    Game.Log("Join request from player (" + PlayerId + ") rejected, no slots available.");
                }

                // Check if host
                // left
                if (Game.State == States.Started)
                {
                    code = 3;
                    Game.Log("Join request from player (" + PlayerId + ") rejected, match is deleted.");
                }

                // Send reply
                byte[] joinReplyPayload        = new byte[19];
                byte[] hostPlayerIdPaddedBytes = new byte[15];

                byte[] codeBytes = BitConverter.GetBytes(code);

                byte[] hostPlayerIdBytes = Encoding.ASCII.GetBytes(Game.PlayerId);
                System.Buffer.BlockCopy(hostPlayerIdBytes, 0, hostPlayerIdPaddedBytes, 0, hostPlayerIdBytes.Length);

                ushort hostReadyStatus = 0;

                if (Game.Ready)
                {
                    hostReadyStatus = 1;
                }

                byte[] readyStatusBytes = BitConverter.GetBytes(hostReadyStatus);

                System.Buffer.BlockCopy(codeBytes, 0, joinReplyPayload, 0, codeBytes.Length);
                System.Buffer.BlockCopy(hostPlayerIdPaddedBytes, 0, joinReplyPayload, codeBytes.Length, hostPlayerIdPaddedBytes.Length);
                System.Buffer.BlockCopy(readyStatusBytes, 0, joinReplyPayload,
                                        codeBytes.Length + hostPlayerIdPaddedBytes.Length,
                                        readyStatusBytes.Length);

                Tuple <short, byte[]> joinReplyPacket = CreatePacket(Packets.JoinReply, joinReplyPayload);
                await SendPacket(joinReplyPacket);

                if (code > 0)
                {
                    this.Close();
                    return;
                }

                if (Game.State == States.MatchEnded || Game.State == States.CleanupMatchDetails1 || Game.State == States.CleanupMatchDetails2)
                {
                    CleanupCompleted = true;
                    server.PlayerCleanupCompleted(this);
                }

                // Send player list
                Game.Log("Sending player list to player (" + PlayerId + ").");
                server.SendPlayerList(this);

                // Broadcast to all players
                byte[] playerJoinedPayload = Encoding.ASCII.GetBytes(PlayerId);
                Tuple <short, byte[]> playerJoinedPacket = server.CreatePacket(Packets.PlayerJoined, playerJoinedPayload);
                await server.SendPacket(playerJoinedPacket);

                // Send match state
                byte[] statePayload = BitConverter.GetBytes(Game.State);
                Tuple <short, byte[]> statePacket = CreatePacket(Packets.GameState, statePayload);
                await SendPacket(statePacket);

                // Add to connections list
                server.PlayerJoined(this);

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerJoinedEvent?.Invoke(this, new PlayerJoinedArguments(PlayerId, false, Ready));
                });

                // Request ping every second
                PingTimer         = new Stopwatch();
                pingInterval      = new Timer(SendPing, null, 0, 1000);
                pingReplyReceived = true;

                Joined = true;
            }
            else if (packet.commandType == 3)
            {
                // <Player>
                // Join reply from host

                byte[] codeBytes = new byte[2];
                System.Buffer.BlockCopy(packet.payload, 0, codeBytes, 0, 2);
                ushort code = BitConverter.ToUInt16(codeBytes, 0);

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    JoinMatchEvent?.Invoke(null, new JoinMatchArguments(
                                               code
                                               ));
                });

                if (code > 0)
                {
                    this.Close();
                    return;
                }
                else
                {
                    byte[] playerIdBytes    = new byte[15];
                    byte[] playerReadyBytes = new byte[2];

                    System.Buffer.BlockCopy(packet.payload, codeBytes.Length, playerIdBytes, 0, playerIdBytes.Length);
                    System.Buffer.BlockCopy(packet.payload, codeBytes.Length + playerIdBytes.Length, playerReadyBytes, 0, playerReadyBytes.Length);

                    PlayerId = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);
                    Joined   = true;

                    ushort playerStatus = BitConverter.ToUInt16(playerReadyBytes, 0);
                    bool   playerReady  = false;

                    if (playerStatus == 1)
                    {
                        playerReady = true;
                    }

                    // Check ping every second
                    PingTimer = new Stopwatch();
                    PingTimer.Start();
                    pingInterval = new Timer(CheckPing, null, 0, 1000);

                    // Update UI
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                    {
                        PlayerJoinedEvent?.Invoke(this, new PlayerJoinedArguments(PlayerId, true, playerReady));
                    });
                }
            }
            else if (packet.commandType == 4)
            {
                // <Player>
                // Game state from host

                byte[] stateBytes = new byte[2];
                System.Buffer.BlockCopy(packet.payload, 0, stateBytes, 0, stateBytes.Length);

                ushort state = BitConverter.ToUInt16(stateBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    MatchStateEvent?.Invoke(null, new MatchStateArguments(
                                                state
                                                ));
                });
            }
            else if (packet.commandType == 5)
            {
                // <Host/Player>
                // Leave request from host/player

                leaveRequestReceived = true;

                if (host)
                {
                    // Broadcast to all players
                    byte[] playerLeftPayload = Encoding.ASCII.GetBytes(PlayerId);
                    Tuple <short, byte[]> playerLeftPacket = server.CreatePacket(Packets.PlayerLeft, playerLeftPayload);
                    await server.SendPacket(playerLeftPacket);
                }

                // Stop ping interval
                if (pingInterval != null)
                {
                    pingInterval.Dispose();
                }

                // Remove from connections list
                server.PlayerLeft(this);

                // Update player list and UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerLeftEvent?.Invoke(this, new PlayerLeftArguments(PlayerId, false));
                });

                // Disconnect player
                Tuple <short, byte[]> leaveReplyPacket = CreatePacket(Packets.LeaveReply, null);
                await SendPacket(leaveReplyPacket);

                this.Close();
            }
            else if (packet.commandType == 6)
            {
                // <Host/Player>
                // Leave reply from host/player

                LeaveRequestAccepted = true;

                if (host)
                {
                    Game.Log("Player (" + PlayerId + ") accepted the leave request.");
                }
                else
                {
                    Game.Log("Host accepted the leave request.");
                }

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    LeaveReplyEvent.Invoke(null);
                });
            }
            else if (packet.commandType == 7)
            {
                // <Player>
                // Ping request from host

                Tuple <short, byte[]> pingReplyPacket = CreatePacket(Packets.PingReply, null);
                await SendPacket(pingReplyPacket);

                if (PingTimer != null)
                {
                    PingTimer.Restart();
                }
            }
            else if (packet.commandType == 8 && host)
            {
                // <Host>
                // Ping reply from player

                PingTimer.Stop();
                PingTime = (ulong)PingTimer.ElapsedMilliseconds;

                pingReplyReceived = true;

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerPingEvent?.Invoke(this, new PlayerPingArguments(PlayerId, PingTime));
                });

                // Send to all players
                byte[] playerPingTimePayload = new byte[23];
                byte[] playerIdPaddedBytes   = new byte[15];

                byte[] playerIdBytes       = Encoding.ASCII.GetBytes(PlayerId);
                byte[] playerPingTimeBytes = BitConverter.GetBytes(PingTime);

                System.Buffer.BlockCopy(playerIdBytes, 0, playerIdPaddedBytes, 0, playerIdBytes.Length);

                System.Buffer.BlockCopy(playerIdPaddedBytes, 0, playerPingTimePayload, 0, playerIdPaddedBytes.Length);
                System.Buffer.BlockCopy(playerPingTimeBytes, 0, playerPingTimePayload, playerIdPaddedBytes.Length, playerPingTimeBytes.Length);

                Tuple <short, byte[]> playerPingTimePacket = server.CreatePacket(Packets.PlayerPing, playerPingTimePayload);
                await server.SendPacket(playerPingTimePacket);
            }
            else if (packet.commandType == 9)
            {
                // <Player>
                // Player from list

                byte[] playerIdBytes = new byte[15];
                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, 15);
                string playerId = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);

                byte[] playerReadyStatusBytes = new byte[2];
                System.Buffer.BlockCopy(packet.payload, 15, playerReadyStatusBytes, 0, 2);
                ushort playerReadyStatus = BitConverter.ToUInt16(playerReadyStatusBytes, 0);

                bool playerReady = false;

                if (playerReadyStatus == 1)
                {
                    playerReady = true;
                }

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerJoinedEvent?.Invoke(this, new PlayerJoinedArguments(playerId, false, playerReady));
                });
            }
            else if (packet.commandType == 10)
            {
                // <Player>
                // Player joined

                byte[] playerIdBytes = new byte[packet.payload.Length];
                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, packet.payload.Length);
                string playerId = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerJoinedEvent?.Invoke(this, new PlayerJoinedArguments(playerId, false, false));
                });
            }
            else if (packet.commandType == 11)
            {
                // <Player>
                // Player left

                byte[] playerIdBytes = new byte[packet.payload.Length];
                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, packet.payload.Length);
                string playerId = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerLeftEvent?.Invoke(this, new PlayerLeftArguments(playerId, false));
                });
            }
            else if (packet.commandType == 12)
            {
                // <Player>
                // Ready time left

                byte[] readyTimeLeftBytes = new byte[packet.payload.Length];
                System.Buffer.BlockCopy(packet.payload, 0, readyTimeLeftBytes, 0, 4);

                uint readyTimeLeft = BitConverter.ToUInt32(readyTimeLeftBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    ReadyTimeEvent?.Invoke(null, new ReadyTimeLeftArguments(readyTimeLeft));
                });
            }
            else if (packet.commandType == 13)
            {
                // <Player>
                // Player ping time

                byte[] playerIdBytes       = new byte[15];
                byte[] playerPingTimeBytes = new byte[8];

                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, 15);
                System.Buffer.BlockCopy(packet.payload, 15, playerPingTimeBytes, 0, 8);

                string playerId       = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);
                ulong  playerPingTime = BitConverter.ToUInt64(playerPingTimeBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerPingEvent?.Invoke(null, new PlayerPingArguments(playerId, playerPingTime));
                });
            }
            else if (packet.commandType == 14 && host)
            {
                // <Host>
                // Player ready

                byte[] readyStatusBytes = new byte[packet.payload.Length];
                System.Buffer.BlockCopy(packet.payload, 0, readyStatusBytes, 0, packet.payload.Length);
                ushort readyStatus = BitConverter.ToUInt16(readyStatusBytes, 0);

                if (readyStatus == 0)
                {
                    Ready = false;
                }
                else
                {
                    Ready = true;
                }

                // Broadcast to all players
                byte[] playerStatusPayload    = new byte[17];
                byte[] playerIdPaddedBytes    = new byte[15];
                byte[] playerIdBytes          = Encoding.ASCII.GetBytes(PlayerId);
                byte[] playerReadyStatusBytes = BitConverter.GetBytes(readyStatus);

                System.Buffer.BlockCopy(playerIdBytes, 0, playerIdPaddedBytes, 0, playerIdBytes.Length);

                System.Buffer.BlockCopy(playerIdPaddedBytes, 0, playerStatusPayload, 0, playerIdPaddedBytes.Length);
                System.Buffer.BlockCopy(playerReadyStatusBytes, 0, playerStatusPayload, playerIdPaddedBytes.Length, playerReadyStatusBytes.Length);

                Tuple <short, byte[]> playeReadyStatusPacket = server.CreatePacket(Packets.PlayerReady, playerStatusPayload);
                await server.SendPacket(playeReadyStatusPacket);

                // Update ready state
                server.PlayerReady(this);

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerReadyEvent?.Invoke(null, new PlayerReadyArguments(PlayerId, Ready));
                });
            }
            else if (packet.commandType == 15)
            {
                // <Player>
                // Player ready

                byte[] playerIdBytes          = new byte[15];
                byte[] playerReadyStatusBytes = new byte[2];

                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, 15);
                System.Buffer.BlockCopy(packet.payload, 15, playerReadyStatusBytes, 0, 2);

                string playerId          = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);
                ushort playerReadyStatus = BitConverter.ToUInt16(playerReadyStatusBytes, 0);

                bool ready = false;

                if (playerReadyStatus == 1)
                {
                    ready = true;
                }

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerReadyEvent?.Invoke(null, new PlayerReadyArguments(playerId, ready));
                });
            }
            else if (packet.commandType == 16)
            {
                // <Player>
                // Load map

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    LoadMapEvent?.Invoke(null);
                });
            }
            else if (packet.commandType == 17)
            {
                // <Player>
                // Map objects created

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    MapObjectsCreatedEvent?.Invoke(null);
                });
            }
            else if (packet.commandType == 18 && host)
            {
                // <Host>
                // Map objects requested

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerMapObjectsRequestedEvent?.Invoke(this);
                });
            }
            else if (packet.commandType == 19)
            {
                // <Player>
                // Target position

                byte[] targetXBytes = new byte[4];
                byte[] targetYBytes = new byte[4];

                System.Buffer.BlockCopy(packet.payload, 0, targetXBytes, 0, 4);
                System.Buffer.BlockCopy(packet.payload, targetXBytes.Length, targetYBytes, 0, 4);

                float targetX = BitConverter.ToSingle(targetXBytes, 0);
                float targetY = BitConverter.ToSingle(targetYBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    TargetPositionEvent?.Invoke(null, new TargetPositionArguments(targetX, targetY));
                });
            }
            else if (packet.commandType == 20)
            {
                // <Player>
                // Score

                byte[] playerIdBytes = new byte[15];
                byte[] scoreBytes    = new byte[2];

                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, 15);
                System.Buffer.BlockCopy(packet.payload, playerIdBytes.Length, scoreBytes, 0, scoreBytes.Length);

                string playerId = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);
                ushort score    = BitConverter.ToUInt16(scoreBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerScoreEvent?.Invoke(null, new PlayerScoreArguments(playerId, score));
                });
            }
            else if (packet.commandType == 21)
            {
                // <Player>
                // Map data end

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    MapDataEndEvent?.Invoke(null);
                });
            }
            else if (packet.commandType == 22 && host)
            {
                // <Host>
                // Player map loaded

                if (!MapLoaded)
                {
                    MapLoaded = true;
                    server.PlayerMapLoaded(this);
                }
            }
            else if (packet.commandType == 23)
            {
                // <Player>
                // Show map

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    ShowMapEvent?.Invoke(this);
                });
            }
            else if (packet.commandType == 24 && host)
            {
                // <Host>
                // Player map shown

                if (!MapShown)
                {
                    MapShown = true;
                    server.PlayerMapShown(this);
                }
            }
            else if (packet.commandType == 25)
            {
                // <Player>
                // Match countdown

                byte[] countdownBytes = new byte[packet.payload.Length];
                System.Buffer.BlockCopy(packet.payload, 0, countdownBytes, 0, 4);

                uint countdownSeconds = BitConverter.ToUInt32(countdownBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    MatchCountdownEvent?.Invoke(this, new MatchCountdownArguments(countdownSeconds));
                });
            }
            else if (packet.commandType == 26)
            {
                // <Player>
                // Match started

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    MatchStartedEvent?.Invoke(this);
                });
            }
            else if (packet.commandType == 27 && host)
            {
                // <Host>
                // Player position

                byte[] playerXBytes1     = new byte[4];
                byte[] playerYBytes1     = new byte[4];
                byte[] playerAngleBytes1 = new byte[4];

                System.Buffer.BlockCopy(packet.payload, 0, playerXBytes1, 0, 4);
                System.Buffer.BlockCopy(packet.payload, playerXBytes1.Length, playerYBytes1, 0, 4);
                System.Buffer.BlockCopy(packet.payload, playerXBytes1.Length + playerYBytes1.Length, playerAngleBytes1, 0, 4);

                PlayerX     = BitConverter.ToSingle(playerXBytes1, 0);
                PlayerY     = BitConverter.ToSingle(playerYBytes1, 0);
                PlayerAngle = BitConverter.ToSingle(playerAngleBytes1, 0);

                // Broadcast to all players
                byte[] positionPayload     = new byte[27];
                byte[] playerIdPaddedBytes = new byte[15];

                byte[] playerIdBytes = Encoding.ASCII.GetBytes(PlayerId);
                byte[] playerX       = BitConverter.GetBytes(PlayerX);
                byte[] playerY       = BitConverter.GetBytes(PlayerY);
                byte[] playerAngle   = BitConverter.GetBytes(PlayerAngle);

                System.Buffer.BlockCopy(playerIdBytes, 0, playerIdPaddedBytes, 0, playerIdBytes.Length);

                System.Buffer.BlockCopy(playerIdPaddedBytes, 0, positionPayload, 0, playerIdPaddedBytes.Length);
                System.Buffer.BlockCopy(playerX, 0, positionPayload, playerIdPaddedBytes.Length, playerX.Length);
                System.Buffer.BlockCopy(playerY, 0, positionPayload, playerIdPaddedBytes.Length + playerX.Length, playerY.Length);
                System.Buffer.BlockCopy(playerAngle, 0, positionPayload, playerIdPaddedBytes.Length + playerX.Length + playerY.Length, playerAngle.Length);

                Tuple <short, byte[]> playerPositionPacket = server.CreatePacket(Packets.PlayerPosition, positionPayload);
                await server.SendPacket(playerPositionPacket);

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerPositionEvent?.Invoke(null, new PlayerPositionArguments(PlayerId, PlayerX, PlayerY, PlayerAngle));
                });
            }
            else if (packet.commandType == 28)
            {
                // <Player>
                // Player position

                byte[] playerIdBytes    = new byte[15];
                byte[] playerXBytes     = new byte[4];
                byte[] playerYBytes     = new byte[4];
                byte[] playerAngleBytes = new byte[4];

                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, 15);
                System.Buffer.BlockCopy(packet.payload, playerIdBytes.Length, playerXBytes, 0, playerXBytes.Length);
                System.Buffer.BlockCopy(packet.payload, playerIdBytes.Length + playerXBytes.Length, playerYBytes, 0, playerYBytes.Length);
                System.Buffer.BlockCopy(packet.payload, playerIdBytes.Length + playerXBytes.Length + playerYBytes.Length, playerAngleBytes, 0, playerAngleBytes.Length);

                string playerId    = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);
                float  playerX     = BitConverter.ToSingle(playerXBytes, 0);
                float  playerY     = BitConverter.ToSingle(playerYBytes, 0);
                float  playerAngle = BitConverter.ToSingle(playerAngleBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerPositionEvent?.Invoke(null, new PlayerPositionArguments(playerId, playerX, playerY, playerAngle));
                });
            }
            else if (packet.commandType == 29)
            {
                // <Player>
                // Match time left

                byte[] timeLeftBytes = new byte[packet.payload.Length];
                System.Buffer.BlockCopy(packet.payload, 0, timeLeftBytes, 0, 4);

                uint timeLeftSeconds = BitConverter.ToUInt32(timeLeftBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    MatchTimeLeftEvent?.Invoke(null, new MatchTimeLeftArguments(timeLeftSeconds));
                });
            }
            else if (packet.commandType == 30 && host)
            {
                // <Host>
                // Target reached

                TargetIndex++;

                // Sent to all players
                byte[] targetReachedPayload = new byte[17];
                byte[] playerIdPaddedBytes  = new byte[15];

                byte[] playerIdBytes = Encoding.ASCII.GetBytes(PlayerId);
                byte[] targetIndex   = BitConverter.GetBytes(TargetIndex);

                System.Buffer.BlockCopy(playerIdBytes, 0, playerIdPaddedBytes, 0, playerIdBytes.Length);

                System.Buffer.BlockCopy(playerIdPaddedBytes, 0, targetReachedPayload, 0, playerIdPaddedBytes.Length);
                System.Buffer.BlockCopy(targetIndex, 0, targetReachedPayload, playerIdPaddedBytes.Length, targetIndex.Length);

                Tuple <short, byte[]> targetReachedPacket = server.CreatePacket(Packets.PlayerTargetReached, targetReachedPayload);
                await server.SendPacket(targetReachedPacket);

                // Update UI
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerTargetReachedEvent?.Invoke(null, new PlayerTargetReachedArguments(PlayerId, TargetIndex));
                });
            }
            else if (packet.commandType == 31)
            {
                // <Player>
                // Target reached

                byte[] playerIdBytes          = new byte[15];
                byte[] playerTargetIndexBytes = new byte[2];

                System.Buffer.BlockCopy(packet.payload, 0, playerIdBytes, 0, playerIdBytes.Length);
                System.Buffer.BlockCopy(packet.payload, playerIdBytes.Length, playerTargetIndexBytes, 0, playerTargetIndexBytes.Length);

                string playerId          = Encoding.UTF8.GetString(playerIdBytes).Replace("\0", String.Empty);
                ushort playerTargetIndex = BitConverter.ToUInt16(playerTargetIndexBytes, 0);

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    PlayerTargetReachedEvent?.Invoke(null, new PlayerTargetReachedArguments(playerId, playerTargetIndex));
                });
            }
            else if (packet.commandType == 32)
            {
                // <Player>
                // Match ended

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    MatchEndedEvent?.Invoke(this);
                });
            }
            else if (packet.commandType == 33)
            {
                // <Player>
                // Load match details

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    LoadMatchDetailsEvent?.Invoke(this);
                });
            }
            else if (packet.commandType == 34 && host)
            {
                // <Host>
                // Cleanup completed

                if (!CleanupCompleted)
                {
                    CleanupCompleted = true;
                    server.PlayerCleanupCompleted(this);
                }
            }
            else if (packet.commandType == 35)
            {
                // <Player>
                // Show match details

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    ShowMatchDetailsEvent?.Invoke(this);
                });
            }
        }
        /// <summary>
        /// Core event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void HandleBehaviorEvents(Object sender, EventArgs e)
        {
            try
            {
                // If the event is a chat event
                if (e is ChatEvent)
                {
                    // Cast the event to a chat event
                    ChatEvent chatEvent = (e as ChatEvent);

                    // Copy the chat event
                    ChatEvent behaviorChat = new ChatEvent();
                    behaviorChat.Message    = chatEvent.Message;
                    behaviorChat.PlayerName = chatEvent.PlayerName;
                    behaviorChat.PlayerId   = chatEvent.PlayerId;
                    behaviorChat.ChatType   = chatEvent.ChatType;
                    behaviorChat.SoundCode  = chatEvent.SoundCode;
                    behaviorChat.ModLevel   = chatEvent.ModLevel;

                    // Process the chat message type
                    switch (behaviorChat.ChatType)
                    {
                    case ChatTypes.Arena:
                        // insert the *arena prefix
                        behaviorChat.ChatType = ChatTypes.Public;
                        behaviorChat.Message  = behaviorChat.Message.Insert(0, "*arena ");
                        break;

                    case ChatTypes.Zone:
                        // insert the *zone prefix
                        behaviorChat.ChatType = ChatTypes.Public;
                        behaviorChat.Message  = behaviorChat.Message.Insert(0, "*zone ");
                        break;

                    // If the chat type is remote private
                    case ChatTypes.RemotePrivate:
                        string strPrefix = ":" + behaviorChat.PlayerName + ":";
                        behaviorChat.Message  = behaviorChat.Message.Insert(0, strPrefix);
                        behaviorChat.PlayerId = 0xFFFF;
                        break;

                    case ChatTypes.TeamPrivate:
                    // If the chat type is private
                    case ChatTypes.Private:
                        // If the player name is set
                        if ((behaviorChat.PlayerName != null) && (behaviorChat.PlayerName.Length > 0))
                        {
                            // Get the player information from the player database
                            PlayerInfo p = m_playerHandler.PlayerInformation(behaviorChat.PlayerName);

                            if (p != null)
                            {
                                // Set the player identifier
                                behaviorChat.PlayerId = p.PlayerId;
                            }
                        }
                        break;
                    }

                    // Createm and send the chat packet
                    ChatPacket packet = new ChatPacket();
                    packet.Event = behaviorChat;
                    m_session.TransmitPacket(packet);
                }
                // If the event is a player spectate request
                else if (e is SpectatePlayerEvent)
                {
                    SpectatePlayerEvent specPlayer = (e as SpectatePlayerEvent);

                    // If the player name is set
                    if ((specPlayer.PlayerId == 0xFFFF) && (specPlayer.PlayerName.Length > 0))
                    {
                        PlayerInfo p = m_playerHandler.PlayerInformation(specPlayer.PlayerName);

                        // Set the player identifier
                        specPlayer.PlayerId = p.PlayerId;
                    }

                    // Create and send the spectate player packet
                    SpectatePlayerPacket packet = new SpectatePlayerPacket();
                    packet.Event = specPlayer;
                    m_session.TransmitPacket(packet);
                }
                // If the event is a player information request
                else if (e is PlayerInfoEvent)
                {
                    PlayerInfoEvent players = (e as PlayerInfoEvent);

                    // If there are players specified
                    if (players.Players.Count > 0)
                    {
                        // Add the player information to the list
                        foreach (string s in players.Players)
                        {
                            PlayerInfo p = m_playerHandler.PlayerInformation(s);

                            if (p != null)
                            {
                                // Add the player information to the list
                                players.PlayerList.Add(p);
                            }
                        }
                    }
                    else
                    {
                        // Add all player information to the list
                        foreach (PlayerInfo p in m_playerHandler.PlayerData.Values)
                        {
                            // Add the player information
                            players.PlayerList.Add(p);
                        }
                    }

                    // Create the player information response
                    ResponseEvent response = new ResponseEvent();
                    response.destination = sender;
                    response.e           = e;

                    // add the response to the event queue
                    m_eventQueue.Add(response);
                }
                // If the event is a bot information request
                else if (e is BotInfoRequest)
                {
                    // Create the player information response
                    ResponseEvent response = new ResponseEvent();
                    response.destination = sender;

                    BotInfoRequest b = new BotInfoRequest();
                    b.BotName = m_playerHandler.PlayerInformation(m_botIdentifier).PlayerName;
                    b.MapFile = m_session.SecurityChecksum.MapFile;
                    b.MapData = m_session.SecurityChecksum.PsyLevelData;

                    response.e = b;

                    // add the response to the event queue
                    m_eventQueue.Add(response);
                }
                // If the event is a bot information request
                else if (e is BotInfoEvent)
                {
                    BotInfoEvent botInfo = (e as BotInfoEvent);

                    // Get the bot information from the player data
                    botInfo.BotInfo = m_playerHandler.PlayerInformation(m_botIdentifier);

                    // Create the bot information response
                    ResponseEvent response = new ResponseEvent();
                    response.destination = sender;
                    response.e           = e;

                    // add the response to the event queue
                    m_eventQueue.Add(response);
                }
                // If the event is a player position event
                else if (e is SqlQueryEvent)
                {
                    // Execute the SQL query
                    ExecuteSqlQuery(sender, e as SqlQueryEvent);
                }
                else if (e is SqlCommandEvent)
                {
                    // Execute the SQL command
                    ExecuteSqlCommand(sender, e as SqlCommandEvent);
                }
                // If the event is a player position event
                else if (e is PlayerPositionEvent)
                {
                    PlayerPositionEvent positionEvent = (e as PlayerPositionEvent);

                    // Set the new player position
                    m_playerHandler.PlayerInformation(m_botIdentifier).Position = positionEvent;

                    PlayerPositionPacket packet = new PlayerPositionPacket();
                    packet.Event = positionEvent;
                    m_session.TransmitPacket(packet);
                }
                else if (e is PlayerDeathEvent)
                {
                    // Create the player death packet
                    PlayerDeathPacket packet     = new PlayerDeathPacket();
                    PlayerDeathEvent  deathEvent = (e as PlayerDeathEvent);

                    if (deathEvent.KillerName.Length != 0)
                    {
                        // Get the player information from the player database
                        PlayerInfo p = m_playerHandler.PlayerInformation(deathEvent.KillerName);

                        if (p != null)
                        {
                            deathEvent.KillerId = p.PlayerId;
                        }
                    }
                    // Send the player death packet
                    packet.Event = deathEvent;
                    m_session.TransmitPacket(packet);
                }
                else if (e is CreateTurretEvent)
                {
                    // Create the turret packet
                    CreateTurretPacket packet      = new CreateTurretPacket();
                    CreateTurretEvent  turretEvent = (e as CreateTurretEvent);

                    if (turretEvent.TurretHostName.Length != 0)
                    {
                        // Get the player information from the player database
                        PlayerInfo p = m_playerHandler.PlayerInformation(turretEvent.TurretHostName);

                        if (p != null)
                        {
                            turretEvent.TurretHostId = p.PlayerId;
                        }
                    }

                    // Send the turret creation request packet
                    packet.Event = turretEvent;
                    m_session.TransmitPacket(packet);
                }
                else if (e is DestroyTurretEvent)
                {
                    // Create the turret packet
                    DestroyTurretPacket packet      = new DestroyTurretPacket();
                    DestroyTurretEvent  turretEvent = (e as DestroyTurretEvent);

                    // Don't need to check for playername or id...packet contents are hardcoded
                    // It doesn't seem to work otherwise >_>

                    // Send the turret creation request packet
                    packet.Event = turretEvent;
                    m_session.TransmitPacket(packet);
                }
                else if (e is LVZToggleEvent)
                {
                    // Create the turret packet
                    LVZTogglePacket packet    = new LVZTogglePacket();
                    LVZToggleEvent  lvz_event = (e as LVZToggleEvent);

                    // Don't need to check for playername or id...packet contents are hardcoded
                    // It doesn't seem to work otherwise >_>

                    // Send the turret creation request packet
                    packet.Event = lvz_event;
                    m_session.TransmitPacket(packet);
                }
                else if (e is ArenaListEvent)
                {
                    ChatPacket cP = new ChatPacket();
                    ChatEvent  c  = new ChatEvent();
                    c.Message  = "?arena";
                    c.ChatType = ChatTypes.Public;
                    cP.Event   = c;
                    m_session.TransmitPacket(cP);
                }
            }
            catch (Exception ex)
            {
                // Write the line to the console
                Console.WriteLine(ex.Message);
            }
        }
Example #10
0
 // Simple collision check
 private bool player_InRegion(PlayerPositionEvent p, ushort[] region)
 {
     return(player_InRegion(p, region, 0));
 }