123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /**
- * 背包数据管理
- */
- let BagMgr = {
- // # 物品背包属性
- // .DataItemBag {
- // id 0: integer # 物品ID
- // count 1: integer # 物品数量
- // endTime 2: integer # 到期时间戳
- // }
- _items: [], // 背包基础信息
- init() {
- if (CC_EDITOR) {
- return;
- }
- this._items = [];
- this._initEventListener();
- this._initPublishListener();
- },
- _initEventListener() {
- // 通用消息
- G.PublicMgr.on(JMC.PUBLIC_MSG.SWITCH_ACCOUNT, JMC.PUBLIC_MSG_ORDER.BAG, this.handleDidSwitchAccount, this);
- G.PublicMgr.on(JMC.PUBLIC_MSG.LOGIN_SUCCESS, JMC.PUBLIC_MSG_ORDER.BAG, this.handleDidLoginSuccess, this);
- G.PublicMgr.on(JMC.PUBLIC_MSG.DISCONNECTED, JMC.PUBLIC_MSG_ORDER.BAG, this.handleDidDisconnected, this);
- },
- _initPublishListener() {
- cc.game.on('on_user_items', this.handleUpdateItems, this);
- },
- handleDidLoginSuccess() {
- this.requestInfo();
- },
- handleDidDisconnected() {
- },
- handleDidSwitchAccount() {
- this._items = [];
- },
- _addItemData(data) {
- let isUpdate = false;
- for (let [idx, itemData] of Object.entries(this._items)) {
- if (itemData.id == data.id) {
- isUpdate = true;
- if (data.count <= 0) {
- // 删除操作
- this._items.splice(idx, 1);
- } else {
- // 更新操作
- this._items[idx] = data;
- }
- break;
- }
- }
- if (!isUpdate && data.count > 0) {
- // new操作
- this._items.push(data);
- }
- },
- //* ************* 用户信息获取 ************* *//
- getItemDataById(itemid) {
- for (const data of this._items) {
- if (data.id == itemid) {
- return data;
- }
- }
- return undefined;
- },
- getItemNumById(itemid) {
- for (const data of this._items) {
- if (data.id == itemid) {
- return data.count;
- }
- }
- return 0;
- },
- //* ************* 客户端请求/响应 ************* *//
- requestInfo() {
- G.NetworkMgr.sendSocketRequest('bag_get_info', {}, this._responseBagGetInfo.bind(this));
- },
- _responseBagGetInfo(data) {
- let responseInfo = data.responseInfo;
- if (responseInfo.code === 200) {
- // 玩家基础信息
- this._items = responseInfo.items;
- G.PublicMgr.emit(JMC.PUBLIC_MSG.BAG_INFO);
- } else {
- // 断开网络并且弹出重连窗口
- G.NetworkMgr.closeSocket();
- G.AppUtils.getSceneCtrl().showOfflineAlert();
- }
- },
- _onUserItems(data) {
- if (!data || !data.items || data.items.length == 0) return;
- for (const item of data.items) {
- this._addItemData(item);
- }
- }
- }
- module.exports = BagMgr;
|