Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

3.6 KB
Raw Blame
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Statecraft.Game.UI
{
    /// <summary>The advisors' lines in the settings file (spec 15; SettingsStore: presentation only, never the sim or the
    /// save): the help level, the messages seen (one line, advisor_seen=a,b,c), the opening chain's mark and the new-player course's. The marks
    /// are also kept in memory, so a failed write (logged) or no file at all (automated runs) only costs persistence.</summary>
    public sealed class AdvisorSettings
    {
        public const string LevelKey = "advisor_level", SeenKey = "advisor_seen", OpeningKey = "advisor_opening", CourseKey = "advisor_course";
        /// <summary>The legacy first-turn guide's saved line (spec 9.3: honoured, so a player who saw the guide gets no chain by itself).</summary>
        public const string GuideSeenKey = "first_turn_guide";
        /// <summary>Spec 6, from the chattiest; a message shows when its level's index (AdvisorCatalog.Levels) is at least this one's.</summary>
        public static readonly string[] Levels = { "new", "experienced", "warnings", "off" };

        readonly SettingsStore? store;
        readonly SortedSet<string> seen = new SortedSet<string>(StringComparer.Ordinal);
        bool openingSeen, courseOpen;

        public AdvisorSettings(SettingsStore? store)
        {
            this.store = store;
            var stored = store?.Read(LevelKey);
            Level = Array.IndexOf(Levels, stored) >= 0 ? stored! : Levels[0];
            foreach (var id in (store?.Read(SeenKey) ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) seen.Add(id);
            openingSeen = store?.Read(OpeningKey) == "seen" || store?.Read(GuideSeenKey) == "seen";
            courseOpen = store?.Read(CourseKey) == "open";
        }

        /// <summary>A settings file exists to remember the marks in (none in automated runs and EditMode tests).</summary>
        public bool HasFile => store != null;
        public string Level { get; private set; }
        public int LevelRank => Array.IndexOf(Levels, Level);
        /// <summary>The help level changed (Settings): the popup applies it at once (spec 6).</summary>
        public event Action? LevelChanged;

        public void SetLevel(string level)
        {
            if (Array.IndexOf(Levels, level) < 0) throw new ArgumentException("unknown help level: " + level, nameof(level));
            if (level == Level) return;
            Level = level;
            Write(LevelKey, level);
            LevelChanged?.Invoke();
        }

        public bool Seen(string id) => seen.Contains(id);

        public void MarkSeen(string id)
        {
            if (!seen.Add(id)) return;
            Write(SeenKey, string.Join(",", seen));
        }

        public bool OpeningSeen => openingSeen;

        public void MarkOpeningSeen()
        {
            openingSeen = true;
            Write(OpeningKey, "seen");
        }

        /// <summary>The chain opened by itself on this player's first game (#2933), so the course's rows may show; a returning
        /// player, whose chain mark predates this line, has none.</summary>
        public bool CourseOpen => courseOpen;

        public void MarkCourseOpen()
        {
            courseOpen = true;
            Write(CourseKey, "open");
        }

        void Write(string key, string value)
        {
            if (store == null) return;
            try { store.Write(key, value); }
            catch (Exception e) { Debug.LogWarning($"The advisors could not save {key}: {e.Message}"); }
        }
    }
}