Skip to content

Commit

Permalink
add more regions, fix regionmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Bims-sh committed Aug 9, 2024
1 parent f3c870d commit 89e2fa0
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 101 deletions.
2 changes: 1 addition & 1 deletion ChatCommands/CustomGamemode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CustomGamemode() : base(
}

var customGamemodeToAdd = string.Join(" ", args).ToUpperInvariant();
if (CustomGamemodeHelper.isValidCustomGamemode(customGamemodeToAdd))
if (CustomGamemodeHelper.IsValidCustomGamemode(customGamemodeToAdd))
{
CustomGamemodeHelper.SetCustomGameMode(customGamemodeToAdd, Server);

Expand Down
3 changes: 2 additions & 1 deletion Data/CustomGameModeEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static class CustomGameModeEvents

public static readonly List<Event> VipEvents = new()
{
new VipGamemode()
new VipGamemode(),
new RegionManager()
};

public static readonly List<Event> HideAndSeekEvents = new()
Expand Down
5 changes: 5 additions & 0 deletions Data/RegionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class RegionList
LonovoRegions.TeamASafeZone,
LonovoRegions.TeamBSafeZone
},
"WineParadise" => new List<RegionHelper.Region?>
{
WineparadiseRegions.TeamASafeZone,
WineparadiseRegions.TeamBSafeZone
},
_ => new List<RegionHelper.Region?>()
};
}
Expand Down
42 changes: 42 additions & 0 deletions Data/Regions/WineparadiseRegions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Numerics;
using BattleBitMinigames.Handlers;

namespace BattleBitMinigames.Data.Regions;

public class WineparadiseRegions
{
// US safe zone
public static readonly RegionHelper.Region TeamASafeZone = new (
"US Safe Zone",
"You've entered the US Safe Zone. Please leave immediately.",
new List<Vector2>
{
new (-92, -271),
new (-197, -142),
new (-338, -18),
new (-387, 190),
new (-605, -27),
new (-660, -275),
new (-510, -372),
new (-297, -352)
}
);

// RU safe zone region
public static readonly RegionHelper.Region? TeamBSafeZone = new (
"RU Safe Zone",
"You've entered the RU Safe Zone. Please leave immediately.",
new List<Vector2>
{
new (-121, 397),
new (0, 346),
new (140, 182),
new (313, 90),
new (353, 124),
new (380, 289),
new (332, 455),
new (160, 526),
new (-54, 455)
}
);
}
110 changes: 97 additions & 13 deletions Events/RegionManager.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,128 @@
using System.Numerics;
using System.Globalization;
using System.Numerics;
using BattleBitAPI.Common;
using BattleBitMinigames.Api;
using BattleBitMinigames.Data;
using BattleBitMinigames.Handlers;
using BattleBitMinigames.Helpers;
using BattleBitMinigames.Interfaces;

namespace BattleBitMinigames.Events;

public class RegionManager : Event
{
bool Enabled = true;
private CancellationTokenSource? _cancellationTokenSource;

private void StartRegionManager()
{
Program.Logger.Info("Started RegionManager!");
_cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = _cancellationTokenSource.Token;

Task.Run(async () =>
{
while (Server.IsConnected)
while (Server.IsConnected && !cancellationToken.IsCancellationRequested)
{
foreach (var player in Server.AllPlayers.Where(player => player.IsAlive && player.Position != Vector3.Zero))
{
var region = RegionHelper.GetIsPlayerInRegion(RegionList.GetMapRegions(Server.Map), player);
if (region != null)
{
// TODO: Remove in prod
player.Message($"You are in {RichTextHelper.Bold(true)}{region.Name}{RichTextHelper.Bold(false)}!", 999);
// TODO: Spawn task to kill player after X amount of time, make message prettier.
}
var now = DateTime.UtcNow;
var spawnedInSpawn = player.GetPlayerProperty(PlayerProperties.IVipPlayerProperties.SpawnedInSpawn);
var spawnedInSpawnTimeStr = player.GetPlayerProperty(PlayerProperties.IVipPlayerProperties.SpawnedInSpawnTime);
var enteredSpawnTimeStr = player.GetPlayerProperty(PlayerProperties.IVipPlayerProperties.EnteredSpawnTime);

if (spawnedInSpawn == "true")
{
if (DateTime.TryParse(spawnedInSpawnTimeStr, out var spawnedInSpawnTime))
{
if ((now - spawnedInSpawnTime).TotalSeconds >= 90)
{
player.Kill();
player.Message($"You spawned in the {region.Name} and stayed for too long!", 15);
}
else
{
player.Message($"You spawned in the {region.Name}, please leave within {(int)(90 - (now - spawnedInSpawnTime).TotalSeconds)} seconds!");
}
}
}
else
{
if (enteredSpawnTimeStr == string.Empty)
{
player.SetPlayerProperty(PlayerProperties.IVipPlayerProperties.EnteredSpawnTime, now.ToUniversalTime().ToString());
}
else if (DateTime.TryParse(enteredSpawnTimeStr, out var enteredSpawnTime))
{
if ((now - enteredSpawnTime).TotalSeconds >= 20)
{
player.Kill();
player.Message($"You were in the {region.Name} for too long!", 15);
}
else
{
player.Message($"You entered the {region.Name}, please leave within {(int)(20 - (now - enteredSpawnTime).TotalSeconds)} seconds!");
}
}
}
}
else
{
player.Message("You are not in a region!", 1);
// Reset properties when a player leaves the spawn region
player.RemovePlayerProperty(PlayerProperties.IVipPlayerProperties.SpawnedInSpawn);
player.RemovePlayerProperty(PlayerProperties.IVipPlayerProperties.EnteredSpawnTime);
player.RemovePlayerProperty(PlayerProperties.IVipPlayerProperties.SpawnedInSpawnTime);
}
}

await Task.Delay(1000);
await Task.Delay(1000, cancellationToken);
}

Program.Logger.Info("RegionManager stopped.");
}, cancellationToken);
}

private void StopRegionManager()
{
if (_cancellationTokenSource is { IsCancellationRequested: false })
{
_cancellationTokenSource.Cancel();
Program.Logger.Info("Stopping RegionManager...");
}
}

public override Task<bool> OnPlayerTypedMessage(BattleBitPlayer player, ChatChannel channel, string msg)
{
if (player.GetHighestRole() == Enums.PlayerRoles.Admin)
{
if (msg.Contains("?rgm start"))
{
StartRegionManager();
}
else if (msg.Contains("?rgm stop"))
{
StopRegionManager();
}
});
}

return base.OnPlayerTypedMessage(player, channel, msg);
}

public override Task OnPlayerSpawned(BattleBitPlayer player)
{
var region = RegionHelper.GetIsPlayerInRegion(RegionList.GetMapRegions(Server.Map), player);
if (region != null)
{
player.SetPlayerProperty(PlayerProperties.IVipPlayerProperties.SpawnedInSpawn, "true");
player.SetPlayerProperty(PlayerProperties.IVipPlayerProperties.SpawnedInSpawnTime, DateTime.UtcNow.ToString());
}

return base.OnPlayerSpawned(player);
}

public override Task OnConnected()
{
StartRegionManager();
return Task.CompletedTask;
}
}
}
Loading

0 comments on commit 89e2fa0

Please sign in to comment.