Skip to content
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Nothing goes to NuGet. ktsu.Sdk marks every library packable from its targets, a

- **`Core/Progression/ProgressionEngine.cs`:** turns hits, kills, reloads, life summaries, assists, flashes, and clutches into rank XP, weapon XP, ladder progress, and notices.
- **`Core/SkillShots/SkillShotCatalog.cs`:** every skill shot, with its rarity, the weapons it applies to, and its detection rule. Ids are stored as challenge ids, so never rename one.
- **`Core/Contracts/ContractCatalog.cs`:** the daily and weekly contracts, and which run on a day. The picks hash the UTC date in `Core/Rotation/DailyRotation.cs`, so the plugin and site agree with nothing stored. Progress is saved per period key in `csp_contracts`, and contract ids are stored, so never rename one.
- **`Core/Weapons/WeaponCatalog.cs`:** every tracked weapon, and the normalization of CS2's weapon names (prefixes, knife skins, grenade aliases, and alternates told apart by definition index).
- **`Core/Tracking/`:** `RoundTracker` (opening kills, multi-kills, collaterals, trades, clutches), `PlayerCombatTracker` (landing, scope, weapon switch, reload, flick), and `LifeTracker` (per-life accuracy).
- **`Core/Storage/`:** `ProgressSnapshot` (dirty rows copied on the game thread), `MySqlProgressionStore`, and `InMemoryProgressionStore`. Each save also upserts the player's total rank XP for the day into `csp_daily`, and `XpHistory` turns those rows into XP per day and XP since a date (the player page's History tab and the "XP this week" board on Ranks).
Expand Down
1 change: 1 addition & 0 deletions CounterStrikeProgression.Core/Admin/AdminCorrection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public CorrectionOutcome Apply(PlayerProgress player, RankLadder ranks)
{
weaponsCleared = player.Weapons.Count;
player.Weapons.Clear();
player.Contracts.Clear();
player.RankXp = 0;
player.Rank = 0;
player.PendingXp = 0;
Expand Down
116 changes: 116 additions & 0 deletions CounterStrikeProgression.Core/Contracts/ContractCatalog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.CounterStrikeProgression.Core.Contracts;

using System.Collections.Frozen;
using ktsu.CounterStrikeProgression.Core.Rotation;
using ktsu.CounterStrikeProgression.Core.SkillShots;
using ktsu.CounterStrikeProgression.Core.Weapons;

/// <summary>
/// Every contract, and which ones run on a given day. The picks come from a hash of the date, so the plugin and
/// the site agree on them with nothing stored. Contract ids are stored with progress, so never rename one.
/// </summary>
internal static class ContractCatalog
{
/// <summary>
/// Gets the daily pool, as groups of variants. A day draws whole groups, so it never gets two contracts of the
/// same kind, and then one variant from each group drawn.
/// </summary>
public static IReadOnlyList<IReadOnlyList<ContractDefinition>> DailyGroups { get; } =
[
[
ClassKills(WeaponClass.Pistol, "kills_pistol", 15),
ClassKills(WeaponClass.Smg, "kills_smg", 20),
ClassKills(WeaponClass.Rifle, "kills_rifle", 25),
ClassKills(WeaponClass.Sniper, "kills_sniper", 10),
ClassKills(WeaponClass.Shotgun, "kills_shotgun", 12),
ClassKills(WeaponClass.MachineGun, "kills_machine_gun", 12),
],
[Daily("headshots", "Get 15 headshot kills", 15, s => (s.Kind == SignalKind.Kill) && s.Headshot)],
[Daily("skill_shots", "Earn 10 skill shots", 10, s => s.Kind == SignalKind.SkillShot)],
[Daily("rare_skill_shots", "Earn 3 rare or epic skill shots", 3, s => s.SkillShot is { Rarity: not Rarity.Common })],
[Daily("wallbangs", "Get 2 kills through walls", 2, SkillShots("wallbang"))],
[Daily("movement", "Earn 5 movement skill shots, such as Jumpshot or Moving target", 5, SkillShots("moving_target", "airborne_target", "jumpshot", "air_to_air", "run_and_gun", "landing_shot"))],
[Daily("multi_kills", "Earn Double kill or better 3 times", 3, SkillShots("double_kill", "triple_kill", "rampage"))],
[Daily("utility_kills", "Get 2 kills with the knife, a grenade, or the Zeus", 2, s => (s.Kind == SignalKind.Kill) && s.Weapon is { Class: WeaponClass.Knife or WeaponClass.Grenade or WeaponClass.Taser })],
[Daily("chickens", "Kill 5 chickens", 5, s => s.Kind == SignalKind.Chicken)],
[Daily("assists", "Get 5 assists", 5, s => s.Kind == SignalKind.Assist)],
[Daily("scope_tricks", "Earn 2 No-scopes, Quickscopes, or Scoped on the moves", 2, SkillShots("no_scope", "quickscope", "scoped_on_the_move"))],
[Daily("precision", "Earn One tap or Flick 3 times", 3, SkillShots("one_tap", "flick"))],
];

/// <summary>Gets the weekly pool. A week draws one.</summary>
public static IReadOnlyList<ContractDefinition> Weekly { get; } =
[
Week("weekly_kills", "Get 250 kills", 250, s => s.Kind == SignalKind.Kill),
Week("weekly_headshots", "Get 100 headshot kills", 100, s => (s.Kind == SignalKind.Kill) && s.Headshot),
Week("weekly_skill_shots", "Earn 60 skill shots", 60, s => s.Kind == SignalKind.SkillShot),
Week("weekly_epic", "Earn 3 epic skill shots", 3, s => s.SkillShot is { Rarity: Rarity.Epic }),
Week("weekly_tiers", "Finish 5 challenge tiers", 5, s => s.Kind == SignalKind.Tier),
Week("weekly_levels", "Gain 10 weapon levels", 10, s => s.Kind == SignalKind.WeaponLevel),
];

private static FrozenDictionary<string, ContractDefinition> ById { get; } =
DailyGroups.SelectMany(g => g).Concat(Weekly).ToFrozenDictionary(c => c.Id, StringComparer.Ordinal);

/// <summary>
/// Looks up a contract by id.
/// </summary>
/// <param name="id">The contract id.</param>
/// <returns>The contract, or <see langword="null"/> when there's no such id.</returns>
public static ContractDefinition? Find(string id) => ById.GetValueOrDefault(id);

/// <summary>
/// Gets the contracts running on a day: its daily contracts first, then the week's.
/// </summary>
/// <param name="day">The UTC day.</param>
/// <param name="settings">How many daily contracts there are and what each pays.</param>
/// <returns>The contracts, or none when contracts are off.</returns>
public static IReadOnlyList<ActiveContract> Active(DateOnly day, ProgressionSettings settings)
{
Ensure.NotNull(settings);
if (!settings.ContractsEnabled)
{
return [];
}

string dayKey = DailyRotation.DayKey(day);
DateTime dayEnds = day.AddDays(1).ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);
DateTime weekEnds = DailyRotation.WeekStart(day).AddDays(7).ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc);

int count = Math.Clamp(settings.DailyContracts, 0, DailyGroups.Count);
List<ActiveContract> active =
[
.. Enumerable.Range(0, DailyGroups.Count)
.OrderBy(i => DailyRotation.Hash(dayKey, $"daily:{i}"))
.Take(count)
.Order()
.Select(i => Variant(DailyGroups[i], dayKey, i))
.Select(c => new ActiveContract(c, dayKey, dayEnds, settings.DailyContractXp)),
];

string weekKey = DailyRotation.WeekKey(day);
ContractDefinition weekly = Weekly[(int)(DailyRotation.Hash(weekKey, "weekly") % (ulong)Weekly.Count)];
active.Add(new ActiveContract(weekly, weekKey, weekEnds, settings.WeeklyContractXp));
return active;
}

