Stall.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. Stall = {}
  2. local this = {}
  3. HOUR_SECOND = 60 * 60
  4. ROLE_STALL_START = "R$roleStallInfo"
  5. -- 获取地图上的摆摊位置
  6. function Stall.stallPosition(actor, msgData)
  7. local id = msgData.id
  8. local spotData = ConfigDataManager.getTable("cfg_spot", "id", id)
  9. if not spotData then
  10. -- error("当前摆摊城市错误".. id)
  11. return
  12. end
  13. local spot = spotData[1]
  14. local positionString = spot["position"]
  15. local position = string.split(positionString, "#")
  16. local minX = position[3]
  17. local maxX = position[1]
  18. local minY = position[4]
  19. local maxY = position[2]
  20. local mapId = tonumber(spot["map"])
  21. local serverType = ConfigDataManager.getTableValue("cfg_map_info","serverType", "id", mapId)
  22. getmapstallinfo(actor,mapId,minX,maxX,minY,maxY,serverType)
  23. end
  24. -- 发送摊位
  25. function Stall.sendStallPosition(actor,stallPosition)
  26. local pointx = stallPosition["pointx"]
  27. if not pointx then
  28. tipinfo(actor,"当前地图没有合适的摆摊位置")
  29. return
  30. end
  31. sendluamsg(actor,LuaMessageIdToClient.RES_STALL_POSITION , stallPosition)
  32. end
  33. -- 获取摆摊结束位置
  34. function Stall.getStallTimeInfo(actor)
  35. ---type Stall.Info 摆摊信息
  36. local allStallInfo = getsysvar(actor,ROLE_STALL_START)
  37. if not allStallInfo then
  38. return nil
  39. end
  40. return allStallInfo[actor:toString()]
  41. end
  42. ---@class Stall.Info 摆摊摊位信息
  43. ---@field mapId number 摆摊地图配置id
  44. ---@field line number 摆摊地图线信息
  45. ---@field pointX number 摆摊点x坐标
  46. ---@field pointY number 摆摊点y坐标
  47. ---@field endTime number 结束时间
  48. -- 开启摆摊
  49. function Stall.startStall(actor, msgData)
  50. local title = msgData.title
  51. local id = msgData.id
  52. local costIndex = msgData.cost
  53. local spotData = ConfigDataManager.getTable("cfg_spot", "id", id)
  54. if not spotData then
  55. -- error("当前摆摊城市错误".. id)
  56. return
  57. end
  58. local stallInfo = Stall.getStallTimeInfo(actor)
  59. if stallInfo then
  60. if stallInfo == "{}" then
  61. else
  62. return
  63. end
  64. end
  65. local spot = spotData[1]
  66. local allCost = spot["cost"]
  67. local mapId = tonumber(spot["map"])
  68. local costDetail = string.split(allCost, "|")[costIndex]
  69. local cost = string.split(costDetail, "#")
  70. local time = tonumber(cost[1])
  71. local itemCfgId = tonumber(cost[2])
  72. local count = tonumber(cost[3])
  73. local haveCount = getbagitemcountbyid(actor,itemCfgId)
  74. if haveCount < count then
  75. tipinfo(actor,"背包道具不足")
  76. return
  77. end
  78. local serverType = ConfigDataManager.getTableValue("cfg_map_info","serverType", "id", mapId)
  79. local stallPosition = getmapstallinfo(actor,mapId,0,0,0,0,serverType)
  80. local haveStallCount = stallPosition["count"]
  81. local maxspot = spot["maxspot"]
  82. if haveStallCount == tonumber(maxspot) then
  83. tipinfo(actor,"当前地图摊位已满,请切换地图开启摆摊")
  84. return
  85. end
  86. local deleteItem = removeitemfrombag(actor,itemCfgId,count,0,9999,'摆摊')
  87. if not deleteItem then
  88. tipinfo(actor,"消耗道具失败")
  89. return
  90. end
  91. startstall(actor,mapId,msgData.pointX,msgData.pointY,time,title,serverType)
  92. sendluamsg(actor, LuaMessageIdToClient.RES_START_STALL, true)
  93. local now = getbaseinfo("nowsec")
  94. ---type Stall.Info 摆摊信息
  95. local stall = {
  96. mapId = mapId,
  97. line = 1,
  98. pointX = msgData.pointX,
  99. pointY = msgData.pointY,
  100. endTime = now + (time * HOUR_SECOND),
  101. title = title,
  102. }
  103. this.startStallSave(actor,stall)
  104. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_INFO, stall)
  105. end
  106. function this.startStallSave(actor,stall)
  107. callonserial(actor, "start_stall_save", stall)
  108. end
  109. function start_stall_save(actor,stall)
  110. local allStallInfo = getsysvar(actor,ROLE_STALL_START)
  111. if not allStallInfo then
  112. allStallInfo = {}
  113. end
  114. allStallInfo[actor:toString()] = stall
  115. setsysvar(actor,ROLE_STALL_START,allStallInfo)
  116. end
  117. -- 结束摆摊
  118. function Stall.endStall(actor)
  119. endstall(actor)
  120. sendluamsg(actor, LuaMessageIdToClient.RES_END_STALL, true)
  121. this.endStallSave(actor)
  122. end
  123. function this.endStallSave(actor)
  124. callonserial(actor, "end_stall_save")
  125. end
  126. function end_stall_save(actor)
  127. local allStallInfo = getsysvar(actor,ROLE_STALL_START)
  128. if table.isNullOrEmpty(allStallInfo) then
  129. return
  130. end
  131. allStallInfo[actor:toString()] = nil
  132. setsysvar(actor,ROLE_STALL_START,allStallInfo)
  133. end
  134. -- 合服务的时候结束全部摆摊
  135. function Stall.combineglobalvar(varName,varData)
  136. info("合服时全部摊位信息", varData)
  137. for index, allStallInfo in pairs(varData) do
  138. if not table.isNullOrEmpty(allStallInfo) then
  139. for index, stallInfo in pairs(allStallInfo) do
  140. local stallActor = getactor(index)
  141. endstall(stallActor)
  142. if not table.isNullOrEmpty(stallInfo) and not table.isNullOrEmpty(stallActor) then
  143. jprint("结束摆摊")
  144. end
  145. end
  146. end
  147. end
  148. setsysvar(ROLE_STALL_START,{})
  149. end
  150. -- 摆摊喊话消耗道具
  151. function Stall.deleteItem(actor,msgData)
  152. removeitemfrombag(actor,msgData[1],msgData[2],0,9999,'摆摊')
  153. end
  154. -- 获取摊位商品信息
  155. function Stall.getStallGoods(actor,rid)
  156. local stallActor = getactor(actor,rid)
  157. local stallInfo = Stall.getStallTimeInfo(stallActor)
  158. if table.isEmpty(stallInfo) then
  159. return
  160. end
  161. local listingGood = {}
  162. listingGood["rid"] = rid
  163. listingGood["title"] = stallInfo.title
  164. local allgoods = {}
  165. local allWorldGoods = getsysvar(stallActor, SYS_TRADE_WORLD_GOODS)
  166. if not allWorldGoods then
  167. allWorldGoods = {}
  168. listingGood["number"] = 0
  169. listingGood["allgoods"] = allWorldGoods
  170. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_GOODS, listingGood)
  171. return
  172. end
  173. local roleWorld = allWorldGoods[stallActor:toString()]
  174. if not roleWorld then
  175. roleWorld = {}
  176. listingGood["number"] = 0
  177. listingGood["allgoods"] = roleWorld
  178. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_GOODS, listingGood)
  179. return
  180. end
  181. local now = getbaseinfo("now")
  182. for index, good in pairs(roleWorld) do
  183. if good.itemcfgid ~= 30030305 then
  184. local publicityTime = good.listingtime + good.publicitytime
  185. local offShelfTime = publicityTime + good.upshelfduration
  186. if publicityTime > now then
  187. good["publicity"] = 1
  188. good["timename"] = "公示中"
  189. good["time"] = (publicityTime - now) / 1000
  190. elseif publicityTime < now and offShelfTime > now then
  191. good["publicity"] = 0
  192. good["timename"] = "下架时间"
  193. good["time"] = (offShelfTime - now) / 1000
  194. end
  195. table.insert(allgoods, good)
  196. end
  197. end
  198. if allgoods then
  199. Trade.sortGoods(allgoods, 0)
  200. end
  201. listingGood["number"] = #allgoods
  202. listingGood["allgoods"] = allgoods
  203. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_GOODS, listingGood)
  204. end
  205. -- 在全局变量添加摊位信息
  206. --function this.allStall(actor)
  207. -- local allStall = getsysvar(actor, ALL_STALL_START)
  208. -- if not allStall or allStall == "" then
  209. -- allStall = {}
  210. -- end
  211. -- table.insert(allStall,actor:toString())
  212. -- setsysvar(actor, ALL_STALL_START,allStall)
  213. --end
  214. --function clearallstallinfo(actor)
  215. -- setsysvar(actor,ALL_STALL_START,{})
  216. --end
  217. -- 在全局变量减少摊位信息
  218. --function this.deleteAllStall(actor)
  219. -- local allStall = getsysvar(actor, ALL_STALL_START)
  220. -- if not allStall then
  221. -- return
  222. -- end
  223. -- local deleteIndex = 0
  224. -- for index, stallActor in pairs(allStall) do
  225. -- if actor:toString() == stallActor then
  226. -- deleteIndex = index
  227. -- end
  228. -- end
  229. -- allStall[deleteIndex] = nil
  230. -- setsysvar(actor, ALL_STALL_START,allStall)
  231. --end
  232. function Stall.flushStall()
  233. callonserial("stall_flush")
  234. end
  235. function stall_flush()
  236. local allStallInfo = getsysvar(ROLE_STALL_START)
  237. if table.isEmpty(allStallInfo) then
  238. return
  239. end
  240. local stallActor
  241. local now = getbaseinfo("nowsec")
  242. for index, stallInfo in pairs(allStallInfo) do
  243. stallActor = getactor(index)
  244. if not table.isEmpty(stallInfo) and stallActor then
  245. if now > stallInfo.endTime then
  246. allStallInfo[index] = nil
  247. -- this.deleteAllStall(actor)
  248. endstall(stallActor)
  249. -- Stall.endStall(stallActor)
  250. end
  251. end
  252. stallActor = nil
  253. end
  254. setsysvar(ROLE_STALL_START,allStallInfo)
  255. end