main.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // main.js
  2. let levelSending = false;
  3. let gameOverSending = false;
  4. let lastExitGame=false;
  5. let lastLevelCompleted = 0;
  6. let lastGameOver = 0;
  7. runOnStartup(async runtime =>
  8. {
  9. runtime.addEventListener(
  10. "beforeprojectstart",
  11. () => OnBeforeProjectStart(runtime)
  12. );
  13. runtime.addEventListener("function", OnFunction);
  14. });
  15. async function OnBeforeProjectStart(runtime)
  16. {
  17. console.log("Construct3 Started");
  18. // Nhận dữ liệu từ MVC
  19. window.addEventListener(
  20. "message",
  21. (event) => HandleMessage(runtime, event)
  22. );
  23. // Tick
  24. runtime.addEventListener(
  25. "tick",
  26. () => Tick(runtime)
  27. );
  28. console.log("Add event success");
  29. // Báo MVC biết game đã sẵn sàng
  30. window.parent.postMessage(
  31. {
  32. type: "GAME_READY"
  33. },
  34. "*"
  35. );
  36. console.log("GAME_READY sent");
  37. }
  38. function HandleMessage(runtime, event)
  39. {
  40. console.log("MESSAGE RECEIVED");
  41. console.log(event.data);
  42. if (!event.data || !event.data.type)
  43. return;
  44. switch (event.data.type)
  45. {
  46. case "INIT_GAME":
  47. console.log("INIT_GAME received: "+ event.data);
  48. runtime.globalVars.gameType =
  49. event.data.gameType ?? "";
  50. runtime.globalVars.msisdn =
  51. event.data.msisdn ?? "";
  52. runtime.globalVars.token =
  53. event.data.token ?? "";
  54. runtime.globalVars.transId =
  55. event.data.transId ?? "";
  56. runtime.globalVars.root =
  57. event.data.root ?? "";
  58. console.log(
  59. "Game Init:",
  60. runtime.globalVars.gameType,
  61. runtime.globalVars.msisdn
  62. );
  63. break;
  64. }
  65. }
  66. function Tick(runtime)
  67. {
  68. // ===== LEVEL COMPLETE (CHỈ 1 LẦN) =====
  69. if (
  70. runtime.globalVars.levelCompleted === 1 &&
  71. lastLevelCompleted === 0
  72. )
  73. {
  74. lastLevelCompleted = 1;
  75. SaveLevelResult(runtime);
  76. }
  77. if (runtime.globalVars.levelCompleted === 0)
  78. {
  79. lastLevelCompleted = 0;
  80. }
  81. // ===== GAME OVER (CHỈ 1 LẦN) =====
  82. /*
  83. if (
  84. runtime.globalVars.gameOver === 1 &&
  85. lastGameOver === 0
  86. )
  87. {
  88. lastGameOver = 1;
  89. SaveGameOver(runtime);
  90. }
  91. if (runtime.globalVars.gameOver === 0)
  92. {
  93. lastGameOver = 0;
  94. }
  95. */
  96. // ===== Exit game =====
  97. if (
  98. runtime.globalVars.exitGame === 1 &&
  99. lastExitGame === 0
  100. )
  101. {
  102. lastExitGame = 1;
  103. SaveExitGame(runtime);
  104. }
  105. if (runtime.globalVars.exitGame === 0)
  106. {
  107. lastExitGame = 0;
  108. }
  109. }
  110. //chu ý là khi hoàn thành level cần set
  111. //Set levelCompleted = 1
  112. //Sau khi vao level moi can set lại
  113. //Set levelCompleted = 0
  114. //Set showPrizePopup = 0
  115. async function SaveLevelResult(runtime)
  116. {
  117. try
  118. {
  119. console.log("Calling SaveLevelResult...");
  120. const body = {
  121. token: runtime.globalVars.token,
  122. msisdn: runtime.globalVars.msisdn,
  123. transId: runtime.globalVars.transId,
  124. level: runtime.globalVars.cur_level,
  125. score: runtime.globalVars.score,
  126. gameType: runtime.globalVars.gameType,
  127. gameOver: 0
  128. };
  129. console.log("Request Body:", body);
  130. console.log("JSON:", JSON.stringify(body));
  131. const response = await fetch(runtime.globalVars.root+"/Game/SaveLevelResult",
  132. {
  133. method: "POST",
  134. headers: {
  135. "Content-Type": "application/json"
  136. },
  137. body: JSON.stringify(body)
  138. });
  139. const result = await response.json();
  140. console.log("Level Result:", result);
  141. // Ví dụ dữ liệu trả về
  142. runtime.globalVars.point =
  143. result.point ?? "";
  144. runtime.globalVars.data =
  145. result.data ?? "";
  146. runtime.globalVars.score =
  147. result.score ?? "";
  148. runtime.globalVars.transId =
  149. result.transId ?? "";
  150. runtime.globalVars.showPrizePopup = 1;
  151. console.log("Chuẩn bị gọi vào function trong event sheet:");
  152. //runtime.callFunction("Quangbh");
  153. console.log("Da goi goi vao function event sheet xong nhe!!!");
  154. }
  155. catch (error)
  156. {
  157. console.error("SaveLevelResult Error:", error);
  158. }
  159. finally
  160. {
  161. levelSending = false;
  162. }
  163. }
  164. // Khi muốn thoát game về MVC
  165. function SaveExitGame(runtime)
  166. {
  167. try
  168. {
  169. const form = document.createElement("form");
  170. form.method = "POST";
  171. form.action = runtime.globalVars.root + "/Game/ExitGame";
  172. // Thêm input hidden
  173. function add(name, value)
  174. {
  175. const input = document.createElement("input");
  176. input.type = "hidden";
  177. input.name = name;
  178. input.value = value ?? "";
  179. form.appendChild(input);
  180. }
  181. add("token", runtime.globalVars.token);
  182. add("msisdn", runtime.globalVars.msisdn);
  183. add("transId", runtime.globalVars.transId);
  184. add("level", runtime.globalVars.cur_level);
  185. add("score", runtime.globalVars.score);
  186. add("gameType", runtime.globalVars.gameType);
  187. add("gameOver", runtime.globalVars.gameOver);
  188. document.body.appendChild(form);
  189. // Submit form
  190. HTMLFormElement.prototype.submit.call(form);
  191. }
  192. catch (error)
  193. {
  194. console.error("SaveExitGame Error:", error);
  195. }
  196. }
  197. //Khi thua game chi can set
  198. //Set gameOver = 1
  199. async function SaveGameOver(runtime)
  200. {
  201. try
  202. {
  203. console.log("Calling GameOver...");
  204. const response = await fetch(
  205. runtime.globalVars.root+"/Game/GameOver",
  206. {
  207. method: "POST",
  208. headers:
  209. {
  210. "Content-Type": "application/json"
  211. },
  212. body: JSON.stringify(
  213. {
  214. token: runtime.globalVars.token,
  215. msisdn: runtime.globalVars.msisdn,
  216. transId: runtime.globalVars.transId,
  217. score: runtime.globalVars.score,
  218. gameType: runtime.globalVars.gameType
  219. })
  220. }
  221. );
  222. const result = await response.json();
  223. console.log("GameOver Result:", result);
  224. runtime.globalVars.finalPrize =
  225. result.finalPrize ?? "";
  226. runtime.globalVars.showGameOverPopup = 1;
  227. }
  228. catch (error)
  229. {
  230. console.error("GameOver Error:", error);
  231. }
  232. finally
  233. {
  234. gameOverSending = false;
  235. }
  236. }
  237. function OnFunction(e)
  238. {
  239. switch (e.name)
  240. {
  241. case "ShowMessage":
  242. ShowMessage(e.args[0]);
  243. break;
  244. case "AddScore":
  245. AddScore(e.args[0]);
  246. break;
  247. case "MovePlayer":
  248. MovePlayer(e.args[0], e.args[1]);
  249. break;
  250. }
  251. }
  252. //===================================
  253. // Hiện thông báo
  254. //===================================
  255. function ShowMessage(message)
  256. {
  257. console.log(message);
  258. }
  259. //===================================
  260. // Cộng điểm
  261. //===================================
  262. function AddScore(score)
  263. {
  264. runtime.globalVars.Score += score;
  265. console.log("Score:", runtime.globalVars.Score);
  266. }
  267. //===================================
  268. // Di chuyển Player
  269. //===================================
  270. function MovePlayer(offsetX, offsetY)
  271. {
  272. const player = runtime.objects.Player.getFirstInstance();
  273. if (!player)
  274. return;
  275. player.x += offsetX;
  276. player.y += offsetY;
  277. }