Round Settings

This commit is contained in:
MrOkiDoki 2023-08-11 14:44:54 +03:00
commit 4ef76c739d
8 changed files with 216 additions and 52 deletions

View File

@ -0,0 +1,10 @@
namespace BattleBitAPI.Common
{
public enum GameState : byte
{
WaitingForPlayers = 0,
CountingDown = 1,
Playing = 2,
EndingGame = 3
}
}

View File

@ -1,4 +1,5 @@
using System.Net;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Numerics;
using System.Text;
@ -29,9 +30,10 @@ namespace BattleBitAPI.Server
public int MaxPlayers => mInternal.MaxPlayers;
public string LoadingScreenText => mInternal.LoadingScreenText;
public string ServerRulesText => mInternal.ServerRulesText;
public ServerSettings<TPlayer> Settings => mInternal.Settings;
public ServerSettings<TPlayer> ServerSettings => mInternal.ServerSettings;
public MapRotation<TPlayer> MapRotation => mInternal.MapRotation;
public GamemodeRotation<TPlayer> GamemodeRotation => mInternal.GamemodeRotation;
public RoundSettings<TPlayer> RoundSettings => mInternal.RoundSettings;
public string TerminationReason => mInternal.TerminationReason;
public bool ReconnectFlag => mInternal.ReconnectFlag;
@ -44,21 +46,21 @@ namespace BattleBitAPI.Server
if (!this.IsConnected)
return;
if (this.mInternal.IsDirtySettings)
if (this.mInternal.IsDirtyRoomSettings)
{
this.mInternal.IsDirtySettings = false;
this.mInternal.IsDirtyRoomSettings = false;
//Send new settings
using (var pck = Common.Serialization.Stream.Get())
{
pck.Write((byte)NetworkCommuncation.SetNewRoomSettings);
this.mInternal._Settings.Write(pck);
this.mInternal._RoomSettings.Write(pck);
WriteToSocket(pck);
}
}
if (this.mInternal.MapRotationDirty)
if (this.mInternal.IsDirtyMapRotation)
{
this.mInternal.MapRotationDirty = false;
this.mInternal.IsDirtyMapRotation = false;
this.mInternal.mBuilder.Clear();
this.mInternal.mBuilder.Append("setmaprotation ");
@ -70,9 +72,9 @@ namespace BattleBitAPI.Server
}
this.ExecuteCommand(this.mInternal.mBuilder.ToString());
}
if (this.mInternal.GamemodeRotationDirty)
if (this.mInternal.IsDirtyGamemodeRotation)
{
this.mInternal.GamemodeRotationDirty = false;
this.mInternal.IsDirtyGamemodeRotation = false;
this.mInternal.mBuilder.Clear();
this.mInternal.mBuilder.Append("setgamemoderotation ");
@ -299,7 +301,6 @@ namespace BattleBitAPI.Server
}
// ---- Functions ----
public void WriteToSocket(Common.Serialization.Stream pck)
{
@ -534,9 +535,10 @@ namespace BattleBitAPI.Server
public int MaxPlayers;
public string LoadingScreenText;
public string ServerRulesText;
public ServerSettings<TPlayer> Settings;
public ServerSettings<TPlayer> ServerSettings;
public MapRotation<TPlayer> MapRotation;
public GamemodeRotation<TPlayer> GamemodeRotation;
public RoundSettings<TPlayer> RoundSettings;
public string TerminationReason;
public bool ReconnectFlag;
@ -575,25 +577,30 @@ namespace BattleBitAPI.Server
this.mLastPackageSent = Extentions.TickCount;
this.mBuilder = new StringBuilder(4096);
this.Settings = new ServerSettings<TPlayer>(this);
this.ServerSettings = new ServerSettings<TPlayer>(this);
this.MapRotation = new MapRotation<TPlayer>(this);
this.GamemodeRotation = new GamemodeRotation<TPlayer>(this);
this.RoundSettings = new RoundSettings<TPlayer>(this);
}
// ---- Players In Room ----
public Dictionary<ulong, Player<TPlayer>> Players = new Dictionary<ulong, Player<TPlayer>>(254);
// ---- Room Settings ----
public mRoomSettings _Settings = new mRoomSettings();
public bool IsDirtySettings;
public mRoomSettings _RoomSettings = new mRoomSettings();
public bool IsDirtyRoomSettings;
// ---- Round Settings ----
public mRoundSettings _RoundSettings = new mRoundSettings();
public bool IsDirtyRoundSettings;
// ---- Map Rotation ----
public HashSet<string> _MapRotation = new HashSet<string>(8);
public bool MapRotationDirty = false;
public bool IsDirtyMapRotation = false;
// ---- Gamemode Rotation ----
public HashSet<string> _GamemodeRotation = new HashSet<string>(8);
public bool GamemodeRotationDirty = false;
public bool IsDirtyGamemodeRotation = false;
// ---- Public Functions ----
public void Set(Func<GameServer<TPlayer>, Internal, Common.Serialization.Stream, Task> func, TcpClient socket, IPAddress iP, int port, bool isPasswordProtected, string serverName, string gamemode, string map, MapSize mapSize, MapDayNight dayNight, int currentPlayers, int inQueuePlayers, int maxPlayers, string loadingScreenText, string serverRulesText)
@ -616,17 +623,21 @@ namespace BattleBitAPI.Server
this.LoadingScreenText = loadingScreenText;
this.ServerRulesText = serverRulesText;
this.Settings.Reset();
this._Settings.Reset();
this.IsDirtySettings = false;
this.ServerSettings.Reset();
this._RoomSettings.Reset();
this.IsDirtyRoomSettings = false;
this.MapRotation.Reset();
this._MapRotation.Clear();
this.MapRotationDirty = false;
this.IsDirtyMapRotation = false;
this.GamemodeRotation.Reset();
this._GamemodeRotation.Clear();
this.GamemodeRotationDirty = false;
this.IsDirtyGamemodeRotation = false;
this.RoundSettings.Reset();
this._RoundSettings.Reset();
this.IsDirtyRoundSettings = false;
this.TerminationReason = string.Empty;
this.ReconnectFlag = false;
@ -726,5 +737,47 @@ namespace BattleBitAPI.Server
this.ReconLimitPerSquad = 8;
}
}
public class mRoundSettings
{
public GameState State = GameState.WaitingForPlayers;
public int TeamATickets = 0;
public int TeamAMaxTickets = 1;
public int TeamBTickets = 0;
public int TeamBMaxTickets = 1;
public int PlayersToStart = 16;
public int SecondsLeftToEndOfRound = 60;
public void Write(Common.Serialization.Stream ser)
{
ser.Write((byte)this.State);
ser.Write(this.TeamATickets);
ser.Write(this.TeamAMaxTickets);
ser.Write(this.TeamBTickets);
ser.Write(this.TeamBMaxTickets);
ser.Write(this.PlayersToStart);
ser.Write(this.SecondsLeftToEndOfRound);
}
public void Read(Common.Serialization.Stream ser)
{
this.State = (GameState)ser.ReadInt8();
this.TeamATickets = ser.ReadInt32();
this.TeamAMaxTickets = ser.ReadInt32();
this.TeamBTickets = ser.ReadInt32();
this.TeamBMaxTickets = ser.ReadInt32();
this.PlayersToStart = ser.ReadInt32();
this.SecondsLeftToEndOfRound = ser.ReadInt32();
}
public void Reset()
{
this.State = GameState.WaitingForPlayers;
this.TeamATickets = 0;
this.TeamAMaxTickets = 1;
this.TeamBTickets = 0;
this.TeamBMaxTickets = 1;
this.PlayersToStart = 16;
this.SecondsLeftToEndOfRound = 60;
}
}
}
}

