GameDetailLayer.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. GiftItemInfo,
  14. GoodInfo,
  15. } from "../../scripts/api/UserM";
  16. import Utils from "../../scripts/utils/Utils";
  17. import ConfigM, { ConfigGame } from "../../scripts/api/ConfigM";
  18. import AB from "../../scripts/base/AB";
  19. import GameM, { GameSpinResult } from "../../scripts/api/GameM";
  20. import { GameTaskItem, GameTaskItemCallback } from "../play/GameTaskItem";
  21. import TgM from "../../scripts/mgr/TgM";
  22. import { Tips } from "../../scripts/mgr/Tips";
  23. import { TipsData, TipsLayer } from "./TipsLayer";
  24. import { GoodsId } from "../../scripts/api/GoodsId";
  25. import ItemsM from "../../scripts/mgr/ItemsM";
  26. import { RewardLayer } from "./RewardLayer";
  27. import { PlayGamePassReward } from "../play/PlayGamePassReward";
  28. const { ccclass, property } = _decorator;
  29. @ccclass("GameDetailLayer")
  30. export class GameDetailLayer extends BaseUI implements GameTaskItemCallback {
  31. private claiming: boolean = false;
  32. onClaim(taskId: number, refreshTimes: number, node: Node): void {
  33. if (this.claiming) {
  34. return;
  35. }
  36. this.claiming = true;
  37. let self = this;
  38. let a = instantiate(node);
  39. a.parent = node;
  40. a.setPosition(0, 0, 0);
  41. UIUtils.moveToNewParent(this.FindNode("icon_spin_slot"), a);
  42. UIUtils.AnimBezierMoveAndScale(a, new Vec3(0, 0, 0), 1.2, 0.8, () => {
  43. self.claiming = false;
  44. a.destroy();
  45. self.onClaimSpinCount(refreshTimes);
  46. });
  47. }
  48. onClaimSpinCount(refreshTimes: number) {
  49. this.data.currentSpin = refreshTimes;
  50. this.refreshSpinTimes();
  51. UIUtils.AnimScaleShake(this.FindNode("lbl_game_spin_times"), 2, 0.6, false);
  52. }
  53. static async show(gameId: number) {
  54. let layer = await Hall.ins.showLayer("prefab/layer/GameDetailLayer");
  55. layer.getComponent(GameDetailLayer).init(gameId);
  56. }
  57. protected onLoad(): void {
  58. super.onLoad();
  59. // this.FindNode("game_pass_pvz_node").active = false;
  60. }
  61. // private spinTimes: number = 0;
  62. private data: GameInfo;
  63. async init(gameId: number) {
  64. let data = await GameM.ins.getGameDetailInfo(gameId);
  65. if (!data) {
  66. return;
  67. }
  68. this.data = data;
  69. if (data.gameId == 2) {
  70. this.FindNode("btn_play").active = false;
  71. }
  72. this.refreshGamePassReward();
  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. async refreshGamePassReward() {
  103. let giftList = this.data?.giftList;
  104. let game_pass_node = this.FindNode("game_pass_reward_layout");
  105. game_pass_node.removeAllChildren();
  106. if (!giftList || giftList.length == 0) {
  107. return;
  108. }
  109. let prefab = await AB.ins.loadPrefab("prefab/play/PlayGamePassReward");
  110. for (let i = 0; i < giftList.length; i++) {
  111. let gift: GiftItemInfo = giftList[i];
  112. let item = instantiate(prefab);
  113. item.parent = game_pass_node;
  114. item
  115. .getComponent(PlayGamePassReward)
  116. .init(
  117. this.data.gameId,
  118. gift,
  119. this.onClaimGamePassGiftSuccess.bind(this)
  120. );
  121. }
  122. }
  123. private onClaimGamePassGiftSuccess(gift: GiftItemInfo) {
  124. Tips.show("Claimed successfully");
  125. this.init(this.data.gameId);
  126. }
  127. showButtons(c: ConfigGame) {
  128. this.FindNode("btn_start_game_web").active = c.webLink && c.webLink != "";
  129. this.FindNode("btn_start_game_tg").active = c.tgLink && c.tgLink != "";
  130. this.FindNode("btn_start_game_line").active =
  131. c.lineLink && c.lineLink != "";
  132. }
  133. async initTask(taskList: GameTaskInfo[]) {
  134. if (!taskList || taskList.length == 0) {
  135. this.FindNode("NoData").active = true;
  136. return;
  137. }
  138. this.FindNode("NoData").active = false;
  139. let prefab = await AB.ins.loadPrefab("prefab/play/GameTaskItem");
  140. let normal_task_layout = this.FindNode("task_layout_normal");
  141. let normalTaskList = taskList.filter((item) => item.taskType == 1);
  142. for (let i = 0; i < normalTaskList.length; i++) {
  143. let normalTask = normalTaskList[i];
  144. let item = instantiate(prefab);
  145. item.parent = normal_task_layout;
  146. item.getComponent(GameTaskItem).init(this.data.gameId, normalTask, this);
  147. }
  148. let seasonTaskList = taskList.filter((item) => item.taskType == 2);
  149. if (seasonTaskList.length == 0) {
  150. this.FindNode("lbl_title_task_season").active = false;
  151. return;
  152. }
  153. let season_task_layout = this.FindNode("task_layout_season");
  154. for (let i = 0; i < seasonTaskList.length; i++) {
  155. let seasonTask = seasonTaskList[i];
  156. let item = instantiate(prefab);
  157. item.parent = season_task_layout;
  158. item.getComponent(GameTaskItem).init(this.data.gameId, seasonTask, this);
  159. }
  160. }
  161. async setIcon(gameId: number) {
  162. let path = "texture/play/icon_" + gameId;
  163. let spriteFrame = await AB.ins.loadSpriteFrame(path);
  164. this.FindAs("icon_game", Sprite).spriteFrame = spriteFrame;
  165. }
  166. protected simpleOnBtnClick(name: string): void {
  167. let c = ConfigM.ins.getGame(this.data.gameId);
  168. switch (name) {
  169. case "btn_start_spin":
  170. this.startSpin();
  171. break;
  172. case "btn_start_spin_10":
  173. this.spin10times();
  174. break;
  175. case "btn_start_game_web":
  176. ConfigM.ins.openLink(c.webLink);
  177. break;
  178. case "btn_start_game_tg":
  179. ConfigM.ins.openLink(c.tgLink);
  180. break;
  181. case "btn_start_game_line":
  182. ConfigM.ins.openLink(c.lineLink);
  183. break;
  184. }
  185. }
  186. async choose10times() {
  187. return new Promise((resolve) => {
  188. let tips = Utils.setI18nLabel("Play.TipsChoose10Times");
  189. let d = new TipsData(tips);
  190. d.onCancel = () => {
  191. resolve(false);
  192. };
  193. d.onConfirm = () => {
  194. resolve(true);
  195. };
  196. d.forceConfirm = false;
  197. d.show();
  198. });
  199. }
  200. async startSpin() {
  201. if (!this.data) {
  202. Tips.show("Please wait for the game data to load");
  203. return;
  204. }
  205. if (!this.data.gameId) {
  206. Tips.show("Game id not found");
  207. return;
  208. }
  209. if (this.data.currentSpin <= 0) {
  210. return;
  211. }
  212. // if (this.data.currentSpin >= 10) {
  213. // let ok = await this.choose10times();
  214. // if (ok) {
  215. // this.spin10times();
  216. // return;
  217. // }
  218. // }
  219. WheelDialog.show(this.data.gameId);
  220. this.data.currentSpin--;
  221. this.refreshSpinTimes();
  222. }
  223. async spin10times() {
  224. if (this.data.currentSpin < 10) {
  225. return;
  226. }
  227. let loadingNode = this.FindNode("requesting_10_times");
  228. loadingNode.active = true;
  229. let onceTimes = 10;
  230. this.data.currentSpin -= onceTimes;
  231. this.refreshSpinTimes();
  232. let critCount = UserM.ins.getGoodsCount(GoodsId.POG_CRITICAL_CARD);
  233. let rewards = [];
  234. for (let index = 0; index < onceTimes; index++) {
  235. let spinResult: GameSpinResult = await GameM.ins.gameSpin(
  236. this.data.gameId
  237. );
  238. if (!spinResult) {
  239. continue;
  240. }
  241. let pog = spinResult.totalPog;
  242. if (critCount > 0) {
  243. critCount--;
  244. let critResult = await GameM.ins.getCritReward(
  245. this.data.gameId,
  246. spinResult.spinId
  247. );
  248. if (!critResult) {
  249. continue;
  250. }
  251. pog += critResult.critPog;
  252. }
  253. let g = new GoodInfo();
  254. g.count = pog;
  255. if (pog > 0) {
  256. g.id = GoodsId.POG;
  257. } else {
  258. g.id = GoodsId.AIR;
  259. }
  260. rewards.push(g);
  261. }
  262. RewardLayer.show(rewards);
  263. await UserM.ins.refreshInfo();
  264. loadingNode.active = false;
  265. }
  266. refreshSpinTimes() {
  267. this.setText("lbl_game_spin_times", "x" + this.data.currentSpin + "");
  268. this.FindAs("btn_start_spin", Sprite).grayscale =
  269. this.data.currentSpin <= 0;
  270. this.FindAs("btn_start_spin_10", Sprite).grayscale =
  271. this.data.currentSpin < 10;
  272. }
  273. }