ManageDapp.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // LIFE-CYCLE CALLBACKS:
  137. onLoad() {
  138. this.villageAndMayorScript = this.villageAndMayorNode.getComponent("villageAndMayor");
  139. //开启两个列表面板
  140. //先加载数据
  141. GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
  142. const { err, res } = data;
  143. // if (err === null) {
  144. // console.log(res) //
  145. // }
  146. this.cntData = res.data;
  147. });
  148. this.cntShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
  149. // let agent_level = GlobalD.Dapp.UserInfo.agent_level;
  150. // if(0 === agent_level){
  151. // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请先升级村长!", 120);
  152. // return;
  153. // }
  154. this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, true);
  155. }, this);
  156. //snb数据
  157. GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
  158. // console.log("==========",data);
  159. this.snbData = data.content;
  160. });
  161. this.snbShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
  162. this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, true);
  163. }, this);
  164. },
  165. onUpdateCntList() {
  166. GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
  167. const { err, res } = data;
  168. this.cntData = res.data;
  169. this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, false);
  170. });
  171. },
  172. onUpdateSnbList() {
  173. GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
  174. // console.log("==========",data);
  175. this.snbData = data.content;
  176. this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, false);
  177. });
  178. },
  179. start() {
  180. /**
  181. * 用户信息初始化到头像部分
  182. *
  183. */
  184. // this.UserInfo = {
  185. // "id": 1, // 账户ID
  186. // "pid": 0, // 上级ID,0未绑定,显示可绑定邀请码的按钮
  187. // "address": "TDw6xsVnDJWsdRBLkWAwXbv4hE2X2JQs5z", // 账户地址
  188. // "invite_code": "MzhISO1Mzt", // 本人邀请码
  189. // "agent_level": 0, // 0普通用户,1 村长身份,2 镇长身份
  190. // "create_time": "2021-12-30 09:23:18",
  191. // "parent": null // 上级不存在,上级存在时,返回上级对象,属性同本消息体一致
  192. // }
  193. this.InitAvatarInfo();
  194. },
  195. InitAvatarInfo() {
  196. let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
  197. switch (agent_level) {
  198. case 0:
  199. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
  200. // this.avatarButtonLabel.string = '申请村长';
  201. // this.avatarTipLabel.string = '村民';
  202. this.avatarButtonBG.spriteFrame = this.villageButton;
  203. this.avatarTipBG.spriteFrame = this.farmeTextBg;
  204. break;
  205. case 1:
  206. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
  207. // this.avatarButtonLabel.string = '任职镇长';
  208. // this.avatarTipLabel.string = '村长';
  209. this.avatarButtonBG.spriteFrame = this.mayorButton;
  210. this.avatarTipBG.spriteFrame = this.villageText;
  211. break;
  212. case 2:
  213. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
  214. // this.avatarButtonLabel.node.active = false;
  215. this.avatarButtonBG.node.parent.active = false;
  216. // this.avatarTipLabel.string = '镇长';
  217. this.avatarTipBG.spriteFrame = this.mayorText;
  218. break;
  219. }
  220. },
  221. /**
  222. * 根据按钮显示状态
  223. */
  224. onPromotionInfo() {
  225. let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
  226. switch (agent_level) {
  227. case 0:
  228. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
  229. this.villageAndMayorScript.setVillageAndMayorInfo({
  230. title: this.villageTitle, content: this.villageContent, confirmText: '申请', callback: () => {
  231. this._becomeVillageChief();
  232. this.villageAndMayorNode.active = false;
  233. },
  234. type: "village"
  235. });
  236. break;
  237. case 1:
  238. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
  239. this.villageAndMayorScript.setVillageAndMayorInfo({
  240. title: this.mayorTitle, content: this.mayorContent, confirmText: '任职', callback: () => {
  241. this._applyMayor();
  242. this.villageAndMayorNode.active = false;
  243. },
  244. type: "mayor"
  245. });
  246. break;
  247. case 2:
  248. this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
  249. console.log("你已经不需要升级了!");
  250. break;
  251. }
  252. },
  253. _becomeVillageChief() {
  254. if (!GlobalD.dapp) {
  255. console.error("onBecomeVillageChief:dapp未初始化!");
  256. return;
  257. }
  258. GlobalD.dapp.becomeVillageChief().then((data) => {
  259. console.log("申请村长:" + JSON.stringify(data));
  260. const [err, tx] = data;
  261. if (err === null) {
  262. console.log('申请成功:' + tx) // 申请提交成功,等待审核
  263. //成功设置村长状态,并且更新
  264. GlobalD.Dapp.UserInfo.agent_level = 1;
  265. this.InitAvatarInfo();
  266. GlobalD.GameData.showToast(this.ToastParent, "申请成功!", 3, () => {
  267. console.log("finish toast!");
  268. });
  269. } else {
  270. console.log(err) // 申请失败
  271. GlobalD.GameData.showToast(this.ToastParent, err, 3, () => {
  272. console.log("finish toast!");
  273. });
  274. }
  275. })
  276. // async function becomeInit() {
  277. // try {
  278. // const { err, tx } = await GlobalD.dapp.becomeVillageChief()
  279. // if (err === null) {
  280. // // TODO 成功, 任职村长需要区块确认,需要回调接口
  281. // console.log(tx) // 交易hash,唯一标识符
  282. // } else {
  283. // console.log(err)
  284. // }
  285. // } catch (err) {
  286. // console.error("申请村长:", JSON.stringify(err));
  287. // }
  288. // }
  289. // becomeInit();
  290. },
  291. _applyMayor() {
  292. if (!GlobalD.dapp) {
  293. console.error("onApplyMayor:dapp未初始化!");
  294. return;
  295. }
  296. let _self = this;
  297. GlobalD.dapp.applyMayor().then((data) => {
  298. console.log("任职镇长:", JSON.stringify(data));
  299. if (data.err === null) {
  300. console.log(data.res) // 申请提交成功,等待审核
  301. GlobalD.GameData.showToast(_self.ToastParent, data.res, 3, () => {
  302. console.log("finish toast!");
  303. });
  304. } else {
  305. console.log(data.err) // 申请失败
  306. GlobalD.GameData.showToast(_self.ToastParent, data.err, 3, () => {
  307. console.log("finish toast!");
  308. });
  309. }
  310. })
  311. }
  312. // update (dt) {},
  313. });