2022-02-14 19:45:58 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2022-02-16 00:35:38 +00:00
|
|
|
|
using System.Numerics;
|
2022-02-14 19:45:58 +00:00
|
|
|
|
using Server;
|
2022-02-18 01:56:12 +00:00
|
|
|
|
using Shared;
|
2022-02-14 19:45:58 +00:00
|
|
|
|
using Shared.Packet.Packets;
|
2022-02-14 20:40:42 +00:00
|
|
|
|
using Timer = System.Timers.Timer;
|
2022-02-14 19:45:58 +00:00
|
|
|
|
|
|
|
|
|
Server.Server server = new Server.Server();
|
2022-02-14 20:40:42 +00:00
|
|
|
|
HashSet<int> shineBag = new HashSet<int>();
|
2022-02-22 04:05:13 +00:00
|
|
|
|
// int shineTx = 0; // used for logging
|
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource();
|
|
|
|
|
Task listenTask = server.Listen(cts.Token);
|
|
|
|
|
Logger consoleLogger = new Logger("Console");
|
2022-02-14 19:45:58 +00:00
|
|
|
|
|
|
|
|
|
server.ClientJoined += async (c, type) => {
|
|
|
|
|
c.Metadata["shineSync"] = new ConcurrentBag<int>();
|
|
|
|
|
c.Metadata["loadedSave"] = false;
|
2022-03-01 21:08:53 +00:00
|
|
|
|
c.Metadata["scenario"] = 0;
|
|
|
|
|
c.PacketTransformer += (sender, packet) => {
|
|
|
|
|
if (Settings.Instance.Scenario.MergeEnabled && packet is PlayerPacket playerPacket) {
|
|
|
|
|
playerPacket.ScenarioNum = (int) c.Metadata["scenario"];
|
|
|
|
|
return playerPacket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
|
};
|
2022-02-14 19:45:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async Task ClientSyncShineBag(Client client) {
|
2022-02-23 02:51:48 +00:00
|
|
|
|
try {
|
|
|
|
|
foreach (int shine in shineBag.Except((ConcurrentBag<int>) client.Metadata["shineSync"]))
|
|
|
|
|
await client.Send(new ShinePacket {
|
|
|
|
|
ShineId = shine
|
|
|
|
|
});
|
2022-03-01 21:08:53 +00:00
|
|
|
|
}
|
|
|
|
|
catch {
|
2022-02-23 02:51:48 +00:00
|
|
|
|
// errors that can happen when sending will crash the server :)
|
|
|
|
|
}
|
2022-02-14 19:45:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void SyncShineBag() {
|
2022-02-18 01:56:12 +00:00
|
|
|
|
await Parallel.ForEachAsync(server.Clients, async (client, _) => { await ClientSyncShineBag(client); });
|
2022-02-14 19:45:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 20:40:42 +00:00
|
|
|
|
Timer timer = new Timer(120000);
|
|
|
|
|
timer.AutoReset = true;
|
|
|
|
|
timer.Enabled = true;
|
2022-02-18 01:56:12 +00:00
|
|
|
|
timer.Elapsed += (_, _) => { SyncShineBag(); };
|
2022-02-14 20:40:42 +00:00
|
|
|
|
timer.Start();
|
2022-02-18 01:56:12 +00:00
|
|
|
|
bool flipEnabled = Settings.Instance.Flip.EnabledOnStart;
|
2022-02-14 20:40:42 +00:00
|
|
|
|
|
2022-03-01 21:08:53 +00:00
|
|
|
|
float MarioSize(bool is2d) {
|
|
|
|
|
return is2d ? 180 : 160;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 00:35:38 +00:00
|
|
|
|
server.PacketHandler = (c, p) => {
|
2022-03-01 21:08:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (p is PlayerPacket playerPacket) c.Metadata["scenario"] = playerPacket.ScenarioNum;
|
|
|
|
|
}
|
2022-02-14 19:45:58 +00:00
|
|
|
|
switch (p) {
|
|
|
|
|
case CostumePacket:
|
2022-02-16 00:35:38 +00:00
|
|
|
|
ClientSyncShineBag(c);
|
2022-02-14 19:45:58 +00:00
|
|
|
|
c.Metadata["loadedSave"] = true;
|
|
|
|
|
break;
|
|
|
|
|
case ShinePacket shinePacket: {
|
2022-02-16 00:35:38 +00:00
|
|
|
|
if (c.Metadata["loadedSave"] is false) break;
|
2022-02-14 19:45:58 +00:00
|
|
|
|
ConcurrentBag<int> playerBag = (ConcurrentBag<int>) c.Metadata["shineSync"];
|
|
|
|
|
shineBag.Add(shinePacket.ShineId);
|
2022-02-16 00:35:38 +00:00
|
|
|
|
if (playerBag.Contains(shinePacket.ShineId)) break;
|
2022-02-15 20:26:50 +00:00
|
|
|
|
c.Logger.Info($"Got shine {shinePacket.ShineId}");
|
2022-02-14 19:45:58 +00:00
|
|
|
|
playerBag.Add(shinePacket.ShineId);
|
|
|
|
|
SyncShineBag();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-02-18 01:56:12 +00:00
|
|
|
|
case PlayerPacket playerPacket when flipEnabled && Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Others && Settings.Instance.Flip.Players.Contains(c.Id): {
|
|
|
|
|
playerPacket.Position += Vector3.UnitY * MarioSize(playerPacket.Is2d);
|
2022-02-16 21:00:52 +00:00
|
|
|
|
playerPacket.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI));
|
2022-02-16 00:35:38 +00:00
|
|
|
|
server.Broadcast(playerPacket, c);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-18 01:56:12 +00:00
|
|
|
|
case PlayerPacket playerPacket when flipEnabled && Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Self && !Settings.Instance.Flip.Players.Contains(c.Id): {
|
2022-02-16 20:36:24 +00:00
|
|
|
|
server.BroadcastReplace(playerPacket, c, (from, to, sp) => {
|
2022-02-18 01:56:12 +00:00
|
|
|
|
if (Settings.Instance.Flip.Players.Contains(to.Id)) {
|
|
|
|
|
sp.Position += Vector3.UnitY * MarioSize(playerPacket.Is2d);
|
2022-02-16 20:36:24 +00:00
|
|
|
|
sp.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI));
|
|
|
|
|
}
|
2022-02-18 01:56:12 +00:00
|
|
|
|
|
2022-02-16 20:36:24 +00:00
|
|
|
|
to.Send(sp, from);
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-14 19:45:58 +00:00
|
|
|
|
}
|
2022-02-16 00:35:38 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
2022-02-14 19:45:58 +00:00
|
|
|
|
};
|
2022-03-01 21:08:53 +00:00
|
|
|
|
|
|
|
|
|
CommandHandler.RegisterCommand("scenario", args => {
|
|
|
|
|
const string optionUsage = "Valid options: merge <true/false>";
|
|
|
|
|
if (args.Length < 1)
|
|
|
|
|
return optionUsage;
|
|
|
|
|
switch (args[0]) {
|
|
|
|
|
case "merge" when args.Length == 2: {
|
|
|
|
|
if (bool.TryParse(args[1], out bool result)) {
|
|
|
|
|
Settings.Instance.Scenario.MergeEnabled = result;
|
|
|
|
|
Settings.SaveSettings();
|
|
|
|
|
return result ? "Enabled scenario merge" : "Disabled scenario merge";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return optionUsage;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return optionUsage;
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-02-22 21:28:51 +00:00
|
|
|
|
|
|
|
|
|
CommandHandler.RegisterCommand("list", _ => $"List: {string.Join(", ", server.Clients.Select(x => $"{x.Name} ({x.Id})"))}");
|
2021-11-29 04:04:34 +00:00
|
|
|
|
|
2022-02-18 01:56:12 +00:00
|
|
|
|
CommandHandler.RegisterCommand("flip", args => {
|
|
|
|
|
const string optionUsage = "Valid options: list, add <user id>, remove <user id>, set <true/false>, pov <both/self/others>";
|
|
|
|
|
if (args.Length < 1)
|
|
|
|
|
return optionUsage;
|
|
|
|
|
switch (args[0]) {
|
|
|
|
|
case "list" when args.Length == 1:
|
|
|
|
|
return "User ids: " + string.Join(", ", Settings.Instance.Flip.Players.ToList());
|
|
|
|
|
case "add" when args.Length == 2: {
|
|
|
|
|
if (Guid.TryParse(args[1], out Guid result)) {
|
|
|
|
|
Settings.Instance.Flip.Players.Add(result);
|
|
|
|
|
Settings.SaveSettings();
|
|
|
|
|
return $"Added {result} to flipped players";
|
2022-02-18 02:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $"Invalid user id {args[1]}";
|
2022-02-18 01:56:12 +00:00
|
|
|
|
}
|
|
|
|
|
case "remove" when args.Length == 2: {
|
|
|
|
|
if (Guid.TryParse(args[1], out Guid result)) {
|
|
|
|
|
string output = Settings.Instance.Flip.Players.Remove(result) ? $"Removed {result} to flipped players" : $"User {result} wasn't in the flipped players list";
|
|
|
|
|
Settings.SaveSettings();
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $"Invalid user id {args[1]}";
|
|
|
|
|
}
|
|
|
|
|
case "set" when args.Length == 2: {
|
|
|
|
|
if (bool.TryParse(args[1], out bool result)) {
|
|
|
|
|
flipEnabled = result;
|
|
|
|
|
return result ? "Enabled player flipping for session" : "Disabled player flipping for session";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return optionUsage;
|
|
|
|
|
}
|
|
|
|
|
case "pov" when args.Length == 2: {
|
|
|
|
|
if (Enum.TryParse(args[1], true, out FlipOptions result)) {
|
|
|
|
|
Settings.Instance.Flip.Pov = result;
|
|
|
|
|
Settings.SaveSettings();
|
|
|
|
|
return $"Point of view set to {result}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return optionUsage;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return optionUsage;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-18 02:39:48 +00:00
|
|
|
|
CommandHandler.RegisterCommand("shine", args => {
|
2022-02-23 02:51:48 +00:00
|
|
|
|
const string optionUsage = "Valid options: list, clear";
|
2022-02-18 01:56:12 +00:00
|
|
|
|
if (args.Length < 1)
|
|
|
|
|
return optionUsage;
|
|
|
|
|
switch (args[0]) {
|
|
|
|
|
case "list" when args.Length == 1:
|
|
|
|
|
return $"Shines: {string.Join(", ", shineBag)}";
|
2022-02-23 02:51:48 +00:00
|
|
|
|
case "clear" when args.Length == 1:
|
|
|
|
|
shineBag.Clear();
|
2022-03-01 21:08:53 +00:00
|
|
|
|
foreach (ConcurrentBag<int> playerBag in server.Clients.Select(serverClient => (ConcurrentBag<int>) serverClient.Metadata["shineSync"])) playerBag.Clear();
|
|
|
|
|
|
2022-02-23 02:51:48 +00:00
|
|
|
|
return "Cleared shine bags";
|
2022-02-18 01:56:12 +00:00
|
|
|
|
default:
|
|
|
|
|
return optionUsage;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-18 02:39:48 +00:00
|
|
|
|
CommandHandler.RegisterCommand("loadsettings", _ => {
|
|
|
|
|
Settings.LoadSettings();
|
|
|
|
|
return "Loaded settings.json";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CommandHandler.RegisterCommand("savesettings", _ => {
|
|
|
|
|
Settings.SaveSettings();
|
|
|
|
|
return "Saved settings.json";
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-22 04:05:13 +00:00
|
|
|
|
Console.CancelKeyPress += (_, e) => {
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
consoleLogger.Info("Received Ctrl+C");
|
|
|
|
|
cts.Cancel();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CommandHandler.RegisterCommand("exit", _ => {
|
|
|
|
|
cts.Cancel();
|
|
|
|
|
return "Shutting down clients";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CommandHandler.RegisterCommand("quit", _ => {
|
|
|
|
|
cts.Cancel();
|
|
|
|
|
return "Shutting down clients";
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-16 00:35:38 +00:00
|
|
|
|
Task.Run(() => {
|
2022-02-22 04:05:13 +00:00
|
|
|
|
consoleLogger.Info("Run help command for valid commands.");
|
2022-02-16 00:35:38 +00:00
|
|
|
|
while (true) {
|
2022-02-18 01:56:12 +00:00
|
|
|
|
string? text = Console.ReadLine();
|
2022-02-22 04:05:13 +00:00
|
|
|
|
if (text != null) consoleLogger.Info(CommandHandler.GetResult(text));
|
2022-02-16 00:35:38 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-22 04:05:13 +00:00
|
|
|
|
await listenTask;
|