Skip to content

Commit

Permalink
Partially rewrite sessions to support new squad stuff and added confi…
Browse files Browse the repository at this point in the history
…g option for logging.
  • Loading branch information
DanielWillett committed Nov 8, 2024
1 parent 4e5691c commit f3f64fd
Show file tree
Hide file tree
Showing 36 changed files with 1,086 additions and 810 deletions.
30 changes: 30 additions & 0 deletions UncreatedWarfare/Events/Models/Kits/PlayerKitChanged.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Uncreated.Warfare.Kits;
using Uncreated.Warfare.Models.Kits;

namespace Uncreated.Warfare.Events.Models.Kits;

/// <summary>
/// Handles when a player's kit is changed, re-equipped, or dequiped.
/// </summary>
public class PlayerKitChanged : PlayerEvent
{
/// <summary>
/// The kit the player changed to, or <see langword="null"/> if the kit was dequipped.
/// </summary>
public required Kit? Kit { get; init; }

/// <summary>
/// The ID of the kit the player changed to, or 0 if the kit was dequipped.
/// </summary>
public required uint KitId { get; init; }

/// <summary>
/// The name of the kit the player changed to, or <see langword="null"/> if the kit was dequipped.
/// </summary>
public required string? KitName { get; init; }

/// <summary>
/// The class of the kit that was changed to.
/// </summary>
public required Class Class { get; init; }
}
19 changes: 12 additions & 7 deletions UncreatedWarfare/Events/Models/Squads/SquadCreated.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Squads;

namespace Uncreated.Warfare.Events.Models.Squads;

/// <summary>
/// Event listener args which fires after a <see cref="Squad"/> is created.
/// </summary>
internal class SquadCreated
[EventModel(SynchronizedModelTags = [ "squads" ])]
public class SquadCreated : SquadUpdated, IPlayerEvent
{
/// <summary>
/// The <see cref="Squad"/> that was created.
/// The player that created the squad.
/// </summary>
public required WarfarePlayer Player { get; init; }

/// <summary>
/// The ID of the player that created the squad.
/// </summary>
public required Squad Squad { get; init; }
}
public CSteamID Steam64 => Player.Steam64;
}
15 changes: 4 additions & 11 deletions UncreatedWarfare/Events/Models/Squads/SquadDisbanded.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using Uncreated.Warfare.Squads;
using Uncreated.Warfare.Squads;

namespace Uncreated.Warfare.Events.Models.Squads;

/// <summary>
/// Event listener args which fires after a <see cref="Squad"/> is disbanded.
/// </summary>
internal class SquadDisbanded
{
/// <summary>
/// The <see cref="Squad"/> that was disbanded.
/// </summary>
public required Squad Squad { get; init; }
}
[EventModel(SynchronizedModelTags = [ "squads" ])]
public class SquadDisbanded : SquadUpdated;
21 changes: 21 additions & 0 deletions UncreatedWarfare/Events/Models/Squads/SquadLeaderUpdated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Squads;

namespace Uncreated.Warfare.Events.Models.Squads;

/// <summary>
/// Event listener args which fires after a player joines a <see cref="Squad"/>.
/// </summary>
[EventModel(SynchronizedModelTags = [ "squads" ])]
public class SquadLeaderUpdated : SquadUpdated
{
/// <summary>
/// The player that used to be the squad leader. This player may not be online.
/// </summary>
public required WarfarePlayer OldLeader { get; init; }

/// <summary>
/// The player that is now the squad leader. This player may not be online.
/// </summary>
public required WarfarePlayer NewLeader { get; init; }
}
15 changes: 15 additions & 0 deletions UncreatedWarfare/Events/Models/Squads/SquadLockUpdated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Uncreated.Warfare.Squads;

namespace Uncreated.Warfare.Events.Models.Squads;

/// <summary>
/// Event listener args which fires after a player joines a <see cref="Squad"/>.
/// </summary>
[EventModel(SynchronizedModelTags = [ "squads" ])]
public class SquadLockUpdated : SquadUpdated
{
/// <summary>
/// If the squad is now locked. The old state is just the opposite of this.
/// </summary>
public required bool NewLockState { get; init; }
}
21 changes: 11 additions & 10 deletions UncreatedWarfare/Events/Models/Squads/SquadMemberJoined.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Squads;

