mirror of
https://github.com/Sanae6/SmoOnlineServer.git
synced 2024-11-22 11:15:15 +00:00
Reformat Program.cs
This commit is contained in:
parent
22a8e91f85
commit
5d78814620
1 changed files with 45 additions and 26 deletions
|
@ -16,7 +16,8 @@ DiscordBot bot = new DiscordBot();
|
||||||
bot.Run();
|
bot.Run();
|
||||||
|
|
||||||
server.ClientJoined += (c, _) => {
|
server.ClientJoined += (c, _) => {
|
||||||
if (Settings.Instance.BanList.Enabled && (Settings.Instance.BanList.Players.Contains(c.Id) || Settings.Instance.BanList.IpAddresses.Contains(
|
if (Settings.Instance.BanList.Enabled && (Settings.Instance.BanList.Players.Contains(c.Id) ||
|
||||||
|
Settings.Instance.BanList.IpAddresses.Contains(
|
||||||
((IPEndPoint) c.Socket!.RemoteEndPoint!).Address.ToString())))
|
((IPEndPoint) c.Socket!.RemoteEndPoint!).Address.ToString())))
|
||||||
throw new Exception($"Banned player attempted join: {c.Name}");
|
throw new Exception($"Banned player attempted join: {c.Name}");
|
||||||
c.Metadata["shineSync"] = new ConcurrentBag<int>();
|
c.Metadata["shineSync"] = new ConcurrentBag<int>();
|
||||||
|
@ -27,8 +28,7 @@ server.ClientJoined += (c, _) => {
|
||||||
foreach (Client client in server.Clients.Where(client => client.Metadata.ContainsKey("lastGamePacket")).ToArray()) {
|
foreach (Client client in server.Clients.Where(client => client.Metadata.ContainsKey("lastGamePacket")).ToArray()) {
|
||||||
try {
|
try {
|
||||||
c.Send((GamePacket) client.Metadata["lastGamePacket"]!, client).Wait();
|
c.Send((GamePacket) client.Metadata["lastGamePacket"]!, client).Wait();
|
||||||
}
|
} catch {
|
||||||
catch {
|
|
||||||
// lol who gives a fuck
|
// lol who gives a fuck
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,7 @@ async Task ClientSyncShineBag(Client client) {
|
||||||
ShineId = shine
|
ShineId = shine
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
} catch {
|
||||||
catch {
|
|
||||||
// errors that can happen when sending will crash the server :)
|
// errors that can happen when sending will crash the server :)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,8 +52,7 @@ async Task ClientSyncShineBag(Client client) {
|
||||||
async void SyncShineBag() {
|
async void SyncShineBag() {
|
||||||
try {
|
try {
|
||||||
await Parallel.ForEachAsync(server.Clients.ToArray(), async (client, _) => await ClientSyncShineBag(client));
|
await Parallel.ForEachAsync(server.Clients.ToArray(), async (client, _) => await ClientSyncShineBag(client));
|
||||||
}
|
} catch {
|
||||||
catch {
|
|
||||||
// errors that can happen shines change will crash the server :)
|
// errors that can happen shines change will crash the server :)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +90,7 @@ server.PacketHandler = (c, p) => {
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Settings.Instance.Scenario.MergeEnabled) {
|
if (Settings.Instance.Scenario.MergeEnabled) {
|
||||||
server.BroadcastReplace(gamePacket, c, (from, to, gp) => {
|
server.BroadcastReplace(gamePacket, c, (from, to, gp) => {
|
||||||
gp.ScenarioNum = (byte?) to.Metadata["scenario"] ?? 200;
|
gp.ScenarioNum = (byte?) to.Metadata["scenario"] ?? 200;
|
||||||
|
@ -99,11 +98,13 @@ server.PacketHandler = (c, p) => {
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TagPacket tagPacket: {
|
case TagPacket tagPacket: {
|
||||||
if ((tagPacket.UpdateType & TagPacket.TagUpdate.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt;
|
if ((tagPacket.UpdateType & TagPacket.TagUpdate.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt;
|
||||||
if ((tagPacket.UpdateType & TagPacket.TagUpdate.Time) != 0) c.Metadata["time"] = new Time(tagPacket.Minutes, tagPacket.Seconds, DateTime.Now);
|
if ((tagPacket.UpdateType & TagPacket.TagUpdate.Time) != 0)
|
||||||
|
c.Metadata["time"] = new Time(tagPacket.Minutes, tagPacket.Seconds, DateTime.Now);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CostumePacket:
|
case CostumePacket:
|
||||||
|
@ -124,7 +125,8 @@ server.PacketHandler = (c, p) => {
|
||||||
&& Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Others
|
&& Settings.Instance.Flip.Pov is FlipOptions.Both or FlipOptions.Others
|
||||||
&& Settings.Instance.Flip.Players.Contains(c.Id): {
|
&& Settings.Instance.Flip.Players.Contains(c.Id): {
|
||||||
playerPacket.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]);
|
playerPacket.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]);
|
||||||
playerPacket.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI));
|
playerPacket.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) *
|
||||||
|
Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI));
|
||||||
server.Broadcast(playerPacket, c);
|
server.Broadcast(playerPacket, c);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -134,7 +136,8 @@ server.PacketHandler = (c, p) => {
|
||||||
server.BroadcastReplace(playerPacket, c, (from, to, sp) => {
|
server.BroadcastReplace(playerPacket, c, (from, to, sp) => {
|
||||||
if (Settings.Instance.Flip.Players.Contains(to.Id)) {
|
if (Settings.Instance.Flip.Players.Contains(to.Id)) {
|
||||||
sp.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]);
|
sp.Position += Vector3.UnitY * MarioSize((bool) c.Metadata["2d"]);
|
||||||
sp.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) * Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI));
|
sp.Rotation *= Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationX(MathF.PI)) *
|
||||||
|
Quaternion.CreateFromRotationMatrix(Matrix4x4.CreateRotationY(MathF.PI));
|
||||||
}
|
}
|
||||||
|
|
||||||
to.Send(sp, from);
|
to.Send(sp, from);
|
||||||
|
@ -208,6 +211,7 @@ CommandHandler.RegisterCommand("ban", args => {
|
||||||
Settings.SaveSettings();
|
Settings.SaveSettings();
|
||||||
return $"Banned {builder}.";
|
return $"Banned {builder}.";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Usage: ban <usernames...>";
|
return "Usage: ban <usernames...>";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -227,9 +231,13 @@ CommandHandler.RegisterCommand("send", args => {
|
||||||
return "Invalid Stage Name!";
|
return "Invalid Stage Name!";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sbyte.TryParse(args[2], out sbyte scenario) || scenario < -1) return $"Invalid scenario number {args[2]} (range: [-1 to 127])";
|
if (!sbyte.TryParse(args[2], out sbyte scenario) || scenario < -1)
|
||||||
Client[] players = args[3] == "*" ? server.Clients.Where(c => c.Connected).ToArray() : server.Clients.Where(c => c.Connected && args[3..].Any(x => c.Name.StartsWith(x) ||
|
return $"Invalid scenario number {args[2]} (range: [-1 to 127])";
|
||||||
(Guid.TryParse(x, out Guid result) && result == c.Id))).ToArray();
|
Client[] players = args[3] == "*"
|
||||||
|
? server.Clients.Where(c => c.Connected).ToArray()
|
||||||
|
: server.Clients.Where(c => c.Connected && args[3..].Any(x => c.Name.StartsWith(x) ||
|
||||||
|
(Guid.TryParse(x, out Guid result) &&
|
||||||
|
result == c.Id))).ToArray();
|
||||||
Parallel.ForEachAsync(players, async (c, _) => {
|
Parallel.ForEachAsync(players, async (c, _) => {
|
||||||
await c.Send(new ChangeStagePacket {
|
await c.Send(new ChangeStagePacket {
|
||||||
Stage = stage,
|
Stage = stage,
|
||||||
|
@ -293,15 +301,18 @@ CommandHandler.RegisterCommand("scenario", args => {
|
||||||
});
|
});
|
||||||
|
|
||||||
CommandHandler.RegisterCommand("tag", args => {
|
CommandHandler.RegisterCommand("tag", args => {
|
||||||
const string optionUsage = "Valid options:\n\ttime <user/*> <minutes[0-65535]> <seconds[0-59]>\n\tseeking <user/*> <true/false>\n\tstart <time> <seekers>";
|
const string optionUsage =
|
||||||
|
"Valid options:\n\ttime <user/*> <minutes[0-65535]> <seconds[0-59]>\n\tseeking <user/*> <true/false>\n\tstart <time> <seekers>";
|
||||||
if (args.Length < 3)
|
if (args.Length < 3)
|
||||||
return optionUsage;
|
return optionUsage;
|
||||||
switch (args[0]) {
|
switch (args[0]) {
|
||||||
case "time" when args.Length == 4: {
|
case "time" when args.Length == 4: {
|
||||||
if (args[1] != "*" && server.Clients.All(x => x.Name != args[1])) return $"Cannot find user {args[1]}";
|
if (args[1] != "*" && server.Clients.All(x => x.Name != args[1])) return $"Cannot find user {args[1]}";
|
||||||
Client? client = server.Clients.FirstOrDefault(x => x.Name == args[1]);
|
Client? client = server.Clients.FirstOrDefault(x => x.Name == args[1]);
|
||||||
if (!ushort.TryParse(args[2], out ushort minutes)) return $"Invalid time for minutes {args[2]} (range: 0-65535)";
|
if (!ushort.TryParse(args[2], out ushort minutes))
|
||||||
if (!byte.TryParse(args[3], out byte seconds) || seconds >= 60) return $"Invalid time for seconds {args[3]} (range: 0-59)";
|
return $"Invalid time for minutes {args[2]} (range: 0-65535)";
|
||||||
|
if (!byte.TryParse(args[3], out byte seconds) || seconds >= 60)
|
||||||
|
return $"Invalid time for seconds {args[3]} (range: 0-59)";
|
||||||
TagPacket tagPacket = new TagPacket {
|
TagPacket tagPacket = new TagPacket {
|
||||||
UpdateType = TagPacket.TagUpdate.Time,
|
UpdateType = TagPacket.TagUpdate.Time,
|
||||||
Minutes = minutes,
|
Minutes = minutes,
|
||||||
|
@ -332,7 +343,8 @@ CommandHandler.RegisterCommand("tag", args => {
|
||||||
string[] seekerNames = args[2..];
|
string[] seekerNames = args[2..];
|
||||||
Client[] seekers = server.Clients.Where(c => seekerNames.Contains(c.Name)).ToArray();
|
Client[] seekers = server.Clients.Where(c => seekerNames.Contains(c.Name)).ToArray();
|
||||||
if (seekers.Length != seekerNames.Length)
|
if (seekers.Length != seekerNames.Length)
|
||||||
return $"Couldn't find seeker{(seekerNames.Length > 1 ? "s" : "")}: {string.Join(", ", seekerNames.Where(name => server.Clients.All(c => c.Name != name)))}";
|
return
|
||||||
|
$"Couldn't find seeker{(seekerNames.Length > 1 ? "s" : "")}: {string.Join(", ", seekerNames.Where(name => server.Clients.All(c => c.Name != name)))}";
|
||||||
Task.Run(async () => {
|
Task.Run(async () => {
|
||||||
int realTime = 1000 * time;
|
int realTime = 1000 * time;
|
||||||
await Task.Delay(realTime);
|
await Task.Delay(realTime);
|
||||||
|
@ -369,10 +381,12 @@ CommandHandler.RegisterCommand("maxplayers", args => {
|
||||||
return $"Saved and set max players to {maxPlayers}";
|
return $"Saved and set max players to {maxPlayers}";
|
||||||
});
|
});
|
||||||
|
|
||||||
CommandHandler.RegisterCommand("list", _ => $"List: {string.Join("\n\t", server.Clients.Where(x => x.Connected).Select(x => $"{x.Name} ({x.Id})"))}");
|
CommandHandler.RegisterCommand("list",
|
||||||
|
_ => $"List: {string.Join("\n\t", server.Clients.Where(x => x.Connected).Select(x => $"{x.Name} ({x.Id})"))}");
|
||||||
|
|
||||||
CommandHandler.RegisterCommand("flip", args => {
|
CommandHandler.RegisterCommand("flip", args => {
|
||||||
const string optionUsage = "Valid options: \n\tlist\n\tadd <user id>\n\tremove <user id>\n\tset <true/false>\n\tpov <both/self/others>";
|
const string optionUsage =
|
||||||
|
"Valid options: \n\tlist\n\tadd <user id>\n\tremove <user id>\n\tset <true/false>\n\tpov <both/self/others>";
|
||||||
if (args.Length < 1)
|
if (args.Length < 1)
|
||||||
return optionUsage;
|
return optionUsage;
|
||||||
switch (args[0]) {
|
switch (args[0]) {
|
||||||
|
@ -389,7 +403,9 @@ CommandHandler.RegisterCommand("flip", args => {
|
||||||
}
|
}
|
||||||
case "remove" when args.Length == 2: {
|
case "remove" when args.Length == 2: {
|
||||||
if (Guid.TryParse(args[1], out Guid result)) {
|
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";
|
string output = Settings.Instance.Flip.Players.Remove(result)
|
||||||
|
? $"Removed {result} to flipped players"
|
||||||
|
: $"User {result} wasn't in the flipped players list";
|
||||||
Settings.SaveSettings();
|
Settings.SaveSettings();
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
@ -428,7 +444,8 @@ CommandHandler.RegisterCommand("shine", args => {
|
||||||
return $"Shines: {string.Join(", ", shineBag)}";
|
return $"Shines: {string.Join(", ", shineBag)}";
|
||||||
case "clear" when args.Length == 1:
|
case "clear" when args.Length == 1:
|
||||||
shineBag.Clear();
|
shineBag.Clear();
|
||||||
foreach (ConcurrentBag<int> playerBag in server.Clients.Select(serverClient => (ConcurrentBag<int>) serverClient.Metadata["shineSync"])) playerBag.Clear();
|
foreach (ConcurrentBag<int> playerBag in server.Clients.Select(serverClient =>
|
||||||
|
(ConcurrentBag<int>) serverClient.Metadata["shineSync"])) playerBag.Clear();
|
||||||
|
|
||||||
return "Cleared shine bags";
|
return "Cleared shine bags";
|
||||||
case "sync" when args.Length == 1:
|
case "sync" when args.Length == 1:
|
||||||
|
@ -436,7 +453,9 @@ CommandHandler.RegisterCommand("shine", args => {
|
||||||
return "Synced shine bag automatically";
|
return "Synced shine bag automatically";
|
||||||
case "send" when args.Length >= 3:
|
case "send" when args.Length >= 3:
|
||||||
if (int.TryParse(args[1], out int id)) {
|
if (int.TryParse(args[1], out int id)) {
|
||||||
Client[] players = args[2] == "*" ? server.Clients.Where(c => c.Connected).ToArray() : server.Clients.Where(c => c.Connected && args[3..].Contains(c.Name)).ToArray();
|
Client[] players = args[2] == "*"
|
||||||
|
? server.Clients.Where(c => c.Connected).ToArray()
|
||||||
|
: server.Clients.Where(c => c.Connected && args[3..].Contains(c.Name)).ToArray();
|
||||||
Parallel.ForEachAsync(players, async (c, _) => {
|
Parallel.ForEachAsync(players, async (c, _) => {
|
||||||
await c.Send(new ShinePacket {
|
await c.Send(new ShinePacket {
|
||||||
ShineId = id
|
ShineId = id
|
||||||
|
|
Loading…
Reference in a new issue