Skip to content

Commit

Permalink
Implement start/end zone hooking
Browse files Browse the repository at this point in the history
  • Loading branch information
1zc committed Dec 10, 2023
1 parent 5b34ff1 commit 7ac7f39
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
11 changes: 6 additions & 5 deletions src/ST-Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class Map
public int DateAdded {get; set;} = 0;

// Zone Information
public Vector StartZoneOrigin {get; set;} = new Vector();
public Vector EndZoneOrigin {get; set;} = new Vector();
public Vector StartZoneOrigin {get;} = new Vector(0,0,0);
public Vector EndZoneOrigin {get;} = new Vector(0,0,0);

internal Map(string Name, TimerDatabase DB)
{
Expand All @@ -29,11 +29,12 @@ internal Map(string Name, TimerDatabase DB)
if (trigger.Entity!.Name != null)
{
if (trigger.Entity!.Name.Contains("map_start") || trigger.Entity!.Name.Contains("stage1_start") || trigger.Entity!.Name.Contains("s1_start"))
this.StartZoneOrigin = trigger.AbsOrigin!;
this.StartZoneOrigin = new Vector(trigger.AbsOrigin!.X, trigger.AbsOrigin!.Y, trigger.AbsOrigin!.Z);
else if (trigger.Entity!.Name.Contains("map_end"))
this.EndZoneOrigin = trigger.AbsOrigin!;
this.EndZoneOrigin = new Vector(trigger.AbsOrigin!.X, trigger.AbsOrigin!.Y, trigger.AbsOrigin!.Z);
}
}
Console.WriteLine($"[CS2 Surf] Identifying start zone: {this.StartZoneOrigin.X},{this.StartZoneOrigin.Y},{this.StartZoneOrigin.Z}\nIdentifying end zone: {this.EndZoneOrigin.X},{this.EndZoneOrigin.Y},{this.EndZoneOrigin.Z}");

// Gather map information OR create entry
Task<MySqlDataReader> reader = DB.Query($"SELECT * FROM Maps WHERE name='{MySqlHelper.EscapeString(Name)}'");
Expand All @@ -56,7 +57,7 @@ internal Map(string Name, TimerDatabase DB)
else
{
mapData.Close();
Task<int> writer = DB.Write($"INSERT INTO Maps (name, author, tier, stages, ranked, date_added) VALUES ('{MySqlHelper.EscapeString(Name)}', 'Unknown', 0, 0, 0, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()})");
Task<int> writer = DB.Write($"INSERT INTO Maps (name, author, tier, stages, ranked, date_added, last_played) VALUES ('{MySqlHelper.EscapeString(Name)}', 'Unknown', 0, 0, 0, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()}, {(int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()})");
int writerRows = writer.Result;
if (writerRows != 1)
throw new Exception($"CS2 Surf ERROR >> OnRoundStart -> new Map() -> Failed to write new map to database, this shouldnt happen. Map: {Name}");
Expand Down
71 changes: 40 additions & 31 deletions src/SurfTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ public partial class SurfTimer : BasePlugin
public string PluginPath = Server.GameDirectory + "/csgo/addons/counterstrikesharp/plugins/SurfTimer/";
internal Map CurrentMap;

Check warning on line 55 in src/SurfTimer.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'CurrentMap' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 55 in src/SurfTimer.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'CurrentMap' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

/* ========== ROUND HOOKS ========== */
[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
/* ========== MAP START HOOK ========== */
public void OnMapStart(string mapName)
{
// Initialise Map Object
CurrentMap = new Map(Server.MapName, DB!);
if (CurrentMap == null || CurrentMap.Name != mapName)
{
AddTimer(3.0f, () => CurrentMap = new Map(mapName, DB!));
}

// Execute server_settings.cfg
Server.ExecuteCommand("execifexists SurfTimer/server_settings.cfg");
Console.WriteLine("[CS2 Surf] Executed configuration: server_settings.cfg");
return HookResult.Continue;
}

/* ========== PLAYER HOOKS ========== */
Expand Down Expand Up @@ -224,6 +225,8 @@ public override void Load(bool hotReload)
+ $"[CS2 Surf] SurfTimer plugin loaded. Version: {ModuleVersion}"
));

// Map Start Hook
RegisterListener<Listeners.OnMapStart>(OnMapStart);
// Tick listener
RegisterListener<Listeners.OnTick>(() =>
{
Expand Down Expand Up @@ -259,31 +262,34 @@ public override void Load(bool hotReload)
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> {trigger.DesignerName} -> {trigger.Entity!.Name}");
#endif
if (trigger.Entity.Name == "map_end") // TO-DO: IMPLEMENT ZONES IN ST-Map
if (trigger.Entity!.Name != null)
{
// MAP END ZONE
if (player.Timer.IsRunning)
if (trigger.Entity.Name == "map_end") // TO-DO: IMPLEMENT ZONES IN ST-Map
{
player.Timer.Stop();
player.Stats.PB[0,0] = player.Timer.Ticks;
player.Controller.PrintToChat($"{PluginPrefix} You finished the map in {player.HUD.FormatTime(player.Stats.PB[0,0])}!");
// player.Timer.Reset();
// MAP END ZONE
if (player.Timer.IsRunning)
{
player.Timer.Stop();
player.Stats.PB[0,0] = player.Timer.Ticks;
player.Controller.PrintToChat($"{PluginPrefix} You finished the map in {player.HUD.FormatTime(player.Stats.PB[0,0])}!");
// player.Timer.Reset();
}
#if DEBUG
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> trigger_stop actioned");
#endif
}
#if DEBUG
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> trigger_stop actioned");
#endif
}
else if (trigger.Entity.Name == "map_start") // TO-DO: IMPLEMENT ZONES IN ST-Map
{
// MAP START ZONE
player.Timer.Reset();
else if (trigger.Entity.Name.Contains("map_start") || trigger.Entity.Name.Contains("s1_start") || trigger.Entity.Name.Contains("stage1_start")) // TO-DO: IMPLEMENT ZONES IN ST-Map
{
// MAP START ZONE
player.Timer.Reset();
#if DEBUG
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> trigger_start actioned");
// player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> KeyValues: {trigger.Entity.KeyValues3}");
#endif
#if DEBUG
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> trigger_start actioned");
// player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> KeyValues: {trigger.Entity.KeyValues3}");
#endif
}
}
return HookResult.Continue;
Expand Down Expand Up @@ -311,14 +317,17 @@ public override void Load(bool hotReload)
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_EndTouchFunc -> {trigger.DesignerName} -> {trigger.Entity!.Name}");
#endif
if (trigger.Entity.Name == "map_start") // TO-DO: IMPLEMENT ZONES IN ST-Map
if (trigger.Entity!.Name != null)
{
// MAP START ZONE
player.Timer.Start();
if (trigger.Entity.Name.Contains("map_start") || trigger.Entity.Name.Contains("s1_start") || trigger.Entity.Name.Contains("stage1_start")) // TO-DO: IMPLEMENT ZONES IN ST-Map
{
// MAP START ZONE
player.Timer.Start();
#if DEBUG
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> trigger_stop actioned");
#endif
#if DEBUG
player.Controller.PrintToChat($"CS2 Surf DEBUG >> CBaseTrigger_StartTouchFunc -> trigger_stop actioned");
#endif
}
}
return HookResult.Continue;
Expand Down

0 comments on commit 7ac7f39

Please sign in to comment.