GameDetailLayer.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { _decorator, Component, Node, Event } from "cc";
  2. import { game } from "cc";
  3. import { instantiate } from "cc";
  4. import { Vec3 } from "cc";
  5. import { Sprite } from "cc";
  6. import BaseUI from "../../scripts/base/BaseUI";
  7. import UIUtils from "../../scripts/base/UIUtils";
  8. import { Hall } from "../hall/Hall";
  9. import { WheelDialog } from "../play/WheelDialog";
  10. import UserM, {
  11. GameInfo,
  12. GameTaskInfo,
  13. GoodInfo,
  14. } from "../../scripts/api/UserM";
  15. import Utils from "../../scripts/utils/Utils";
  16. import ConfigM, { ConfigGame } from "../../scripts/api/ConfigM";
  17. import AB from "../../scripts/base/AB";
  18. import GameM, { GameSpinResult } from "../../scripts/api/GameM";
  19. import { GameTaskItem, GameTaskItemCallback } from "../play/GameTaskItem";
  20. import TgM from "../../scripts/mgr/TgM";
  21. import { Tips } from "../../scripts/mgr/Tips";
  22. import { TipsData, TipsLayer } from "./TipsLayer";
  23. import { GoodsId } from "../../scripts/api/GoodsId";
  24. import ItemsM from "../../scripts/mgr/ItemsM";
  25. import { RewardLayer } from "./RewardLayer";
  26. const { ccclass, property } = _decorator;
  27. @ccclass("GameDetailLayer")
  28. export class GameDetailLayer extends BaseUI implements GameTaskItemCallback {
  29. private claiming: boolean = false;
  30. onClaim(taskId: number, refreshTimes: number, node: Node): void {
  31. if (this.claiming) {
  32. return;
  33. }
  34. this.claiming = true;
  35. let self = this;
  36. let a = instantiate(node);
  37. a.parent = node;
  38. a.setPosition(0, 0, 0);
  39. UIUtils.moveToNewParent(this.FindNode("icon_spin_slot"), a);
  40. UIUtils.AnimBezierMoveAndScale(a, new Vec3(0, 0, 0), 1.2, 0.8, () => {
  41. self.claiming = false;
  42. a.destroy();
  43. self.onClaimSpinCount(refreshTimes);
  44. });
  45. }
  46. onClaimSpinCount(refreshTimes: number) {
  47. this.data.currentSpin = refreshTimes;
  48. this.refreshSpinTimes();
  49. UIUtils.AnimScaleShake(this.FindNode("lbl_game_spin_times"), 2, 0.6, false);
  50. }
  51. static async show(gameId: number) {
  52. let layer = await Hall.ins.showLayer("prefab/layer/GameDetailLayer");
  53. layer.getComponent(GameDetailLayer).init(gameId);
  54. }
  55. protected onLoad(): void {
  56. super.onLoad();
  57. this.FindNode("game_pass_pvz_node").active = false;
  58. }
  59. // private spinTimes: number = 0;
  60. private data: GameInfo;
  61. async init(gameId: number) {
  62. let data = await GameM.ins.getGameDetailInfo(gameId);
  63. if (!data) {
  64. return;
  65. }
  66. this.data = data;
  67. if (data.gameId == 2) {
  68. this.FindNode("btn_play").active = false;
  69. }
  70. if (data.gameId == 1) {
  71. this.FindNode("game_pass_pvz_node").active = true;
  72. }
  73. if (data.gameUser != null) {
  74. this.setText("lbl_link_user_name", data.gameUser.userName);
  75. }
  76. let c = ConfigM.ins.getGame(data.gameId);
  77. this.showButtons(c);
  78. this.setText("lbl_name", c.gameName);
  79. console.log("item.remainPog", data.remainPog);
  80. if (data.seasonPog > 0) {
  81. this.setText(
  82. "lbl_remain_info",
  83. Utils.formatNumber(data.remainPog, 2) +
  84. "/" +
  85. Utils.formatNumber(data.seasonPog, 0)
  86. );
  87. this.setText(
  88. "lbl_my_reward_pog",
  89. Utils.formatNumber(data.myRewardPog, 0)
  90. );
  91. } else {
  92. this.setText("lbl_remain_info", "Coming soon");
  93. this.setText(
  94. "lbl_my_reward_pog",
  95. Utils.formatNumber(data.myRewardPog, 0)
  96. );
  97. }
  98. this.setIcon(data.gameId);
  99. this.initTask(data.taskList);
  100. this.refreshSpinTimes();
  101. }
  102. showButtons(c: ConfigGame) {
  103. this.FindNode("btn_start_game_web").active = c.webLink && c.webLink != "";
  104. this.FindNode("btn_start_game_tg").active = c.tgLink && c.tgLink != "";
  105. this.FindNode("btn_start_game_line").active =
  106. c.lineLink && c.lineLink != "";
  107. }
  108. async initTask(taskList: GameTaskInfo[]) {
  109. if (!taskList || taskList.length == 0) {
  110. this.FindNode("NoData").active = true;
  111. return;
  112. }
  113. this.FindNode("NoData").active = false;
  114. let prefab = await AB.ins.loadPrefab("prefab/play/GameTaskItem");
  115. let normal_task_layout = this.FindNode("task_layout_normal");
  116. let normalTaskList = taskList.filter((item) => item.taskType == 1);
  117. for (let i = 0; i < normalTaskList.length; i++) {
  118. let normalTask = normalTaskList[i];
  119. let item = instantiate(prefab);
  120. item.parent = normal_task_layout;
  121. item.getComponent(GameTaskItem).init(this.data.gameId, normalTask, this);
  122. }
  123. let seasonTaskList = taskList.filter((item) => item.taskType == 2);
  124. if (seasonTaskList.length == 0) {
  125. this.FindNode("lbl_title_task_season").active = false;
  126. return;
  127. }
  128. let season_task_layout = this.FindNode("task_layout_season");
  129. for (let i = 0; i < seasonTaskList.length; i++) {
  130. let seasonTask = seasonTaskList[i];
  131. let item = instantiate(prefab);
  132. item.parent = season_task_layout;
  133. item.getComponent(GameTaskItem).init(this.data.gameId, seasonTask, this);
  134. }
  135. }
  136. async setIcon(gameId: number) {
  137. let path = "texture/play/icon_" + gameId;
  138. let spriteFrame = await AB.ins.loadSpriteFrame(path);
  139. this.FindAs("icon_game", Sprite).spriteFrame = spriteFrame;
  140. }
  141. protected simpleOnBtnClick(name: string): void {
  142. let c = ConfigM.ins.getGame(this.data.gameId);
  143. switch (name) {
  144. case "btn_start_spin":
  145. this.startSpin();
  146. break;
  147. case "btn_start_game_web":
  148. ConfigM.ins.openLink(c.webLink);
  149. break;
  150. case "btn_start_game_tg":
  151. ConfigM.ins.openLink(c.tgLink);
  152. break;
  153. case "btn_start_game_line":
  154. ConfigM.ins.openLink(c.lineLink);
  155. break;
  156. case "btn_game_pass_claim_pvz":
  157. Tips.show("Coming soon");
  158. break;
  159. }
  160. }
  161. async choose10times() {
  162. return new Promise((resolve) => {
  163. let tips = Utils.setI18nLabel("Play.TipsChoose10Times");
  164. let d = new TipsData(tips);
  165. d.onCancel = () => {
  166. resolve(false);
  167. };
  168. d.onConfirm = () => {
  169. resolve(true);
  170. };
  171. d.forceConfirm = false;
  172. d.show();
  173. });
  174. }
  175. async startSpin() {
  176. if (this.data.currentSpin <= 0) {
  177. return;
  178. }
  179. if (this.data.currentSpin >= 10) {
  180. let ok = await this.choose10times();
  181. if (ok) {
  182. this.spin10times();
  183. return;
  184. }
  185. }
  186. WheelDialog.show(this.data.gameId);
  187. this.data.currentSpin--;
  188. this.refreshSpinTimes();
  189. }
  190. async spin10times() {
  191. let loadingNode = this.FindNode("requesting_10_times");
  192. loadingNode.active = true;
  193. let onceTimes = 10;
  194. this.data.currentSpin -= onceTimes;
  195. this.refreshSpinTimes();
  196. let critCount = UserM.ins.getGoodsCount(GoodsId.POG_CRITICAL_CARD);
  197. let rewards = [];
  198. for (let index = 0; index < onceTimes; index++) {
  199. let spinResult: GameSpinResult = await GameM.ins.gameSpin(
  200. this.data.gameId
  201. );
  202. if (!spinResult) {
  203. continue;
  204. }
  205. let pog = spinResult.totalPog;
  206. if (critCount > 0) {
  207. critCount--;
  208. let critResult = await GameM.ins.getCritReward(
  209. this.data.gameId,
  210. spinResult.spinId
  211. );
  212. if (!critResult) {
  213. continue;
  214. }
  215. pog += critResult.critPog;
  216. }
  217. let g = new GoodInfo();
  218. g.count = pog;
  219. g.id = GoodsId.POG;
  220. rewards.push(g);
  221. }
  222. RewardLayer.show(rewards);
  223. await UserM.ins.refreshInfo();
  224. loadingNode.active = false;
  225. }
  226. refreshSpinTimes() {
  227. this.setText("lbl_game_spin_times", "x" + this.data.currentSpin + "");
  228. this.FindAs("btn_start_spin", Sprite).grayscale =
  229. this.data.currentSpin <= 0;
  230. }
  231. }