Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/Perpetuum.Bootstrapper/PerpetuumBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@ private static void InitGame(IComponentContext container)

private void InitContainer(string gameRoot)
{
_ = _builder.Register(c => new FileSystem(gameRoot)).As<IFileSystem>();
_ = _builder.Register(c =>
{
IFileSystem fileManager = c.Resolve<IFileSystem>();
string settingsFile = fileManager.ReadAllText("perpetuum.ini");
GlobalConfiguration configuration = JsonConvert.DeserializeObject<GlobalConfiguration>(settingsFile);
configuration.GameRoot = gameRoot;

return configuration;
}).SingleInstance();

_builder.RegisterModule(new CommandsModule());
_builder.RegisterModule(new RequestHandlersModule());
_builder.RegisterModule(new ZoneRequestHandlersModule());
Expand Down Expand Up @@ -356,16 +367,6 @@ private void InitContainer(string gameRoot)

InitRelayManager();

_ = _builder.Register(c => new FileSystem(gameRoot)).As<IFileSystem>();
_ = _builder.Register(c =>
{
IFileSystem fileManager = c.Resolve<IFileSystem>();
string settingsFile = fileManager.ReadAllText("perpetuum.ini");
GlobalConfiguration configuration = JsonConvert.DeserializeObject<GlobalConfiguration>(settingsFile);
configuration.GameRoot = gameRoot;
return configuration;
}).SingleInstance();

_ = _builder.RegisterType<AdminCommandRouter>().SingleInstance();

_ = _builder.RegisterType<Gang>();
Expand Down
11 changes: 10 additions & 1 deletion src/Perpetuum/Services/Channels/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Channel

public bool IsForcedJoin { get; private set; }

public ulong? DiscordId { get; private set; }

public IChannelLogger Logger { get; private set; }
private ChannelType _type;
private ChannelType _prevType;
Expand All @@ -33,12 +35,13 @@ private Channel()

}

public Channel(int id, ChannelType type, string name, string topic, string password, bool isForcedJoin, IChannelLogger logger) : this(type, name, logger)
public Channel(int id, ChannelType type, string name, string topic, string password, bool isForcedJoin, ulong? discordId, IChannelLogger logger) : this(type, name, logger)
{
Id = id;
Topic = topic;
Password = password;
IsForcedJoin = isForcedJoin;
DiscordId = discordId;
}

public Channel(ChannelType type, string name, IChannelLogger logger)
Expand All @@ -63,6 +66,7 @@ public Channel SetId(int id)
Topic = Topic,
Password = Password,
IsForcedJoin = IsForcedJoin,
DiscordId = DiscordId,
Logger = Logger,
_members = new Dictionary<Character, ChannelMember>(_members)
};
Expand All @@ -85,6 +89,7 @@ public Channel SetTopic(string topic)
Topic = topic,
Password = Password,
IsForcedJoin = IsForcedJoin,
DiscordId = DiscordId,
Logger = Logger,
_members = new Dictionary<Character, ChannelMember>(_members)
};
Expand All @@ -102,7 +107,9 @@ public Channel SetPassword(string password)
Topic = Topic,
Password = password,
IsForcedJoin = IsForcedJoin,
DiscordId = DiscordId,
Logger = Logger,

_members = new Dictionary<Character, ChannelMember>(_members)
};
}
Expand All @@ -124,6 +131,7 @@ public Channel SetMember(ChannelMember member)
Topic = Topic,
Password = Password,
IsForcedJoin = IsForcedJoin,
DiscordId = DiscordId,
Logger = Logger,
_members = members
};
Expand All @@ -148,6 +156,7 @@ public Channel RemoveMember(Character member)
Topic = Topic,
Password = Password,
IsForcedJoin = IsForcedJoin,
DiscordId = DiscordId,
Logger = Logger,
_members = members
};
Expand Down
31 changes: 25 additions & 6 deletions src/Perpetuum/Services/Channels/ChannelManager.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using Newtonsoft.Json;
using Perpetuum.Accounting.Characters;
using Perpetuum.Accounting.Characters;
using Perpetuum.Common.Loggers;
using Perpetuum.Host.Requests;
using Perpetuum.Services.Channels.ChatCommands;
using Perpetuum.Services.EventServices;
using Perpetuum.Services.EventServices.EventMessages;
using Perpetuum.Services.Sessions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

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

public ChannelManager(
ISessionManager sessionManager,
Expand All @@ -35,6 +34,7 @@ public ChannelManager(
IChannelBanRepository banRepository,
ChannelLoggerFactory channelLoggerFactory,
AdminCommandRouter adminCommand,
EventListenerService eventListener,
GlobalConfiguration globalConfiguration)
{
_sessionManager = sessionManager;
Expand All @@ -51,6 +51,7 @@ public ChannelManager(
_channels[channel.Name] = channel;
}

_eventChannel = eventListener;
_globalConfiguration = globalConfiguration;
}

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

if (channel.DiscordId != null)
{
_eventChannel.PublishMessage(
new DiscordIntegrationMessage(
EventType.PerpetuumToDiscord,
channel.DiscordId.Value,
sender.Nick,
message));
}

/*
if (channel.Name == HelpChat)
{
// Sending message to discord
Expand Down Expand Up @@ -340,6 +352,7 @@ public void Talk(string channelName, Character sender, string message, IRequest
HttpResponseMessage response = await httpClient.PostAsync(url, content);
});
}
*/
}
}

Expand Down Expand Up @@ -461,11 +474,17 @@ public IEnumerable<Channel> GetAllChannels()
{
return _channels.Values;
}

