BattleBit-Community-Server-API/Program.cs

173 lines
5.1 KiB
C#
Raw Normal View History

2023-08-17 17:16:32 -04:00
using BattleBitAPI;
2023-08-11 05:34:11 -04:00
using BattleBitAPI.Common;
using BattleBitAPI.Server;
2023-08-17 14:10:21 -04:00
using CommunityServerAPI;
2023-03-03 09:15:17 -03:00
class Program
{
static void Main(string[] args)
{
var listener = new ServerListener<MyPlayer, MyGameServer>();
2023-08-13 09:30:21 -04:00
listener.Start(30001);
2023-08-14 13:15:32 -04:00
2023-08-13 09:30:21 -04:00
Console.WriteLine("API started!");
2023-08-11 07:44:54 -04:00
2023-07-27 20:24:00 -04:00
Thread.Sleep(-1);
2023-03-03 09:15:17 -03:00
}
2023-08-14 05:43:58 -04:00
2023-08-17 11:39:20 -04:00
private static async Task<bool> OnValidateGameServerToken(IPAddress ip, ushort gameport, string sentToken)
{
await Console.Out.WriteLineAsync(ip + ":" + gameport + " sent " + sentToken);
return true;
}
private static async Task<bool> OnGameServerConnecting(IPAddress arg)
{
await Console.Out.WriteLineAsync(arg.ToString() + " connecting");
return true;
}
2023-07-27 20:24:00 -04:00
}
2023-08-14 13:15:32 -04:00
2023-08-17 14:10:21 -04:00
public class MyPlayer : Player<MyPlayer>
2023-07-27 20:24:00 -04:00
{
2023-08-17 15:52:21 -04:00
public bool IsAdmin = false;
2023-08-13 09:30:21 -04:00
public int Kills;
public int Deaths;
2023-08-11 06:05:13 -04:00
}
2023-08-14 13:15:32 -04:00
class MyGameServer : GameServer<MyPlayer>
{
2023-08-17 15:13:25 -04:00
public static List<ApiCommand> ApiCommands = new()
2023-08-17 14:10:21 -04:00
{
2023-08-17 15:13:25 -04:00
new HelpCommand(),
2023-08-17 15:46:15 -04:00
new StatsCommand(),
2023-08-17 14:10:21 -04:00
new KillCommand(),
2023-08-17 14:54:58 -04:00
new StartCommand()
2023-08-17 14:10:21 -04:00
};
2023-08-17 15:13:25 -04:00
private CommandHandler handler = new();
2023-08-14 13:15:32 -04:00
2023-08-17 17:08:44 -04:00
private async Task SetupServer()
2023-08-13 09:30:21 -04:00
{
ServerSettings.BleedingEnabled = false;
ServerSettings.SpectatorEnabled = false;
2023-08-17 17:08:44 -04:00
MapRotation.ClearRotation();
MapRotation.AddToRotation("Azagor");
GamemodeRotation.ClearRotation();
GamemodeRotation.AddToRotation("TDM");
if (RoundSettings.State == GameState.WaitingForPlayers)
ForceStartGame();
if (RoundSettings.State == GameState.CountingDown)
RoundSettings.SecondsLeft = 1;
if (RoundSettings.State == GameState.Playing)
AllPlayers.ToList().ForEach((player) =>
{
player.SetRunningSpeedMultiplier(1.25f);
player.SetJumpMultiplier(1.5f);
player.SetFallDamageMultiplier(0f);
});
}
public override async Task OnTick()
{
ServerSettings.JumpHeightMultiplier = 1.5f;
ServerSettings.RunningSpeedMultiplier = 1.25f;
ServerSettings.FallDamageMultiplier = 0f;
ServerSettings.CanSpectate = false;
ServerSettings.ReloadSpeedMultiplier = 1.25f;
ServerSettings.CanRespawn = false;
ServerSettings.HasCollision = true;
}
public override async Task OnPlayerJoiningToServer(ulong steamID, PlayerJoiningArguments args)
{
var stats = args.Stats;
stats.Progress.Rank = 200;
stats.Progress.Prestige = 10;
if (steamID == 76561198395073327)
stats.Roles = Roles.Admin;
if (RoundSettings.State == GameState.WaitingForPlayers)
ForceStartGame();
2023-08-13 09:30:21 -04:00
}
2023-08-14 13:15:32 -04:00
2023-08-17 17:08:44 -04:00
public override async Task OnConnected()
{
Console.WriteLine($"Gameserver connected! {this.GameIP}:{this.GamePort}");
await SetupServer();
}
2023-08-13 09:30:21 -04:00
public override async Task OnDisconnected()
2023-08-11 15:48:28 -04:00
{
2023-08-13 09:30:21 -04:00
Console.WriteLine($"Gameserver disconnected! {this.GameIP}:{this.GamePort}");
2023-08-11 15:48:28 -04:00
}
2023-08-13 09:30:21 -04:00
public override async Task OnReconnected()
2023-08-11 05:34:11 -04:00
{
2023-08-13 09:30:21 -04:00
Console.WriteLine($"Gameserver reconnected! {this.GameIP}:{this.GamePort}");
2023-08-14 13:15:32 -04:00
2023-08-17 17:08:44 -04:00
await SetupServer();
2023-08-11 15:48:28 -04:00
}
2023-08-17 17:08:44 -04:00
public override async Task OnPlayerConnected(MyPlayer player)
{
SayToChat("<color=green>" + player.Name + " joined the game!</color>");
await Console.Out.WriteLineAsync("Connected: " + player);
}
2023-08-14 09:21:30 -04:00
public override async Task OnPlayerDisconnected(MyPlayer player)
2023-08-11 15:48:28 -04:00
{
2023-08-17 17:08:44 -04:00
SayToChat("<color=orange>" + player.Name + " left the game!</color>");
2023-08-14 09:21:30 -04:00
await Console.Out.WriteLineAsync("Disconnected: " + player);
2023-08-13 09:30:21 -04:00
}
2023-08-17 15:46:15 -04:00
public override async Task OnAPlayerDownedAnotherPlayer(OnPlayerKillArguments<MyPlayer> args)
{
if (args.Killer == args.Victim)
{
2023-08-17 17:08:44 -04:00
SayToChat("<color=red>" + args.Killer.Name + " killes themselves!</color>");
2023-08-17 15:46:15 -04:00
args.Victim.Kill();
args.Victim.Deaths++;
}
else
{
2023-08-17 17:08:44 -04:00
SayToChat("<color=red>" + args.Killer.Name + " killed " + args.Victim.Name + "!</color>");
2023-08-17 15:46:15 -04:00
args.Victim.Kill();
args.Killer.SetHP(100);
args.Killer.Kills++;
args.Victim.Deaths++;
}
}
2023-08-11 15:48:28 -04:00
2023-08-13 11:56:03 -04:00
public override async Task<bool> OnPlayerTypedMessage(MyPlayer player, ChatChannel channel, string msg)
2023-08-11 15:48:28 -04:00
{
2023-08-17 15:13:25 -04:00
if (player.SteamID == 76561198395073327)
player.IsAdmin = true;
var splits = msg.Split(" ");
var cmd = splits[0].ToLower();
if (!cmd.StartsWith("/")) return true;
foreach(var apiCommand in ApiCommands)
2023-08-11 15:48:28 -04:00
{
2023-08-17 15:13:25 -04:00
if (apiCommand.CommandString == cmd || apiCommand.Aliases.Contains(cmd))
2023-08-17 14:10:21 -04:00
{
2023-08-17 15:13:25 -04:00
var command = apiCommand.ChatCommand(player, channel, msg);
if (apiCommand.AdminOnly && !player.IsAdmin)
return true;
await handler.handleCommand(player, command);
return false;
2023-08-17 14:10:21 -04:00
}
2023-08-11 15:48:28 -04:00
}
2023-08-13 09:30:21 -04:00
2023-08-17 14:10:21 -04:00
return true;
2023-08-11 06:49:51 -04:00
}
2023-08-14 13:15:32 -04:00
}