Skip to content

Commit d6e60f3

Browse files
authored
p34.10 (#514)
1 parent 8a38f03 commit d6e60f3

File tree

9 files changed

+92
-32
lines changed

9 files changed

+92
-32
lines changed

src/Perpetuum.Bootstrapper/PerpetuumBootstrapper.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ private static void InitGame(IComponentContext container)
301301

302302
private void InitContainer(string gameRoot)
303303
{
304+
_ = _builder.Register(c => new FileSystem(gameRoot)).As<IFileSystem>();
305+
_ = _builder.Register(c =>
306+
{
307+
IFileSystem fileManager = c.Resolve<IFileSystem>();
308+
string settingsFile = fileManager.ReadAllText("perpetuum.ini");
309+
GlobalConfiguration configuration = JsonConvert.DeserializeObject<GlobalConfiguration>(settingsFile);
310+
configuration.GameRoot = gameRoot;
311+
312+
return configuration;
313+
}).SingleInstance();
314+
304315
_builder.RegisterModule(new CommandsModule());
305316
_builder.RegisterModule(new RequestHandlersModule());
306317
_builder.RegisterModule(new ZoneRequestHandlersModule());
@@ -356,16 +367,6 @@ private void InitContainer(string gameRoot)
356367

357368
InitRelayManager();
358369

359-
_ = _builder.Register(c => new FileSystem(gameRoot)).As<IFileSystem>();
360-
_ = _builder.Register(c =>
361-
{
362-
IFileSystem fileManager = c.Resolve<IFileSystem>();
363-
string settingsFile = fileManager.ReadAllText("perpetuum.ini");
364-
GlobalConfiguration configuration = JsonConvert.DeserializeObject<GlobalConfiguration>(settingsFile);
365-
configuration.GameRoot = gameRoot;
366-
return configuration;
367-
}).SingleInstance();
368-
369370
_ = _builder.RegisterType<AdminCommandRouter>().SingleInstance();
370371

371372
_ = _builder.RegisterType<Gang>();

src/Perpetuum/Services/Channels/Channel.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class Channel
1515

1616
public bool IsForcedJoin { get; private set; }
1717

18+
public ulong? DiscordId { get; private set; }
19+
1820
public IChannelLogger Logger { get; private set; }
1921
private ChannelType _type;
2022
private ChannelType _prevType;
@@ -33,12 +35,13 @@ private Channel()
3335

3436
}
3537

36-
public Channel(int id, ChannelType type, string name, string topic, string password, bool isForcedJoin, IChannelLogger logger) : this(type, name, logger)
38+
public Channel(int id, ChannelType type, string name, string topic, string password, bool isForcedJoin, ulong? discordId, IChannelLogger logger) : this(type, name, logger)
3739
{
3840
Id = id;
3941
Topic = topic;
4042
Password = password;
4143
IsForcedJoin = isForcedJoin;
44+
DiscordId = discordId;
4245
}
4346

4447
public Channel(ChannelType type, string name, IChannelLogger logger)
@@ -63,6 +66,7 @@ public Channel SetId(int id)
6366
Topic = Topic,
6467
Password = Password,
6568
IsForcedJoin = IsForcedJoin,
69+
DiscordId = DiscordId,
6670
Logger = Logger,
6771
_members = new Dictionary<Character, ChannelMember>(_members)
6872
};
@@ -85,6 +89,7 @@ public Channel SetTopic(string topic)
8589
Topic = topic,
8690
Password = Password,
8791
IsForcedJoin = IsForcedJoin,
92+
DiscordId = DiscordId,
8893
Logger = Logger,
8994
_members = new Dictionary<Character, ChannelMember>(_members)
9095
};
@@ -102,7 +107,9 @@ public Channel SetPassword(string password)
102107
Topic = Topic,
103108
Password = password,
104109
IsForcedJoin = IsForcedJoin,
110+
DiscordId = DiscordId,
105111
Logger = Logger,
112+
106113
_members = new Dictionary<Character, ChannelMember>(_members)
107114
};
108115
}
@@ -124,6 +131,7 @@ public Channel SetMember(ChannelMember member)
124131
Topic = Topic,
125132
Password = Password,
126133
IsForcedJoin = IsForcedJoin,
134+
DiscordId = DiscordId,
127135
Logger = Logger,
128136
_members = members
129137
};
@@ -148,6 +156,7 @@ public Channel RemoveMember(Character member)
148156
Topic = Topic,
149157
Password = Password,
150158
IsForcedJoin = IsForcedJoin,
159+
DiscordId = DiscordId,
151160
Logger = Logger,
152161
_members = members
153162
};

