using System; using System.Collections.Generic; using System.Linq; namespace Olympus { public enum MatchPhase { Lobby, Matching, Intro, Playing, Answer, Intermission, Result } public sealed class Rival { public RivalConfig config; public int score, roundRank; public double answerAt; public bool answered; } public sealed class MatchEngine { readonly GameConfig config; readonly PlayerStore store; readonly Random random; double phaseElapsed, roundElapsed; double hintBoost; int answeredCount; Question[] rounds; string matchId; public MatchPhase Phase { get; private set; } = MatchPhase.Lobby; public bool Paused { get; set; } public Realm Realm { get; private set; } public Rival[] Rivals { get; private set; } public Question Current => rounds == null ? null : rounds[Round]; public int Round { get; private set; } public int Score { get; private set; } public int Correct { get; private set; } public int FirstAnswers { get; private set; } public int Hints { get; private set; } public int LastGain { get; private set; } public int LastRank { get; private set; } public int PreviousBest { get; private set; } public string Outcome { get; private set; } public string Clue => Current == null ? "" : new string(Current.Normalized.Select((c,i) => i < Hints ? c : 'ยท').ToArray()); public char[] Pool { get; private set; } public double TimeLeft { get; private set; } public double TimeTotal { get; private set; } public double Reveal { get; private set; } public int Joined => Math.Min(5, (int)(phaseElapsed / .62)); public int FinalRank => Rules.Rank(Score, Rivals.Select(r => r.score)); public int RevealStyle => (Round + Array.IndexOf(config.questions, Current)) % 3; public event Action Changed; public event Action Feedback; public event Action RivalAnswered; public MatchEngine(GameConfig config, PlayerStore store, Random random = null) { this.config = config; this.store = store; this.random = random ?? new Random(); } public bool Start(string realmId, bool restart = false) { if (!restart && Phase != MatchPhase.Lobby && Phase != MatchPhase.Result) return false; if (restart && (Realm == null || realmId != Realm.id || Phase == MatchPhase.Result || Phase == MatchPhase.Lobby)) return false; var realm = config.realms.First(r => r.id == realmId); if (!restart && !store.Enter(realm)) return false; Realm = realm; matchId = Guid.NewGuid().ToString("N"); var all = config.questions.Where(q => q.realm == realmId).ToList(); var unseen = all.Where(q => !store.Data.found.Contains(q.id)).ToList(); var source = unseen.Count >= 5 ? unseen : all; Rules.Shuffle(source, random); rounds = source.Take(5).ToArray(); Rivals = config.rivals.Select(c => new Rival {config = c}).ToArray(); Round = 0; Score = Correct = FirstAnswers = 0; Paused = false; PreviousBest = store.Progress(realm.id).best; Move(MatchPhase.Matching); return true; } void Move(MatchPhase phase) { Phase = phase; phaseElapsed = 0; Changed?.Invoke(); } void Prepare() { TimeTotal = Rules.Duration(Current.answer); TimeLeft = TimeTotal; roundElapsed = hintBoost = 0; Reveal = 6; Hints = LastGain = LastRank = answeredCount = 0; Outcome = ""; Pool = Rules.Pool(Current.answer, random); foreach (var r in Rivals) { r.answered = false; r.roundRank = 0; bool knows = random.NextDouble() < .34 + r.config.skill * .62 - Current.difficulty * .055; r.answerAt = knows ? Math.Max(1.4, TimeTotal * (.14 + (1 - r.config.skill) * .66 + random.NextDouble() * .16)) : double.PositiveInfinity; } Move(MatchPhase.Intro); } public void Tick(double delta) { if (Paused || delta <= 0 || Phase == MatchPhase.Intermission) return; phaseElapsed += delta; switch (Phase) { case MatchPhase.Matching: if (phaseElapsed >= 3.72) Prepare(); break; case MatchPhase.Intro: if (phaseElapsed >= 1.25) Move(MatchPhase.Playing); break; case MatchPhase.Playing: double active = Math.Min(delta, TimeLeft); roundElapsed += active; TimeLeft = Math.Max(0, TimeLeft - delta); foreach (var r in Rivals.OrderBy(r => r.answerAt)) if (!r.answered && roundElapsed >= r.answerAt) { r.answered = true; r.roundRank = ++answeredCount; int gain = Rules.Round10(420 + 580 * Math.Max(0, 1 - r.answerAt / TimeTotal)); r.score += gain; RivalAnswered?.Invoke(Array.IndexOf(Rivals,r),gain); Feedback?.Invoke("rival"); } if (store.Data.autoReveal) Reveal = Math.Max(Reveal, Math.Min(96, 6 + 84 * (1 - TimeLeft / TimeTotal) + hintBoost)); if (TimeLeft <= 0) EndRound("Time's up"); break; case MatchPhase.Answer: if (phaseElapsed >= 1.5) { if (Round == 4) Finish(); else Move(MatchPhase.Intermission); } break; } } public void Continue() { if (Phase != MatchPhase.Intermission || Paused) return; Round++; Prepare(); } public bool Submit(string input) { if (Phase != MatchPhase.Playing || Paused || string.IsNullOrEmpty(Rules.Normalize(input))) return false; if (Rules.Normalize(input) != Current.Normalized) { TimeLeft = Math.Max(0, TimeLeft - 2); Feedback?.Invoke("wrong"); if (TimeLeft <= 0) EndRound("Time's up"); return false; } LastRank = answeredCount + 1; LastGain = Rules.Score(TimeLeft, TimeTotal, LastRank); Score += LastGain; Correct++; if (LastRank == 1) FirstAnswers++; store.Found(Current.id); Feedback?.Invoke("correct"); EndRound("Correct"); return true; } public bool CanUse(Item item) { if (Phase != MatchPhase.Playing || Paused || store.Data.items[(int)item] <= 0) return false; switch (item) { case Item.Hint: return Hints < Math.Min(2, Current.Normalized.Length); case Item.Time: return TimeLeft < TimeTotal - .001; case Item.Reveal: return Reveal < 97; default: return true; } } public bool Use(Item item) { if (!CanUse(item) || !store.Consume(item)) return false; switch (item) { case Item.Hint: Hints++; hintBoost += 12; Reveal = Math.Min(97, Reveal + 12); break; case Item.Time: TimeLeft = Math.Min(TimeTotal, TimeLeft + 5); break; case Item.Reveal: Reveal = Math.Min(97, Reveal + 15); break; case Item.Skip: EndRound("Skipped"); break; } Feedback?.Invoke("item"); Changed?.Invoke(); return true; } void EndRound(string outcome) { Outcome = outcome; Reveal = 100; Move(MatchPhase.Answer); } void Finish() { store.Settle(matchId, Realm, Score, FinalRank, config); Move(MatchPhase.Result); Feedback?.Invoke("result"); } public void Quit() { Paused = false; Move(MatchPhase.Lobby); } } }