| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- local RemoteProxy = requireProxy("remote/RemoteProxy")
- local NPCStorageProxy = class("NPCStorageProxy", RemoteProxy)
- NPCStorageProxy.NAME = global.ProxyTable.NPCStorageProxy
- function NPCStorageProxy:ctor()
- NPCStorageProxy.super.ctor(self)
- local StorageVO = requireVO("remote/StorageVO")
- self.VOdata = StorageVO.new()
- self.storageData = {}
- self.maxPage = 1 -- 仓库的最大页
- self.npcIndex = nil
- self.touchType = 0
- self.openedCount = 0
- self.pageIndex = 1
- self._schedule_id = nil --请求存储的计时器 0.5s
- end
- function NPCStorageProxy:LoadConfig()
- local maxNum = SL:GetMetaValue("GAME_DATA","warehouse_max_num") or 240
- self.maxPage = math.ceil(maxNum / (GUIDefine.STORAGE_PER_PAGE_MAX or global.MMO.NPC_STORAGE_MAX_PAGE))
- end
- -- 获取仓库的最大页
- function NPCStorageProxy:GetMaxPage()
- return self.maxPage
- end
- function NPCStorageProxy:SetStorageData(data)
- local newList = {}
- for _, v in ipairs(data) do
- local changeData = ChangeItemServersSendDatas(v)
- table.insert(newList, changeData)
- end
- self.storageData = newList or {}
- -- clear data
- self.VOdata:ClearItemData()
- -- 更新位置
- self.VOdata:AmendHistoryPos(newList)
- local makeindex = {}
- for _, v in ipairs(newList) do
- local itemPos = self.VOdata:GetHistoryPos(v.MakeIndex) or self.VOdata:NewItemPos()
- if itemPos then
- self.VOdata:SetItemPosData(v.MakeIndex, itemPos)
- end
- self.VOdata:AddItem(v, itemPos)
- end
- self.VOdata:SetPosMarkData()
- -- 重新排列数据
- self.storageData = {}
- local data = {}
- data = self.VOdata:GetStorageData()
- for i, v in pairs(data) do
- local pos = self.VOdata:GetStoragePosByMakeIndex(v.MakeIndex)
- if pos then
- self.storageData[pos] = v
- end
- end
- end
- function NPCStorageProxy:GetStorageData()
- return self.storageData
- end
- function NPCStorageProxy:SetStoragePageIndex(Index)
- self.pageIndex = math.min(Index or 1, self.maxPage)
- end
- function NPCStorageProxy:GetStoragePageIndex()
- return self.pageIndex
- end
- function NPCStorageProxy:GetStorageDataByPage(page)
- local newList = {}
- if not page then
- return newList
- end
- for k, v in pairs(self.storageData) do
- if v.Where == page then
- table.insert(newList, v)
- end
- end
-
- return newList
- end
- function NPCStorageProxy:SetNpcIndex(idx)
- self.npcIndex = idx
- end
- function NPCStorageProxy:GetNpcIndex()
- return self.npcIndex
- end
- function NPCStorageProxy:SetOpenSize(openCount)
- local size = SL:GetMetaValue("GAME_DATA","warehouse_num")
- local setBaseSize = size and tonumber(size)
- local base = setBaseSize or 24
- local open = openCount or 0
- self.openedCount = base + open
- if self.openedCount > 240 then
- self.openedCount = 240
- end
- self.VOdata:SetMaxStorage(self.openedCount)
- self.VOdata:SetNewStoragePosMark()
- end
- function NPCStorageProxy:GetOpenSize()
- return self.openedCount
- end
- function NPCStorageProxy:GetTouchType()
- return self.touchType
- end
- function NPCStorageProxy:SetTouchType(type)
- self.touchType = type
- end
- function NPCStorageProxy:CleanData()
- self.storageData = {}
- self.npcIndex = nil
- if self._schedule_id then
- UnSchedule(self._schedule_id)
- self._schedule_id = nil
- end
- end
- function NPCStorageProxy:AddStorageData(data, where)
- local itemData = data
- if not itemData or next(itemData) == nil then
- return
- end
- -- 记录位置
- local itemPos = self.VOdata:GetHistoryPos(itemData.MakeIndex) or self.VOdata:NewItemPos()
- if itemPos then
- self.VOdata:AddItem(itemData, itemPos)
- self.VOdata:SetItemPosData(itemData.MakeIndex, itemPos)
- self.VOdata:SetPosMarkData()
- end
- itemData.Where = where
- table.insert(self.storageData, itemData)
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, where)
- local data = {
- opera = global.MMO.Operator_Add,
- item = itemData,
- page = where
- }
- SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
- end
- function NPCStorageProxy:DeleteStorageData(data)
- if data and data.MakeIndex and self.storageData and next(self.storageData) then
- for k, v in pairs(self.storageData) do
- if data.MakeIndex and v.MakeIndex and data.MakeIndex == v.MakeIndex then
- -- 记录位置
- self.VOdata:SubItem(data.MakeIndex)
- self.VOdata:CleanItemPosData(data.MakeIndex)
- self.VOdata:SetPosMarkData()
- table.remove(self.storageData, k)
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, data.Where)
- local data = {
- opera = global.MMO.Operator_Sub,
- item = data,
- page = data.Where
- }
- SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
- break
- end
- end
- end
- end
- -- 更新物品的叠加数据
- function NPCStorageProxy:UpdateStorageData(MakeIndex, nums)
- if MakeIndex then
- for _, v in pairs(self.storageData) do
- if v.MakeIndex and MakeIndex == v.MakeIndex then
- v.OverLap = nums
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, v.Where)
- local data = {
- opera = global.MMO.Operator_Change,
- item = v,
- page = v.Where
- }
- SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
- break
- end
- end
- end
- end
- function NPCStorageProxy:GetStorageDataByMakeIndex(MakeIndex)
- local MakeIndex = MakeIndex or 0
- local data = nil
- for _, v in pairs(self.storageData) do
- if MakeIndex and v.MakeIndex and MakeIndex == v.MakeIndex then
- data = v
- break
- end
- end
- return data
- end
- -- 整理仓库
- function NPCStorageProxy:ArrangeStorageData(page)
- local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
- local function sortFunc(a, b)
- local powera = GUIFunction:GetEquipPower(a)
- local powerb = GUIFunction:GetEquipPower(b)
- if powera ~= powerb then
- return powera > powerb
- else
- local isBindA, bindIndexA = ItemConfigProxy:CheckItemBind(a.Index)
- local isBindB, bindIndexB = ItemConfigProxy:CheckItemBind(b.Index)
- if isBindA == isBindB then
- return a.Index < b.Index
- else
- return isBindA
- end
- end
- end
- local data = {}
- for _, v in pairs(self.storageData) do
- table.insert(data, v)
- end
- if not data or next(data) == nil then
- return
- end
- -- 按照战力排序
- table.sort(data, sortFunc)
- -- 更新位置
- self.VOdata:CleanStoragePosData()
- self.VOdata:AmendHistoryPos(data)
- self.VOdata:SetPosMarkData()
- -- 重新排列数据
- self.storageData = {}
- local data = {}
- data = self.VOdata:GetStorageData()
- for i, v in pairs(data) do
- local pos = self.VOdata:GetStoragePosByMakeIndex(v.MakeIndex)
- if pos then
- self.storageData[pos] = v
- end
- end
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, page)
- local data = {
- opera = 4, -- 整理/刷新页
- page = page
- }
- SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
- end
- -- 请求存储
- function NPCStorageProxy:RequestSaveToStorage(MakeIndex, Name)
- local npcIndex = self:GetNpcIndex()
- if not npcIndex then
- return
- end
- if not self:CheckPutInStorageState() or self._schedule_id then
- local systipStr = self._schedule_id and GET_STRING(90100005) or GET_STRING(90100004)
- global.Facade:sendNotification(global.NoticeTable.SystemTips, systipStr)
- -- 背包中的重新刷出来
- local data = {
- storage = {
- state = 1,
- MakeIndex = MakeIndex,
- }
- }
- global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
- return
- end
- self._schedule_id = PerformWithDelayGlobal(function()
- self._schedule_id = nil
- end, 0.3)
- local pageIndex = self:GetStoragePageIndex() or 1
- LuaSendMsg(global.MsgType.MSG_CS_STORAGE_STORE_REQUEST, npcIndex, MakeIndex, pageIndex, 0, Name, string.len(Name))
- end
- function NPCStorageProxy:RequestSaveToStorageAllPage(MakeIndex, Name, setPageIndex)
- --请求存储
- local npcIndex = self:GetNpcIndex()
- if not npcIndex then
- return
- end
- local pageIndex = 1
- local needCheckSpace = false
- if setPageIndex then
- pageIndex = setPageIndex
- needCheckSpace = true
- else
- for i = 1, self.maxPage do
- local index = i
- local openCount = self.openedCount
- local pageData = self:GetStorageDataByPage(index)
- local itemCount = #pageData
- local pageMaxNum = GUIDefine.STORAGE_PER_PAGE_MAX or global.MMO.NPC_STORAGE_MAX_PAGE
- local thisPageOpenCount = openCount - (index-1) * pageMaxNum
- if thisPageOpenCount > pageMaxNum then
- thisPageOpenCount = pageMaxNum
- end
- if itemCount < thisPageOpenCount then
- pageIndex = i
- break
- end
- pageIndex = nil
- end
- end
- if not pageIndex or self._schedule_id or (needCheckSpace and not self:CheckPutInStorageState()) then
- local systipStr = self._schedule_id and GET_STRING(90100005) or GET_STRING(90100004)
- global.Facade:sendNotification(global.NoticeTable.SystemTips, systipStr)
- -- 背包中的重新刷出来
- local data = {
- storage = {
- MakeIndex = MakeIndex,
- state = 1
- }
- }
- global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
- return
- end
- self._schedule_id = PerformWithDelayGlobal(function()
- self._schedule_id = nil
- end, 0.3)
- LuaSendMsg(global.MsgType.MSG_CS_STORAGE_STORE_REQUEST, npcIndex, MakeIndex, pageIndex, 0, Name, string.len(Name))
- end
- function NPCStorageProxy:CheckPutInStorageState()
- local index = self:GetStoragePageIndex()
- local pageData = self:GetStorageDataByPage(index)
- local itemCount = #pageData
- local pageMaxNum = GUIDefine.STORAGE_PER_PAGE_MAX or global.MMO.NPC_STORAGE_MAX_PAGE
- local thisPageOpenCount = self.openedCount - (index - 1) * pageMaxNum
- local isWinMode = SL:GetMetaValue("WINPLAYMODE")
- local storage_row_col = SL:GetMetaValue("GAME_DATA", "bag_storage_row_col_max")
- local bBig = false -- 是否大仓库
- if isWinMode and storage_row_col then
- local slices = string.split(storage_row_col, "|")
- local row = tonumber(slices[1]) or 8
- local col = tonumber(slices[2]) or 6
- if row * col > pageMaxNum then
- bBig = true
- end
- end
- if not bBig and thisPageOpenCount > pageMaxNum then
- thisPageOpenCount = pageMaxNum
- end
- return itemCount < thisPageOpenCount
- end
- --请求仓库数据
- function NPCStorageProxy:RequestStorageData()
- LuaSendMsg(global.MsgType.MSG_CS_STORAGEDATA_REQUEST, 0, 0, 0, 0, 0, 0)
- end
- -- 请求取出
- function NPCStorageProxy:RequestPutOutStorageData(MakeIndex, Name)
- local npcIndex = self:GetNpcIndex()
- if not npcIndex then
- return
- end
- -- 背包已满,仓库中重新刷出来
- local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
- if BagProxy:isToBeFull(true) then
- local data = {
- resetItem = {
- state = 1,
- MakeIndex = MakeIndex,
- }
- }
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Item_State, data)
- return
- end
- LuaSendMsg(global.MsgType.MSG_CS_STORAGE_PICK_OUT_REQUEST, npcIndex, MakeIndex, 0, 0, Name, string.len(Name))
- end
- function NPCStorageProxy:handle_MSG_SC_STOR_DATA(msg)
- dump("handle_MSG_SC_STOR_DATA___")
- local data = ParseRawMsgToJson(msg)
- if not data or not next(data) then
- return
- end
- self:CleanData()
- self:SetOpenSize(data.Count)
- self:SetStorageData(data.Items)
- self:SetNpcIndex(data.index)
- local npcProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
- npcProxy:Cleanup()
- local header = msg:GetHeader()
- local isself = header.recog == 1 --是自己请求的还是脚本发的
- if not isself then
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Open)
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_STORE_STORED_ADD(msg)
- print("存仓成功")
- local header = msg:GetHeader()
- local MakeIndex = header.recog
- if MakeIndex and MakeIndex ~= 0 then
- local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
- local item = BagProxy:GetItemDataByMakeIndex(MakeIndex)
- if item then
- local pageIdnex = header.param1
- self:AddStorageData(item, pageIdnex)
- BagProxy:DelItemData(item, true)
- end
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_STORE_FULL(msg)
- print("存仓失败, 仓库满")
- local header = msg:GetHeader()
- local MakeIndex = header.recog
- if MakeIndex then
- -- 背包中的重新刷出来
- local data = {
- storage = {
- state = 1,
- MakeIndex = MakeIndex,
- }
- }
- global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_STORE_FORBIDDEN(msg)
- print("存仓失败")
- local header = msg:GetHeader()
- local MakeIndex = header.recog
- if MakeIndex then
- -- 背包中的重新刷出来
- local data = {
- storage = {
- state = 1,
- MakeIndex = MakeIndex,
- }
- }
- global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_PICK_OUT_FORBIDDEN(msg)
- print("取出失败")
- local header = msg:GetHeader()
- local MakeIndex = header.recog
- if MakeIndex then
- -- 仓库中重新刷出来
- local data = {
- resetItem = {
- state = 1,
- MakeIndex = MakeIndex,
- }
- }
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Item_State, data)
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_PICK_OUT_SUCC(msg)
- print("取出成功")
- local header = msg:GetHeader()
- local MakeIndex = header.recog
- if MakeIndex and MakeIndex ~= 0 then
- local item = self:GetStorageDataByMakeIndex(MakeIndex)
- if item then
- self:DeleteStorageData(item)
- end
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_PICK_OUT_FULL(msg)
- print("背包满 取出失败")
- local header = msg:GetHeader()
- local MakeIndex = header.recog
- if MakeIndex then
- -- 背包已满,仓库中重新刷出来
- local data = {
- resetItem = {
- state = 1,
- MakeIndex = MakeIndex,
- }
- }
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Item_State, data)
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_UPDATE(msg)
- local header = msg:GetHeader()
- local MakeIndex = header.recog
- if MakeIndex and MakeIndex ~= 0 then
- local itemNums = header.param1
- self:UpdateStorageData(MakeIndex, itemNums)
- local bagMakeIndex = ParseRawMsgToJson(msg)
- local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
- local item = BagProxy:GetItemDataByMakeIndex(bagMakeIndex)
- if item then
- BagProxy:DelItemData(item, true)
- end
- end
- end
- function NPCStorageProxy:handle_MSG_SC_STORAGE_COUNT_CHANGE(msg)
- local header = msg:GetHeader()
- local count = header.recog
- self:SetOpenSize(count)
- global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Size_Change)
- end
- function NPCStorageProxy:RegisterMsgHandler()
- NPCStorageProxy.super.RegisterMsgHandler(self)
- local msgType = global.MsgType
-
- -- 仓库数据
- LuaRegisterMsgHandler(msgType.MSG_SC_STOR_DATA, handler(self, self.handle_MSG_SC_STOR_DATA))
- -- 存仓成功
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_STORE_STORED_ADD, handler(self, self.handle_MSG_SC_STORAGE_STORE_STORED_ADD))
- -- 仓库满 存仓失败
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_STORE_FULL, handler(self, self.handle_MSG_SC_STORAGE_STORE_FULL))
- -- 其他 存仓失败
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_STORE_FORBIDDEN, handler(self, self.handle_MSG_SC_STORAGE_STORE_FORBIDDEN))
- -- 其他 取出失败
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_PICK_OUT_FORBIDDEN, handler(self, self.handle_MSG_SC_STORAGE_PICK_OUT_FORBIDDEN))
- -- 取出成功
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_PICK_OUT_SUCC, handler(self, self.handle_MSG_SC_STORAGE_PICK_OUT_SUCC))
- -- 背包满 取出失败
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_PICK_OUT_FULL, handler(self, self.handle_MSG_SC_STORAGE_PICK_OUT_FULL))
- --仓库,存物品时候,存的叠加物品,需要更新
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_UPDATE, handler(self, self.handle_MSG_SC_STORAGE_UPDATE))
- -- 仓库格子数增加
- LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_COUNT_CHANGE, handler(self, self.handle_MSG_SC_STORAGE_COUNT_CHANGE))
- end
- return NPCStorageProxy
|