src/Perpetuum/Services/Channels/ChannelManager.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
using Newtonsoft.Json;
2-
using Perpetuum.Accounting.Characters;
1+
using Perpetuum.Accounting.Characters;
32
using Perpetuum.Common.Loggers;
43
using Perpetuum.Host.Requests;
54
using Perpetuum.Services.Channels.ChatCommands;
5+
using Perpetuum.Services.EventServices;
6+
using Perpetuum.Services.EventServices.EventMessages;
67
using Perpetuum.Services.Sessions;
78
using System;
89
using System.Collections.Concurrent;
910
using System.Collections.Generic;
1011
using System.Linq;
11-
using System.Net.Http;
12-
using System.Text;
1312
using System.Threading;
14-
using System.Threading.Tasks;
1513

1614
namespace Perpetuum.Services.Channels
1715
{
@@ -27,6 +25,7 @@ public class ChannelManager : IChannelManager
2725
private readonly ConcurrentDictionary<string, Channel> _channels = new ConcurrentDictionary<string, Channel>();
2826
private readonly AdminCommandRouter _adminCommand;
2927
private readonly GlobalConfiguration _globalConfiguration;
28+
private readonly EventListenerService _eventChannel;
3029

3130
public ChannelManager(
3231
ISessionManager sessionManager,
@@ -35,6 +34,7 @@ public ChannelManager(
3534
IChannelBanRepository banRepository,
3635
ChannelLoggerFactory channelLoggerFactory,
3736
AdminCommandRouter adminCommand,
37+
EventListenerService eventListener,
3838
GlobalConfiguration globalConfiguration)
3939
{
4040
_sessionManager = sessionManager;
@@ -51,6 +51,7 @@ public ChannelManager(
5151
_channels[channel.Name] = channel;
5252
}
5353

54+
_eventChannel = eventListener;
5455
_globalConfiguration = globalConfiguration;
5556
}
5657

@@ -313,6 +314,17 @@ public void Talk(string channelName, Character sender, string message, IRequest
313314
{
314315
channel.SendMessageToAll(_sessionManager, sender, message);
315316

317+
if (channel.DiscordId != null)
318+
{
319+
_eventChannel.PublishMessage(
320+
new DiscordIntegrationMessage(
321+
EventType.PerpetuumToDiscord,
322+
channel.DiscordId.Value,
323+
sender.Nick,
324+
message));
325+
}
326+
327+
/*
316328
if (channel.Name == HelpChat)
317329
{
318330
// Sending message to discord
@@ -340,6 +352,7 @@ public void Talk(string channelName, Character sender, string message, IRequest
340352
HttpResponseMessage response = await httpClient.PostAsync(url, content);
341353
});
342354
}
355+
*/
343356
}
344357
}
345358

@@ -461,11 +474,17 @@ public IEnumerable<Channel> GetAllChannels()
461474
{
462475
return _channels.Values;
463476
}
477+
478+
public string GetChannelNameByDiscordId(ulong discordId)
479+
{
480+
return _channels.FirstOrDefault(x => x.Value.DiscordId.GetValueOrDefault() == discordId).Key;
481+
}
464482
}
465483

466484
internal class DiscordPayload
467485
{
468-
public DateTime Timestamp { get; set; }
469486
public string content { get; set; }
487+
488+
public object allowed_mentions { get; } = new { parse = new[] { "users" } };
470489
}
471490
}

src/Perpetuum/Services/Channels/ChannelRepository.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ public IEnumerable<Channel> GetAll()
5252
string topic = record.GetValue<string>("topic");
5353
string password = record.GetValue<string>("password");
5454
bool isForcedJoin = record.GetValueOrDefault<bool>("isForcedJoin");
55+
string discordIdString = record.GetValueOrDefault<string>("DiscordId");
56+
ulong? discordId = ulong.TryParse(discordIdString, out ulong parsedId)
57+
? parsedId
58+
: (ulong?)null;
5559

5660
IChannelLogger logger = _channelLoggerFactory(name);
5761

58-
return new Channel(id, type, name, topic, password, isForcedJoin, logger);
62+
return new Channel(id, type, name, topic, password, isForcedJoin, discordId, logger);
5963
}).ToArray();
6064
}
6165
}

src/Perpetuum/Services/Channels/IChannelManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Collections.Generic;
2-
using Perpetuum.Accounting.Characters;
1+
using Perpetuum.Accounting.Characters;
32
using Perpetuum.Host.Requests;
3+
using System.Collections.Generic;
44

