TileAnswerAudit.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using TMPro;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. using UnityEngine.UI;
  10. namespace Olympus.Editor
  11. {
  12. public static class TileAnswerAudit
  13. {
  14. const BindingFlags Flags=BindingFlags.Instance|BindingFlags.NonPublic;
  15. static T Field<T>(OlympusGame app,string key)=>(T)typeof(OlympusGame).GetField(key,Flags).GetValue(app);
  16. static void Refresh(OlympusGame app)=>typeof(OlympusGame).GetMethod("RefreshTrial",Flags).Invoke(app,null);
  17. static TileAnswerSelection Selection(OlympusGame app)=>Field<TileAnswerSelection>(app,"selectedAnswer");
  18. public static void SelectAnswer(OlympusGame app,string value)
  19. {
  20. var v=Field<ReferenceAView>(app,"referenceView");
  21. while(Selection(app).Count>0)
  22. {
  23. if(!v.FindButton("Delete").interactable)throw new Exception("Delete disabled during answer selection");
  24. v.FindButton("Delete").onClick.Invoke();
  25. }
  26. var game=Field<MatchEngine>(app,"game");
  27. foreach(char letter in Rules.Normalize(value))
  28. {
  29. int index=Enumerable.Range(0,game.Pool.Length).First(i=>game.Pool[i]==letter&&v.letterButtons[i].interactable);
  30. v.letterButtons[index].onClick.Invoke();
  31. }
  32. if(Selection(app).Value!=Rules.Normalize(value))throw new Exception("Tile clicks produced the wrong answer");
  33. }
  34. static void CheckHit(Button button)
  35. {
  36. var data=new PointerEventData(EventSystem.current){position=RectTransformUtility.WorldToScreenPoint(null,((RectTransform)button.transform).TransformPoint(((RectTransform)button.transform).rect.center))};
  37. var hits=new System.Collections.Generic.List<RaycastResult>();EventSystem.current.RaycastAll(data,hits);
  38. if(hits.Count==0||hits[0].gameObject.GetComponentInParent<Button>()!=button)throw new Exception("Button covered: "+button.name);
  39. }
  40. // Run after the newly opened page has rendered and registered its graphics.
  41. public static string PointerTargets()
  42. {
  43. var v=UnityEngine.Object.FindFirstObjectByType<ReferenceAView>();
  44. if(!v||!v.trial)throw new Exception("Open the trial page and let it render first.");
  45. foreach(var b in v.letterButtons.Where(b=>b.gameObject.activeSelf))CheckHit(b);
  46. CheckHit(v.FindButton("Delete"));CheckHit(v.FindButton("OK"));
  47. string report="PASS all "+v.letterButtons.Count(b=>b.gameObject.activeSelf)+" candidates, X and OK receive pointer hits\n";
  48. File.AppendAllText("docs/验证/候选字母交互验收.txt",report);return report;
  49. }
  50. public static string Run()
  51. {
  52. var app=UnityEngine.Object.FindFirstObjectByType<OlympusGame>();
  53. if(!Application.isPlaying||!app||!app.IsEditorPreviewSave)throw new Exception("Use isolated Art Review save.");
  54. var report=new StringBuilder();Action<bool,string> check=(ok,label)=>{if(!ok)throw new Exception("FAIL "+label);report.AppendLine("PASS "+label);};
  55. var game=Field<MatchEngine>(app,"game");var store=Field<PlayerStore>(app,"store");var config=Field<GameConfig>(app,"config");
  56. app.ShowPreview("Lobby");store.Reset();store.Data.letterPool=false;
  57. game.Start("olympus");var q=config.questions.First(x=>x.Normalized=="POSEIDON");
  58. typeof(MatchEngine).GetField("rounds",Flags).SetValue(game,new[]{q,q,q,q,q});game.Tick(4);game.Tick(2);app.ArtReviewMode=true;
  59. var v=Field<ReferenceAView>(app,"referenceView");var selection=Selection(app);
  60. check(v.GetComponentsInChildren<TMP_InputField>(true).Length==0,"answer has no editable input or keyboard entry");
  61. check(v.letterButtons.Count(b=>b.gameObject.activeSelf)==game.Pool.Length,"all candidates visible despite old letterPool=false setting");
  62. check(!v.FindButton("Delete").interactable&&!v.FindButton("OK").interactable,"empty answer disables X and OK");
  63. double time=game.TimeLeft;v.FindButton("OK").onClick.Invoke();v.FindButton("Delete").onClick.Invoke();check(game.TimeLeft==time&&selection.Count==0,"empty submit/delete have no effect or time penalty");
  64. int[] repeated=Enumerable.Range(0,game.Pool.Length).Where(i=>game.Pool[i]=='O').Take(2).ToArray();
  65. v.letterButtons[repeated[1]].onClick.Invoke();v.letterButtons[repeated[1]].onClick.Invoke();
  66. check(selection.Value=="O"&&!v.letterButtons[repeated[1]].interactable&&v.letterButtons[repeated[0]].interactable,"same physical tile cannot be selected twice; matching tile stays available");
  67. v.letterButtons[repeated[0]].onClick.Invoke();check(selection.Value=="OO","duplicate letters use separate candidate tiles");
  68. v.FindButton("Delete").onClick.Invoke();check(selection.Value=="O"&&v.letterButtons[repeated[0]].interactable&&!v.letterButtons[repeated[1]].interactable,"X restores exactly the last selected tile");
  69. v.FindButton("OK").onClick.Invoke();check(game.TimeLeft==time-2&&selection.Value=="O","wrong answer costs two seconds and stays editable");
  70. v.itemButtons[0].onClick.Invoke();check(selection.Value=="O"&&!v.letterButtons[repeated[1]].interactable,"hint preserves selected tile identities");
  71. game.Paused=true;Refresh(app);time=game.TimeLeft;v.letterButtons[repeated[0]].onClick.Invoke();v.FindButton("Delete").onClick.Invoke();v.FindButton("OK").onClick.Invoke();
  72. check(selection.Value=="O"&&game.TimeLeft==time&&v.letterButtons.Where(b=>b.gameObject.activeSelf).All(b=>!b.interactable),"pause blocks candidate, delete and submit actions");
  73. game.Paused=false;Refresh(app);SelectAnswer(app,q.answer);
  74. v.FindButton("OK").onClick.Invoke();int score=game.Score;v.FindButton("OK").onClick.Invoke();v.FindButton("Delete").onClick.Invoke();
  75. check(game.Phase==MatchPhase.Answer&&game.Score==score&&selection.Value==q.Normalized,"correct answer locks controls and duplicate submission");
  76. game.Tick(1.6);game.Continue();game.Tick(1.3);check(Selection(app).Count==0&&Field<ReferenceAView>(app,"referenceView").letterButtons.Where(b=>b.gameObject.activeSelf).All(b=>b.interactable),"next question clears answer and restores every tile");
  77. SelectAnswer(app,"P");game.Start("olympus",true);game.Tick(4);game.Tick(2);check(Selection(app).Count==0,"restart starts with an empty selection");
  78. // Every shipped question is solved through native buttons, including multiword and repeated-letter answers.
  79. foreach(var question in config.questions)
  80. {
  81. app.ShowPreview("Lobby");store.Reset();game.Start(question.realm);
  82. typeof(MatchEngine).GetField("rounds",Flags).SetValue(game,new[]{question,question,question,question,question});game.Tick(4);game.Tick(2);
  83. SelectAnswer(app,question.answer);Field<ReferenceAView>(app,"referenceView").FindButton("OK").onClick.Invoke();
  84. if(game.Phase!=MatchPhase.Answer||game.LastGain<=0)throw new Exception("Cannot solve through tiles: "+question.id);
  85. }
  86. check(true,"all 40 questions submit successfully using only candidate buttons");
  87. app.ShowDesignPage("Settings");var settings=app.GetComponentsInChildren<SuiteView>().First(x=>x.screen=="Settings");
  88. check(!settings.Button("Toggle 4").interactable&&settings.Text("Toggle state 4").text=="ON","settings cannot hide candidates");
  89. Directory.CreateDirectory("docs/验证");File.WriteAllText("docs/验证/候选字母交互验收.txt",report.ToString());return report.ToString();
  90. }
  91. }
  92. }