Skip to content

Commit

Permalink
Merge pull request #96 from sebastian-heinz/refactor-chat-and-party
Browse files Browse the repository at this point in the history
Refactor chat and party
  • Loading branch information
sebastian-heinz authored Oct 8, 2022
2 parents 7554105 + c4a7c95 commit a701be3
Show file tree
Hide file tree
Showing 91 changed files with 152,307 additions and 5,154 deletions.
1 change: 1 addition & 0 deletions Arrowgene.Ddon.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internal class Program
PacketId.S2C_CONNECTION_PING_RES,
PacketId.C2L_PING_REQ,
PacketId.L2C_PING_RES,
PacketId.S2C_LOBBY_LOBBY_DATA_MSG_NTC,
};

private static void Main(string[] args)
Expand Down
111 changes: 59 additions & 52 deletions Arrowgene.Ddon.GameServer/Chat/ChatManager.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,100 @@
using System;
using System.Collections.Generic;
using Arrowgene.Ddon.GameServer.Party;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Logging;
using static Arrowgene.Ddon.Shared.Util;
using Arrowgene.Ddon.Server.Network;

namespace Arrowgene.Ddon.GameServer.Chat
{
public class ChatManager
{
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(ChatManager));

public IEnumerable<ChatMessageLogEntry> ChatMessageLog { get => _chatMessageLog; }

private readonly List<IChatHandler> _handler;
private readonly GameRouter _router;
private readonly DdonGameServer _server;
private readonly RollingList<ChatMessageLogEntry> _chatMessageLog;

public ChatManager(DdonGameServer server, GameRouter router)
{
_server = server;
_router = router;
_handler = new List<IChatHandler>();
_chatMessageLog = new RollingList<ChatMessageLogEntry>(100); // TODO: Move to server config
}

public void AddHandler(IChatHandler handler)
{
_handler.Add(handler);
}

public void Handle(IPartyMember messageSender, ChatMessage message)
public void SendMessage(string message, string firstName, string lastName, LobbyChatMsgType type,
params uint[] characterIds)
{
ChatResponse response = new ChatResponse();
response.Deliver = true;
response.Message = message;
response.FirstName = firstName;
response.LastName = lastName;
response.CharacterId = 0;
response.Type = type;
response.Unk2 = 0;
response.Unk3 = 0;
response.Unk4 = 0;
foreach (uint characterId in characterIds)
{
GameClient client = _server.ClientLookup.GetClientByCharacterId(characterId);
if (client == null)
{
continue;
}

response.Recipients.Add(client);
}

_router.Send(response);
}

public void SendMessage(string message, string firstName, string lastName, LobbyChatMsgType type,
List<GameClient> recipients)
{
ChatResponse response = new ChatResponse();
response.Deliver = true;
response.Message = message;
response.FirstName = firstName;
response.LastName = lastName;
response.CharacterId = 0;
response.Type = type;
response.Unk2 = 0;
response.Unk3 = 0;
response.Unk4 = 0;
response.Recipients.AddRange(recipients);
_router.Send(response);
}

public void Handle(GameClient client, ChatMessage message)
{
if (messageSender == null)
if (client == null)
{
Logger.Debug("Client is Null");
return;
}

if (message == null)
{
if(messageSender is GameClient)
{
Logger.Debug(messageSender as GameClient, "Chat Message is Null");
}
else
{
Logger.Debug("Chat Message is Null");
}
Logger.Debug(client, "Chat Message is Null");
return;
}

ChatMessageLogEntry logEntry = new ChatMessageLogEntry(messageSender.Character, message);
_chatMessageLog.Add(logEntry);

List<ChatResponse> responses = new List<ChatResponse>();
foreach (IChatHandler handler in _handler)
{
handler.Handle(messageSender, message, responses);
handler.Handle(client, message, responses);
}

if (message.Deliver)
{
// deliver original chat message
ChatResponse response = ChatResponse.FromMessage(messageSender, message);
Deliver(messageSender, response);
ChatResponse response = ChatResponse.FromMessage(client, message);
Deliver(client, response);
}

foreach (ChatResponse response in responses)
Expand All @@ -77,11 +105,11 @@ public void Handle(IPartyMember messageSender, ChatMessage message)
continue;
}

Deliver(messageSender, response);
Deliver(client, response);
}
}

private void Deliver(IPartyMember messageSender, ChatResponse response)
private void Deliver(GameClient client, ChatResponse response)
{
switch (response.Type)
{
Expand All @@ -90,39 +118,18 @@ private void Deliver(IPartyMember messageSender, ChatResponse response)
response.Recipients.AddRange(_server.Clients);
break;
case LobbyChatMsgType.Party:
if(messageSender.Party != null)
response.Recipients.AddRange(messageSender.Party.Members);
PartyGroup party = client.Party;
if (party != null)
{
response.Recipients.AddRange(party.Clients);
}
break;
default:
response.Recipients.Add(messageSender);
response.Recipients.Add(client);
break;
}

_router.Send(response);
}