55
namespace Perpetuum.Services.Channels
66
{
@@ -11,6 +11,8 @@ public interface IChannelManager
1111
[CanBeNull]
1212
Channel GetChannelByName(string name);
1313

14+
string GetChannelNameByDiscordId(ulong discordId);
15+
1416
void CreateChannel(ChannelType type, string name);
1517
void DeleteChannel(string channelName);
1618
void JoinChannel(string channelName, Character member, ChannelMemberRole role, string password);

src/Perpetuum/Services/EventServices/EventListenerService.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,21 @@ public EventListenerService(GlobalConfiguration globalConfiguration)
4141
/// <param name="message">EventMessage of the type</param>
4242
public void PublishMessage(IEventMessage message)
4343
{
44-
_queue.Enqueue(message);
44+
if (message is DiscordIntegrationMessage discordMessage &&
45+
discordMessage.Type == EventType.PerpetuumToDiscord)
46+
{
47+
if (_client.GetChannel(discordMessage.ChannelDiscordId) is IMessageChannel discordChannel)
48+
{
49+
string messageToSend = $"**<{discordMessage.Nick}>**: {discordMessage.Message}";
50+
discordChannel.SendMessageAsync(
51+
messageToSend,
52+
allowedMentions: new AllowedMentions { AllowedTypes = AllowedMentionTypes.Users });
53+
}
54+
}
55+
else
56+
{
57+
_queue.Enqueue(message);
58+
}
4559
}
4660

4761
public void NotifyListeners(IEventMessage message)
@@ -127,17 +141,20 @@ private Task OnMessageReceived(SocketMessage message)
127141
{
128142
ulong.TryParse(_globalConfiguration.OpHelpChannelId, out ulong channelId);
129143

130-
if (!message.Author.IsBot && message.Channel.Id == channelId && !string.IsNullOrEmpty(message.Content))
144+
if (!message.Author.IsBot && /*message.Channel.Id == channelId &&*/ !string.IsNullOrEmpty(message.CleanContent))
131145
{
132146
string nick = message.Author.GlobalName;
133147

148+
// No more imposting until we find a better approach
149+
/*
134150
if (message.Author is SocketGuildUser guildUser &&
135151
!string.IsNullOrEmpty(guildUser.DisplayName))
136152
{
137153
nick = guildUser.DisplayName;
138154
}
155+
*/
139156

140-
PublishMessage(new DiscordIntegrationMessage(nick, message.Content));
157+
PublishMessage(new DiscordIntegrationMessage(EventType.DiscordToPerpetuum, message.Channel.Id, nick, message.CleanContent));
141158
}
142159

143160
return Task.CompletedTask;

src/Perpetuum/Services/EventServices/EventMessages/DiscordIntegrationMessage.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
/// </summary>
66
public class DiscordIntegrationMessage : IEventMessage
77
{
8-
public EventType Type => EventType.DiscordIntegration;
8+
public EventType Type { get; private set; }
9+
public ulong ChannelDiscordId { get; private set; }
910
public string Nick { get; private set; }
1011
public string Message { get; private set; }
11-
public DiscordIntegrationMessage(string nick, string message)
12+
public DiscordIntegrationMessage(EventType type, ulong channelDiscordId, string nick, string message)
1213
{
14+
Type = type;
15+
ChannelDiscordId = channelDiscordId;
1316
Nick = nick;
1417
Message = message;
1518
}

src/Perpetuum/Services/EventServices/EventProcessors/DiscordIntegrationHandler.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,27 @@ public class DiscordIntegrationHandler : EventProcessor
1111
{
1212
private readonly IChannelManager _channelManager;
1313
private const string SENDER_CHARACTER_NICKNAME = "Discord";
14-
private const string HelpChat = "regchannel_help";
15-
private readonly Character _announcer;
14+
private readonly Character _discordIntegrationCharacter;
1615

1716
public DiscordIntegrationHandler(IChannelManager channelManager)
1817
{
19-
_announcer = Character.GetByNick(SENDER_CHARACTER_NICKNAME);
18+
_discordIntegrationCharacter = Character.GetByNick(SENDER_CHARACTER_NICKNAME);
2019
_channelManager = channelManager;
2120
}
2221

23-
public override EventType Type => EventType.DiscordIntegration;
22+
public override EventType Type => EventType.DiscordToPerpetuum;
2423
public override void HandleMessage(IEventMessage message)
2524
{
2625
if (message is DiscordIntegrationMessage discordMessage)
2726
{
28-
string chatMessage = $"{discordMessage.Nick}: {discordMessage.Message}";
27+
string channelName = _channelManager.GetChannelNameByDiscordId(discordMessage.ChannelDiscordId);
2928

30-
_channelManager.Announcement(HelpChat, _announcer, chatMessage);
29+
if (!string.IsNullOrEmpty(channelName))
30+
{
31+
string chatMessage = $"{discordMessage.Nick}: {discordMessage.Message}";
32+
33+
_channelManager.Announcement(channelName, _discordIntegrationCharacter, chatMessage);
34+
}
3135
}
3236
}
3337
}

src/Perpetuum/Services/EventServices/EventType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum EventType
1313
Environmental,
1414
PortalSpawn,
1515
NpcSapAttackers,
16-
DiscordIntegration,
16+
DiscordToPerpetuum,
17+
PerpetuumToDiscord,
1718
}
1819
}

0 commit comments

Comments
 (0)