FightItem.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { _decorator, Component, Node, Sprite, Label, TERRAIN_HEIGHT_BASE, UITransform, ImageAsset, SpriteFrame, Texture2D } from 'cc';
  2. import { RewardVideoSystem } from '../../ad/RewardVideoSystem';
  3. import { DataSystem } from '../../core/data/DataSystem';
  4. import { Http, HttpResponseCode } from '../../core/net/Http';
  5. import { HttpSystem } from '../../core/net/HttpSystem';
  6. import { ResourceLoader } from '../../core/resourceManager/ResourceLoader';
  7. import { BitmapFont } from '../../core/ui/BitmapFont';
  8. import { CountDown } from '../../core/ui/CountDown';
  9. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  10. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  11. import { Utils } from '../../core/utils/Utils';
  12. import { ConfigData } from '../../data/ConfigData';
  13. import { ISJSB } from '../../data/jsb/platform';
  14. import { UserData } from '../../data/UserData';
  15. import { UserHeroData } from '../../hero/UserHeroData';
  16. import { PVPCardData } from '../../pvp/PVP';
  17. import { RecruitData } from '../../recruit/RecruitData';
  18. import { UpdateRecruit } from '../../recruit/ui/UpdateRecruit';
  19. import { ReportThinking } from '../../ReportThinking';
  20. import { FightData, FightItemData } from '../FightData';
  21. const { ccclass, property } = _decorator;
  22. /**切磋 复仇Item*/
  23. @ccclass('FightItem')
  24. export class FightItem extends Component {
  25. @property({ type: ResourceLoader, displayName: "资源加载组件", tooltip: "资源加载组件" }) res: ResourceLoader = null;
  26. @property({ type: Http, displayName: "Http组件", tooltip: "Http组件" }) http: Http = null;
  27. @property({ type: Sprite, displayName: '头像', tooltip: "头像" }) imgHead: Sprite = null;
  28. @property({ type: Node, displayName: '随机玩家头像', tooltip: "随机玩家头像" }) imgHeadRandom: Node = null;
  29. @property({ type: Label, displayName: '文本昵称', tooltip: "文本昵称" }) txtNickName: Label = null;
  30. @property({ type: BitmapFont, displayName: '文本秒伤', tooltip: "文本秒伤" }) txtAttack: BitmapFont = null;
  31. @property({ type: Node, displayName: '按钮红包', tooltip: "按钮红包" }) btnRedbag: Node = null;
  32. @property({ type: Node, displayName: '按钮查看', tooltip: "按钮查看" }) btnCheck: Node = null;
  33. @property({ type: Node, displayName: '按钮切磋', tooltip: "按钮切磋" }) btnFight: Node = null;
  34. @property({ type: Node, displayName: '按钮复仇', tooltip: "按钮复仇" }) btnRevenge: Node = null;
  35. @property({ type: Node, displayName: '红包节点', tooltip: "红包节点" }) redbagNode: Node = null;
  36. @property({ type: Node, displayName: '广告图标', tooltip: "广告图标" }) adfight: Node = null;
  37. @property({ type: BitmapFont, displayName: '文本倒计时', tooltip: "文本倒计时" }) txtTime: BitmapFont = null;
  38. @property({ type: CountDown, displayName: '倒计时组件', tooltip: "倒计时组件" }) countDown: CountDown = null;
  39. private remaindTime = 0;
  40. private fightData: FightItemData;
  41. private isCanClick = true;
  42. /**当数据改动时*/
  43. public async onDataChange(fightItemData: FightItemData, index: number) {
  44. this.fightData = fightItemData;
  45. this.imgHeadRandom.active = fightItemData.isRandomPlayer;
  46. if (fightItemData.isRandomPlayer) {
  47. this.txtNickName.string = `随机玩家`;
  48. this.txtAttack.string = `???`;
  49. this.btnRedbag.active = false;
  50. this.btnCheck.active = false;
  51. this.btnFight.active = true;
  52. this.btnRevenge.active = false;
  53. this.redbagNode.active = false;
  54. this.adfight.active = DataSystem.getData(FightData).pvpData.times <= 0;
  55. } else {
  56. let curTime = HttpSystem.serverTime * 0.001;
  57. this.remaindTime = (fightItemData.itemType == 1 && fightItemData.redbagType != -1 && fightItemData.canOpenTime > curTime) ? fightItemData.canOpenTime - curTime : 0;
  58. this.txtNickName.string = `${fightItemData.nickName}`;
  59. this.txtAttack.string = `${fightItemData.attack > 10000 ? (fightItemData.attack / 1000).toFixed(1) + "k" : fightItemData.attack + ""}`;
  60. this.btnRedbag.active = fightItemData.itemType == 1 && fightItemData.redbagType != -1;
  61. this.btnCheck.active = fightItemData.itemType == 1 ? fightItemData.canOpenTime > curTime : false;
  62. this.btnFight.active = fightItemData.itemType == 1 && fightItemData.redbagType == -1;
  63. this.btnRevenge.active = fightItemData.itemType == 2;
  64. this.redbagNode.active = fightItemData.itemType == 1 && fightItemData.redbagType != -1;
  65. this.txtTime.string = (!fightItemData.canOpenTime || fightItemData.canOpenTime <= curTime) ? "" : Utils.formatCountDown((fightItemData.canOpenTime - curTime) * 1000) + "";
  66. if (this.remaindTime > 0) { this.countDown.stop(); this.countDown.value = this.remaindTime; this.countDown.play() }
  67. this.adfight.active = fightItemData.itemType == 1 && fightItemData.redbagType == -1 && DataSystem.getData(FightData).pvpData.times <= 0;
  68. }
  69. if (ISJSB() && !fightItemData.isRandomPlayer && fightItemData.avator) {
  70. let img = await this.res.loadRemote<ImageAsset>(fightItemData.avator, { ext: ".png" });
  71. const spriteFrame = new SpriteFrame();
  72. const texture = new Texture2D();
  73. texture.image = img;
  74. spriteFrame.texture = texture;
  75. this.imgHead.spriteFrame = spriteFrame;
  76. }
  77. }
  78. public updateTxtTime(progress) {
  79. this.txtTime.string = Utils.formatCountDown(progress * 1000);
  80. }
  81. public completeTime() {
  82. this.remaindTime = 0;
  83. this.txtTime.string = "";
  84. this.btnCheck.active = false;
  85. this.btnRedbag.active = true;
  86. }
  87. private getPVPParam(data: any) {
  88. let g1: Array<PVPCardData> = [];
  89. let g2: Array<PVPCardData> = [];
  90. let key: number = parseInt(data.key);
  91. let baseHit = DataSystem.getData(ConfigData).get("serverConfig").pvpBaseDamge;
  92. let buff = DataSystem.getData(ConfigData).get("buff");
  93. let myHeros: any[] = data.myHeroDatas.heroDatas;
  94. let myFHeros: any[] = data.myFriendHeroDatas;
  95. let enemyHeros: any[] = data.enemyHeroDatas.heroDatas;
  96. let enemyFHeros: any[] = data.enemyFriendHeroDatas;
  97. let userData = DataSystem.getData(UserHeroData);
  98. for (let i = 0; i < myHeros.length; i++) {
  99. let tempHero = userData.get(myHeros[i].id).client;
  100. let temp = new PVPCardData(myHeros[i].id, myHeros[i].lv, myHeros[i].star, tempHero.color, tempHero.camp, data.myHeroDatas.avator);
  101. g1.push(temp);
  102. }
  103. for (let i = 0; i < myFHeros.length; i++) {
  104. let tempHero = userData.get(myFHeros[i].heroData.id).client;
  105. let temp = new PVPCardData(myFHeros[i].heroData.id, myFHeros[i].heroData.lv, myFHeros[i].heroData.star, tempHero.color, tempHero.camp, myFHeros[i].avator);
  106. g1.push(temp);
  107. }
  108. for (let i = 0; i < enemyHeros.length; i++) {
  109. let tempHero = userData.get(enemyHeros[i].id).client;
  110. let temp = new PVPCardData(enemyHeros[i].id, enemyHeros[i].lv, enemyHeros[i].star, tempHero.color, tempHero.camp, data.enemyHeroDatas.avator);
  111. g2.push(temp);
  112. }
  113. for (let i = 0; i < enemyFHeros.length; i++) {
  114. let tempHero = userData.get(enemyFHeros[i].heroData.id).client;
  115. let temp = new PVPCardData(enemyFHeros[i].heroData.id, enemyFHeros[i].heroData.lv, enemyFHeros[i].heroData.star, tempHero.color, tempHero.camp, enemyFHeros[i].avator);
  116. g2.push(temp);
  117. }
  118. return { g1: g1, g2: g2, key: key, baseHit: baseHit, buff: buff };
  119. }
  120. /**获取奖励
  121. * @param rewardType 奖励类型 除了 money diamond bonus prestige 之外为武将碎片id
  122. * @param numData 数量
  123. */
  124. private getReward(rewardType: string, numData: number) {
  125. console.log("btnRedbag World Pos: (" + this.btnRedbag.worldPosition.x + "," + this.btnRedbag.worldPosition.y + ")");
  126. let type = 0;
  127. switch (rewardType) {
  128. case "money": type = 1; break;
  129. case "diamond": type = 2; break;
  130. case "bonus": type = 3; break;
  131. //case "prestige": type = 7; break;
  132. case "prestige": type = 4; break;
  133. default:
  134. //rewardType 为碎片id
  135. /**更新武将数据*/
  136. DataSystem.getData(UserHeroData).addChip(parseInt(rewardType), numData);
  137. let hero = DataSystem.getData(UserHeroData).get(parseInt(rewardType));
  138. let heroData = { id: hero.server.id, star: hero.server.star, chip: hero.server.chip, lv: hero.server.lv, percent: hero.server.percent };
  139. this.getComponent(UpdateRecruit).addRecruitResults([{ chip: numData, hero: heroData }]);
  140. if (DataSystem.getData(RecruitData).recruitResults && DataSystem.getData(RecruitData).recruitResults.length > 0) {
  141. WindowSystem.open("prefabs/ui/recruit/recruitResult", WindowOpenMode.NotCloseAndCover);
  142. }
  143. break;
  144. }
  145. if (type != 0) {
  146. WindowSystem.open("prefabs/ui/fight/fightRewardUI", WindowOpenMode.NotCloseAndCover, [[{ rewardType: type, num: numData }]]);
  147. //WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndCover, [{ type: type, num: numData, position: this.btnRedbag.worldPosition, isWorldPos: true }]);
  148. }
  149. }
  150. /**
  151. * PVP准备
  152. * @param type pvp类型 0 随机 1 好友 2 复仇 3 挑战
  153. * @param enemyID 敌人编号 如果是随机 就填0
  154. */
  155. private async PvpReady(type: number, enemyID: number): Promise<boolean> {
  156. let result = await this.http.send("/api/pvp/ReadyPVP", { type: type, enemyID: enemyID });
  157. if (result && result.code == HttpResponseCode.Success) {
  158. let param = this.getPVPParam(result.data);
  159. console.log("Ready Param: ", param);
  160. WindowSystem.open("prefabs/pvp/pvp", WindowOpenMode.NotCloseAndCover, [param]);
  161. return true;
  162. } else {
  163. WindowSystem.showTips("请求失败");
  164. return false;
  165. }
  166. }
  167. /**更新红包数据*/
  168. private updateBonusData() {
  169. let fightData = DataSystem.getData(FightData);
  170. let rbIndex;
  171. if (this.fightData.redbagType == 1) { //刪除攻击红包数据
  172. rbIndex = fightData.attackBonusList.indexOf(this.fightData.redbagData);
  173. if (rbIndex != -1) {
  174. fightData.attackBonusList.splice(rbIndex, 1);
  175. }
  176. fightData.attackBonusList = fightData.attackBonusList;
  177. console.log("attack rbindex: " + rbIndex);
  178. } else if (this.fightData.redbagType == 2) {//修改被攻击红包数据 openID
  179. rbIndex = fightData.beAttackBonusList.indexOf(this.fightData.redbagData);
  180. fightData.beAttackBonusList[rbIndex].openID = DataSystem.getData(UserData).id;
  181. fightData.beAttackBonusList = fightData.beAttackBonusList;
  182. console.log("beAttack rbindex: " + rbIndex);
  183. }
  184. }
  185. /**切磋 包括随机*/
  186. //g1: Array<PVPCardData>, g2: Array<PVPCardData>, key: number, baseHit: number, buff: any
  187. private async onClickFight() {
  188. if (!this.isCanClick) return;
  189. this.isCanClick = false;
  190. console.log("click fight");
  191. let fightData = DataSystem.getData(FightData);
  192. if (fightData.pvpData.times > 0) {
  193. let result = await this.PvpReady(this.fightData.isRandomPlayer ? 0 : 1, this.fightData.isRandomPlayer ? 0 : this.fightData.id);
  194. if (result) {
  195. fightData.pvpData.times--;
  196. fightData.pvpData = fightData.pvpData;
  197. }
  198. this.isCanClick = true;
  199. } else {
  200. //判断观看视频切磋次数
  201. if (fightData.pvpData.videoTimes > 0) {
  202. let resultAd = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  203. if (resultAd && resultAd.code == HttpResponseCode.Success) { //可以观看视频
  204. ReportThinking.ad_init('add_pvptimes');
  205. let adData = await RewardVideoSystem.show();
  206. if (adData) {
  207. let addPvpTimes = await this.getComponent(Http).send("/api/pvp/AddPVPTimes", { adData: adData.obj });
  208. if (addPvpTimes && addPvpTimes.code == HttpResponseCode.Success) {
  209. ReportThinking.ad_end('add_pvptimes');
  210. let result = await this.PvpReady(this.fightData.isRandomPlayer ? 0 : 1, this.fightData.isRandomPlayer ? 0 : this.fightData.id);
  211. if (result) {
  212. fightData.pvpData.videoTimes--;
  213. fightData.pvpData = fightData.pvpData;
  214. }
  215. this.isCanClick = true;
  216. } else {
  217. WindowSystem.showTips("请求失败");
  218. this.isCanClick = true;
  219. }
  220. } else {
  221. WindowSystem.showTips("观看视频失败");
  222. this.isCanClick = true;
  223. }
  224. } else {
  225. WindowSystem.showTips("视频请求失败");
  226. this.isCanClick = true;
  227. }
  228. } else {
  229. WindowSystem.showTips("观看视频切磋次数不足");
  230. this.isCanClick = true;
  231. }
  232. }
  233. }
  234. /**查看红包*/
  235. private onClickCheck() {
  236. console.log("click check");
  237. WindowSystem.open("prefabs/ui/fight/fightRedbagUI", WindowOpenMode.NotCloseAndCover, [this.fightData]);
  238. }
  239. /**获取红包*/
  240. private async onClickGetRedbag() {
  241. if (!this.isCanClick) return;
  242. this.isCanClick = false;
  243. console.log("click get redbag", this.fightData);
  244. let result = await this.http.send("/api/pvp/ReceivePVPBonus", { key: this.fightData.redbagKey });
  245. if (result && result.code == HttpResponseCode.Success) {
  246. this.getReward(result.data.rewardType, result.data.rewardNum);
  247. this.updateBonusData();
  248. this.isCanClick = true;
  249. }
  250. else if (result && result.code == 115) {
  251. WindowSystem.showTips("已被领取");
  252. this.updateBonusData();
  253. this.isCanClick = true;
  254. }
  255. else {
  256. WindowSystem.showTips("请求失败");
  257. this.isCanClick = true;
  258. }
  259. }
  260. /**复仇*/
  261. private async onClickRevenge() {
  262. if (!this.isCanClick) return;
  263. this.isCanClick = false;
  264. console.log("click revenge");
  265. await this.PvpReady(2, this.fightData.id);
  266. this.isCanClick = true;
  267. }
  268. }