public class ChatMessageLogEntry
{
// For the JSON deserializer
public ChatMessageLogEntry()
{
}

public ChatMessageLogEntry(Character character, ChatMessage chatMessage)
{
DateTime = DateTime.Now;
FirstName = character.FirstName;
LastName = character.LastName;
CharacterId = character.Id;
ChatMessage = chatMessage;
}

public DateTime DateTime { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public uint CharacterId { get; set; }
public ChatMessage ChatMessage { get; set; }
}
}
}
10 changes: 5 additions & 5 deletions Arrowgene.Ddon.GameServer/Chat/ChatResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Arrowgene.Ddon.GameServer.Chat
{
public class ChatResponse
{
public static ChatResponse CommandError(IPartyMember client, string message)
public static ChatResponse CommandError(GameClient client, string message)
{
return new ChatResponse()
{
Expand All @@ -16,7 +16,7 @@ public static ChatResponse CommandError(IPartyMember client, string message)
};
}

public static ChatResponse ServerMessage(IPartyMember client, string message)
public static ChatResponse ServerMessage(GameClient client, string message)
{
return new ChatResponse()
{
Expand All @@ -27,7 +27,7 @@ public static ChatResponse ServerMessage(IPartyMember client, string message)
};
}

public static ChatResponse FromMessage(IPartyMember client, ChatMessage message)
public static ChatResponse FromMessage(GameClient client, ChatMessage message)
{
return new ChatResponse()
{
Expand All @@ -46,7 +46,7 @@ public static ChatResponse FromMessage(IPartyMember client, ChatMessage message)

public ChatResponse()
{
Recipients = new List<IPartyMember>();
Recipients = new List<GameClient>();
Deliver = true;
Type = LobbyChatMsgType.Say;
Unk2 = 0;
Expand All @@ -59,7 +59,7 @@ public ChatResponse()
ClanName = "";
}

public List<IPartyMember> Recipients { get; }
public List<GameClient> Recipients { get; }
public bool Deliver { get; set; }
public LobbyChatMsgType Type { get; set; }
public byte Unk2 { get; set; }
Expand Down
8 changes: 0 additions & 8 deletions Arrowgene.Ddon.GameServer/Chat/Command/ChatCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,5 @@ public void Handle(GameClient client, ChatMessage message, List<ChatResponse> re

chatCommand.Execute(subCommand, client, message, responses);
}

public void Handle(IPartyMember client, ChatMessage message, List<ChatResponse> responses)
{
if(client is GameClient)
{
Handle(client as GameClient, message, responses);
}
}
}
}
2 changes: 1 addition & 1 deletion Arrowgene.Ddon.GameServer/Chat/IChatHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Arrowgene.Ddon.GameServer.Chat
{
public interface IChatHandler
{
void Handle(IPartyMember client, ChatMessage message, List<ChatResponse> responses);
void Handle(GameClient client, ChatMessage message, List<ChatResponse> responses);
}
}
25 changes: 16 additions & 9 deletions Arrowgene.Ddon.GameServer/Chat/Log/ChatLogHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Arrowgene.Ddon.GameServer.Chat.Command;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Ddon.Shared;
using Arrowgene.Logging;

namespace Arrowgene.Ddon.GameServer.Chat.Log
Expand All @@ -10,17 +10,24 @@ public class ChatLogHandler : IChatHandler
{
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(ChatCommandHandler));

public void Handle(GameClient client, ChatMessage message, List<ChatResponse> responses)
private readonly RollingList<ChatMessageLogEntry> _chatMessageLog;

public ChatLogHandler()
{
Logger.Info(client, message.Message);
_chatMessageLog = new RollingList<ChatMessageLogEntry>(100); // TODO: Move to server config
}

public void Handle(IPartyMember client, ChatMessage message, List<ChatResponse> responses)
public IEnumerable<ChatMessageLogEntry> ChatMessageLog
{
if(client is GameClient)
{
Handle(client as GameClient, message, responses);
}
get => _chatMessageLog;
}

public void Handle(GameClient client, ChatMessage message, List<ChatResponse> responses)
{
Logger.Info(client, message.Message);

ChatMessageLogEntry logEntry = new ChatMessageLogEntry(client.Character, message);
_chatMessageLog.Add(logEntry);
}
}
}
27 changes: 27 additions & 0 deletions Arrowgene.Ddon.GameServer/Chat/Log/ChatMessageLogEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Arrowgene.Ddon.Shared.Model;

namespace Arrowgene.Ddon.GameServer.Chat.Log;

public class ChatMessageLogEntry
{
// For the JSON deserializer
public ChatMessageLogEntry()
{
}

public ChatMessageLogEntry(Character character, ChatMessage chatMessage)
{
DateTime = DateTime.Now;
FirstName = character.FirstName;
LastName = character.LastName;
CharacterId = character.Id;
ChatMessage = chatMessage;
}

public DateTime DateTime { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public uint CharacterId { get; set; }
public ChatMessage ChatMessage { get; set; }
}
Loading

0 comments on commit a701be3

Please sign in to comment.