Skip to content

Commit

Permalink
Added command for starting a server shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
justalemon committed Feb 7, 2020
1 parent 5a63ca6 commit 8ce15cd
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Bridge.Server/Script.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,6 +43,7 @@ public BridgeServer()
// Add the commands that we need
API.RegisterCommand("bridgekickall", new Action<int, List<object>, string>((s, a, r) => CommandKickAll(s, a, r)), false);
API.RegisterCommand("bridgenotify", new Action<int, List<object>, string>((s, a, r) => CommandNotify(s, a, r)), false);
API.RegisterCommand("bridgeshutdown", new Action<int, List<object>, string>((s, a, r) => CommandShutdown(s, a, r)), false);
}

private void CommandKickAll(int source, List<object> args, string raw)
Expand Down Expand Up @@ -93,5 +96,27 @@ private void CommandNotify(int source, List<object> 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<object> 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("");
}
}
}

0 comments on commit 8ce15cd

Please sign in to comment.