private static ContractDefinition Variant(IReadOnlyList<ContractDefinition> group, string dayKey, int groupIndex) =>
group.Count == 1 ? group[0] : group[(int)(DailyRotation.Hash(dayKey, $"variant:{groupIndex}") % (ulong)group.Count)];

private static ContractDefinition ClassKills(WeaponClass weaponClass, string id, int target) =>
Daily(id, $"Get {target} kills with {DailyRotation.ClassName(weaponClass)}", target, s => (s.Kind == SignalKind.Kill) && (s.Weapon?.Class == weaponClass));

private static Func<ContractSignal, bool> SkillShots(params string[] ids)
{
HashSet<string> set = new(ids, StringComparer.Ordinal);
return s => (s.SkillShot is not null) && set.Contains(s.SkillShot.Id.WeakString);
}

private static ContractDefinition Daily(string id, string description, int target, Func<ContractSignal, bool> counts) =>
new(id, description, ContractPeriod.Daily, target, counts);

private static ContractDefinition Week(string id, string description, int target, Func<ContractSignal, bool> counts) =>
new(id, description, ContractPeriod.Weekly, target, counts);
}
70 changes: 70 additions & 0 deletions CounterStrikeProgression.Core/Contracts/ContractDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.CounterStrikeProgression.Core.Contracts;

using ktsu.CounterStrikeProgression.Core.SkillShots;
using ktsu.CounterStrikeProgression.Core.Weapons;

/// <summary>
/// How long a contract runs.
/// </summary>
internal enum ContractPeriod
{
/// <summary>One UTC day.</summary>
Daily = 0,

/// <summary>One ISO week, Monday to Sunday in UTC.</summary>
Weekly,
}

/// <summary>
/// The kinds of thing a contract can count.
/// </summary>
internal enum SignalKind
{
/// <summary>A kill of a player or bot.</summary>
Kill = 0,

/// <summary>A skill shot earned.</summary>
SkillShot,

/// <summary>A chicken killed.</summary>
Chicken,

/// <summary>An assist.</summary>
Assist,

/// <summary>A challenge tier finished.</summary>
Tier,

/// <summary>A weapon level gained.</summary>
WeaponLevel,
}

