using System;
using System.Collections.Generic;
using Steamworks;
using UnityEngine;
namespace GrassCutter
{
public class Award
{
/// English patterns; {0} in them is (as money when ).
public string id, nameEn, descEn;
public double value;
public bool money;
public Func done;
object Arg => money ? UIK.Money(value) : (object)value;
public string name => Loc.F(nameEn, Arg);
public string desc => Loc.F(descEn, Arg);
}
///
/// The 33 awards: grass shipped, money, sectors, the planet, what was found and what was built. Each player earns them on
/// their own computer, and on Steam as well once the game has an AppID of its own: on the test AppID the calls go nowhere.
///
public static class Achievements
{
const float Every = 2f;
public static readonly List All = new List();
static readonly HashSet s_got = new HashSet();
static float s_next;
static bool s_read;
static bool Dev => CommandLine.Has("-autodemo") || CommandLine.Has("-mpdemo") || EndingCinematic.Preview;
static string Key => Dev ? "gc_awards_dev" : "gc_awards";
static Achievements()
{
foreach (double kg in new[] { 1d, 100d, 1000d, 10000d, 100000d })
Add($"SHIP_{kg:0}", kg < 2d ? Loc.N("First Delivery") : Loc.N("{0:N0} kg for Earth"), Loc.N("Ship {0:N0} kg of grass"), (t, p) => t.Shipped.Value >= kg, kg);
foreach (double cash in new[] { 1000d, 10000d, 100000d, 1000000d })
Add($"CASH_{cash:0}", Loc.N("{0} in the Bank"), Loc.N("Have {0} in the crew wallet"), (t, p) => t.Money.Value >= cash, cash, true);
foreach (int n in new[] { 1, 10, 100, 1000, 10000 })
Add($"SECTOR_{n}", n == 1 ? Loc.N("Clean Sweep") : Loc.N("{0:N0} Sectors"), Loc.N("Mow {0:N0} sectors clean"), (t, p) => t.Closed.Value >= n, n);
foreach (double pc in new[] { 0.01d, 0.1d, 1d, 10d, 50d, 100d })
Add($"PLANET_{pc * 100d:0}", pc >= 100d ? Loc.N("Herbaria Mown") : Loc.N("{0:0.##}% of Herbaria-7"), Loc.N("Mow {0:0.##}% of the planet"), (t, p) => t.Mown01 * 100d >= pc, pc);
Add("STAKE_1", Loc.N("Untangled"), Loc.N("Sort out a survey stake"), (t, p) => t.FoundCount(false) >= 1);
Add("STAKE_50", Loc.N("Fifty Stakes"), Loc.N("Sort out 50 survey stakes"), (t, p) => t.FoundCount(false) >= 50);
Add("STAKE_ALL", Loc.N("Every Last Stake"), Loc.N("Sort out all {0} survey stakes"), (t, p) => t.FoundCount(false) >= Landmarks.Stakes, Landmarks.Stakes);
Add("POINT_1", Loc.N("Surveyor"), Loc.N("Read a survey point"), (t, p) => t.FoundCount(true) >= 1);
Add("POINT_ALL", Loc.N("The Whole Map"), Loc.N("Read all {0} survey points"), (t, p) => t.FoundCount(true) >= Landmarks.Points, Landmarks.Points);
Add("STATION_1", Loc.N("Not Alone Out Here"), Loc.N("Find a far station"), (t, p) => t.Opened.Count >= 1);
Add("STATION_5", Loc.N("Well Travelled"), Loc.N("Find 5 far stations"), (t, p) => t.Opened.Count >= 5);
Add("STATION_ALL", Loc.N("All Twelve"), Loc.N("Find every station"), (t, p) => t.Opened.Count >= Stations.Count - 1);
Add("TOOL_SCYTHE", Loc.N("A Proper Scythe"), Loc.N("Own the Scythe"), (t, p) => p.Owns(ToolId.Scythe));
Add("TOOL_ALL", Loc.N("Full Tool Belt"), Loc.N("Own every blade on the wall"), (t, p) => p.Owns(ToolId.WideScythe) && p.Owns(ToolId.LongScythe) && p.Owns(ToolId.Scythe));
Add("ROBOT_1", Loc.N("Let the Robot Do It"), Loc.N("Build a robot dock"), (t, p) => Mowbot.All.Count >= 1);
Add("ROBOT_10", Loc.N("Robot Crew"), Loc.N("Have 10 robots at work"), (t, p) => Mowbot.All.Count >= 10);
Add("REAPER", Loc.N("Fear the Reaper"), Loc.N("Set a Reaper loose"), (t, p) => Mowbot.All.Exists(m => m is Reaper));
}
static void Add(string id, string name, string desc, Func done, double value = 0d, bool money = false) => All.Add(new Award { id = id, nameEn = name, descEn = desc, done = done, value = value, money = money });
static void Read()
{
if (s_read) return;
s_read = true;
foreach (var id in PlayerPrefs.GetString(Key, "").Split(',')) if (id.Length > 0) s_got.Add(id);
}
public static bool Has(Award a)
{
Read();
return s_got.Contains(a.id);
}
public static int Earned
{
get
{
Read();
return s_got.Count;
}
}
/// The demo starts with a clean slate.
public static void Forget()
{
s_got.Clear();
s_read = true;
PlayerPrefs.DeleteKey(Key);
}
public static void Tick()
{
if (Time.time < s_next) return;
s_next = Time.time + Every;
var ts = TeamState.Instance;
var me = PlayerAgent.Local;
if (!ts || !me || Demo.On) return;
Read();
foreach (var a in All)
{
if (s_got.Contains(a.id) || !a.done(ts, me)) continue;
s_got.Add(a.id);
PlayerPrefs.SetString(Key, string.Join(",", s_got));
PlayerPrefs.Save();
Hud.Toast(Loc.F("Achievement: {0}", a.name), Hud.ToastKind.Info);
Sfx.Play("milestone", 0.7f);
// test runs earn them on this computer only, never on the player's Steam account
if (SteamManager.Initialized && !Dev)
{
SteamUserStats.SetAchievement(a.id);
SteamUserStats.StoreStats();
}
}
}
}
}