public string GetChannelNameByDiscordId(ulong discordId)
{
return _channels.FirstOrDefault(x => x.Value.DiscordId.GetValueOrDefault() == discordId).Key;
}
}

internal class DiscordPayload
{
public DateTime Timestamp { get; set; }
public string content { get; set; }

public object allowed_mentions { get; } = new { parse = new[] { "users" } };
}
}
6 changes: 5 additions & 1 deletion src/Perpetuum/Services/Channels/ChannelRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ public IEnumerable<Channel> GetAll()
string topic = record.GetValue<string>("topic");
string password = record.GetValue<string>("password");
bool isForcedJoin = record.GetValueOrDefault<bool>("isForcedJoin");
string discordIdString = record.GetValueOrDefault<string>("DiscordId");
ulong? discordId = ulong.TryParse(discordIdString, out ulong parsedId)
? parsedId
: (ulong?)null;

IChannelLogger logger = _channelLoggerFactory(name);

return new Channel(id, type, name, topic, password, isForcedJoin, logger);
return new Channel(id, type, name, topic, password, isForcedJoin, discordId, logger);
}).ToArray();
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Perpetuum/Services/Channels/IChannelManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using Perpetuum.Accounting.Characters;
using Perpetuum.Accounting.Characters;
using Perpetuum.Host.Requests;
using System.Collections.Generic;

namespace Perpetuum.Services.Channels
{
Expand All @@ -11,6 +11,8 @@ public interface IChannelManager
[CanBeNull]
Channel GetChannelByName(string name);

string GetChannelNameByDiscordId(ulong discordId);

void CreateChannel(ChannelType type, string name);
void DeleteChannel(string channelName);
void JoinChannel(string channelName, Character member, ChannelMemberRole role, string password);
Expand Down
23 changes: 20 additions & 3 deletions src/Perpetuum/Services/EventServices/EventListenerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ public EventListenerService(GlobalConfiguration globalConfiguration)
/// <param name="message">EventMessage of the type</param>
public void PublishMessage(IEventMessage message)
{
_queue.Enqueue(message);
if (message is DiscordIntegrationMessage discordMessage &&
discordMessage.Type == EventType.PerpetuumToDiscord)
{
if (_client.GetChannel(discordMessage.ChannelDiscordId) is IMessageChannel discordChannel)
{
string messageToSend = $"**<{discordMessage.Nick}>**: {discordMessage.Message}";
discordChannel.SendMessageAsync(
messageToSend,
allowedMentions: new AllowedMentions { AllowedTypes = AllowedMentionTypes.Users });
}
}
else
{
_queue.Enqueue(message);
}
}

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

if (!message.Author.IsBot && message.Channel.Id == channelId && !string.IsNullOrEmpty(message.Content))
if (!message.Author.IsBot && /*message.Channel.Id == channelId &&*/ !string.IsNullOrEmpty(message.CleanContent))
{
string nick = message.Author.GlobalName;

// No more imposting until we find a better approach
/*
if (message.Author is SocketGuildUser guildUser &&
!string.IsNullOrEmpty(guildUser.DisplayName))
{
nick = guildUser.DisplayName;
}
*/

PublishMessage(new DiscordIntegrationMessage(nick, message.Content));
PublishMessage(new DiscordIntegrationMessage(EventType.DiscordToPerpetuum, message.Channel.Id, nick, message.CleanContent));
}

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
/// </summary>
public class DiscordIntegrationMessage : IEventMessage
{
public EventType Type => EventType.DiscordIntegration;
public EventType Type { get; private set; }
public ulong ChannelDiscordId { get; private set; }
public string Nick { get; private set; }
public string Message { get; private set; }
public DiscordIntegrationMessage(string nick, string message)
public DiscordIntegrationMessage(EventType type, ulong channelDiscordId, string nick, string message)
{
Type = type;
ChannelDiscordId = channelDiscordId;
Nick = nick;
Message = message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ public class DiscordIntegrationHandler : EventProcessor
{
private readonly IChannelManager _channelManager;
private const string SENDER_CHARACTER_NICKNAME = "Discord";
private const string HelpChat = "regchannel_help";
private readonly Character _announcer;
private readonly Character _discordIntegrationCharacter;

public DiscordIntegrationHandler(IChannelManager channelManager)
{
_announcer = Character.GetByNick(SENDER_CHARACTER_NICKNAME);
_discordIntegrationCharacter = Character.GetByNick(SENDER_CHARACTER_NICKNAME);
_channelManager = channelManager;
}

public override EventType Type => EventType.DiscordIntegration;
public override EventType Type => EventType.DiscordToPerpetuum;
public override void HandleMessage(IEventMessage message)
{
if (message is DiscordIntegrationMessage discordMessage)
{
string chatMessage = $"{discordMessage.Nick}: {discordMessage.Message}";
string channelName = _channelManager.GetChannelNameByDiscordId(discordMessage.ChannelDiscordId);

_channelManager.Announcement(HelpChat, _announcer, chatMessage);
if (!string.IsNullOrEmpty(channelName))
{
string chatMessage = $"{discordMessage.Nick}: {discordMessage.Message}";

_channelManager.Announcement(channelName, _discordIntegrationCharacter, chatMessage);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Perpetuum/Services/EventServices/EventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum EventType
Environmental,
PortalSpawn,
NpcSapAttackers,
DiscordIntegration,
DiscordToPerpetuum,
PerpetuumToDiscord,
}
}