namespace Uncreated.Warfare.Events.Models.Squads;

/// <summary>
/// Event listener args which fires after a player joines a squad.
/// Event listener args which fires after a player joines a <see cref="Squad"/>.
/// </summary>
internal class SquadMemberJoined
[EventModel(SynchronizedModelTags = [ "squads" ])]
public class SquadMemberJoined : SquadUpdated, IPlayerEvent
{
/// <summary>
/// The squad that the player joined.
/// </summary>
public required Squad Squad { get; init; }
/// <summary>
/// The player that joined the squad.
/// </summary>
public required WarfarePlayer Player { get; init; }

/// <summary>
/// The ID of the player that joined the squad.
/// </summary>
public CSteamID Steam64 => Player.Steam64;

/// <summary>
/// <see langword="true"/> if this event was invoked because <see cref="Player"/> created a new squad and is the squad leader, otherwise <see langword="false"/>.
/// </summary>
Expand Down
21 changes: 11 additions & 10 deletions UncreatedWarfare/Events/Models/Squads/SquadMemberLeft.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Players;
using Uncreated.Warfare.Squads;

namespace Uncreated.Warfare.Events.Models.Squads;

/// <summary>
/// Event listener args which fires after a player leaves a squad.
/// Event listener args which fires after a player leaves a <see cref="Squad"/>.
/// </summary>
internal class SquadMemberLeft
[EventModel(SynchronizedModelTags = [ "squads" ])]
public class SquadMemberLeft : SquadUpdated, IPlayerEvent
{
/// <summary>
/// The squad that the left joined.
/// </summary>
public required Squad Squad { get; init; }
/// <summary>
/// The player that left the squad.
/// </summary>
public required WarfarePlayer Player { get; init; }

/// <summary>
/// The ID of the player that left the squad.
/// </summary>
public CSteamID Steam64 => Player.Steam64;

/// <summary>
/// <see langword="true"/> if this event was invoked because the <see cref="Squad"/> was forcibly disbanded causing all members to leave, otherwise <see langword="false"/>.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions UncreatedWarfare/Events/Models/Squads/SquadUpdated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Uncreated.Warfare.Squads;

namespace Uncreated.Warfare.Events.Models.Squads;

/// <summary>
/// Base event for all <see cref="Warfare.Squads.Squad"/> updates.
/// </summary>
[EventModel(SynchronizedModelTags = [ "squads" ])]
public abstract class SquadUpdated
{
/// <summary>
/// The squad that the player joined.
/// </summary>
public required Squad Squad { get; init; }
}
16 changes: 16 additions & 0 deletions UncreatedWarfare/Kits/KitRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Linq;
using Uncreated.Warfare.Database.Abstractions;
using Uncreated.Warfare.Events;
using Uncreated.Warfare.Events.Models.Kits;
using Uncreated.Warfare.Interaction.Commands;
using Uncreated.Warfare.Kits.Items;
using Uncreated.Warfare.Kits.Translations;
Expand All @@ -25,6 +27,7 @@ public class KitRequests
private readonly RequestTranslations _translations;
private readonly ITranslationValueFormatter _valueFormatter;
private readonly LanguageService _languageService;
private readonly EventDispatcher2 _eventDispatcher;
private readonly IServiceProvider _serviceProvider;
private readonly IPlayerService _playerService;
private readonly ILogger<KitRequests> _logger;
Expand All @@ -39,6 +42,7 @@ public KitRequests(KitManager manager, IServiceProvider serviceProvider)
_playerService = serviceProvider.GetRequiredService<IPlayerService>();
_cooldownManager = serviceProvider.GetRequiredService<CooldownManager>();
_logger = serviceProvider.GetRequiredService<ILogger<KitRequests>>();
_eventDispatcher = serviceProvider.GetRequiredService<EventDispatcher2>();
_serviceProvider = serviceProvider;
Manager = manager;
}
Expand Down Expand Up @@ -318,9 +322,19 @@ private void GrantKit(WarfarePlayer player, Kit? kit, bool tip = true)
if (kit == null)
{
ItemUtility.ClearInventoryAndSlots(player, true);
_ = _eventDispatcher.DispatchEventAsync(new PlayerKitChanged { Player = player, Class = Class.None, Kit = null, KitId = 0, KitName = null });
return;
}

PlayerKitChanged args = new PlayerKitChanged
{
KitId = kit.PrimaryKey,
Kit = kit,
KitName = kit.InternalName,
Class = kit.Class,
Player = player
};

Manager.Distribution.DistributeKitItems(player, kit, _logger, true, tip, false);

// bind hotkeys
Expand Down Expand Up @@ -356,6 +370,8 @@ private void GrantKit(WarfarePlayer player, Kit? kit, bool tip = true)
if (asset != null && KitEx.CanBindHotkeyTo(asset, page))
player.UnturnedPlayer.equipment.ServerBindItemHotkey(index, asset, (byte)page, x, y);
}

