WeeklyAndPoolAudit.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using TMPro;
  9. using UnityEngine;
  10. using UnityEngine.EventSystems;
  11. using UnityEngine.UI;
  12. namespace Olympus.Editor
  13. {
  14. public static class WeeklyAndPoolAudit
  15. {
  16. const BindingFlags Flags=BindingFlags.Instance|BindingFlags.NonPublic;
  17. static T Field<T>(OlympusGame app,string key)=>(T)typeof(OlympusGame).GetField(key,Flags).GetValue(app);
  18. static void Call(OlympusGame app,string method,params object[] args)=>typeof(OlympusGame).GetMethod(method,Flags).Invoke(app,args);
  19. static OlympusGame App(){var app=UnityEngine.Object.FindFirstObjectByType<OlympusGame>();if(!Application.isPlaying||!app||!app.IsEditorPreviewSave)throw new Exception("Use isolated Art Review save");return app;}
  20. static void Require(bool valid,string text){if(!valid)throw new Exception(text);}
  21. static SuiteView Modal(OlympusGame app)=>Field<RectTransform>(app,"modal").GetComponentInChildren<SuiteView>();
  22. static void OpenQuestion(OlympusGame app,Question q,bool blueberry=false)
  23. {
  24. app.ShowDesignPage("Lobby");var game=Field<MatchEngine>(app,"game");Require(game.Start(q.realm),"Cannot enter "+q.realm);
  25. typeof(MatchEngine).GetField("rounds",Flags).SetValue(game,new[]{q,q,q,q,q});
  26. if(blueberry)
  27. {
  28. int seed=Enumerable.Range(0,10000).First(s=>{var p=Rules.Pool(q.answer,new System.Random(s));return p.Count(c=>c=='U')==1&&Array.IndexOf(p,'U')>=18;});
  29. typeof(MatchEngine).GetField("random",Flags).SetValue(game,new System.Random(seed));
  30. }
  31. game.Tick(4);game.Tick(2);app.ArtReviewMode=true;Field<TextMeshProUGUI>(app,"toast").gameObject.SetActive(false);
  32. }
  33. static string BoardSignature(ReferenceAView v)=>string.Join("|",Enumerable.Range(0,3).Select(i=>v.leaderboardNames[i].text+":"+v.leaderboardScores[i].text));
  34. static ReferenceAView CompareBoard(OlympusGame app,ReferenceAView lobby)
  35. {
  36. lobby.FindButton("Leaderboard").onClick.Invoke();var popup=Modal(app);
  37. Require(Field<string>(app,"currentPanel")=="Weekly","lobby opens the weekly tab");
  38. for(int i=0;i<3;i++)
  39. {
  40. Require(lobby.leaderboardNames[i].text==popup.Text("Podium name "+i).text,"weekly name mismatch "+i);
  41. Require(lobby.leaderboardScores[i].text==popup.Text("Podium score "+i).text,"weekly score mismatch "+i);
  42. Require(lobby.leaderboardAvatars[i].sprite==popup.Image("Podium portrait "+i).sprite,"weekly avatar mismatch "+i);
  43. }
  44. popup.Button("Close").onClick.Invoke();
  45. return Field<ReferenceAView>(app,"referenceView");
  46. }
  47. public static string RunUi()
  48. {
  49. var app=App();var store=Field<PlayerStore>(app,"store");store.Reset();app.ShowDesignPage("Lobby");
  50. var lobby=CompareBoard(app,Field<ReferenceAView>(app,"referenceView"));string initial=BoardSignature(lobby);
  51. Call(app,"RefreshLobbyLeaderboard",true);Require(BoardSignature(lobby)==initial,"weekly data rerolled within the same week");
  52. store.Data.name="Weekly champion";store.Data.avatar=3;store.Data.weeklyScore=15000;Call(app,"RefreshLobbyLeaderboard",true);lobby=CompareBoard(app,lobby);
  53. Require(lobby.leaderboardNames[0].text==store.Data.name&&lobby.leaderboardAvatars[0].sprite.texture.name=="avatar_3","own updated weekly score and portrait did not reach lobby");
  54. store.Data.weeklyKey="2000-01-01";store.Data.weeklyScore=20000;Call(app,"RefreshLobbyLeaderboard",true);lobby=CompareBoard(app,lobby);
  55. Require(store.Data.weeklyScore==0&&BoardSignature(lobby)==initial,"week rollover did not refresh lobby and popup together");
  56. var cfg=Field<GameConfig>(app,"config");OpenQuestion(app,cfg.questions[0]);var game=Field<MatchEngine>(app,"game");
  57. game.Submit(game.Current.answer);game.Tick(1.6);var round=Field<SuiteView>(app,"suiteView");
  58. Require(round.GetComponentsInChildren<Button>().Length==2&&round.Button("Continue").gameObject.activeInHierarchy&&round.Button("Quit").gameObject.activeInHierarchy,"intermission must have Continue and QUIT");
  59. game.Tick(3600);Require(game.Phase==MatchPhase.Intermission&&game.Round==0,"standings advanced without Continue");
  60. round.Button("Continue").onClick.Invoke();round.Button("Continue").onClick.Invoke();Require(game.Round==1&&game.Phase==MatchPhase.Intro,"Continue did not advance exactly one round");app.ArtReviewMode=true;
  61. game.Tick(1.3);
  62. for(int n=1;n<5;n++){game.Submit(game.Current.answer);game.Tick(1.6);if(n<4){game.Continue();game.Tick(1.3);}}
  63. var result=Field<SuiteView>(app,"suiteView");Require(game.Phase==MatchPhase.Result&&result.GetComponentsInChildren<Button>().Length==2&&!result.Button("Continue").gameObject.activeSelf,"final result does not have exactly two actions");
  64. Require(result.Button("Quit").gameObject.activeInHierarchy&&result.Text("Quit label").text=="QUIT","final result is missing QUIT");
  65. app.ShowDesignPage("Result");Field<SuiteView>(app,"suiteView").Button("Play again").onClick.Invoke();Require(game.Phase==MatchPhase.Lobby,"PLAY AGAIN does not return to realm choice");app.ArtReviewMode=true;
  66. string report="PASS intermission has Continue and QUIT, waits indefinitely for Continue\nPASS Continue advances exactly once; final has PLAY AGAIN and QUIT\nPASS lobby and weekly popup match names, scores and avatars\nPASS player score/profile changes and week rollover refresh both views consistently\n";
  67. Directory.CreateDirectory("docs/验证");File.WriteAllText("docs/验证/轮间与周榜验收.txt",report);return report;
  68. }
  69. static void CheckHit(Button b,bool click=false)
  70. {
  71. var r=(RectTransform)b.transform;var point=RectTransformUtility.WorldToScreenPoint(null,r.TransformPoint(r.rect.center));
  72. Require(point.x>=0&&point.x<=Screen.width&&point.y>=0&&point.y<=Screen.height,"tile outside viewport: "+b.name);
  73. var data=new PointerEventData(EventSystem.current){position=point,button=PointerEventData.InputButton.Left};var hits=new System.Collections.Generic.List<RaycastResult>();EventSystem.current.RaycastAll(data,hits);
  74. Require(hits.Count>0&&hits[0].gameObject.GetComponentInParent<Button>()==b,"covered tile: "+b.name);
  75. if(click){Require(b.interactable,"disabled tile: "+b.name);ExecuteEvents.Execute(b.gameObject,data,ExecuteEvents.pointerClickHandler);}
  76. }
  77. static string InspectQuestion(OlympusGame app,Question q)
  78. {
  79. var v=Field<ReferenceAView>(app,"referenceView");var game=Field<MatchEngine>(app,"game");
  80. Require(v.question.sprite&&v.question.sprite.texture.name==q.id,"wrong picture bound: "+q.id);
  81. Require(v.letterButtons.Count(b=>b.gameObject.activeSelf)==game.Pool.Length,"candidate count differs from pool: "+q.id);
  82. var displayed=new StringBuilder();
  83. for(int i=0;i<game.Pool.Length;i++)
  84. {
  85. var b=v.letterButtons[i];var label=b.GetComponentInChildren<TextMeshProUGUI>();label.ForceMeshUpdate();
  86. var tileRect=((RectTransform)b.transform).rect;Require(Mathf.Abs(tileRect.width-tileRect.height)<.1f&&tileRect.width>=44,"distorted or undersized tile: "+q.id+" #"+i);
  87. Require(label.text==game.Pool[i].ToString()&&label.textInfo.characterCount==1&&label.textInfo.characterInfo[0].isVisible&&label.textInfo.characterInfo[0].character==game.Pool[i],"missing/wrong rendered glyph: "+q.id+" #"+i);
  88. CheckHit(b);displayed.Append(label.text);
  89. }
  90. Require(q.Normalized.GroupBy(c=>c).All(g=>displayed.ToString().Count(c=>c==g.Key)>=g.Count()),"displayed pool cannot spell "+q.answer);
  91. if(q.id=="food_05")
  92. {
  93. int u=Array.IndexOf(game.Pool,'U');Require(u>=18&&game.Pool.Count(c=>c=='U')==1,"BLUEBERRY fixture must have sole U in last row");
  94. CheckHit(v.letterButtons[u],true);Require(v.answerText.text=="U"&&!v.letterButtons[u].interactable,"U not selectable");CheckHit(v.FindButton("Delete"),true);
  95. Require(v.letterButtons[u].interactable&&Field<TileAnswerSelection>(app,"selectedAnswer").Count==0,"X did not return U");
  96. }
  97. return displayed.ToString();
  98. }
  99. static void SelectThroughPointers(OlympusGame app,Question q)
  100. {
  101. var v=Field<ReferenceAView>(app,"referenceView");var game=Field<MatchEngine>(app,"game");
  102. foreach(char letter in q.Normalized)
  103. {
  104. int i=Enumerable.Range(0,game.Pool.Length).First(n=>game.Pool[n]==letter&&v.letterButtons[n].interactable);CheckHit(v.letterButtons[i],true);
  105. }
  106. Require(v.answerText.text==q.Normalized,"pointer clicks assembled wrong answer: "+q.id);
  107. }
  108. public static string QuestionStatus {get;private set;}="Not started";
  109. public static string StartQuestions()
  110. {
  111. Require(!QuestionStatus.StartsWith("Running"),"Question audit already running");var app=App();var store=Field<PlayerStore>(app,"store");store.Reset();store.Data.coins=100000;app.ArtReviewMode=true;
  112. app.StartCoroutine(QuestionSequence(app));return QuestionStatus;
  113. }
  114. static IEnumerator QuestionSequence(OlympusGame app)
  115. {
  116. var cfg=Field<GameConfig>(app,"config");string error=null;var report=new StringBuilder("id,answer,length,pool_count,rendered_pool,result\n");
  117. var prototype=Regex.Matches(File.ReadAllText("docs/Guess_of_Olympus_UI演示.html"),@"\{a:'([^']+)',\s*k:'([^']+)',\s*d:(\d+)").Cast<Match>().ToDictionary(m=>m.Groups[2].Value,m=>m);
  118. string[] countryKeys={"country_t01","country_t02","country_t03","country_t04","country_t05","country_d09","country_d10","country_d08","country_d06","country_d07"};
  119. Directory.CreateDirectory("docs/运行截图/WeeklyAndPool");
  120. for(int n=0;n<cfg.questions.Length;n++)
  121. {
  122. var q=cfg.questions[n];QuestionStatus="Running "+(n+1)+" / 40: "+q.id;
  123. try
  124. {
  125. string key=q.realm=="country"?countryKeys[int.Parse(q.id.Substring(8))-1]:q.id;
  126. Require(prototype.ContainsKey(key)&&prototype[key].Groups[1].Value==q.answer&&int.Parse(prototype[key].Groups[3].Value)==q.difficulty,"prototype answer/difficulty mismatch: "+q.id);
  127. OpenQuestion(app,q,q.id=="food_05");
  128. }catch(Exception e){error=e.Message;}
  129. if(error!=null)break;yield return null;yield return null;
  130. string pool="";try{pool=InspectQuestion(app,q);}catch(Exception e){error=e.Message;}
  131. if(error!=null)break;
  132. if(q.id=="food_05"){ScreenCapture.CaptureScreenshot("docs/运行截图/WeeklyAndPool/Blueberry_U.png");yield return null;}
  133. try{SelectThroughPointers(app,q);}catch(Exception e){error=e.Message;}
  134. if(error!=null)break;
  135. if(q.id=="food_05"){ScreenCapture.CaptureScreenshot("docs/运行截图/WeeklyAndPool/Blueberry_Selected.png");yield return null;}
  136. try
  137. {
  138. CheckHit(Field<ReferenceAView>(app,"referenceView").FindButton("OK"),true);
  139. var game=Field<MatchEngine>(app,"game");Require(game.Phase==MatchPhase.Answer&&game.LastGain>0,"answer rejected: "+q.id);
  140. report.AppendLine(q.id+","+q.answer+","+q.Normalized.Length+","+pool.Length+","+pool+",PASS");
  141. }catch(Exception e){error=e.Message;}
  142. if(error!=null)break;
  143. }
  144. QuestionStatus=error==null?"PASS all 40 questions: prototype mapping, actual rendered glyphs, required repeated letters, pointer hits, submission; BLUEBERRY sole U in row 3 selectable and restored by X":"FAIL "+error;
  145. Directory.CreateDirectory("docs/验证");File.WriteAllText("docs/验证/全题库屏幕点选验收.csv",report.ToString());File.WriteAllText("docs/验证/全题库屏幕点选结果.txt",QuestionStatus);
  146. app.ShowDesignPage("Lobby");
  147. }
  148. public static string IntermissionPreview()
  149. {
  150. var app=App();OpenQuestion(app,Field<GameConfig>(app,"config").questions[0]);var game=Field<MatchEngine>(app,"game");game.Submit(game.Current.answer);game.Tick(1.6);game.Paused=true;Field<TextMeshProUGUI>(app,"toast").gameObject.SetActive(false);return "Intermission frozen for screenshot";
  151. }
  152. }
  153. }