PurchaseProxy.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local PurchaseProxy = class("PurchaseProxy", RemoteProxy)
  3. PurchaseProxy.NAME = global.ProxyTable.PurchaseProxy
  4. local socket = require("socket")
  5. local cjson = require("cjson")
  6. -- guid 标识
  7. -- userid 归属者ID
  8. -- itemid 道具ID
  9. -- stdmode 道具类型stdmode
  10. -- currency 货币ID
  11. -- totalqty 总数量
  12. -- minqty 最小数量
  13. -- remainqty 剩余数量
  14. -- waitrecqty 待提取数量
  15. -- recedqty 已提取数量
  16. -- price 单价
  17. -- qty 数量
  18. -- amount 金额
  19. -- starttime 上架时间
  20. -- endtime 下架时间(为0不限制下架时间)
  21. -- pageindex 页码
  22. -- pagemax 总共页数
  23. local function getUnitNumber(n)
  24. if n >= 100000000 then
  25. return string.format("%.1f%s", n / 100000000, GET_STRING(1045))
  26. end
  27. if n >= 10000 then
  28. return string.format("%.1f%s", n / 10000, GET_STRING(1005))
  29. end
  30. return tostring(n)
  31. end
  32. local function parseItem(item)
  33. if not item then
  34. return nil
  35. end
  36. local titem = ChangeItemServersSendDatas(item.useritem)
  37. setmetatable( item, { __index = titem })
  38. return item
  39. end
  40. function fixPurchasePrice(price, unit)
  41. return unit and getUnitNumber(price) or price
  42. end
  43. function PurchaseProxy:ctor()
  44. PurchaseProxy.super.ctor(self)
  45. self._config_type = {}
  46. self._currencies = {} -- 可求购货币
  47. self._currencyMap = {}
  48. self._receivelist = {} -- 我的 - 已收列表
  49. self._putlist = {} -- 我的 - 求购列表
  50. self._src = 0 -- 请求来源 0.世界求购 1.我的求购 2.我的已收
  51. self._sort = 0 -- 排序 0.默认 1.单价正序 2.单价倒序 3.总价正序 4.总价倒序
  52. self._page = 0
  53. self._complete = false
  54. self._itemConfig = {}
  55. self._requestMinTime = 600 -- 拍卖行数据请求最小时间间隔(毫秒)
  56. self._lastRequestTime = nil
  57. self._filter1Item = nil -- 分类一级数据
  58. self._itemListByType = {} -- 按类分物品列表
  59. self:Init()
  60. end
  61. function PurchaseProxy:Init()
  62. self._config_type = clone(requireGameConfig("cfg_auction_type"))
  63. end
  64. function PurchaseProxy:InitItemConfig(...)
  65. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  66. self._itemConfig = ItemConfigProxy:GetItemConfigData()
  67. end
  68. function PurchaseProxy:Clear()
  69. self._page = 0
  70. self._complete = false
  71. end
  72. function PurchaseProxy:IsComplete()
  73. return self._complete
  74. end
  75. function PurchaseProxy:getCurrencies()
  76. return self._currencies
  77. end
  78. -- #分隔
  79. function PurchaseProxy:SetCurrencies(str)
  80. if str and string.len(str) > 0 then
  81. local data = string.split(str, "#")
  82. for k, v in ipairs(data) do
  83. local id = tonumber(v)
  84. if id and not self._currencyMap[id] then
  85. self._currencyMap[id] = true
  86. table.insert(self._currencies, {id = id})
  87. end
  88. end
  89. end
  90. end
  91. function PurchaseProxy:SetTypeItemList(list)
  92. self._itemListByType = list or {}
  93. end
  94. function PurchaseProxy:GetItemListByType(key)
  95. return self._itemListByType and self._itemListByType[key] or {}
  96. end
  97. function PurchaseProxy:clearData()
  98. self._itemListByType = {}
  99. end
  100. function PurchaseProxy:SetSortType(type)
  101. self._sort = type
  102. end
  103. function PurchaseProxy:GetSortType()
  104. return self._sort
  105. end
  106. function PurchaseProxy:GetCTypeByID(id)
  107. return self._config_type[id]
  108. end
  109. function PurchaseProxy:GetStdModeByID(id)
  110. local config = self:GetCTypeByID(id)
  111. if not config then
  112. return {}
  113. end
  114. local slices = string.split(tostring(config.stdmode), "#")
  115. local stdmode = {}
  116. for key, value in ipairs(slices) do
  117. table.insert(stdmode, tonumber(value))
  118. end
  119. return stdmode
  120. end
  121. -- 一级页签
  122. function PurchaseProxy:GetFirstFilterItem()
  123. if not self._filter1Item then
  124. local items = {}
  125. for i, v in pairs(self._config_type) do
  126. if not items[v.firstlevel] then
  127. items[v.firstlevel] = {}
  128. end
  129. table.insert(items[v.firstlevel], v)
  130. end
  131. for i, v in pairs(items) do
  132. table.sort(v, function(a, b) return a.id < b.id end)
  133. end
  134. self._filter1Item = items
  135. end
  136. return self._filter1Item
  137. end
  138. -- 二级页签
  139. function PurchaseProxy:GetSecondFilterItem(g)
  140. local items = self:GetFirstFilterItem()[g] or {}
  141. -- table.sort(items, function(a, b) return a.id < b.id end)
  142. return items
  143. end
  144. function PurchaseProxy:GetItemConfig()
  145. return self._itemConfig
  146. end
  147. function PurchaseProxy:SetRequestMinTime(time)
  148. self._requestMinTime = time
  149. end
  150. ----------------------------------------------------------------------------------
  151. -- 拉取求购数据:发送消息号:1510, recog 世界=0, 我的求购=1, 我的已收=2
  152. function PurchaseProxy:RequestItemList(data)
  153. local curTime = socket.gettime()
  154. local needDelay = false
  155. -- if self._lastRequestTime and self._requestMinTime then
  156. -- if (curTime - self._lastRequestTime) * 1000 < self._requestMinTime then
  157. -- needDelay = true
  158. -- SL:ShowSystemTips("<font color = '#00FF00'>正在读取数据中...</font>")
  159. -- end
  160. -- end
  161. local function callback()
  162. local sendData = data
  163. self._lastRequestTime = socket.gettime()
  164. local jsonStr = cjson.encode(sendData)
  165. LuaSendMsg(global.MsgType.MSG_CS_PURCHASE_ITEM_LIST, 0, 0, 0, data.type or 0, jsonStr, string.len(jsonStr))
  166. end
  167. PerformWithDelayGlobal(callback, needDelay and (self._requestMinTime / 1000) or 0)
  168. end
  169. function PurchaseProxy:RespItemList(msg)
  170. HideLoadingBar()
  171. local header = msg:GetHeader()
  172. local len = msg:GetDataLength()
  173. if len <= 0 then
  174. return false
  175. end
  176. local jsonData = ParseRawMsgToJson(msg)
  177. local type = header.recog -- 0 1 2
  178. -- complete
  179. dump(jsonData, "_______________itemList")
  180. local data = jsonData and jsonData.data
  181. if #data == 0 then
  182. self._complete = true
  183. global.Facade:sendNotification(global.NoticeTable.PurchaseItemListComplete, {type = type})
  184. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_ITEM_LIST_COMPLETE, {type = type})
  185. return
  186. end
  187. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_ITEM_LIST_PULL, {type = type, items = data, pageIndex = jsonData.pageindex})
  188. end
  189. function PurchaseProxy:RequestPutIn(data)
  190. SendTableToServer(global.MsgType.MSG_CS_PURCHASE_PUT_IN, data)
  191. end
  192. function PurchaseProxy:RequestPutOut(guid)
  193. if not guid then -- 全部取消
  194. LuaSendMsg(global.MsgType.MSG_CS_PURCHASE_PUT_OUT, 0, 0, 0, 1)
  195. else
  196. LuaSendMsg(global.MsgType.MSG_CS_PURCHASE_PUT_OUT, 0, 0, 0, 0, guid, string.len(guid))
  197. end
  198. end
  199. function PurchaseProxy:RequestTakeOut(guid)
  200. if not guid then -- 全部取出
  201. LuaSendMsg(global.MsgType.MSG_CS_PURCHASE_TAKE_OUT, 0, 0, 0, 1)
  202. else
  203. LuaSendMsg(global.MsgType.MSG_CS_PURCHASE_TAKE_OUT, 0, 0, 0, 0, guid, string.len(guid))
  204. end
  205. end
  206. function PurchaseProxy:RequestSell(data)
  207. SendTableToServer(global.MsgType.MSG_CS_PURCHASE_SELL, data)
  208. end
  209. function PurchaseProxy:NotifyClosePurchase()
  210. LuaSendMsg(global.MsgType.MSG_CS_PURCHASE_CLOSE)
  211. end
  212. -- 操作返回 recog=类型 1=上架 2=取出 3=出售 4=下架, nparam 0 成功 小于0 失败
  213. function PurchaseProxy:RespOperaBack(msg)
  214. local header = msg:GetHeader()
  215. local type = header.recog
  216. local errorcode = header.param1
  217. if errorcode == 0 then
  218. ShowSystemTips("成功")
  219. elseif errorcode == -1 then
  220. ShowSystemTips("道具不合法")
  221. elseif errorcode == -2 then
  222. ShowSystemTips("单价不合法")
  223. elseif errorcode == -3 then
  224. ShowSystemTips("最小数量不合法")
  225. elseif errorcode == -4 then
  226. ShowSystemTips("货币类型不合法")
  227. elseif errorcode == -5 then
  228. ShowSystemTips("数量不合法")
  229. elseif errorcode == -6 then
  230. ShowSystemTips("金额不合法")
  231. elseif errorcode == -7 then
  232. ShowSystemTips("玩家货币不足")
  233. elseif errorcode == -8 then
  234. ShowSystemTips("有未提取道具不允许下架")
  235. end
  236. global.Facade:sendNotification(global.NoticeTable.PurchaseOperaResp, {errorcode = errorcode})
  237. end
  238. -- recog 类型 1=上架 2=取出 3=出售 4=下架
  239. function PurchaseProxy:RespItemUpdate(msg)
  240. local header = msg:GetHeader()
  241. local action = header.recog
  242. local jsonData = ParseRawMsgToJson(msg)
  243. print("==================RespItemUpdate===============", action)
  244. dump(jsonData)
  245. local PlayerProperty = global.Facade:retrieveProxy(global.ProxyTable.PlayerProperty)
  246. local myUid = PlayerProperty:GetRoleUID()
  247. local isMy = jsonData.userid == myUid
  248. -- 已收列表 待提取数量 > 0
  249. local isReceive = jsonData.waitrecqty > 0
  250. -- type: 1 新增
  251. local TYPE = {
  252. ADD = 1,
  253. REMOVE = 2,
  254. UPDATE = 3,
  255. }
  256. if action == 1 then -- 上架
  257. if isMy then
  258. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_MYITEM_UPDATE, {isReceive = isReceive, type = TYPE.ADD, item = jsonData})
  259. else
  260. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_WORLDITEM_UPDATE, {type = TYPE.ADD, item = jsonData})
  261. end
  262. elseif action == 2 then -- 取出
  263. if isMy then -- 已收列表
  264. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_MYITEM_UPDATE, {isReceive = true, type = TYPE.REMOVE, item = jsonData})
  265. end
  266. elseif action == 3 then -- 出售 / 已收列表新增
  267. if isMy then
  268. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_MYITEM_UPDATE, {isReceive = isReceive, type = TYPE.UPDATE, item = jsonData})
  269. else
  270. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_WORLDITEM_UPDATE, {type = TYPE.UPDATE, item = jsonData})
  271. end
  272. elseif action == 4 then -- 下架
  273. if isMy then
  274. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_MYITEM_UPDATE, {type = TYPE.REMOVE, item = jsonData})
  275. else
  276. SLBridge:onLUAEvent(LUA_EVENT_PURCHASE_WORLDITEM_UPDATE, {type = TYPE.REMOVE, item = jsonData})
  277. end
  278. end
  279. end
  280. function PurchaseProxy:RegisterMsgHandler()
  281. PurchaseProxy.super.RegisterMsgHandler(self)
  282. local msgType = global.MsgType
  283. LuaRegisterMsgHandler(msgType.MSG_SC_PURCHASE_ITEM_LIST, handler(self, self.RespItemList))
  284. LuaRegisterMsgHandler(msgType.MSG_SC_PURCHASE_OPERA, handler(self, self.RespOperaBack))
  285. LuaRegisterMsgHandler(msgType.MSG_SC_PURCHASE_UPDATE, handler(self, self.RespItemUpdate))
  286. end
  287. return PurchaseProxy