Skip to content

Commit

Permalink
Changed some plugins to be async
Browse files Browse the repository at this point in the history
They will needed to be async for some duel updates which send messages.
  • Loading branch information
sven-n committed Jul 7, 2024
1 parent 28fadb3 commit 9816233
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 40 deletions.
16 changes: 10 additions & 6 deletions src/GameLogic/GameContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,19 @@ public async ValueTask<IEnumerable<GameMap>> GetMapsAsync()
}

this._mapList.Add(mapId, createdMap);
createdMap.ObjectAdded += args =>
createdMap.ObjectAdded += async args =>
{
this.PlugInManager.GetPlugInPoint<IObjectAddedToMapPlugIn>()?.ObjectAddedToMap(args.Map, args.Object);
return ValueTask.CompletedTask;
if (this.PlugInManager.GetPlugInPoint<IObjectAddedToMapPlugIn>() is { } plugInPoint)
{
await plugInPoint.ObjectAddedToMapAsync(args.Map, args.Object).ConfigureAwait(false);
}
};
createdMap.ObjectRemoved += args =>
createdMap.ObjectRemoved += async args =>
{
this.PlugInManager.GetPlugInPoint<IObjectRemovedFromMapPlugIn>()?.ObjectRemovedFromMap(args.Map, args.Object);
return ValueTask.CompletedTask;
if (this.PlugInManager.GetPlugInPoint<IObjectRemovedFromMapPlugIn>() is { } plugInPoint)
{
await plugInPoint.ObjectRemovedFromMapAsync(args.Map, args.Object).ConfigureAwait(false);
}
};
}

