From 8ce15cdf73151e88559cfd98a9245e57d56b7dd2 Mon Sep 17 00:00:00 2001 From: Hannele Ruiz Date: Fri, 7 Feb 2020 00:11:50 -0300 Subject: [PATCH] Added command for starting a server shutdown --- Bridge.Server/Script.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Bridge.Server/Script.cs b/Bridge.Server/Script.cs index 4698afc..2184729 100644 --- a/Bridge.Server/Script.cs +++ b/Bridge.Server/Script.cs @@ -1,7 +1,9 @@ using CitizenFX.Core; using CitizenFX.Core.Native; +using Flurl.Http; using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace LambentLight.Bridge.Server @@ -41,6 +43,7 @@ public BridgeServer() // Add the commands that we need API.RegisterCommand("bridgekickall", new Action, string>((s, a, r) => CommandKickAll(s, a, r)), false); API.RegisterCommand("bridgenotify", new Action, string>((s, a, r) => CommandNotify(s, a, r)), false); + API.RegisterCommand("bridgeshutdown", new Action, string>((s, a, r) => CommandShutdown(s, a, r)), false); } private void CommandKickAll(int source, List args, string raw) @@ -93,5 +96,27 @@ private void CommandNotify(int source, List args, string raw) // And notify the user on the console Console.WriteLine($"The message '{builder.ToString()}' was sent to all connected players"); } + + private async void CommandShutdown(int source, List args, string raw) + { + // If the API is not available, notify it and return + if (!IsApiAvailable) + { + Debug.WriteLine("This feature cannot be used because the API is not available"); + return; + } + + // Notify that a shutdown was initiated + Debug.WriteLine("Shutdown initiated, Bridge will close the server once is completed"); + + // Wait until the server is empty + while (Players.Count() != 0) + { + await Delay(0); + } + + // If the server is empty, make the API call + await $"{Bind}/bridge/serverempty".WithHeader("Authorization", $"Bearer {Token}").PostStringAsync(""); + } } }