View File

@ -23,7 +23,7 @@
lock (mResources._GamemodeRotation)
if (!mResources._GamemodeRotation.Remove(gamemode))
return false;
mResources.GamemodeRotationDirty = true;
mResources.IsDirtyGamemodeRotation = true;
return true;
}
public bool AddToRotation(string gamemode)
@ -31,7 +31,7 @@
lock (mResources._GamemodeRotation)
if (!mResources._GamemodeRotation.Add(gamemode))
return false;
mResources.GamemodeRotationDirty = true;
mResources.IsDirtyGamemodeRotation = true;
return true;
}

View File

@ -27,7 +27,7 @@
lock (mResources._MapRotation)
if (!mResources._MapRotation.Remove(map))
return false;
mResources.MapRotationDirty = true;
mResources.IsDirtyMapRotation = true;
return true;
}
public bool AddToRotation(string map)
@ -37,7 +37,7 @@
lock (mResources._MapRotation)
if (!mResources._MapRotation.Add(map))
return false;
mResources.MapRotationDirty = true;
mResources.IsDirtyMapRotation = true;
return true;
}

View File

@ -0,0 +1,77 @@
using BattleBitAPI.Common;
namespace BattleBitAPI.Server
{
public class RoundSettings<TPlayer> where TPlayer : Player<TPlayer>
{
private GameServer<TPlayer>.Internal mResources;
public RoundSettings(GameServer<TPlayer>.Internal resources)
{
mResources = resources;
}
public GameState State
{
get => this.mResources._RoundSettings.State;
}
public int TeamATickets
{
get => this.mResources._RoundSettings.TeamATickets;
set
{
this.mResources._RoundSettings.TeamATickets = value;
this.mResources.IsDirtyRoundSettings = true;
}
}
public int TeamAMaxTickets
{
get => this.mResources._RoundSettings.TeamAMaxTickets;
set
{
this.mResources._RoundSettings.TeamAMaxTickets = value;
this.mResources.IsDirtyRoundSettings = true;
}
}
public int TeamBTickets
{
get => this.mResources._RoundSettings.TeamBTickets;
set
{
this.mResources._RoundSettings.TeamBTickets = value;
this.mResources.IsDirtyRoundSettings = true;
}
}
public int TeamBMaxTickets
{
get => this.mResources._RoundSettings.TeamBMaxTickets;
set
{
this.mResources._RoundSettings.TeamBTickets = value;
this.mResources.IsDirtyRoundSettings = true;
}
}
public int PlayersToStart
{
get => this.mResources._RoundSettings.PlayersToStart;
set
{
this.mResources._RoundSettings.PlayersToStart = value;
this.mResources.IsDirtyRoundSettings = true;
}
}
public int PlayersToStart
{
get => this.mResources._RoundSettings.PlayersToStart;
set
{
this.mResources._RoundSettings.PlayersToStart = value;
this.mResources.IsDirtyRoundSettings = true;
}
}
public void Reset()
{
}
}
}

