ManageDapp.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. * 管理dapp相关信息
  3. */
  4. import gameToast from "../Network/gameToast"
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. avatarFarmerSprite: {
  9. default: null,
  10. tip: "默认的头像",
  11. type: cc.SpriteFrame,
  12. serializable: true,
  13. },
  14. avatarVillageSprite: {
  15. default: null,
  16. tip: "村长的头像",
  17. type: cc.SpriteFrame,
  18. serializable: true,
  19. },
  20. avatarMayorSprite: {
  21. default: null,
  22. tip: "镇长的头像",
  23. type: cc.SpriteFrame,
  24. serializable: true,
  25. },
  26. avatarNode: {
  27. default: null,
  28. type: cc.Node,
  29. serializable: true,
  30. },
  31. avatarButtonLabel: {
  32. default: null,
  33. type: cc.Label,
  34. serializable: true,
  35. },
  36. avatarTipLabel: {
  37. default: null,
  38. type: cc.Label,
  39. serializable: true,
  40. },
  41. avatarButtonBG: {
  42. default: null,
  43. type: cc.Sprite,
  44. serializable: true,
  45. },
  46. avatarTipBG: {
  47. default: null,
  48. type: cc.Sprite,
  49. serializable: true,
  50. },
  51. farmeTextBg: {
  52. default: null,
  53. tip: "村民文字图片",
  54. type: cc.SpriteFrame,
  55. serializable: true,
  56. },
  57. villageTitle: {
  58. default: '任职村长',
  59. serializable: true,
  60. },
  61. villageContent: {
  62. default: '村长可获得村民50%的CNT消费.',
  63. serializable: true,
  64. },
  65. villageText: {
  66. default: null,
  67. tip: "村长文字图片",
  68. type: cc.SpriteFrame,
  69. serializable: true,
  70. },
  71. villageButton: {
  72. default: null,
  73. tip: "村长按钮图片",
  74. type: cc.SpriteFrame,
  75. serializable: true,
  76. },
  77. mayorTitle: {
  78. default: '任职镇长',
  79. serializable: true,
  80. },
  81. mayorContent: {
  82. default: '镇长获得村民10%的CNT,成\n为镇长之后,村长的收益不会消\n失.',
  83. serializable: true,
  84. },
  85. mayorText: {
  86. default: null,
  87. tip: "镇长文字图片",
  88. type: cc.SpriteFrame,
  89. serializable: true,
  90. },
  91. mayorButton: {
  92. default: null,
  93. tip: "镇长按钮图片",
  94. type: cc.SpriteFrame,
  95. serializable: true,
  96. },
  97. villageAndMayorNode: {
  98. default: null,
  99. type: cc.Node,
  100. serializable: true
  101. },
  102. ToastParent: {
  103. default: null,
  104. type: cc.Node,
  105. serializable: true
  106. },
  107. //两个按钮,cnt 和 snb
  108. cntShowNode: {
  109. default: null,
  110. type: cc.Node,
  111. serializable: true
  112. },
  113. snbShowNode: {
  114. default: null,
  115. type: cc.Node,
  116. serializable: true
  117. },
  118. ListInfoNode: {
  119. default: null,
  120. type: cc.Node,
  121. serializable: true
  122. },
  123. snbPage: 1,
  124. snbLimit: 100,
  125. snbData: {
  126. default: null,
  127. visible: false
  128. },
  129. cntPage: 1,
  130. cntLimit: 100,
  131. cntData: {
  132. default: null,
  133. visible: false
  134. },
  135. /**
  136. * 动态修改背景
  137. */
  138. dynamicLoadingBg: {
  139. default: null,
  140. type: cc.Node,
  141. serializable: true
  142. }
  143. },
  144. // LIFE-CYCLE CALLBACKS:
  145. onLoad() {
  146. this.villageAndMayorScript = this.villageAndMayorNode.getComponent("villageAndMayor");
  147. //开启两个列表面板
  148. //先加载数据
  149. GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
  150. const { err, res } = data;
  151. // if (err === null) {
  152. // console.log(res) //
  153. // }
  154. this.cntData = res.data;
  155. });
  156. this.cntShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
  157. // let agent_level = GlobalD.Dapp.UserInfo.agent_level;
  158. // if(0 === agent_level){
  159. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请先升级村长!", 120);
  160. // return;
  161. // }
  162. this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, true);
  163. }, this);
  164. //snb数据
  165. GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
  166. // console.log("==========",data);
  167. this.snbData = data.content;
  168. });
  169. this.snbShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
  170. this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, true);
  171. }, this);
  172. },
  173. onUpdateCntList() {
  174. GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
  175. const { err, res } = data;
  176. this.cntData = res.data;
  177. this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, false);
  178. });
  179. },
  180. onUpdateSnbList() {
  181. GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
  182. // console.log("==========",data);
  183. this.snbData = data.content;
  184. this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, false);
  185. });
  186. },
  187. start() {
  188. /**
  189. * 用户信息初始化到头像部分
  190. *
  191. */
  192. // this.UserInfo = {
  193. // "id": 1, // 账户ID
  194. // "pid": 0, // 上级ID,0未绑定,显示可绑定邀请码的按钮
  195. // "address": "TDw6xsVnDJWsdRBLkWAwXbv4hE2X2JQs5z", // 账户地址
  196. // "invite_code": "MzhISO1Mzt", // 本人邀请码
  197. // "agent_level": 0, // 0普通用户,1 村长身份,2 镇长身份
  198. // "create_time": "2021-12-30 09:23:18",
  199. // "parent": null // 上级不存在,上级存在时,返回上级对象,属性同本消息体一致
  200. // }
  201. this.InitAvatarInfo();
  202. },
  203. InitAvatarInfo() {
  204. let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
  205. switch (agent_level) {
  206. case 0:
  207. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
  208. // this.avatarButtonLabel.string = '申请村长';
  209. // this.avatarTipLabel.string = '村民';
  210. this.avatarButtonBG.spriteFrame = this.villageButton;
  211. this.avatarTipBG.spriteFrame = this.farmeTextBg;
  212. break;
  213. case 1:
  214. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
  215. // this.avatarButtonLabel.string = '任职镇长';
  216. // this.avatarTipLabel.string = '村长';
  217. this.avatarButtonBG.spriteFrame = this.mayorButton;
  218. this.avatarTipBG.spriteFrame = this.villageText;
  219. this.dynamicLoadingBg.getComponent("DynamicLoadingBg").setInfo({
  220. type: "village"
  221. });
  222. break;
  223. case 2:
  224. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
  225. // this.avatarButtonLabel.node.active = false;
  226. this.avatarButtonBG.node.parent.active = false;
  227. // this.avatarTipLabel.string = '镇长';
  228. this.avatarTipBG.spriteFrame = this.mayorText;
  229. this.dynamicLoadingBg.getComponent("DynamicLoadingBg").setInfo({
  230. type: "mayor"
  231. });
  232. break;
  233. }
  234. },
  235. /**
  236. * 根据按钮显示状态
  237. */
  238. onPromotionInfo() {
  239. let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
  240. switch (agent_level) {
  241. case 0:
  242. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
  243. this.villageAndMayorScript.setVillageAndMayorInfo({
  244. title: this.villageTitle, content: this.villageContent, confirmText: '申请', callback: () => {
  245. this._becomeVillageChief();
  246. this.villageAndMayorNode.active = false;
  247. },
  248. type: "village"
  249. });
  250. break;
  251. case 1:
  252. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
  253. this.villageAndMayorScript.setVillageAndMayorInfo({
  254. title: this.mayorTitle, content: this.mayorContent, confirmText: '任职', callback: () => {
  255. this._applyMayor();
  256. this.villageAndMayorNode.active = false;
  257. },
  258. type: "mayor"
  259. });
  260. break;
  261. case 2:
  262. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
  263. console.log("你已经不需要升级了!");
  264. break;
  265. }
  266. },
  267. _becomeVillageChief() {
  268. if (!GlobalD.dapp) {
  269. console.error("onBecomeVillageChief:dapp未初始化!");
  270. return;
  271. }
  272. GlobalD.dapp.becomeVillageChief().then((data) => {
  273. console.log("申请村长:" + JSON.stringify(data));
  274. const [err, tx] = data;
  275. if (err === null) {
  276. console.log('申请成功:' + tx) // 申请提交成功,等待审核
  277. //成功设置村长状态,并且更新
  278. GlobalD.Dapp.UserInfo.agent_level = 1;
  279. this.InitAvatarInfo();
  280. GlobalD.GameData.showToast(this.ToastParent, "申请成功!", 3, () => {
  281. console.log("finish toast!");
  282. });
  283. } else {
  284. console.log(err) // 申请失败
  285. GlobalD.GameData.showToast(this.ToastParent, err, 3, () => {
  286. console.log("finish toast!");
  287. });
  288. }
  289. })
  290. // async function becomeInit() {
  291. // try {
  292. // const { err, tx } = await GlobalD.dapp.becomeVillageChief()
  293. // if (err === null) {
  294. // // TODO 成功, 任职村长需要区块确认,需要回调接口
  295. // console.log(tx) // 交易hash,唯一标识符
  296. // } else {
  297. // console.log(err)
  298. // }
  299. // } catch (err) {
  300. // console.error("申请村长:", JSON.stringify(err));
  301. // }
  302. // }
  303. // becomeInit();
  304. },
  305. _applyMayor() {
  306. if (!GlobalD.dapp) {
  307. console.error("onApplyMayor:dapp未初始化!");
  308. return;
  309. }
  310. let _self = this;
  311. GlobalD.dapp.applyMayor().then((data) => {
  312. console.log("任职镇长:", JSON.stringify(data));
  313. if (data.err === null) {
  314. console.log(data.res) // 申请提交成功,等待审核
  315. GlobalD.GameData.showToast(_self.ToastParent, data.res, 3, () => {
  316. console.log("finish toast!");
  317. });
  318. } else {
  319. console.log(data.err) // 申请失败
  320. GlobalD.GameData.showToast(_self.ToastParent, data.err, 3, () => {
  321. console.log("finish toast!");
  322. });
  323. }
  324. })
  325. }
  326. // update (dt) {},
  327. });