ManageDapp.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. manageMap: {
  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. GlobalD.TiledMap.setMapInfo(agent_level);
  220. break;
  221. case 2:
  222. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
  223. // this.avatarButtonLabel.node.active = false;
  224. this.avatarButtonBG.node.parent.active = false;
  225. // this.avatarTipLabel.string = '镇长';
  226. this.avatarTipBG.spriteFrame = this.mayorText;
  227. GlobalD.TiledMap.setMapInfo(agent_level);
  228. break;
  229. }
  230. },
  231. /**
  232. * 根据按钮显示状态
  233. */
  234. onPromotionInfo() {
  235. let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
  236. switch (agent_level) {
  237. case 0:
  238. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
  239. this.villageAndMayorScript.setVillageAndMayorInfo({
  240. title: this.villageTitle, content: this.villageContent, confirmText: '申请', callback: () => {
  241. this._becomeVillageChief();
  242. this.villageAndMayorNode.active = false;
  243. },
  244. type: "village"
  245. });
  246. break;
  247. case 1:
  248. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
  249. this.villageAndMayorScript.setVillageAndMayorInfo({
  250. title: this.mayorTitle, content: this.mayorContent, confirmText: '任职', callback: () => {
  251. this._applyMayor();
  252. this.villageAndMayorNode.active = false;
  253. },
  254. type: "mayor"
  255. });
  256. break;
  257. case 2:
  258. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
  259. console.log("你已经不需要升级了!");
  260. break;
  261. }
  262. },
  263. _becomeVillageChief() {
  264. if (!GlobalD.dapp) {
  265. console.error("onBecomeVillageChief:dapp未初始化!");
  266. return;
  267. }
  268. GlobalD.dapp.becomeVillageChief().then((data) => {
  269. console.log("申请村长:" + JSON.stringify(data));
  270. const [err, tx] = data;
  271. if (err === null) {
  272. console.log('申请成功:' + tx) // 申请提交成功,等待审核
  273. //成功设置村长状态,并且更新
  274. GlobalD.Dapp.UserInfo.agent_level = 1;
  275. this.InitAvatarInfo();
  276. GlobalD.GameData.showToast(this.ToastParent, "申请成功!", 2);
  277. } else {
  278. console.log(err) // 申请失败
  279. GlobalD.GameData.showToast(this.ToastParent, err, 3);
  280. }
  281. })
  282. // async function becomeInit() {
  283. // try {
  284. // const { err, tx } = await GlobalD.dapp.becomeVillageChief()
  285. // if (err === null) {
  286. // // TODO 成功, 任职村长需要区块确认,需要回调接口
  287. // console.log(tx) // 交易hash,唯一标识符
  288. // } else {
  289. // console.log(err)
  290. // }
  291. // } catch (err) {
  292. // console.error("申请村长:", JSON.stringify(err));
  293. // }
  294. // }
  295. // becomeInit();
  296. },
  297. _applyMayor() {
  298. if (!GlobalD.dapp) {
  299. console.error("onApplyMayor:dapp未初始化!");
  300. return;
  301. }
  302. let _self = this;
  303. GlobalD.dapp.applyMayor().then((data) => {
  304. console.log("任职镇长:", JSON.stringify(data));
  305. if (data.err === null) {
  306. console.log(data.res) // 申请提交成功,等待审核
  307. GlobalD.GameData.showToast(_self.ToastParent, data.res, 2);
  308. } else {
  309. console.log(data.err) // 申请失败
  310. GlobalD.GameData.showToast(_self.ToastParent, data.err, 3);
  311. }
  312. })
  313. }
  314. // update (dt) {},
  315. });