public void HandleMessage(Client.Client sender, PacketReader stream) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } try { if (sender.TryGetControlledNpc(out NpcInst npc)) { sender.RemoveControl(); if (_CharacterService.TryGetMapping(npc, out CharacterMapping mapping)) { mapping.Character.RemoveMapping(); mapping.Character.Save(); } npc.Despawn(); } } catch (Exception e) { _Log.Error($"Something went wrong while handling a '{SupportedMessage}' script message. Disconnecting the client to get back into a defined state. Exception: {e}"); sender.Disconnect(); } }
public void HandleMessage(Client.Client sender, PacketReader stream) { //Only accept messages of logged in clients. if (_AuthenticationService.IsLoggedIn(sender)) { _ScriptMessageHandlerImplementation.HandleMessage(sender, stream); } else { _Log.Warn($"Client '{sender.SystemAddress}' send a '{SupportedMessage}' packet while not being logged in."); sender.Disconnect(); } }
public async void HandleMessage(Client.Client sender, PacketReader stream) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } try { CharCreationInfo creationInfo = new CharCreationInfo(); creationInfo.Read(stream); //Get the account session of the client. if (_AuthenticationService.TryGetSession(sender, out Session session)) { //Lets first check if the account can have an additional account(limit is taken from the rp config). int charOwnershipsCount = await _CharacterService.GetCharacterOwnershipsCountAsync(session.Account); if (charOwnershipsCount >= _RpConfig.MaxCharacterPerAccount) { using (var packet = _PacketWriterPool.GetScriptMessageStream(ScriptMessages.CharacterCreationResult)) { packet.Write(false); packet.Write((byte)CharacterCreationFailure.CharacterLimitReached); sender.SendScriptMessage(packet, NetPriority.Medium, NetReliability.Reliable); } return; } //Create the new character. CharacterCreationResult result = await _CharacterService.CreateHumanPlayerCharacterAsync(creationInfo); if (result is CharacterCreationFailed failed) { using (var packet = _PacketWriterPool.GetScriptMessageStream(ScriptMessages.CharacterCreationResult)) { //Failure packet.Write(false); packet.Write((byte)failed.Reason); sender.SendScriptMessage(packet, NetPriority.Medium, NetReliability.Reliable); } } else if (result is CharacterCreationSuccess success) { //The character was created. Add an ownership entity and set the active character for account of the message sender. await Task.WhenAll(new List <Task> { _CharacterService.AddCharacterOwnershipAsync(session.Account, success.Character), _CharacterService.SetAccountActiveCharacterAsync(session.Account, success.Character) }); using (var packet = _PacketWriterPool.GetScriptMessageStream(ScriptMessages.CharacterCreationResult)) { //Success packet.Write(true); sender.SendScriptMessage(packet, NetPriority.Medium, NetReliability.Reliable); } } } else { //This should not even be possible. Disconnect the client. _Log.Error($"The client '{sender.SystemAddress}' tried to create a character while not being logged in(should never be possible)"); sender.Disconnect(); } } catch (Exception e) { _Log.Error($"Something went wrong while handling a '{SupportedMessage}' message from the client '{sender.SystemAddress}'. Exception: {e}"); sender.Disconnect(); } }
public async void HandleMessage(Client.Client sender, PacketReader stream) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } try { int id = stream.ReadInt(); //Projection against receiving the message again(which would be a client problem). if (sender.TryGetControlledNpc(out NpcInst controlledNpc)) { if (_Service.TryGetMapping(controlledNpc, out CharacterMapping characterMapping)) { characterMapping.Character.RemoveMapping(); } else { controlledNpc.Despawn(); } _Log.Error($"A client send a {SupportedMessage} even through it does already control a npc. Trying to disconnect the player and remove the npc to get back into a defined state"); sender.Disconnect(); return; } //Get and map the selected character of the client. if (_AuthenticationService.TryGetSession(sender, out Session session)) { IList <Character> list = await _Service.GetAccountOwnedCharactersAsync(session.Account); Character selectedChar = list.FirstOrDefault(c => c.CharacterId == id); bool success = true; JoinGameFailedReason failureReason = JoinGameFailedReason.None; if (selectedChar == null) { //Invalid character id... return an error success = false; failureReason = JoinGameFailedReason.InvalidCharacterId; } else { //Save the active character(this causes a database access). await _Service.SetAccountActiveCharacterAsync(session.Account, selectedChar); if (!selectedChar.TryGetMapping(out CharacterMapping mapping)) { mapping = selectedChar.SpawnAndMap(); } if (mapping.CharacterNpc.TryGetControllingClient(out Client.Client controllingClient)) { //Character is in use by someone else. success = false; failureReason = JoinGameFailedReason.CharacterInUse; } else { sender.SetControl(mapping.CharacterNpc); } } //Send the result to the client. using (var packet = _PacketWriterPool.GetScriptMessageStream(ScriptMessages.JoinGameResult)) { packet.Write(success); //Write the error code. if (!success) { packet.Write((byte)failureReason); } sender.SendScriptMessage(packet, NetPriority.Medium, NetReliability.Reliable); } } } catch (Exception e) { _Log.Error($"Something went wrong while handling a '{SupportedMessage}' script message. Disconnecting the client to get back into a defined state. Exception: {e}"); //Disconnect the client to get back into a defined state. sender.Disconnect(); } }
public async void HandleMessage(Client.Client sender, PacketReader stream) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } try { //Get the account session of the client. if (_AuthenticationService.TryGetSession(sender, out Session session)) { var ownedCharsTask = _CharacterService.GetAccountOwnedCharactersAsync(session.Account); var activeCharTask = _CharacterService.GetAccountActiveCharacterTransactionAsync(session.Account); await Task.WhenAll(ownedCharsTask, activeCharTask); //Get a message writer from the pool and use it to send a message. using (var writer = _WriterPool.GetScriptMessageStream(ScriptMessages.CharacterListResult)) { //Write the id of the active character. Write -1 if no active character was found. if (activeCharTask.Result is ActiveCharacterFound activeCharacterFound) { writer.Write(activeCharacterFound.CharacterId); } else { writer.Write((int)-1); } var charList = ownedCharsTask.Result; //Write the length of the character list and the visuals of all characters. writer.Write((byte)charList.Count); foreach (var character in charList) { _VisualsWriter.WriteCharacter(writer, character); } //Send the message to the client. This message does not need to be transmitted quickly but it has to be reliable. sender.SendScriptMessage(writer, NetPriority.Medium, NetReliability.Reliable); } } else { //This should not even be possible. Disconnect the client. _Log.Error($"The client '{sender.SystemAddress}' tried to request a character list while not being logged in(should never be possible)"); sender.Disconnect(); } } catch (Exception e) { _Log.Error($"Something went wrong while handling a script message of type '{SupportedMessage}' from client '{sender.SystemAddress}'. Exception {e}"); //Something went wrong really bad. Disconnect the client to get back to a consistent state. sender.Disconnect(); } }