_ = _eventDispatcher.DispatchEventAsync(args);
}
internal async Task RemoveKit(WarfarePlayer player, bool manual, CancellationToken token = default, bool psLock = true)
{
Expand Down
27 changes: 23 additions & 4 deletions UncreatedWarfare/Layouts/Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Uncreated.Warfare.Configuration;
using Uncreated.Warfare.Layouts.Phases;
using Uncreated.Warfare.Layouts.Teams;
using Uncreated.Warfare.Models.GameData;
using Uncreated.Warfare.Services;
using Uncreated.Warfare.Util;

Expand All @@ -30,6 +31,21 @@ public class Layout : IDisposable

private readonly LayoutFactory _factory;

/// <summary>
/// A unique ID to this layout.
/// </summary>
public ulong LayoutId { get; private set; }

/// <summary>
/// Whether or not the layout was able to fully start.
/// </summary>
public bool WasStarted { get; private set; }

/// <summary>
/// Reference to a database entry for this layout.
/// </summary>
public GameRecord LayoutStats { get; private set; }

/// <summary>
/// If the layout is currently running.
/// </summary>
Expand Down Expand Up @@ -98,7 +114,7 @@ public Layout(ILifetimeScope serviceProvider, LayoutInfo layoutInfo)
ServiceProvider = serviceProvider;
LayoutInfo = layoutInfo;
Logger = (ILogger<Layout>)serviceProvider.Resolve(typeof(ILogger<>).MakeGenericType(GetType()));

_disposableVariationConfigurationRoots = new List<IDisposable>();
PhaseList = new List<ILayoutPhase>();
Phases = new ReadOnlyCollection<ILayoutPhase>(PhaseList);
Expand Down Expand Up @@ -230,8 +246,11 @@ public Layout(ILifetimeScope serviceProvider, LayoutInfo layoutInfo)
/// <summary>
/// Invoked just before <see cref="BeginLayoutAsync"/> as a chance for values to be initialized from <see cref="LayoutConfiguration"/>.
/// </summary>
protected internal virtual async UniTask InitializeLayoutAsync(CancellationToken token = default)
protected internal virtual async UniTask InitializeLayoutAsync(GameRecord stats, CancellationToken token = default)
{
LayoutId = stats.GameId;
LayoutStats = stats;

await ReadTeamInfoAsync(token);

await ReadPhasesAsync(token);
Expand All @@ -244,6 +263,8 @@ protected internal virtual async UniTask InitializeLayoutAsync(CancellationToken
{
await phase.InitializePhaseAsync(token);
}

WasStarted = true;
}


Expand Down Expand Up @@ -507,8 +528,6 @@ private async UniTask ReadPhasesAsync(CancellationToken token = default)
{
IConfigurationSection phaseSection = LayoutConfiguration.GetSection("Phases");

Assembly thisAsm = Assembly.GetExecutingAssembly();

int index = -1;
foreach (IConfigurationSection phaseConfig in phaseSection.GetChildren())
{
Expand Down
Loading

0 comments on commit f3f64fd

Please sign in to comment.