| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- /**
- * 管理dapp相关信息
- */
- import gameToast from "../Network/gameToast"
- cc.Class({
- extends: cc.Component,
- properties: {
- avatarFarmerSprite: {
- default: null,
- tip: "默认的头像",
- type: cc.SpriteFrame,
- serializable: true,
- },
- avatarVillageSprite: {
- default: null,
- tip: "村长的头像",
- type: cc.SpriteFrame,
- serializable: true,
- },
- avatarMayorSprite: {
- default: null,
- tip: "镇长的头像",
- type: cc.SpriteFrame,
- serializable: true,
- },
- avatarNode: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- avatarButtonLabel: {
- default: null,
- type: cc.Label,
- serializable: true,
- },
- avatarTipLabel: {
- default: null,
- type: cc.Label,
- serializable: true,
- },
- avatarButtonBG: {
- default: null,
- type: cc.Sprite,
- serializable: true,
- },
- avatarTipBG: {
- default: null,
- type: cc.Sprite,
- serializable: true,
- },
- farmeTextBg: {
- default: null,
- tip: "村民文字图片",
- type: cc.SpriteFrame,
- serializable: true,
- },
- villageTitle: {
- default: '任职村长',
- serializable: true,
- },
- villageContent: {
- default: '村长可获得村民50%的CNT消费.',
- serializable: true,
- },
- villageText: {
- default: null,
- tip: "村长文字图片",
- type: cc.SpriteFrame,
- serializable: true,
- },
- villageButton: {
- default: null,
- tip: "村长按钮图片",
- type: cc.SpriteFrame,
- serializable: true,
- },
- mayorTitle: {
- default: '任职镇长',
- serializable: true,
- },
- mayorContent: {
- default: '镇长获得村民10%的CNT,成\n为镇长之后,村长的收益不会消\n失.',
- serializable: true,
- },
- mayorText: {
- default: null,
- tip: "镇长文字图片",
- type: cc.SpriteFrame,
- serializable: true,
- },
- mayorButton: {
- default: null,
- tip: "镇长按钮图片",
- type: cc.SpriteFrame,
- serializable: true,
- },
- villageAndMayorNode: {
- default: null,
- type: cc.Node,
- serializable: true
- },
- ToastParent: {
- default: null,
- type: cc.Node,
- serializable: true
- },
- //两个按钮,cnt 和 snb
- cntShowNode: {
- default: null,
- type: cc.Node,
- serializable: true
- },
- snbShowNode: {
- default: null,
- type: cc.Node,
- serializable: true
- },
- ListInfoNode: {
- default: null,
- type: cc.Node,
- serializable: true
- },
- snbPage: 1,
- snbLimit: 100,
- snbData: {
- default: null,
- visible: false
- },
- cntPage: 1,
- cntLimit: 100,
- cntData: {
- default: null,
- visible: false
- },
- /**
- * 动态修改背景
- */
- manageMap: {
- default: null,
- type: cc.Node,
- serializable: true
- }
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- this.villageAndMayorScript = this.villageAndMayorNode.getComponent("villageAndMayor");
- //开启两个列表面板
- //先加载数据
- GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
- const { err, res } = data;
- // if (err === null) {
- // console.log(res) //
- // }
- this.cntData = res.data;
- });
- this.cntShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
- // let agent_level = GlobalD.Dapp.UserInfo.agent_level;
- // if(0 === agent_level){
- // GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请先升级村长!", 120);
- // return;
- // }
- this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, true);
- }, this);
- //snb数据
- GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
- // console.log("==========",data);
- this.snbData = data.content;
- });
- this.snbShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
- this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, true);
- }, this);
- },
- onUpdateCntList() {
- GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
- const { err, res } = data;
- this.cntData = res.data;
- this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, false);
- });
- },
- onUpdateSnbList() {
- GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
- // console.log("==========",data);
- this.snbData = data.content;
- this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, false);
- });
- },
- start() {
- /**
- * 用户信息初始化到头像部分
- *
- */
- // this.UserInfo = {
- // "id": 1, // 账户ID
- // "pid": 0, // 上级ID,0未绑定,显示可绑定邀请码的按钮
- // "address": "TDw6xsVnDJWsdRBLkWAwXbv4hE2X2JQs5z", // 账户地址
- // "invite_code": "MzhISO1Mzt", // 本人邀请码
- // "agent_level": 0, // 0普通用户,1 村长身份,2 镇长身份
- // "create_time": "2021-12-30 09:23:18",
- // "parent": null // 上级不存在,上级存在时,返回上级对象,属性同本消息体一致
- // }
- this.InitAvatarInfo();
- },
- InitAvatarInfo() {
- let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
- switch (agent_level) {
- case 0:
- this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
- // this.avatarButtonLabel.string = '申请村长';
- // this.avatarTipLabel.string = '村民';
- this.avatarButtonBG.spriteFrame = this.villageButton;
- this.avatarTipBG.spriteFrame = this.farmeTextBg;
- break;
- case 1:
- this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
- // this.avatarButtonLabel.string = '任职镇长';
- // this.avatarTipLabel.string = '村长';
- this.avatarButtonBG.spriteFrame = this.mayorButton;
- this.avatarTipBG.spriteFrame = this.villageText;
- GlobalD.TiledMap.setMapInfo(agent_level);
- break;
- case 2:
- this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
- // this.avatarButtonLabel.node.active = false;
- this.avatarButtonBG.node.parent.active = false;
- // this.avatarTipLabel.string = '镇长';
- this.avatarTipBG.spriteFrame = this.mayorText;
- GlobalD.TiledMap.setMapInfo(agent_level);
- break;
- }
- },
- /**
- * 根据按钮显示状态
- */
- onPromotionInfo() {
- let agent_level = GlobalD.Dapp.UserInfo.agent_level; // 0普通用户,1 村长身份,2 镇长身份
- switch (agent_level) {
- case 0:
- this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
- this.villageAndMayorScript.setVillageAndMayorInfo({
- title: this.villageTitle, content: this.villageContent, confirmText: '申请', callback: () => {
- this._becomeVillageChief();
- this.villageAndMayorNode.active = false;
- },
- type: "village"
- });
- break;
- case 1:
- this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
- this.villageAndMayorScript.setVillageAndMayorInfo({
- title: this.mayorTitle, content: this.mayorContent, confirmText: '任职', callback: () => {
- this._applyMayor();
- this.villageAndMayorNode.active = false;
- },
- type: "mayor"
- });
- break;
- case 2:
- this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
- console.log("你已经不需要升级了!");
- break;
- }
- },
- _becomeVillageChief() {
- if (!GlobalD.dapp) {
- console.error("onBecomeVillageChief:dapp未初始化!");
- return;
- }
- GlobalD.dapp.becomeVillageChief().then((data) => {
- console.log("申请村长:" + JSON.stringify(data));
- const [err, tx] = data;
- if (err === null) {
- console.log('申请成功:' + tx) // 申请提交成功,等待审核
- //成功设置村长状态,并且更新
- GlobalD.Dapp.UserInfo.agent_level = 1;
- this.InitAvatarInfo();
- GlobalD.GameData.showToast(this.ToastParent, "申请成功!", 2);
- } else {
- console.log(err) // 申请失败
- GlobalD.GameData.showToast(this.ToastParent, err, 3);
- }
- })
- // async function becomeInit() {
- // try {
- // const { err, tx } = await GlobalD.dapp.becomeVillageChief()
- // if (err === null) {
- // // TODO 成功, 任职村长需要区块确认,需要回调接口
- // console.log(tx) // 交易hash,唯一标识符
- // } else {
- // console.log(err)
- // }
- // } catch (err) {
- // console.error("申请村长:", JSON.stringify(err));
- // }
- // }
- // becomeInit();
- },
- _applyMayor() {
- if (!GlobalD.dapp) {
- console.error("onApplyMayor:dapp未初始化!");
- return;
- }
- let _self = this;
- GlobalD.dapp.applyMayor().then((data) => {
- console.log("任职镇长:", JSON.stringify(data));
- if (data.err === null) {
- console.log(data.res) // 申请提交成功,等待审核
- GlobalD.GameData.showToast(_self.ToastParent, data.res, 2);
- } else {
- console.log(data.err) // 申请失败
- GlobalD.GameData.showToast(_self.ToastParent, data.err, 3);
- }
- })
- }
- // update (dt) {},
- });
|