Expand Down
12 changes: 10 additions & 2 deletions src/GameLogic/MiniGames/MiniGameContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ protected virtual void OnItemDroppedOnMap(DroppedItem item)
/// <param name="args">The event parameters.</param>
protected virtual async ValueTask OnObjectAddedToMapAsync((GameMap Map, ILocateable Object) args)
{
this._gameContext.PlugInManager.GetPlugInPoint<IObjectAddedToMapPlugIn>()?.ObjectAddedToMap(args.Map, args.Object);
if (this._gameContext.PlugInManager.GetPlugInPoint<IObjectAddedToMapPlugIn>() is { } plugInPoint)
{
await plugInPoint.ObjectAddedToMapAsync(args.Map, args.Object).ConfigureAwait(false);
}

if (args.Object is Monster monster)
{
monster.Died += this.OnMonsterDied;
Expand Down Expand Up @@ -390,7 +394,11 @@ protected virtual async ValueTask OnObjectRemovedFromMapAsync((GameMap Map, ILoc
{
try
{
this._gameContext.PlugInManager.GetPlugInPoint<IObjectRemovedFromMapPlugIn>()?.ObjectRemovedFromMap(args.Map, args.Object);
if (this._gameContext.PlugInManager.GetPlugInPoint<IObjectRemovedFromMapPlugIn>() is { } plugInPoint)
{
await plugInPoint.ObjectRemovedFromMapAsync(args.Map, args.Object).ConfigureAwait(false);
}

if (args.Object is not Player player)
{
return;
Expand Down
6 changes: 5 additions & 1 deletion src/GameLogic/NPC/AttackableNpcBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ protected virtual async ValueTask OnDeathAsync(IAttacker attacker)
await player.AfterKilledMonsterAsync().ConfigureAwait(false);
}

player.GameContext.PlugInManager.GetPlugInPoint<IAttackableGotKilledPlugIn>()?.AttackableGotKilled(this, attacker);
if (player.GameContext.PlugInManager.GetPlugInPoint<IAttackableGotKilledPlugIn>() is { } plugInPoint)
{
await plugInPoint.AttackableGotKilledAsync(this, attacker);
}

if (player.SelectedCharacter!.State > HeroState.Normal)
{
player.SelectedCharacter.StateRemainingSeconds -= (int)this.Attributes[Stats.Level];
Expand Down
6 changes: 5 additions & 1 deletion src/GameLogic/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,11 @@ async Task RespawnAsync(CancellationToken cancellationToken)

_ = RespawnAsync(this._respawnAfterDeathCts.Token);

this.GameContext.PlugInManager.GetPlugInPoint<IAttackableGotKilledPlugIn>()?.AttackableGotKilled(this, killer);
if (this.GameContext.PlugInManager.GetPlugInPoint<IAttackableGotKilledPlugIn>() is { } plugInPoint)
{
await plugInPoint.AttackableGotKilledAsync(this, killer);
}

if (this.LastDeath is { } deathInformation)
{
this.Died?.Invoke(this, deathInformation);
Expand Down
2 changes: 1 addition & 1 deletion src/GameLogic/PlugIns/GuildWarSoccerKillScorePlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class GuildWarKillScorePlugIn : IAttackableGotKilledPlugIn
/// </summary>
/// <param name="killed">The killed <see cref="IAttackable" />.</param>
/// <param name="killer">The killer.</param>
public void AttackableGotKilled(IAttackable killed, IAttacker? killer)
public async ValueTask AttackableGotKilledAsync(IAttackable killed, IAttacker? killer)
{
if (killer is Player { GuildWarContext: not null } killerPlayer
&& killed is Player { GuildWarContext: not null } killedPlayer
Expand Down
2 changes: 1 addition & 1 deletion src/GameLogic/PlugIns/IAttackableGotKilledPlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public interface IAttackableGotKilledPlugIn
/// </summary>
/// <param name="killed">The killed <see cref="IAttackable"/>.</param>
/// <param name="killer">The killer.</param>
void AttackableGotKilled(IAttackable killed, IAttacker? killer);
ValueTask AttackableGotKilledAsync(IAttackable killed, IAttacker? killer);
}
2 changes: 1 addition & 1 deletion src/GameLogic/PlugIns/IObjectAddedToMapPlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public interface IObjectAddedToMapPlugIn
/// </summary>
/// <param name="map">The map.</param>
/// <param name="addedObject">The added object.</param>
void ObjectAddedToMap(GameMap map, ILocateable addedObject);
ValueTask ObjectAddedToMapAsync(GameMap map, ILocateable addedObject);
}
2 changes: 1 addition & 1 deletion src/GameLogic/PlugIns/IObjectRemovedFromMapPlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public interface IObjectRemovedFromMapPlugIn
/// </summary>
/// <param name="map">The map.</param>
/// <param name="removedObject">The removed object.</param>
void ObjectRemovedFromMap(GameMap map, ILocateable removedObject);
ValueTask ObjectRemovedFromMapAsync(GameMap map, ILocateable removedObject);
}
24 changes: 8 additions & 16 deletions src/GameLogic/PlugIns/InvasionEvents/BaseInvasionPlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,18 @@ protected BaseInvasionPlugIn(MapEventType? mapEventType, (ushort MapId, ushort M
protected virtual ushort[] PossibleMaps { get; } = { LorenciaId, NoriaId, DeviasId };

/// <inheritdoc />
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "Catching all Exceptions.")]
public virtual async void ObjectAddedToMap(GameMap map, ILocateable addedObject)
public virtual async ValueTask ObjectAddedToMapAsync(GameMap map, ILocateable addedObject)
{
try
if (this._mapEventType is null)
{
if (this._mapEventType is null)
{
return;
}

if (addedObject is Player player)
{
var state = this.GetStateByGameContext(player.GameContext);
var isEnabled = state.State != PeriodicTaskState.NotStarted;
await this.TrySendMapEventStateUpdateAsync(player, isEnabled).ConfigureAwait(false);
}
return;
}
catch

if (addedObject is Player player)
{
// must be catched because it's an async void method.
var state = this.GetStateByGameContext(player.GameContext);
var isEnabled = state.State != PeriodicTaskState.NotStarted;
await this.TrySendMapEventStateUpdateAsync(player, isEnabled).ConfigureAwait(false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/GameLogic/PlugIns/QuestMonsterKillCountPlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class QuestMonsterKillCountPlugIn : IAttackableGotKilledPlugIn
/// </summary>
/// <param name="killed">The killed <see cref="IAttackable" />.</param>
/// <param name="killer">The killer.</param>
public void AttackableGotKilled(IAttackable killed, IAttacker? killer)
public async ValueTask AttackableGotKilledAsync(IAttackable killed, IAttacker? killer)
{
if (!(killer is Player player && killed is Monster monster)
|| player.SelectedCharacter?.QuestStates is null)
Expand Down
19 changes: 10 additions & 9 deletions src/GameLogic/PlugIns/WeatherUpdatePlugIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async ValueTask ExecuteTaskAsync(GameContext gameContext)
this._isRunning = true;
try
{
foreach (var map in await gameContext.GetMapsAsync())
foreach (var map in await gameContext.GetMapsAsync().ConfigureAwait(false))
{
var weather = (byte)Rand.NextInt(0, 3);
var variation = (byte)Rand.NextInt(0, 10);
Expand All @@ -55,19 +55,20 @@ public async ValueTask ExecuteTaskAsync(GameContext gameContext)
}

/// <inheritdoc />
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "Catching all Exceptions.")]
public async void ObjectAddedToMap(GameMap map, ILocateable addedObject)
public async ValueTask ObjectAddedToMapAsync(GameMap map, ILocateable addedObject)
{
if (addedObject is not Player player)
{
return;
}

try
{
if (addedObject is Player player)
{
await this.TrySendPlayerUpdateAsync(player).ConfigureAwait(false);
}
await this.TrySendPlayerUpdateAsync(player).ConfigureAwait(false);
}
catch
catch (Exception ex)
{
// must be catched because it's an async void method.
player.Logger.LogDebug(ex, "Unexpected error sending weather update.");
}
}

Expand Down

0 comments on commit 9816233

Please sign in to comment.