Skip to content

Commit

Permalink
Add server messages
Browse files Browse the repository at this point in the history
motd, msg
  • Loading branch information
silentbaws committed May 29, 2020
1 parent 8d21847 commit 2353c3b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
13 changes: 8 additions & 5 deletions XLMultiplayer/MultiplayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
using XLShredLib;
using XLShredLib.UI;

// TODO LIST FOR 0.8.0

// TODO: Redo the multiplayer texture system
// -> Send paths for non-custom gear
// -> Send hashes of full size textures for custom gear along with compressed texture
// -> Only send hashes/paths from server unless client requests texture data

// TODO: Create 2 connections -> One for gameplay
// -> One for files
// TODO: message of the day

// TODO: Fix dawgs board mod for mp
// TODO: server announce

namespace XLMultiplayer {
public enum OpCode : byte {
Expand All @@ -43,6 +40,7 @@ public enum OpCode : byte {
MapHash = 7,
MapVote = 8,
MapList = 9,
ServerMessage = 10,
StillAlive = 254,
Disconnect = 255
}
Expand Down Expand Up @@ -749,6 +747,11 @@ private void ProcessMessage(byte[] buffer) {
case OpCode.MapVote:
Main.utilityMenu.SendImportantChat("<b><color=#ff00ffff>MAP VOTING HAS BEGUN. THE MAP WILL CHANGE IN 30 SECONDS</color></b>", 10000);
break;
case OpCode.ServerMessage:
int duration = BitConverter.ToInt32(buffer, 1);
string messageText = ASCIIEncoding.ASCII.GetString(buffer, 5, buffer.Length - 5);
Main.utilityMenu.SendImportantChat(messageText, duration);
break;
case OpCode.StillAlive:
float sentTime = BitConverter.ToSingle(buffer, 1);
pingTimes.Add(Time.time - sentTime);
Expand Down
2 changes: 2 additions & 0 deletions XLMultiplayerServer/FileServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static void StatusCallbackFunction(ref StatusInfo info, IntPtr context) {
break;
}

if (Server.motdBytes != null) server.SendMessageToConnection(info.connection, Server.motdBytes);

Console.WriteLine("connected on file server");
} break;

Expand Down
64 changes: 63 additions & 1 deletion XLMultiplayerServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum OpCode : byte {
MapHash = 7,
MapVote = 8,
MapList = 9,
ServerMessage = 10,
StillAlive = 254,
Disconnect = 255
}
Expand Down Expand Up @@ -137,6 +138,7 @@ class Server {
private static string currentMapHash = "1";

public static List<string> bannedIPs = new List<string>();
public static byte[] motdBytes = null;

public static int Main(String[] args) {
Console.ForegroundColor = ConsoleColor.White;
Expand Down Expand Up @@ -311,8 +313,68 @@ public static void CommandLoop() {
} else {
Console.WriteLine("Invalid player ID");
}
} else if (input.ToLower().StartsWith("msg")) {
byte[] messageBytes = ProcessMessageCommand(input);
if (messageBytes != null) {
foreach(Player player in players) {
if(player != null) {
FileServer.server.SendMessageToConnection(player.fileConnection, messageBytes, SendFlags.Reliable);
}
}
}
} else if (input.ToLower().StartsWith("motd")) {
byte[] messageBytes = ProcessMessageCommand(input);
if (messageBytes != null) {
motdBytes = messageBytes;
}
}
}
}

public static byte[] ProcessMessageCommand(string input) {
//msg:duration:color content of message here
// - duration/color optional parameters so default duration is 10 seconds, default color ff00ff
string[] msgParams = input.Split(new char[] { ' ' }, 2);

if (msgParams.Length < 2) return null;

string[] msgSettings = msgParams[0].Split(':');

int duration = 10;
string msgColor = "ff00ff";

string acceptableCharacters = "abcdef0123456789";

if (msgSettings.Length > 1) {
if (Int32.TryParse(msgSettings[1], out duration)) {
duration = Math.Min(duration, 60);
duration = Math.Max(duration, 5);
} else {
duration = 10;
}
}
if (msgSettings.Length > 2) {
bool validColor = true;

foreach (char c in msgSettings[2]) {
if (!acceptableCharacters.Contains(c.ToString())) {
validColor = false;
}
}
validColor = validColor && (msgSettings[2].Length == 3 || msgSettings[2].Length == 6);

if (validColor) {
msgColor = msgSettings[2];
}
}

string messageText = $"<b><color=#{msgColor}>{msgParams[1]}</color></b>";
byte[] messageBytes = new byte[messageText.Length + 5];
messageBytes[0] = (byte)OpCode.ServerMessage;
Array.Copy(BitConverter.GetBytes(duration * 1000), 0, messageBytes, 1, 4);
Array.Copy(ASCIIEncoding.ASCII.GetBytes(messageText), 0, messageBytes, 5, messageText.Length);

return messageBytes;
}

public static void ProcessMessage(byte[] buffer, byte fromID, NetworkingSockets server) {
Expand Down Expand Up @@ -792,7 +854,7 @@ public static void HandleUsername(object data) {
}
}

Console.WriteLine("Connection {0}'s username is {1}", fromID, username);
Console.WriteLine("Connection {0}'s username is {1}", fromID, RemoveMarkup(username));

if (players[fromID] != null) {
players[fromID].username = username;
Expand Down

0 comments on commit 2353c3b

Please sign in to comment.