Skip to content

Commit

Permalink
Add guild mission logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Nov 5, 2024
1 parent 7e9fd7d commit f2bb053
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
13 changes: 13 additions & 0 deletions GW2SDK.Tests/Features/Guilds/GuildLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public async Task Can_be_found()
case InfluenceActivity influenceActivity:
Assert.True(influenceActivity.Activity.IsDefined());
break;
case GuildMission guildMissionActivity:
Assert.True(guildMissionActivity.State.IsDefined());
if (guildMissionActivity.State == GuildMissionState.Start)
{
Assert.NotEmpty(guildMissionActivity.User);
}
else
{
Assert.Empty(guildMissionActivity.User);
}

Assert.Equal(0, guildMissionActivity.Influence);
break;
}
}
);
Expand Down
2 changes: 2 additions & 0 deletions GW2SDK/Features/Guilds/Logs/GuildLogEntryJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static GuildLogEntry GetGuildLogEntry(this JsonElement json)
return json.GetMemberJoined();
case "kick":
return json.GetMemberKicked();
case "mission":
return json.GetGuildMission();
case "motd":
return json.GetNewMessageOfTheDay();
case "rank_change":
Expand Down
17 changes: 17 additions & 0 deletions GW2SDK/Features/Guilds/Logs/GuildMission.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace GuildWars2.Guilds.Logs;

/// <summary>A log entry about a started or ended guild mission.</summary>
[PublicAPI]
[DataTransferObject]
public sealed record GuildMission : GuildLogEntry
{
/// <summary>The ID of the user who started the mission.</summary>
/// <remarks>Can be empty.</remarks>
public required string User { get; init; }

/// <summary>The state to which the mission transitioned.</summary>
public required Extensible<GuildMissionState> State { get; init; }

/// <summary>The amount of guild influence awarded.</summary>
public required int Influence { get; init; }
}
60 changes: 60 additions & 0 deletions GW2SDK/Features/Guilds/Logs/GuildMissionJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Text.Json;
using GuildWars2.Json;

namespace GuildWars2.Guilds.Logs;

internal static class GuildMissionJson
{
public static GuildMission GetGuildMission(this JsonElement json)
{
RequiredMember id = "id";
RequiredMember time = "time";
RequiredMember state = "state";
RequiredMember influence = "influence";
OptionalMember user = "user";

foreach (var member in json.EnumerateObject())
{
if (member.NameEquals("type"))
{
if (!member.Value.ValueEquals("mission"))
{
ThrowHelper.ThrowInvalidDiscriminator(member.Value.GetString());
}
}
else if (id.Match(member))
{
id = member;
}
else if (time.Match(member))
{
time = member;
}
else if (state.Match(member))
{
state = member;
}
else if (influence.Match(member))
{
influence = member;
}
else if (user.Match(member))
{
user = member;
}
else if (JsonOptions.MissingMemberBehavior == MissingMemberBehavior.Error)
{
ThrowHelper.ThrowUnexpectedMember(member.Name);
}
}

return new GuildMission
{
Id = id.Map(static value => value.GetInt32()),
Time = time.Map(static value => value.GetDateTimeOffset()),
User = user.Map(static value => value.GetString()) ?? "",
State = state.Map(static value => value.GetEnum<GuildMissionState>()),
Influence = influence.Map(static value => value.GetInt32())
};
}
}
15 changes: 15 additions & 0 deletions GW2SDK/Features/Guilds/Logs/GuildMissionState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace GuildWars2.Guilds.Logs;

/// <summary>The guild mission state transitions.</summary>
[PublicAPI]
public enum GuildMissionState
{
/// <summary>Logged when the mission starts.</summary>
Start = 1,

/// <summary>Logged when the mission ends successfully.</summary>
Success,

/// <summary>Logged when the mission ends unsuccessfully.</summary>
Fail
}

0 comments on commit f2bb053

Please sign in to comment.