/// <summary>
/// One thing a player did that a contract might count.
/// </summary>
/// <param name="Kind">What happened.</param>
/// <param name="Weapon">The weapon involved, if any.</param>
/// <param name="Headshot">For kills, whether it was a headshot.</param>
/// <param name="SkillShot">For skill shots, which one.</param>
internal readonly record struct ContractSignal(SignalKind Kind, WeaponInfo? Weapon = null, bool Headshot = false, SkillShotDefinition? SkillShot = null);

/// <summary>
/// One contract: a short goal that pays a bonus once in its day or week.
/// </summary>
/// <param name="Id">The stable id, stored with the period's key. Never rename one.</param>
/// <param name="Description">What the player has to do, such as "Get 20 kills with SMGs".</param>
/// <param name="Period">How long the contract runs.</param>
/// <param name="Target">How many matching signals finish it.</param>
/// <param name="Counts">Whether a signal counts toward it.</param>
internal sealed record ContractDefinition(string Id, string Description, ContractPeriod Period, int Target, Func<ContractSignal, bool> Counts);

/// <summary>
/// A contract running now.
/// </summary>
/// <param name="Definition">The contract.</param>
/// <param name="PeriodKey">The key of the day or week it runs in, such as <c>2026-10-17</c> or <c>2026-W42</c>.</param>
/// <param name="EndsAt">When it ends, in UTC.</param>
/// <param name="Xp">The rank XP it pays.</param>
internal sealed record ActiveContract(ContractDefinition Definition, string PeriodKey, DateTime EndsAt, int Xp);
14 changes: 14 additions & 0 deletions CounterStrikeProgression.Core/Live/FeedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ktsu.CounterStrikeProgression.Core.Live;

using ktsu.CounterStrikeProgression.Core.Contracts;
using ktsu.CounterStrikeProgression.Core.Panel;
using ktsu.CounterStrikeProgression.Core.Progression;

Expand Down Expand Up @@ -33,6 +34,7 @@ public static IReadOnlyList<FeedEntry> From(ProgressionResult result, ulong stea
NoticeKind.WeaponLevelUp => (FeedKind.WeaponLevelUp, $"{notice.Weapon?.DisplayName} level {notice.Value}"),
NoticeKind.TierCompleted => (FeedKind.TierCompleted, $"{notice.ChallengeName} {PanelBuilder.Numeral(notice.Value)}"),
NoticeKind.FeatEarned => (FeedKind.FeatEarned, notice.Feat?.Name),
NoticeKind.ContractCompleted => (FeedKind.ContractCompleted, notice.Contract is { } contract ? ContractTitle(contract) : null),
_ => (FeedKind.SkillShot, null),
};

Expand All @@ -44,4 +46,16 @@ public static IReadOnlyList<FeedEntry> From(ProgressionResult result, ulong stea

return entries;
}

/// <summary>
/// Gets the feed title of a finished contract, such as <c>Daily contract: Get 20 kills with SMGs</c>.
/// </summary>
/// <param name="contract">The contract.</param>
/// <returns>The title.</returns>
public static string ContractTitle(ActiveContract contract)
{
Ensure.NotNull(contract);
string period = contract.Definition.Period == ContractPeriod.Weekly ? "Weekly" : "Daily";
return $"{period} contract: {contract.Definition.Description}";
}
}
1 change: 1 addition & 0 deletions CounterStrikeProgression.Core/Live/LiveContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal enum FeedKind
WeaponLevelUp,
TierCompleted,
FeatEarned,
ContractCompleted,
}

/// <summary>One line of the live feed.</summary>
Expand Down
15 changes: 14 additions & 1 deletion CounterStrikeProgression.Core/Live/ProgressChangeTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// </summary>
/// <param name="progress">The player's progress.</param>
/// <returns>The changed rows, or <see langword="null"/> when nothing changed.</returns>
public ProgressSnapshot? Changes(PlayerProgress progress)

Check warning on line 20 in CounterStrikeProgression.Core/Live/ProgressChangeTracker.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

Check warning on line 20 in CounterStrikeProgression.Core/Live/ProgressChangeTracker.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.
{
Ensure.NotNull(progress);

Expand Down Expand Up @@ -61,6 +61,17 @@
}
}

List<ContractRow> contracts = [];
foreach (ContractRow row in all.Contracts)
{
(string, string) key = (row.Period, row.Contract);
if (!last.Contracts.TryGetValue(key, out ContractRow? previous) || (previous != row))
{
contracts.Add(row);
last.Contracts[key] = row;
}
}

List<SeasonRow> seasons = [];
foreach (SeasonRow row in all.Seasons)
{
Expand All @@ -71,7 +82,7 @@
}
}

ProgressSnapshot changes = new(player, weapons, challenges) { Seasons = seasons };
ProgressSnapshot changes = new(player, weapons, challenges) { Contracts = contracts, Seasons = seasons };
return changes.IsEmpty ? null : changes;
}

Expand All @@ -95,5 +106,7 @@
public Dictionary<string, WeaponRow> Weapons { get; } = new(StringComparer.Ordinal);

public Dictionary<(string, string), ChallengeRow> Challenges { get; } = [];

public Dictionary<(string, string), ContractRow> Contracts { get; } = [];
}
}
Loading
Loading