some error handling

This commit is contained in:
caesarakalaeii 2023-08-12 15:07:12 +02:00
parent 59f2cfabc8
commit 058d19c4d6
3 changed files with 263 additions and 374 deletions

View File

@ -1,113 +1,214 @@
using BattleBitAPI;
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using CommandQueueApp;
using System.Linq;
using System.Threading.Channels;
using System.Xml;
class Program
{
static void Main(string[] args)
{
var listener = new ServerListener<MyPlayer, MyGameServer>();
listener.Start(29294);
listener.Start(55669);
Thread.Sleep(-1);
}
}
class MyPlayer : Player<MyPlayer>
{
public bool IsZombie;
}
class MyGameServer : GameServer<MyPlayer>
{
//public CommandQueue queue = new();
public List<ulong> listed_streamers = new List<ulong>();
public List<MyPlayer> connectedStreamers = new List<MyPlayer>();
public List<string> ChatMessages = new List<string>();
public override async Task OnRoundStarted()
public override async Task<bool> OnPlayerTypedMessage(MyPlayer player, ChatChannel channel, string msg)
{
}
public override async Task OnRoundEnded()
{
}
public override async Task OnPlayerConnected(MyPlayer player)
{
bool anyZombiePlayer = false;
foreach (var item in AllPlayers)
string[] splits = msg.Split(" ");
var c = new Command(); // just to test replace with argument parsing for now
switch (splits[0])
{
if (item.IsZombie)
case "heal":
{
c.Action = ActionType.Heal;
c.Amount = 10;
c.ExecutorName = "Tester";
break;
}
case "kill":
{
c.Action = ActionType.Kill;
c.Amount = 1;
c.ExecutorName = "Tester";
break;
}
case "grenade":
{
c.Action = ActionType.Grenade;
c.Amount = 1;
c.ExecutorName = "Tester";
break;
}
case "teleport":
{
c.Action = ActionType.Teleport;
c.Amount = 10;
c.ExecutorName = "Tester";
break;
}
case "speed":
{
c.Action = ActionType.Speed;
c.Amount = 5;
c.ExecutorName = "Tester";
break;
}
case "changeAttachement":
{
anyZombiePlayer = true;
c.Action = ActionType.ChangeAttachement;
c.Amount = 1;
c.Data = splits.Skip(0).Take(splits.Length);
c.ExecutorName = "Tester";
break;
}
case "changeWeapon":
{
c.Action = ActionType.ChangeWeapon;
c.Amount = 1;
c.Data = splits.Skip(0).Take(splits.Length);
c.ExecutorName = "Tester";
break;
}
case "reveal":
{
c.Action = ActionType.Reveal;
c.Amount = 10;
c.ExecutorName = "Tester";
break;
}
case "changeDamage":
{
c.Action = ActionType.ChangeDamage;
c.Amount = Int32.Parse(splits[1]);
c.ExecutorName = "Tester";
break;
}
case "changeRecievedDamage":
{
c.Action = ActionType.ChangeReceivedDamage;
c.Amount = Int32.Parse(splits[1]);
c.ExecutorName = "Tester";
break;
}
case "changeAmmo":
{
c.Action = ActionType.ChangeAmmo;
c.Amount = Int32.Parse(splits[1]);
c.ExecutorName = "Tester";
break;
}
}
if (!anyZombiePlayer)
{
player.IsZombie = true;
player.Message("You are the zombie.");
player.Kill();
}
await HandleCommand(c);
return true;
}
public override async Task OnAPlayerKilledAnotherPlayer(OnPlayerKillArguments<MyPlayer> args)
{
if (args.Victim.IsZombie)
{
args.Victim.IsZombie = false;
args.Victim.Message("You are no longer zombie");
AnnounceShort("Choosing new zombie in 5");
await Task.Delay(1000);
AnnounceShort("Choosing new zombie in 4");
await Task.Delay(1000);
AnnounceShort("Choosing new zombie in 3");
await Task.Delay(1000);
AnnounceShort("Choosing new zombie in 2");
await Task.Delay(1000);
AnnounceShort("Choosing new zombie in 1");
await Task.Delay(1000);
args.Killer.IsZombie = true;
args.Killer.SetHeavyGadget(Gadgets.SledgeHammer.ToString(), 0, true);
var position = args.Killer.GetPosition();
}
}
public override async Task<OnPlayerSpawnArguments> OnPlayerSpawning(MyPlayer player, OnPlayerSpawnArguments request)
{
if (player.IsZombie)
{
request.Loadout.PrimaryWeapon = default;
request.Loadout.SecondaryWeapon = default;
request.Loadout.LightGadget = null;
request.Loadout.HeavyGadget = Gadgets.SledgeHammer;
request.Loadout.Throwable = null;
}
return request;
}
public override async Task OnPlayerSpawned(MyPlayer player)
{
if(player.IsZombie)
{
player.SetRunningSpeedMultiplier(2f);
player.SetJumpMultiplier(2f);
player.SetFallDamageMultiplier(0f);
player.SetReceiveDamageMultiplier(0.1f);
player.SetGiveDamageMultiplier(4f);
}
}
public override async Task OnConnected()
{
await Console.Out.WriteLineAsync("Current state: " + RoundSettings.State);
await Console.Out.WriteLineAsync(this.GameIP + " Connected");
}
public override async Task OnGameStateChanged(GameState oldState, GameState newState)
public override async Task OnDisconnected()
{
await Console.Out.WriteLineAsync("State changed to -> " + newState);
await Console.Out.WriteLineAsync(this.GameIP + " Disconnected");
}
public override async Task<bool> OnPlayerConnected(MyPlayer player)
{
if (!listed_streamers.Contains(player.SteamID))
{
return true;
}
if (connectedStreamers.Contains(player))
{
return true;
}
connectedStreamers.Add(player);
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 (MyPlayer player in connectedStreamers)
{
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:
{
//set marker on Map
player.Message($"{c.ExecutorName} has set your Ammo to {c.Amount}");
break;
}
}
}
}
}

