min working example

This commit is contained in:
caesarakalaeii 2023-08-13 01:45:50 +02:00
parent 425c9bb4ad
commit d77ca949d0
1 changed files with 1 additions and 259 deletions

View File

@ -1,8 +1,6 @@
using System.Text.Json;
using BattleBitAPI;
using BattleBitAPI;
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using CommunityServerAPI;
internal class Program
{
@ -41,8 +39,6 @@ public class MyPlayer : Player<MyPlayer>
Weapons.SSG69
};
public bool IsAdmin;
public bool IsStreamer;
public int Level;
public void UpdateWeapon()
@ -68,34 +64,6 @@ public class MyPlayer : Player<MyPlayer>
internal class MyGameServer : GameServer<MyPlayer>
{
private readonly string mAdminJson = "./config/admins.json";
private readonly List<ulong> mAdmins = new();
private readonly List<APICommand> mChatCommands = new()
{
new HealCommand(),
new KillCommand(),
new TeleportCommand(),
new GrenadeCommand(),
new ForceStartCommand(),
new SpeedCommand(),
new HelpCommand(),
new RevealCommand(),
new ChangeDamageCommand(),
new ChangeReceivedDamageCommand(),
new ChangeAmmoCommand(),
new SetStreamerCommand(),
new RemoveStreamerCommand(),
new OpCommand(),
new DeopCommand()
};
//CommandAPI
//public CommandQueue queue = new();
private readonly List<ulong> mListedStreamers = new();
private readonly string mSteamIdJson = "./config/streamer_steamids.json";
// Gun Game
public override async Task OnPlayerSpawned(MyPlayer player)
{
@ -134,230 +102,4 @@ internal class MyGameServer : GameServer<MyPlayer>
foreach (var player in AllPlayers) player.Level = 0;
return base.OnRoundEnded();
}
// CommandAPI
public override async Task<bool> OnPlayerTypedMessage(MyPlayer player, ChatChannel channel, string msg)
{
return await Task.Run(() =>
{
if (!player.IsAdmin) return true;
var splits = msg.Split(" ");
foreach (var command in mChatCommands)
if (splits[0] == command.CommandPrefix)
{
var c = command.ChatCommand(player, channel, msg);
HandleCommand(c);
return false;
}
return true;
});
}
public void SaveStreamers()
{
try
{
var newJson =
JsonSerializer.Serialize(mListedStreamers, new JsonSerializerOptions { WriteIndented = true });
// Write the JSON to the file, overwriting its content
File.WriteAllText(mSteamIdJson, newJson);
Console.WriteLine("Steam IDs updated and saved to the file.");
}
catch (Exception ex)
{
Console.WriteLine("Steam IDs couldn't be updated and saved to the file." + ex);
}
}
public void SaveAdmins()
{
try
{
var newJson =
JsonSerializer.Serialize(mAdmins, new JsonSerializerOptions { WriteIndented = true });
// Write the JSON to the file, overwriting its content
File.WriteAllText(mAdminJson, newJson);
Console.WriteLine("Admins updated and saved to the file.");
}
catch (Exception ex)
{
Console.WriteLine("Admins couldn't be updated and saved to the file." + ex);
}
}
public override async Task OnConnected()
{
await Console.Out.WriteLineAsync(GameIP + " Connected");
await Console.Out.WriteLineAsync("Fetching configs");
try
{
// Read the entire JSON file as a string
var jsonFilePath = mSteamIdJson;
var jsonString = File.ReadAllText(jsonFilePath);
// Parse the JSON array using System.Text.Json
var steamIds = JsonSerializer.Deserialize<ulong[]>(jsonString);
foreach (var steamId in steamIds) mListedStreamers.Add(steamId);
await Console.Out.WriteLineAsync("Fetching streamers succeeded");
}
catch (Exception ex)
{
await Console.Out.WriteLineAsync("Fetching streamers failed: " + ex);
}
try
{
// Read the entire JSON file as a string
var jsonFilePath = mAdminJson;
var jsonString = File.ReadAllText(jsonFilePath);
// Parse the JSON array using System.Text.Json
var steamIds = JsonSerializer.Deserialize<ulong[]>(jsonString);
foreach (var steamId in steamIds) mAdmins.Add(steamId);
await Console.Out.WriteLineAsync("Fetching admins succeeded");
}
catch (Exception ex)
{
await Console.Out.WriteLineAsync("Fetching admins failed: " + ex);
}
}
public override async Task OnDisconnected()
{
await Console.Out.WriteLineAsync(GameIP + " Disconnected");
}
public override async Task<bool> OnPlayerConnected(MyPlayer player)
{
await Console.Out.WriteLineAsync(player.Name + " Connected");
if (!mListedStreamers.Contains(player.SteamID)) return true;
player.IsStreamer = true;
if (!mAdmins.Contains(player.SteamID)) return true;
;
player.IsAdmin = true;
return true;
}
//public override async Task OnTick()
//{
// while (!queue.IsEmpty())
// {
// Command c = queue.Dequeue();
// HandleCommand(c);
// }
//}
public async Task HandleCommand(Command c)
{
// need testing if blocking
foreach (var player in AllPlayers)
{
if (!player.IsStreamer) continue;
if (player.SteamID != c.StreamerId) continue;
switch (c.Action)
{
case ActionType.Heal:
{
player.Heal(c.Amount);
player.Message($"{c.ExecutorName} has healed you for {c.Amount}");
break;
}
case ActionType.Kill:
{
player.Kill();
player.Message($"{c.ExecutorName} has killed you");
break;
}
case ActionType.Grenade:
{
//can't get player pos right now
player.Message($"{c.ExecutorName} has spawned a grenade on you");
break;
}
case ActionType.Teleport:
{
//relative teleport????
player.Message($"{c.ExecutorName} has teleported you {c.Data}");
break;
}
case ActionType.Speed:
{
player.SetRunningSpeedMultiplier(c.Amount);
player.Message($"{c.ExecutorName} has set your speed to {c.Amount}x");
break;
}
case ActionType.Reveal:
{
//set marker on Map
player.Message($"{c.ExecutorName} has revealed your Position");
break;
}
case ActionType.ChangeAmmo:
{
player.Message($"{c.ExecutorName} has set your Ammo to {c.Amount}");
break;
}
case ActionType.Start:
{
player.Message("Forcing start");
ForceStartGame();
break;
}
case ActionType.Help:
{
foreach (var command in mChatCommands) SayToChat($"{command.CommandPrefix} {command.Help}");
break;
}
case ActionType.ChangeDamage:
{
player.SetGiveDamageMultiplier(c.Amount);
player.Message($"{c.ExecutorName} has set your Dmg Multiplier to {c.Amount}");
break;
}
case ActionType.ChangeReceivedDamage:
{
player.SetReceiveDamageMultiplier(c.Amount);
player.Message($"{c.ExecutorName} has set your receive Dmg Multiplier to {c.Amount}");
break;
}
case ActionType.SetStreamer:
{
player.IsStreamer = true;
SaveStreamers();
break;
}
case ActionType.RemoveStreamer:
{
player.IsStreamer = false;
SaveStreamers();
break;
}
case ActionType.GrantOP:
{
player.IsAdmin = true;
SaveAdmins();
break;
}
case ActionType.RevokeOP:
{
player.IsAdmin = false;
SaveAdmins();
break;
}
// Add more cases for other ActionType values as needed
}
}
}
}