public void OnLogin(LoginPacket packet) { var client = ServerTcpHandler.GetClient(packet.ClientId); try { var user = AccountService.Login(packet.Login, packet.Password); Log.Info("Sending Login Response"); client.Send(new LoginResponsePacket() { SessionId = user.SessionId, UserId = user.UserId, xLocation = user.X, yLocation = user.Y }); ServerEvents.Call(new PlayerLoginEvent() { Player = user, Client = client }); } catch (AccountError e) { Log.Info("Sending Login Error"); client.Send(new DialogPacket() { Message = e.ErrorMessage }); } }
[EventMethod] // When client finishes updating assets public void OnAssetReady(AssetsReadyPacket packet) { var player = Server.GetPlayer(packet.UserId); if (player != null) { player.AssetsReady = true; } var client = ServerTcpHandler.GetClient(packet.ClientId); // update chunks for that player ChunkProvider.CheckChunks(player); // make the player itself appear client.Send(new PlayerPacket() { Name = player.Login, SpriteIndex = player.SpriteIndex, UserId = player.UserId, X = player.X, Y = player.Y, Speed = player.speed }); ServerEvents.Call(new PlayerJoinEvent() { Player = player }); }
public void Recieve() { DateTime lastPingCheck = DateTime.MinValue; Log.Debug("Starting Listener for client " + ConnectionId); Listening = true; while (Listening) { try { // Check if needs to ping the client to see if its ok DateTime now = DateTime.Now; if (now > lastPingCheck.AddSeconds(PING_CHECK_SECONDS)) { lastPingCheck = now; CheckPing(); } // Read data from socket var socketData = ReadData(); if (socketData == null) { continue; } var packetRead = PacketSerializer.Deserialize(socketData); if (packetRead != null && packetRead is BasePacket) { var packet = (BasePacket)packetRead; packet.ClientId = ConnectionId; if (typeof(PingPacket) == packet.GetType()) { RecievePing((PingPacket)packet); } else { Log.Debug($"Packet {packet.GetType().Name} recieved"); // Put the packet to be processed by the main thread Server.PacketsToProccess.Enqueue(packet); } } } catch (Exception e) { Listening = false; } } ServerEvents.Call(new PlayerQuitEvent() { Client = this, Player = OnlinePlayer, Reason = QuitReason.DISCONNECTED }); Stop(); }
[EventMethod] // When client finishes updating assets public void OnPlayerMovePath(PlayerMovePacket packet) { var player = Server.GetPlayerByConnectionId(packet.ClientId); var distanceMoved = player.GetPosition().GetDistance(packet.To); var timeToMove = Formulas.GetTimeToMoveBetweenTwoTiles(player.speed); var now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; var lastMovementArrival = now + timeToMove; // Player tryng to hack ? if (distanceMoved > 1 || now < player.CanMoveAgainTime) { // send player back to the position client-side player.Tcp.Send(new SyncPacket() { Position = player.GetPosition() }); return; } var playerMoveEvent = new PlayerMoveEvent() { From = packet.From, To = packet.To, Player = player }; ServerEvents.Call(playerMoveEvent); if (playerMoveEvent.IsCancelled) { // send player back to the position client-side player.Tcp.Send(new SyncPacket() { Position = player.GetPosition() }); return; } // subtract the player latency for possibility of lag for a smoother movement player.CanMoveAgainTime = now + timeToMove - player.Tcp.Latency; // Updating player position locally player.X = packet.To.X; player.Y = packet.To.Y; // updating in database PlayerService.UpdatePlayerPosition(player, player.X, player.Y); }
public override bool RunCommand(string[] args) { var players = Server.Players.ToArray(); int ct = 0; foreach (var player in players) { ServerEvents.Call(new PlayerQuitEvent() { Client = player.Tcp, Player = player, Reason = QuitReason.KICKED }); ct++; } return(true); }