AuctionProxy.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local AuctionProxy = class("AuctionProxy", RemoteProxy)
  3. AuctionProxy.NAME = global.ProxyTable.AuctionProxy
  4. local socket = require("socket")
  5. -- useritem 道具基础数据
  6. -- userid 归属者ID
  7. -- type 货币ID
  8. -- price 当前价格
  9. -- lastprice 一口价
  10. -- time 上架时间戳
  11. -- lasttime 最后出价时间戳
  12. -- endtime 下架时间戳
  13. -- curruserid 当前竞价玩家ID
  14. -- joinuser 是否参与过竞价
  15. -- flag 0.正常 1.表示拍卖成功的物品 2.表示流拍的物品
  16. -- page 0.世界 1.行会
  17. -- index
  18. -- meguildrate 同行会折扣率
  19. local function getUnitNumber(n)
  20. if n >= 100000000 then
  21. return string.format("%.1f%s", n / 100000000, GET_STRING(1045))
  22. end
  23. if n >= 10000 then
  24. return string.format("%.1f%s", n / 10000, GET_STRING(1005))
  25. end
  26. return tostring(n)
  27. end
  28. local function parseItem(item)
  29. if not item then
  30. return nil
  31. end
  32. local titem = ChangeItemServersSendDatas(item.useritem)
  33. setmetatable( item, { __index = titem })
  34. return item
  35. end
  36. function fixAuctionPrice(price, unit)
  37. return unit and getUnitNumber(price) or price
  38. end
  39. AuctionProxy.INFINITY_PRICE = 100000000
  40. function AuctionProxy:ctor()
  41. AuctionProxy.super.ctor(self)
  42. self._config_type = {}
  43. self._currencies = {} -- 可拍卖货币
  44. self._qualities = {} -- 品阶
  45. self._biddinglist = {} -- 竞价列表
  46. self._putlist = {} -- 上架列表
  47. self._src = 0 -- 最近一次请求来源 1.世界拍卖 2.行会拍卖
  48. self._page = 0
  49. self._complete = false
  50. self._jobAble = {} -- 职业过滤
  51. self._limitShelf = 6 -- 货架数量
  52. self._lowBidPrice = 1 -- 默认最低竞拍价
  53. self._lowBuyPrice = 1 -- 默认最低一口价
  54. self._highBidPrice = nil
  55. self._highBuyPrice = nil
  56. self._itemConfig = {}
  57. self._requestMinTime = 600 -- 拍卖行数据请求最小时间间隔(毫秒)
  58. self._lastRequestTime = nil
  59. self:Init()
  60. end
  61. function AuctionProxy:Init()
  62. self._config_type = requireGameConfig("cfg_auction_type")
  63. end
  64. function AuctionProxy:InitItemConfig(...)
  65. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  66. self._itemConfig = ItemConfigProxy:GetItemConfigData()
  67. end
  68. function AuctionProxy:Clear()
  69. self._page = 0
  70. self._complete = false
  71. global.Facade:sendNotification(global.NoticeTable.AuctionItemListClear)
  72. end
  73. function AuctionProxy:IsComplete()
  74. return self._complete
  75. end
  76. function AuctionProxy:getCurrencies()
  77. return self._currencies
  78. end
  79. function AuctionProxy:addCurrency(item)
  80. table.insert(self._currencies, { id = item.Index, name = item.Name, item = item })
  81. end
  82. function AuctionProxy:clearCurrencies()
  83. self._currencies = {}
  84. end
  85. function AuctionProxy:getQualities()
  86. return self._qualities
  87. end
  88. function AuctionProxy:setQualities(rawData)
  89. if not rawData then
  90. return
  91. end
  92. for _, value in ipairs(rawData) do
  93. local slices = string.split(value, "#")
  94. table.insert(self._qualities, { id = tonumber(slices[1]), name = slices[2] })
  95. end
  96. end
  97. function AuctionProxy:getJobAble()
  98. return self._jobAble
  99. end
  100. function AuctionProxy:setJobAble(able)
  101. self._jobAble = able
  102. end
  103. function AuctionProxy:getLimitShelf()
  104. return self._limitShelf or 8
  105. end
  106. function AuctionProxy:setLimitShelf(count)
  107. self._limitShelf = count
  108. end
  109. function AuctionProxy:GetCTypeByID(id)
  110. return self._config_type[id]
  111. end
  112. function AuctionProxy:GetStdModeByID(id)
  113. local config = self:GetCTypeByID(id)
  114. if not config then
  115. return nil
  116. end
  117. local slices = string.split(tostring(config.stdmode), "#")
  118. local stdmode = {}
  119. for key, value in ipairs(slices) do
  120. table.insert(stdmode, tonumber(value))
  121. end
  122. return stdmode[1] and stdmode or nil
  123. end
  124. function AuctionProxy:GetCTypeInGroup()
  125. local items = {}
  126. for i, v in pairs(self._config_type) do
  127. if not items[v.firstlevel] then
  128. items[v.firstlevel] = {}
  129. end
  130. table.insert(items[v.firstlevel], v)
  131. end
  132. for i, v in pairs(items) do
  133. table.sort(v, function(a, b) return a.id < b.id end)
  134. end
  135. return items
  136. end
  137. function AuctionProxy:GetCTypeItemsByGroup(g)
  138. local items = {}
  139. for i, v in pairs(self._config_type) do
  140. if v.secondlevel and v.firstlevel == g then
  141. table.insert(items, v)
  142. end
  143. end
  144. table.sort(items, function(a, b) return a.id < b.id end)
  145. return items
  146. end
  147. function AuctionProxy:GetPutListCount()
  148. return #self._putlist
  149. end
  150. function AuctionProxy:checkBidAble(item)
  151. if not item then
  152. return false
  153. end
  154. return item.price and item.price > 0
  155. end
  156. function AuctionProxy:checkBuyAble(item)
  157. if not item then
  158. return false
  159. end
  160. return item.lastprice and item.lastprice > 0
  161. end
  162. function AuctionProxy:calcItemStatus(item)
  163. if not item then
  164. return 3, 0
  165. end
  166. local status = 0 -- 2.竞拍中 3.已超时
  167. local endTime = item.endtime or GetServerTime() + 1
  168. local showTime = item.showtime
  169. local remaining = 0
  170. if GetServerTime() >= endTime then
  171. -- 已超时
  172. status = 3
  173. remaining = 0
  174. else
  175. --竞拍中
  176. status = 2
  177. remaining = endTime - GetServerTime()
  178. end
  179. if showTime and (showTime - GetServerTime()) > 0 then
  180. remaining = showTime - GetServerTime()
  181. end
  182. remaining = math.max(remaining, 0)
  183. return status, remaining
  184. end
  185. function AuctionProxy:IsAuctioningItem(item)
  186. return item.flag == 0
  187. end
  188. function AuctionProxy:IsExistAcquireItem()
  189. local mainPlayerID = global.gamePlayerController:GetMainPlayerID()
  190. for _, item in ipairs(self._biddinglist) do
  191. if item.flag == 1 and item.curruserid == mainPlayerID then
  192. return true
  193. end
  194. end
  195. return false
  196. end
  197. function AuctionProxy:setLowBidPrice(price)
  198. self._lowBidPrice = price
  199. end
  200. function AuctionProxy:setLowBuyPrice(price)
  201. self._lowBuyPrice = price
  202. end
  203. function AuctionProxy:setHighBidPrice(price)
  204. self._highBidPrice = price
  205. end
  206. function AuctionProxy:setHighBuyPrice(price)
  207. self._highBuyPrice = price
  208. end
  209. function AuctionProxy:GetLowBidPrice()
  210. return self._lowBidPrice
  211. end
  212. function AuctionProxy:GetLowBuyPrice()
  213. return self._lowBuyPrice
  214. end
  215. function AuctionProxy:GetHighBidPrice()
  216. return self._highBidPrice
  217. end
  218. function AuctionProxy:GetHighBuyPrice()
  219. return self._highBuyPrice
  220. end
  221. function AuctionProxy:GetItemConfig()
  222. return self._itemConfig
  223. end
  224. function AuctionProxy:GetShowBagItems()
  225. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  226. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  227. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  228. local bagData = BagProxy:GetBagData()
  229. local quickData = QuickUseProxy:GetQuickUseData()
  230. local articleType = ItemConfigProxy:GetArticleType()
  231. local checkArticleType = {[articleType.TYPE_TRADE_AUCTIONA] = true}
  232. local bagItems = {}
  233. local itemMaps = {}
  234. local bindArticleType = ItemConfigProxy:GetBindArticleType()
  235. for _, vItem in pairs(quickData) do
  236. local isBind, isSelf, isMeetType = CheckItemisBind(vItem, bindArticleType.TYPE_NOSTALL)
  237. if not ItemConfigProxy:GetItemArticle(vItem.Index, checkArticleType) and not isMeetType then
  238. table.insert(bagItems, vItem)
  239. itemMaps[vItem.MakeIndex] = vItem
  240. end
  241. end
  242. for _, vItem in pairs(bagData) do
  243. local isBind, isSelf, isMeetType = CheckItemisBind(vItem, bindArticleType.TYPE_NOSTALL)
  244. if not ItemConfigProxy:GetItemArticle(vItem.Index, checkArticleType) and not isMeetType then
  245. table.insert(bagItems, vItem)
  246. itemMaps[vItem.MakeIndex] = vItem
  247. end
  248. end
  249. return bagItems, itemMaps
  250. end
  251. function AuctionProxy:SetRequestMinTime(time)
  252. self._requestMinTime = time
  253. end
  254. ----------------------------------------------------------------------------------
  255. ----------------------------------------------------------------------------------
  256. ----------------------------------------------------------------------------------
  257. -- 拉取拍卖数据:发送消息号:1120,第一个参数为页,世界=0,行会=1
  258. function AuctionProxy:RequestItemList(data)
  259. local curTime = socket.gettime()
  260. local needDelay = false
  261. if self._lastRequestTime and self._requestMinTime then
  262. if (curTime - self._lastRequestTime) * 1000 < self._requestMinTime then
  263. needDelay = true
  264. SL:ShowSystemTips("<font color = '#00FF00'>正在读取数据中...</font>")
  265. end
  266. end
  267. local function callback()
  268. local filter = clone(data)
  269. filter.page = self._page
  270. self._lastRequestTime = socket.gettime()
  271. SendTableToServer(global.MsgType.MSG_CS_AUCTION_ITEM_LIST, filter)
  272. end
  273. PerformWithDelayGlobal(callback, needDelay and (self._requestMinTime / 1000) or 0)
  274. end
  275. function AuctionProxy:RespItemList(msg)
  276. HideLoadingBar()
  277. local header = msg:GetHeader()
  278. local len = msg:GetDataLength()
  279. if len <= 0 then
  280. return false
  281. end
  282. self._page = header.recog + 1
  283. local source = header.param1
  284. local jsonData = ParseRawMsgToJson(msg)
  285. -- complete
  286. if #jsonData == 0 then
  287. self._complete = true
  288. global.Facade:sendNotification(global.NoticeTable.AuctionItemListComplete, { source = source })
  289. return
  290. end
  291. local items = {}
  292. for _, v in ipairs(jsonData) do
  293. local item = parseItem(v)
  294. table.insert(items, item)
  295. end
  296. global.Facade:sendNotification(global.NoticeTable.AuctionItemListResp, { source = source, items = items })
  297. end
  298. -- 你发送1154号消息,第一个参数=1,表示查询自己上架的物品,=2表示查询参与过的。
  299. function AuctionProxy:RequestItems(listType)
  300. LuaSendMsg(global.MsgType.MSG_CS_AUCTION_PUT_LIST, listType)
  301. end
  302. -- 返回1130号消息给你。第一个参数为对应的查询类型
  303. function AuctionProxy:RespItems(msg)
  304. local header = msg:GetHeader()
  305. local len = msg:GetDataLength()
  306. if len <= 0 then
  307. return false
  308. end
  309. local jsonData = ParseRawMsgToJson(msg)
  310. local items = {}
  311. for _, v in ipairs(jsonData) do
  312. local item = parseItem(v)
  313. table.insert(items, item)
  314. end
  315. local isMerge = header.msgId == global.MsgType.MSG_SC_AUCTION_PUT_LIST2
  316. if header.recog == 1 then
  317. if not isMerge then
  318. self._putlist = {}
  319. end
  320. self._putlist = table.order_merge(self._putlist or {}, items)
  321. if header.param1 ~= 11 or (isMerge and header.param1 == 11) then --第二次数据时通知更新界面或者是旧的消息
  322. global.Facade:sendNotification(global.NoticeTable.AuctionPutListResp, self._putlist)
  323. end
  324. elseif header.recog == 2 then
  325. if not isMerge then
  326. self._biddinglist = {}
  327. end
  328. self._biddinglist = table.order_merge(self._biddinglist or {}, items)
  329. if header.param1 ~= 11 or (isMerge and header.param1 == 11) then --第二次数据时通知更新界面或者是旧的消息
  330. global.Facade:sendNotification(global.NoticeTable.AuctionBiddingListResp, self._biddinglist)
  331. end
  332. end
  333. end
  334. function AuctionProxy:RequestPutin(data)
  335. SendTableToServer(global.MsgType.MSG_CS_AUCTION_PUT_IN, data)
  336. end
  337. function AuctionProxy:RequestReputin(data)
  338. SendTableToServer(global.MsgType.MSG_CS_AUCTION_REPUT_IN, data)
  339. end
  340. function AuctionProxy:RespPutin(msg)
  341. local header = msg:GetHeader()
  342. local jsonData = ParseRawMsgToJson(msg)
  343. local item = parseItem(jsonData)
  344. global.Facade:sendNotification(global.NoticeTable.AuctionPutinResp, { item = item })
  345. end
  346. function AuctionProxy:RequestPutout(data)
  347. local makeindex = data.makeindex
  348. LuaSendMsg(global.MsgType.MSG_CS_AUCTION_PUT_OUT, makeindex)
  349. end
  350. function AuctionProxy:RespPutout(msg)
  351. local header = msg:GetHeader()
  352. local makeindex = header.recog
  353. global.Facade:sendNotification(global.NoticeTable.AuctionPutoutResp, { makeindex = makeindex })
  354. end
  355. function AuctionProxy:RequestBid(data)
  356. local sendData = { makeindex = data.makeindex, price = data.price }
  357. SendTableToServer(global.MsgType.MSG_CS_AUCTION_BID, sendData)
  358. end
  359. -- 竞拍返回 消息号:1112,recog=0,失败:recog=-1,表示已经有人出更高价,=-2表示已经被买走 =-3自己的物品
  360. function AuctionProxy:RespBid(msg)
  361. local header = msg:GetHeader()
  362. local errorcode = header.recog
  363. if errorcode == 0 then
  364. ShowSystemTips(GET_STRING(30103015))
  365. elseif errorcode == -1 then
  366. ShowSystemTips(GET_STRING(30103083))
  367. elseif errorcode == -2 then
  368. ShowSystemTips(GET_STRING(30103084))
  369. elseif errorcode == -3 then
  370. ShowSystemTips(GET_STRING(30103034))
  371. elseif errorcode == -4 then
  372. ShowSystemTips(GET_STRING(30103085))
  373. end
  374. global.Facade:sendNotification(global.NoticeTable.AuctionBidResp, { errorcode = errorcode,})
  375. end
  376. function AuctionProxy:RequestAcquireItem(data)
  377. local makeindex = data.makeindex
  378. LuaSendMsg(global.MsgType.MSG_CS_AUCTION_ACQUIRE, makeindex)
  379. end
  380. function AuctionProxy:RespAcquireItem(msg)
  381. local header = msg:GetHeader()
  382. local makeindex = header.recog
  383. global.Facade:sendNotification(global.NoticeTable.AuctionAcquireResp, { makeindex = makeindex })
  384. end
  385. function AuctionProxy:RequestLeaveAuction()
  386. LuaSendMsg(global.MsgType.MSG_CS_AUCTION_LEAVE)
  387. end
  388. -- 1115号消息,recog=1,表示新加,=2表示删除,=3表示更新
  389. function AuctionProxy:RespItemUpdate(msg)
  390. local header = msg:GetHeader()
  391. local action = header.recog
  392. local jsonData = ParseRawMsgToJson(msg)
  393. local item = parseItem(jsonData)
  394. -- 世界拍卖
  395. if action == 1 then
  396. global.Facade:sendNotification(global.NoticeTable.AuctionWorldItemAdd, { item = item })
  397. elseif action == 2 then
  398. for k, v in pairs(self._biddinglist) do
  399. if v.MakeIndex == item.MakeIndex then
  400. table.remove(self._biddinglist, k)
  401. break
  402. end
  403. end
  404. global.Facade:sendNotification(global.NoticeTable.AuctionWorldItemDel, { item = item })
  405. elseif action == 3 then
  406. for k, v in pairs(self._biddinglist) do
  407. if v.MakeIndex == item.MakeIndex then
  408. self._biddinglist[k] = item
  409. break
  410. end
  411. end
  412. global.Facade:sendNotification(global.NoticeTable.AuctionWorldItemChange, { item = item })
  413. end
  414. global.Facade:sendNotification(global.NoticeTable.AuctionItemUpdate, { item = item })
  415. local peopleproxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerProperty)
  416. local myUid = peopleproxy:GetRoleUID()
  417. if jsonData.userid == myUid and action ~= 2 then
  418. global.Facade:sendNotification(global.NoticeTable.AuctionPutinResp, { item = item })
  419. end
  420. end
  421. function AuctionProxy:RespPutCount(msg)
  422. local header = msg:GetHeader()
  423. self:setLimitShelf(header.recog)
  424. end
  425. function AuctionProxy:RegisterMsgHandler()
  426. AuctionProxy.super.RegisterMsgHandler(self)
  427. local msgType = global.MsgType
  428. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_ITEM_LIST, handler(self, self.RespItemList))
  429. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_PUT_LIST, handler(self, self.RespItems))
  430. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_PUT_OUT, handler(self, self.RespPutout))
  431. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_PUT_IN, handler(self, self.RespPutin))
  432. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_BID, handler(self, self.RespBid))
  433. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_ACQUIRE, handler(self, self.RespAcquireItem))
  434. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_ITEM_UPDATE, handler(self, self.RespItemUpdate))
  435. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_PUT_COUNT, handler(self, self.RespPutCount))
  436. LuaRegisterMsgHandler(msgType.MSG_SC_AUCTION_PUT_LIST2, handler(self, self.RespItems))
  437. end
  438. return AuctionProxy