summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking/Snapshots/PlayerSnapshot.cs
blob: e113d27c2239c3b7d5f031b852a76de796956317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI.Enums;
using StardewModdingAPI.Events;
using StardewValley;

namespace StardewModdingAPI.Framework.StateTracking.Snapshots
{
    /// <summary>A frozen snapshot of a tracked player.</summary>
    internal class PlayerSnapshot
    {
        /*********
        ** Fields
        *********/
        /// <summary>An empty item list diff.</summary>
        private readonly SnapshotItemListDiff EmptyItemListDiff = new(Array.Empty<Item>(), Array.Empty<Item>(), Array.Empty<ItemStackSizeChange>());


        /*********
        ** Accessors
        *********/
        /// <summary>The player being tracked.</summary>
        public Farmer Player { get; }

        /// <summary>The player's current location.</summary>
        public SnapshotDiff<GameLocation> Location { get; } = new();

        /// <summary>Tracks changes to the player's skill levels.</summary>
        public IDictionary<SkillType, SnapshotDiff<int>> Skills { get; } =
            Enum
                .GetValues(typeof(SkillType))
                .Cast<SkillType>()
                .ToDictionary(skill => skill, _ => new SnapshotDiff<int>());

        /// <summary>Get a list of inventory changes.</summary>
        public SnapshotItemListDiff Inventory { get; private set; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="player">The player being tracked.</param>
        public PlayerSnapshot(Farmer player)
        {
            this.Player = player;
        }

        /// <summary>Update the tracked values.</summary>
        /// <param name="watcher">The player watcher to snapshot.</param>
        public void Update(PlayerTracker watcher)
        {
            this.Location.Update(watcher.LocationWatcher);
            foreach (var pair in this.Skills)
                pair.Value.Update(watcher.SkillWatchers[pair.Key]);

            this.Inventory = watcher.TryGetInventoryChanges(out SnapshotItemListDiff itemChanges)
                ? itemChanges
                : this.EmptyItemListDiff;
        }
    }
}