View File

@ -1,108 +1,97 @@
using System;
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace CommandQueueApp
{
public class Data
{
}
//public class Data
//{
// bool isData;
//}
public class Command
{
public ActionType Action { get; set; }
public ulong StreamerID { get; set; }
public int Amount { get; set; }
public string Data { get; set;}
public IEnumerable<string> Data { get; set;}
public string ExecutorName{ get; set; }
public Data DataHandler( string data, string Action)
{
return new Data();
}
}
public class CommandQueue
{
private readonly ConcurrentQueue<Command> _queue = new ConcurrentQueue<Command>();
// public class CommandQueue
// {
// private readonly ConcurrentQueue<Command> _queue = new ConcurrentQueue<Command>();
//
// public void Enqueue(Command command)
// {
// _queue.Enqueue(command);
// }
//
// public Command Dequeue()
// {
// _queue.TryDequeue(out Command command);
// return command;
// }
//
// public bool IsEmpty(){
// return _queue.IsEmpty;
// }
// }
//
// public class Startup
// {
// public void ConfigureServices(IServiceCollection services)
// {
// services.AddSingleton<CommandQueue>();
// services.AddControllers();
// }
//
// public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// {
// if (env.IsDevelopment())
// {
// app.UseDeveloperExceptionPage();
// }
//
// app.UseRouting();
//
// app.UseEndpoints(endpoints =>
// {
// endpoints.MapControllers();
// });
// }
// }
//
// [ApiController]
// [Route("api/[controller]")]
// public class CommandController : ControllerBase
// {
// private readonly CommandQueue _commandQueue;
//
// public CommandController(CommandQueue commandQueue)
// {
// _commandQueue = commandQueue;
// }
//
// [HttpPost]
// public IActionResult PostCommand([FromBody] Command command)
// {
// _commandQueue.Enqueue(command);
// return Ok();
// }
// }
public void Enqueue(Command command)
{
_queue.Enqueue(command);
}
public Command Dequeue()
{
_queue.TryDequeue(out Command command);
return command;
}
public bool IsEmpty(){
return _queue.IsEmpty;
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<CommandQueue>();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
[ApiController]
[Route("api/[controller]")]
public class CommandController : ControllerBase
{
private readonly CommandQueue _commandQueue;
public CommandController(CommandQueue commandQueue)
{
_commandQueue = commandQueue;
}
[HttpPost]
public IActionResult PostCommand([FromBody] Command command)
{
_commandQueue.Enqueue(command);
return Ok();
}
}
class Program
{
static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
// class Program
// {
// static void Main(string[] args)
// {
// CreateHostBuilder(args).Build().Run();
// }
//
// public static IHostBuilder CreateHostBuilder(string[] args) =>
// Host.CreateDefaultBuilder(args)
// .ConfigureWebHostDefaults(webBuilder =>
// {
// webBuilder.UseStartup<Startup>();
// });
// }
}

View File

@ -1,201 +0,0 @@
using BattleBitAPI;
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using CommandQueueApp;
using System.Linq;
using System.Threading.Channels;
class Program
{
static void Main(string[] args)
{
var listener = new ServerListener<MyPlayer, MyGameServer>();
listener.Start(55669);
listener.OnPlayerConnected += HandlePlayerConnected;
listener.OnTick += HandleTick;
Thread.Sleep(-1);
}
private static async Task<bool> HandleTick()
{
return true;
}
private static async Task<bool> HandlePlayerConnected(MyPlayer player)
{
MyGameServer server = player.GameServer;
if (!listed_streamers.contains(player.SteamID))
{
return true;
}
if (server.connected_streamers.contains(player.SteamID))
{
return true;
}
server.connected_streamers.Add(player);
}
}
class MyPlayer : Player<MyPlayer>
{
}
class MyGameServer : GameServer<MyPlayer>
{
public CommandQueue queue = new();
public List<MyPlayer> connectedStreamers = new List<MyPlayer>();
public List<string> ChatMessages = new List<string>();
public override async Task<bool> OnPlayerTypedMessage(MyPlayer player, ChatChannel channel, string msg)
{
string[] splits = msg.Split(" ");
var c = new Command(); // just to test replace with argument parsing for now
switch (splits[0])
{
case "heal":
{
c.Action.set(ActionType.Heal);
c.Amount.set(10);
c.ExecutorName.set("Tester");
}
case "kill":
{
c.Action.set(ActionType.Kill);
c.Amount.set(1);
c.ExecutorName.set("Tester");
}
case "grenade":
{
c.Action.set(ActionType.Grenade);
c.Amount.set(1);
c.ExecutorName.set("Tester");
}
case "teleport":
{
c.Action.set(ActionType.Teleport);
c.Amount.set(10);
c.ExecutorName.set("Tester");
}
case "speed":
{
c.Action.set(ActionType.Speed);
c.Amount.set(5);
c.ExecutorName.set("Tester");
}
case "changeAttachement":
{
c.Action.set(ActionType.ChangeAttachement);
c.Amount.set(1);
c.Data(splits.Skip(0).Take(splits.Length()));
c.ExecutorName.set("Tester");
}
case "changeWeapon":
{
c.Action.set(ActionType.ChangeWeapon);
c.Amount.set(1);
c.Data(splits.Skip(0).Take(splits.Length()));
c.ExecutorName.set("Tester");
}
case "reveal":
{
c.Action.set(ActionType.Reveal);
c.Amount.set(10);
c.ExecutorName.set("Tester");
}
case "changeDamage":
{
c.Action.set(ActionType.ChangeDamage);
c.Amount.set(Int32.Parse(splits[1]));
c.ExecutorName.set("Tester");
}
case "changeRecievedDamage":
{
c.Action.set(ActionType.ChangeReceivedDamage);
c.Amount.set(Int32.Parse(splits[1]));
c.ExecutorName.set("Tester");
}
case "changeAmmo":
{
c.Action.set(ActionType.ChangeAmmo);
c.Amount.set(Int32.Parse(splits[1]));
c.ExecutorName.set("Tester");
}
HandleCommand(c);
}
return true;
}
public override async Task OnConnected()
{
await Console.Out.WriteLineAsync(this.GameIP + " Connected");
}
public override async Task OnDisconnected()
{
await Console.Out.WriteLineAsync(this.GameIP + " Disconnected");
}
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 (MyPlayer player in connectedStreamers)
{
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}");
}
case ActionType.Kill:
{
player.Kill();
player.Message($"{c.ExecutorName} has killed you");
}
case ActionType.Grenade:
{
//can't get player pos right now
player.Message($"{c.ExecutorName} has spawned a grenade on you");
}
case ActionType.Teleport:
{
//relative teleport????
player.Message($"{c.ExecutorName} has teleported you {c.Data}");
}
case ActionType.Speed:
{
player.setRunningSpeedMultiplyer(c.Amount);
player.Message($"{c.ExecutorName} has set your speed to {c.Amount}x");
}
case ActionType.Reveal:
{
//set marker on Map
player.Message($"{c.ExecutorName} has revealed your Position");
}
case ActionType.ChangeAmmo:
{
//set marker on Map
player.Message($"{c.ExecutorName} has set your Ammo to {c.Amount}");
}
}
}
}
}