lua -- 商店的数据管理类

时间:2023-03-09 17:50:33
lua -- 商店的数据管理类
module (..., package.seeall)
local ShopM = {}
local SystemPrompt = require(__APP_PACKAGE_NAME__ .. ".scenes.common.SystemPrompt")
local Item = require("res.scripts.data.model.Item"); function ShopM:init()
-- 每个数据类都有一个对应的id
self.mid = getNextID();
self.refeshCountDown =
self.CurrentItem = {};
self.NowItem = nil;
-- 从配置表中获取配置数据,这里是以json的形式
self.shopprop = DB.getTable("shopprop")[]
self.shopitemprop = DB.getTable("shopitemprop")
if self.shopprop == nil then
printf("shopprop is nil");
end
if self.shopitemprop == nil then
printf("shopitemprop is nil");
end
-- 注册网络消息,用于接收服务器返回的数据,并对数据进行处理
NetController:registerMsg(MsgID.msgid_P_OPSHOP_ACK, self.mid, function ( dwSessionID, wMsgSID, wMsgID, pszMsg )
print("==============打开背包界面==============")
-- 将json数据转成表的格式
self.openNetData = CJson.decode(pszMsg);
if ~= self.openNetData.ErrCode then
SystemPrompt.new(Errdef.ERR_ALL[self.openNetData.ErrCode]);
SceneM.destroyNetLayer();
return
end
-- 处理数据
self:setData(self.openNetData);
-- 发送本地的事件消息
NotificationCenter:postEvent(MsgID.msgid_P_OPSHOP_ACK, {});
end, MAXPRIORITY); NetController:registerMsg(MsgID.msgid_P_BITEM_ACK, self.mid, function ( dwSessionID, wMsgSID, wMsgID, pszMsg )
print("==============购买道具==============")
self.openNetData = CJson.decode(pszMsg);
if ~= self.openNetData.ErrCode then
UISystemTips:SystemTips(self.openNetData.ErrCode);
return
end
self:setDataAfterBuy(self.openNetData);
NotificationCenter:postEvent(MsgID.msgid_P_BITEM_ACK, {});
end, MAXPRIORITY); NetController:registerMsg(MsgID.msgid_P_RITEM_ACK, self.mid, function ( dwSessionID, wMsgSID, wMsgID, pszMsg )
print("==============刷新道具==============")
self.openNetData = CJson.decode(pszMsg);
if ~= self.openNetData.ErrCode then
SystemPrompt.new(Errdef.ERR_ALL[self.openNetData.ErrCode]);
SceneM.destroyNetLayer();
return
end
self:setData(self.openNetData);
NotificationCenter:postEvent(MsgID.msgid_P_RITEM_ACK, {});
end, MAXPRIORITY); end function ShopM:cleanup( ) end function ShopM:setData( data )
self.itemList = {};
self.gridList = {};
local pos = ;
local itemInfo = data.ItemInfo
for i = , #itemInfo do
print("========ShopM:setData========" .. itemInfo[i].KindID);
local kindID = itemInfo[i].KindID
local num = itemInfo[i].Num
local itemType = math.modf(kindID/);
local itemData={ID=kindID, Idx=, Level=, Num=num}
self.itemList[pos] = Item.new(itemType, pos, itemData)
if kindID == then
self.itemList[pos].sell = -;
end
pos = pos+
end printf("ShopM:setData RCD:" .. data.RCD)
self.refeshCountDown = data.RCD
PlayerM:getBaseData().coin = data.CurCoin;
PlayerM:getBaseData().gold = data.CurGold;
end function ShopM:setDataAfterBuy( data )
printf(" before buy num:" .. #self.itemList)
if self.itemList[data.GridIdx] == nil then
printf(" item[" .. data.GridIdx .. "] is nil")
end
self.itemList[data.GridIdx].sell = -;
printf(" after buy num:" .. #self.itemList)
PlayerM:getBaseData().coin = data.CurCoin;
PlayerM:getBaseData().gold = data.CurGold;
end function ShopM:getItemLst( )
return self.itemList
end function ShopM:getShowGridNum( )
return -- todo
end function ShopM:getRefeshCountDown( )
return self.refeshCountDown
end function ShopM:getRefeshGold( )
if self.shopprop == nil then
return
end
return self.shopprop.NoCDGold
end
function ShopM:getRefeshCoin( )
printf("getRefeshCoin")
if self.shopprop == nil then
printf("getRefeshCoin2")
return
end
printf("getRefeshCoin3")
return self.shopprop.RefreshCoin
end function ShopM:getBuyGold( gridIdx )
printf("getBuyGold:" .. gridIdx)
if self.shopitemprop == nil then
printf("getBuyGold2")
return
end local gridProp = self.shopitemprop[gridIdx]
if gridProp == nil then
printf("getBuyGold3")
return
end
printf("======gridProp.BuyGold=======" .. gridProp.BuyGold);
return gridProp.BuyGold
end function ShopM:getBuyCoin( gridIdx )
if self.shopitemprop == nil then
return
end local gridProp = self.shopitemprop[gridIdx]
if gridProp == nil then
return
end
return gridProp.BuyCoin
end -- 这里要有一个return
return ShopM;