Level.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Timers;
  3. namespace XamarinInsights
  4. {
  5. public class Level : BaseModel
  6. {
  7. const double StartingTime = 10000;
  8. int currentLevel = 0;
  9. public int CurrentLevel {
  10. get {
  11. return currentLevel;
  12. }
  13. set {
  14. ProcPropertyChanged (ref currentLevel, value);
  15. }
  16. }
  17. string currentCombination;
  18. public string CurrentCombination {
  19. get {
  20. return currentCombination;
  21. }
  22. set {
  23. ProcPropertyChanged (ref currentCombination, value);
  24. }
  25. }
  26. string combinationSoFar;
  27. public string CombinationSoFar {
  28. get {
  29. return combinationSoFar;
  30. }
  31. private set {
  32. ProcPropertyChanged (ref combinationSoFar, value);
  33. }
  34. }
  35. double timeRemaining;
  36. public double TimeRemaining {
  37. get {
  38. return timeRemaining;
  39. }
  40. set {
  41. if (ProcPropertyChanged (ref timeRemaining, value))
  42. TimeRemainingDisplay = string.Format ("Time Remaining: {0}", timeRemaining / 1000);
  43. }
  44. }
  45. string timeRemainingDisplay;
  46. public string TimeRemainingDisplay {
  47. get {
  48. return timeRemainingDisplay;
  49. }
  50. set {
  51. ProcPropertyChanged (ref timeRemainingDisplay, value);
  52. }
  53. }
  54. public void PressKey (string key)
  55. {
  56. //TODO: Log key press
  57. Analytics.LogKeyPress (key);
  58. CombinationSoFar += key;
  59. if (CombinationSoFar == CurrentCombination)
  60. NewLevel ();
  61. if (!CurrentCombination.StartsWith (CombinationSoFar))
  62. WrongKey ();
  63. }
  64. Random random = new Random ();
  65. public void NewLevel ()
  66. {
  67. StopTimer ();
  68. CurrentLevel++;
  69. CombinationSoFar = "";
  70. CurrentCombination = random.Next (0, 9999).ToString ("0000");
  71. StartTimer ();
  72. //TODO: Log start level
  73. Analytics.LogNewLevel (CurrentLevel, CurrentCombination, TimeRemaining);
  74. }
  75. Timer timer;
  76. public void StartTimer ()
  77. {
  78. ResetTimer ();
  79. }
  80. public void StopTimer ()
  81. {
  82. if (timer == null)
  83. return;
  84. timer.Stop ();
  85. }
  86. const double timerTick = 100;
  87. void ResetTimer ()
  88. {
  89. TimeRemaining = StartingTime - (1000 * CurrentLevel);
  90. if (timer == null) {
  91. timer = new Timer (timerTick) {
  92. AutoReset = true,
  93. };
  94. timer.Elapsed += (sender, e) => Tick ();
  95. }
  96. timer.Start ();
  97. }
  98. void Tick ()
  99. {
  100. TimeRemaining -= timerTick;
  101. if (TimeRemaining <= 0)
  102. App.RunOnMainThread (OutOfTime);
  103. }
  104. void OutOfTime ()
  105. {
  106. throw new Exception ("You ran out of time!!!!");
  107. }
  108. void WrongKey ()
  109. {
  110. throw new Exception ("You pressed a wrong key :(");
  111. }
  112. }
  113. }