GameConfig.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Olympus
  5. {
  6. [Serializable] public class Question
  7. {
  8. public string id, realm, answer, image;
  9. public int difficulty;
  10. public string Normalized => Rules.Normalize(answer);
  11. }
  12. [Serializable] public class Realm
  13. {
  14. public string id, name, subtitle;
  15. public int fee;
  16. }
  17. [Serializable] public class RivalConfig
  18. {
  19. public string name;
  20. public double skill;
  21. public int avatar;
  22. }
  23. [Serializable] public class GameConfig
  24. {
  25. public Question[] questions;
  26. public Realm[] realms;
  27. public RivalConfig[] rivals;
  28. public int[] itemPrices = {400, 500, 300, 600};
  29. public int[] prizes = {1000, 500, 200};
  30. public int[] checkinCoins = {200, 0, 300, 0, 400, 500, 600};
  31. public int[] checkinItems = {-1, 0, -1, 1, -1, -1, -1};
  32. public void Validate()
  33. {
  34. if (realms == null || realms.Length != 4 || questions == null || questions.Length != 40 || rivals == null || rivals.Length != 5)
  35. throw new InvalidOperationException("Expected four realms, 40 questions and five rivals.");
  36. if (questions.Select(q => q.id).Distinct().Count() != questions.Length) throw new InvalidOperationException("Duplicate question ID.");
  37. foreach (var realm in realms)
  38. if (questions.Count(q => q.realm == realm.id) != 10) throw new InvalidOperationException("Each realm needs ten questions.");
  39. foreach (var q in questions)
  40. if (q.Normalized.Length < 1 || q.Normalized.Length > 16 || string.IsNullOrEmpty(q.image)) throw new InvalidOperationException("Invalid question " + q.id);
  41. }
  42. }
  43. public enum Item { Hint, Time, Reveal, Skip }
  44. public static class Rules
  45. {
  46. public static readonly string[] ItemNames = {"Free Hint", "+5 Seconds", "Reveal +15%", "Skip Shield"};
  47. public static string Normalize(string value) => new string((value ?? "").ToUpperInvariant().Where(c => c >= 'A' && c <= 'Z').ToArray());
  48. public static double Duration(string answer) => Math.Min(46, 24 + Math.Max(0, Normalize(answer).Length - 5) * 3);
  49. public static int Round10(double value) => (int)(Math.Floor(value / 10 + .5) * 10);
  50. public static int Score(double left, double total, int rank) => Round10(420 + 580 * Math.Max(0, Math.Min(1, left / total))) + (rank == 1 ? 200 : rank == 2 ? 120 : rank == 3 ? 60 : 0);
  51. public static int Rank(int score, IEnumerable<int> scores) => 1 + scores.Count(s => s > score);
  52. public static void Shuffle<T>(IList<T> values, Random random)
  53. {
  54. for (int i = values.Count - 1; i > 0; i--) { int j = random.Next(i + 1); var v = values[i]; values[i] = values[j]; values[j] = v; }
  55. }
  56. public static char[] Pool(string answer, Random random)
  57. {
  58. var list = Normalize(answer).ToList();
  59. int size = Math.Min(26, Math.Max(12, list.Count * 2 + 2));
  60. list.Add("AEIOU"[random.Next(5)]);
  61. while (list.Count < size) list.Add((char)('A' + random.Next(26)));
  62. Shuffle(list, random);
  63. return list.ToArray();
  64. }
  65. // Exact occurrence mapping: typing and tapping use the same multiset.
  66. public static int[] UsedTiles(string input, char[] pool)
  67. {
  68. var used = new List<int>();
  69. foreach (char c in Normalize(input))
  70. {
  71. for (int i = 0; i < pool.Length; i++) if (pool[i] == c && !used.Contains(i)) { used.Add(i); break; }
  72. }
  73. return used.ToArray();
  74. }
  75. }
  76. }