View File

@ -10,74 +10,74 @@
public float DamageMultiplier
{
get => mResources._Settings.DamageMultiplier;
get => mResources._RoomSettings.DamageMultiplier;
set
{
mResources._Settings.DamageMultiplier = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.DamageMultiplier = value;
mResources.IsDirtyRoomSettings = true;
}
}
public bool BleedingEnabled
{
get => mResources._Settings.BleedingEnabled;
get => mResources._RoomSettings.BleedingEnabled;
set
{
mResources._Settings.BleedingEnabled = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.BleedingEnabled = value;
mResources.IsDirtyRoomSettings = true;
}
}
public bool StamineEnabled
{
get => mResources._Settings.StamineEnabled;
get => mResources._RoomSettings.StamineEnabled;
set
{
mResources._Settings.StamineEnabled = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.StamineEnabled = value;
mResources.IsDirtyRoomSettings = true;
}
}
public bool FriendlyFireEnabled
{
get => mResources._Settings.FriendlyFireEnabled;
get => mResources._RoomSettings.FriendlyFireEnabled;
set
{
mResources._Settings.FriendlyFireEnabled = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.FriendlyFireEnabled = value;
mResources.IsDirtyRoomSettings = true;
}
}
public bool OnlyWinnerTeamCanVote
{
get => mResources._Settings.OnlyWinnerTeamCanVote;
get => mResources._RoomSettings.OnlyWinnerTeamCanVote;
set
{
mResources._Settings.OnlyWinnerTeamCanVote = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.OnlyWinnerTeamCanVote = value;
mResources.IsDirtyRoomSettings = true;
}
}
public bool HitMarkersEnabled
{
get => mResources._Settings.HitMarkersEnabled;
get => mResources._RoomSettings.HitMarkersEnabled;
set
{
mResources._Settings.HitMarkersEnabled = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.HitMarkersEnabled = value;
mResources.IsDirtyRoomSettings = true;
}
}
public bool PointLogEnabled
{
get => mResources._Settings.PointLogEnabled;
get => mResources._RoomSettings.PointLogEnabled;
set
{
mResources._Settings.PointLogEnabled = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.PointLogEnabled = value;
mResources.IsDirtyRoomSettings = true;
}
}
public bool SpectatorEnabled
{
get => mResources._Settings.SpectatorEnabled;
get => mResources._RoomSettings.SpectatorEnabled;
set
{
mResources._Settings.SpectatorEnabled = value;
mResources.IsDirtySettings = true;
mResources._RoomSettings.SpectatorEnabled = value;
mResources.IsDirtyRoomSettings = true;
}
}

View File

@ -354,7 +354,7 @@ namespace BattleBitAPI.Server
readStream.Reset();
if (!await networkStream.TryRead(readStream, roomSize, source.Token))
throw new Exception("Unable to read the room");
resources._Settings.Read(readStream);
resources._RoomSettings.Read(readStream);
}
//Map&gamemode rotation

View File

@ -1,6 +1,7 @@
using BattleBitAPI;
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using System.Threading.Channels;
class Program
{
@ -8,21 +9,44 @@ class Program
{
var listener = new ServerListener<MyPlayer, MyGameServer>();
listener.Start(29294);
Thread.Sleep(-1);
}
}
class MyPlayer : Player<MyPlayer>
class MyPlayer : Player<MyPlayer>
{
}
class MyGameServer : GameServer<MyPlayer>
{
public List<string> ChatMessages = new List<string>();
public override async Task<bool> OnPlayerTypedMessage(MyPlayer player, ChatChannel channel, string msg)
{
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()
{
await Task.Delay(3000);
if (RoundSettings.State == GameState.WaitingForPlayers)
{
int numberOfPeopleInServer = this.CurrentPlayers;
if (numberOfPeopleInServer > 4)
{
ForceStartGame();
}
}
else if (RoundSettings.State == GameState.Playing)
{
}
}
}