Chat protocol work

This commit is contained in:
Michael 2017-02-12 12:11:28 -05:00
parent 092b3b49c1
commit 9f782550b5
7 changed files with 187 additions and 3 deletions

View file

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View file

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View file

@ -74,8 +74,26 @@ namespace ShiftOS.Objects
public string Topic { get; set; }
public int MaxUsers { get; set; } //0 for unlimited users (or the MUD maximum)
public List<Save> Users = new List<Save>();
public bool IsDiscordProxy { get; set; }
public string DiscordBotToken { get; set; }
public string DiscordChannelID { get; set; }
}
public class ChatMessage
{
public ChatMessage(string uname, string sys, string message, string chan)
{
Username = uname;
SystemName = sys;
Message = message;
Channel = chan;
}
public string Username { get; private set; }
public string SystemName { get; private set; }
public string Channel { get; private set; }
public string Message { get; private set; }
}
[Serializable]
public class ServerMessage

View file

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View file

@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Objects;
using NetSockets;
using Newtonsoft.Json;
using System.IO;
using static ShiftOS.Server.Program;
using Discord;
namespace ShiftOS.Server
{
public static class ChatBackend
{
public static void StartDiscordBots()
{
Reinitialized?.Invoke();
foreach (var chat in JsonConvert.DeserializeObject<List<ShiftOS.Objects.Channel>>(File.ReadAllText("chats.json")))
{
bool chatKilled = false;
if (chat.IsDiscordProxy == true)
{
DiscordConfigBuilder builder = new DiscordConfigBuilder();
builder.AppName = "ShiftOS";
builder.AppVersion = System.Reflection.Assembly.GetExecutingAssembly().FullName;
builder.AppUrl = "http://getshiftos.ml/";
var client = new DiscordClient(builder);
client.Connect(chat.DiscordBotToken, TokenType.Bot);
client.SetGame("ShiftOS");
client.SetStatus(UserStatus.Online);
client.MessageReceived += (s, e) =>
{
if (chatKilled == false)
{
if (e.Channel.Id.ToString() == chat.DiscordChannelID)
{
server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage
{
Name = "chat_msgreceived",
GUID = "server",
Contents = JsonConvert.SerializeObject(new ChatMessage(e.User.Name, "discord_" + e.Channel.Name, e.Message.Text, chat.ID))
}));
}
}
};
MessageReceived += (g, msg) =>
{
if (chatKilled == false)
{
//Determine if the message was sent to this channel.
if (msg.Channel == chat.ID)
{
//Get the Discord channel for this chat.
var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID));
//Relay the message to Discord.
dChan.SendMessage($"**[{msg.Username}@{msg.SystemName} ({msg.Channel})]: {msg.Message}");
}
//Relay it back to all MUD clients.
RelayMessage(g, msg);
}
};
Reinitialized += () =>
{
client.Disconnect();
client.Dispose();
chatKilled = true;
};
}
else
{
MessageReceived += (g, msg) =>
{
if (chatKilled == false)
{
//Just relay it.
RelayMessage(g, msg);
}
};
Reinitialized += () => { chatKilled = true; };
}
}
}
internal static void RelayMessage(string guid, ChatMessage msg)
{
server.DispatchAllExcept(new Guid(guid), new NetObject("chat_msgreceived", new ServerMessage
{
Name = "chat_msgreceived",
GUID = "server",
Contents = JsonConvert.SerializeObject(msg)
}));
}
public static event Action<string, ChatMessage> MessageReceived;
public static event empty Reinitialized;
public delegate void empty();
[MudRequest("chat_send")]
public static void ReceiveMessage(string guid, object contents)
{
var msg = JsonConvert.DeserializeObject<ChatMessage>(JsonConvert.SerializeObject(contents));
MessageReceived?.Invoke(guid, msg);
}
}
}

View file

@ -33,9 +33,29 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Discord.Net, Version=0.9.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.0.9.6\lib\net45\Discord.Net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NetSockets">
<HintPath>..\Libraries\NetSockets.dll</HintPath>
</Reference>
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.2.3\lib\net451\RestSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
@ -51,8 +71,13 @@
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="WebSocket4Net, Version=0.14.1.0, Culture=neutral, PublicKeyToken=eb4e154b696bf72a, processorArchitecture=MSIL">
<HintPath>..\packages\WebSocket4Net.0.14.1\lib\net45\WebSocket4Net.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ChatBackend.cs" />
<Compile Include="Core.cs" />
<Compile Include="LegionManager.cs" />
<Compile Include="MemoManager.cs" />

View file

@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Discord.Net" version="0.9.6" targetFramework="net451" />
<package id="DynamicLua" version="1.1.2.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="Nito.AsyncEx" version="3.0.1" targetFramework="net451" />
<package id="RestSharp" version="105.2.3" targetFramework="net451" />
<package id="WebSocket4Net" version="0.14.1" targetFramework="net451" />
</packages>