MatchEngine.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Olympus
  5. {
  6. public enum MatchPhase { Lobby, Matching, Intro, Playing, Answer, Intermission, Result }
  7. public sealed class Rival
  8. {
  9. public RivalConfig config;
  10. public int score, roundRank;
  11. public double answerAt;
  12. public bool answered;
  13. }
  14. public sealed class MatchEngine
  15. {
  16. readonly GameConfig config;
  17. readonly PlayerStore store;
  18. readonly Random random;
  19. double phaseElapsed, roundElapsed;
  20. double hintBoost;
  21. int answeredCount;
  22. Question[] rounds;
  23. string matchId;
  24. public MatchPhase Phase { get; private set; } = MatchPhase.Lobby;
  25. public bool Paused { get; set; }
  26. public Realm Realm { get; private set; }
  27. public Rival[] Rivals { get; private set; }
  28. public Question Current => rounds == null ? null : rounds[Round];
  29. public int Round { get; private set; }
  30. public int Score { get; private set; }
  31. public int Correct { get; private set; }
  32. public int FirstAnswers { get; private set; }
  33. public int Hints { get; private set; }
  34. public int LastGain { get; private set; }
  35. public int LastRank { get; private set; }
  36. public int PreviousBest { get; private set; }
  37. public string Outcome { get; private set; }
  38. public string Clue => Current == null ? "" : new string(Current.Normalized.Select((c,i) => i < Hints ? c : '·').ToArray());
  39. public char[] Pool { get; private set; }
  40. public double TimeLeft { get; private set; }
  41. public double TimeTotal { get; private set; }
  42. public double Reveal { get; private set; }
  43. public int Joined => Math.Min(5, (int)(phaseElapsed / .62));
  44. public int FinalRank => Rules.Rank(Score, Rivals.Select(r => r.score));
  45. public int RevealStyle => (Round + Array.IndexOf(config.questions, Current)) % 3;
  46. public event Action Changed;
  47. public event Action<string> Feedback;
  48. public event Action<int,int> RivalAnswered;
  49. public MatchEngine(GameConfig config, PlayerStore store, Random random = null)
  50. { this.config = config; this.store = store; this.random = random ?? new Random(); }
  51. public bool Start(string realmId, bool restart = false)
  52. {
  53. if (!restart && Phase != MatchPhase.Lobby && Phase != MatchPhase.Result) return false;
  54. if (restart && (Realm == null || realmId != Realm.id || Phase == MatchPhase.Result || Phase == MatchPhase.Lobby)) return false;
  55. var realm = config.realms.First(r => r.id == realmId);
  56. if (!restart && !store.Enter(realm)) return false;
  57. Realm = realm; matchId = Guid.NewGuid().ToString("N");
  58. var all = config.questions.Where(q => q.realm == realmId).ToList();
  59. var unseen = all.Where(q => !store.Data.found.Contains(q.id)).ToList();
  60. var source = unseen.Count >= 5 ? unseen : all;
  61. Rules.Shuffle(source, random); rounds = source.Take(5).ToArray();
  62. Rivals = config.rivals.Select(c => new Rival {config = c}).ToArray();
  63. Round = 0; Score = Correct = FirstAnswers = 0; Paused = false;
  64. PreviousBest = store.Progress(realm.id).best;
  65. Move(MatchPhase.Matching); return true;
  66. }
  67. void Move(MatchPhase phase) { Phase = phase; phaseElapsed = 0; Changed?.Invoke(); }
  68. void Prepare()
  69. {
  70. TimeTotal = Rules.Duration(Current.answer); TimeLeft = TimeTotal;
  71. roundElapsed = hintBoost = 0; Reveal = 6; Hints = LastGain = LastRank = answeredCount = 0; Outcome = "";
  72. Pool = Rules.Pool(Current.answer, random);
  73. foreach (var r in Rivals)
  74. {
  75. r.answered = false; r.roundRank = 0;
  76. bool knows = random.NextDouble() < .34 + r.config.skill * .62 - Current.difficulty * .055;
  77. r.answerAt = knows ? Math.Max(1.4, TimeTotal * (.14 + (1 - r.config.skill) * .66 + random.NextDouble() * .16)) : double.PositiveInfinity;
  78. }
  79. Move(MatchPhase.Intro);
  80. }
  81. public void Tick(double delta)
  82. {
  83. if (Paused || delta <= 0 || Phase == MatchPhase.Intermission) return;
  84. phaseElapsed += delta;
  85. switch (Phase)
  86. {
  87. case MatchPhase.Matching: if (phaseElapsed >= 3.72) Prepare(); break;
  88. case MatchPhase.Intro: if (phaseElapsed >= 1.25) Move(MatchPhase.Playing); break;
  89. case MatchPhase.Playing:
  90. double active = Math.Min(delta, TimeLeft);
  91. roundElapsed += active; TimeLeft = Math.Max(0, TimeLeft - delta);
  92. foreach (var r in Rivals.OrderBy(r => r.answerAt))
  93. if (!r.answered && roundElapsed >= r.answerAt)
  94. {
  95. r.answered = true; r.roundRank = ++answeredCount;
  96. int gain = Rules.Round10(420 + 580 * Math.Max(0, 1 - r.answerAt / TimeTotal));
  97. r.score += gain;
  98. RivalAnswered?.Invoke(Array.IndexOf(Rivals,r),gain);
  99. Feedback?.Invoke("rival");
  100. }
  101. if (store.Data.autoReveal) Reveal = Math.Max(Reveal, Math.Min(96, 6 + 84 * (1 - TimeLeft / TimeTotal) + hintBoost));
  102. if (TimeLeft <= 0) EndRound("Time's up");
  103. break;
  104. case MatchPhase.Answer:
  105. if (phaseElapsed >= 1.5) { if (Round == 4) Finish(); else Move(MatchPhase.Intermission); }
  106. break;
  107. }
  108. }
  109. public void Continue() { if (Phase != MatchPhase.Intermission || Paused) return; Round++; Prepare(); }
  110. public bool Submit(string input)
  111. {
  112. if (Phase != MatchPhase.Playing || Paused || string.IsNullOrEmpty(Rules.Normalize(input))) return false;
  113. if (Rules.Normalize(input) != Current.Normalized)
  114. {
  115. TimeLeft = Math.Max(0, TimeLeft - 2); Feedback?.Invoke("wrong");
  116. if (TimeLeft <= 0) EndRound("Time's up"); return false;
  117. }
  118. LastRank = answeredCount + 1; LastGain = Rules.Score(TimeLeft, TimeTotal, LastRank);
  119. Score += LastGain; Correct++; if (LastRank == 1) FirstAnswers++;
  120. store.Found(Current.id); Feedback?.Invoke("correct"); EndRound("Correct"); return true;
  121. }
  122. public bool CanUse(Item item)
  123. {
  124. if (Phase != MatchPhase.Playing || Paused || store.Data.items[(int)item] <= 0) return false;
  125. switch (item)
  126. {
  127. case Item.Hint: return Hints < Math.Min(2, Current.Normalized.Length);
  128. case Item.Time: return TimeLeft < TimeTotal - .001;
  129. case Item.Reveal: return Reveal < 97;
  130. default: return true;
  131. }
  132. }
  133. public bool Use(Item item)
  134. {
  135. if (!CanUse(item) || !store.Consume(item)) return false;
  136. switch (item)
  137. {
  138. case Item.Hint: Hints++; hintBoost += 12; Reveal = Math.Min(97, Reveal + 12); break;
  139. case Item.Time: TimeLeft = Math.Min(TimeTotal, TimeLeft + 5); break;
  140. case Item.Reveal: Reveal = Math.Min(97, Reveal + 15); break;
  141. case Item.Skip: EndRound("Skipped"); break;
  142. }
  143. Feedback?.Invoke("item"); Changed?.Invoke(); return true;
  144. }
  145. void EndRound(string outcome)
  146. {
  147. Outcome = outcome; Reveal = 100;
  148. Move(MatchPhase.Answer);
  149. }
  150. void Finish()
  151. {
  152. store.Settle(matchId, Realm, Score, FinalRank, config);
  153. Move(MatchPhase.Result); Feedback?.Invoke("result");
  154. }
  155. public void Quit() { Paused = false; Move(MatchPhase.Lobby); }
  156. }
  157. }