Stall_1.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. -- 判断地图位置
  87. local playerInfo = getplayermaininfo(actor)
  88. local mapInfo = getmapinfobyid(playerInfo.mapid)
  89. if mapInfo.cfgid ~= mapId then
  90. tipinfo(actor, "请传送至对应地图才可以开始摆摊")
  91. return
  92. end
  93. local allStallInfo = getsysvar(actor, ROLE_STALL_START)
  94. if not allStallInfo then
  95. allStallInfo = {}
  96. end
  97. for key, player in pairs(allStallInfo) do
  98. if math.abs(player.pointX - msgData.pointX) <= 1 and math.abs(player.pointY - msgData.pointY) <= 1 then
  99. tipinfo(actor, "距离其他玩家太近")
  100. return
  101. end
  102. end
  103. if count > 0 then
  104. local deleteItem = removeitemfrombag(actor, itemCfgId, count, 0, 9999, '摆摊')
  105. if not deleteItem then
  106. tipinfo(actor, "消耗道具失败")
  107. return
  108. end
  109. end
  110. startstall(actor, mapId, msgData.pointX, msgData.pointY, time, title, serverType)
  111. sendluamsg(actor, LuaMessageIdToClient.RES_START_STALL, true)
  112. local now = getbaseinfo("nowsec")
  113. ---type Stall.Info 摆摊信息
  114. local stall = {
  115. mapId = mapId,
  116. line = 1,
  117. pointX = msgData.pointX,
  118. pointY = msgData.pointY,
  119. endTime = now + (time * HOUR_SECOND),
  120. title = title,
  121. }
  122. this.startStallSave(actor, stall)
  123. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_INFO, stall)
  124. end
  125. function this.startStallSave(actor, stall)
  126. callonserial(actor, "start_stall_save", stall)
  127. end
  128. function start_stall_save(actor, stall)
  129. local allStallInfo = getsysvar(actor, ROLE_STALL_START)
  130. if not allStallInfo then
  131. allStallInfo = {}
  132. end
  133. allStallInfo[actor:toString()] = stall
  134. setsysvar(actor, ROLE_STALL_START, allStallInfo)
  135. end
  136. -- 结束摆摊
  137. function Stall.endStall(actor)
  138. endstall(actor)
  139. sendluamsg(actor, LuaMessageIdToClient.RES_END_STALL, true)
  140. this.endStallSave(actor)
  141. end
  142. function this.endStallSave(actor)
  143. callonserial(actor, "end_stall_save")
  144. end
  145. function end_stall_save(actor)
  146. local allStallInfo = getsysvar(actor, ROLE_STALL_START)
  147. if table.isNullOrEmpty(allStallInfo) then
  148. return
  149. end
  150. allStallInfo[actor:toString()] = nil
  151. setsysvar(actor, ROLE_STALL_START, allStallInfo)
  152. end
  153. -- 合服务的时候结束全部摆摊
  154. function Stall.combineglobalvar(varName, varData)
  155. -- info("合服时全部摊位信息", varData)
  156. for index, allStallInfo in pairs(varData) do
  157. if not table.isNullOrEmpty(allStallInfo) then
  158. for index, stallInfo in pairs(allStallInfo) do
  159. local stallActor = getactor(index)
  160. endstall(stallActor)
  161. if not table.isNullOrEmpty(stallInfo) and not table.isNullOrEmpty(stallActor) then
  162. -- jprint("结束摆摊")
  163. end
  164. end
  165. end
  166. end
  167. setsysvar(ROLE_STALL_START, {})
  168. end
  169. -- 摆摊喊话消耗道具
  170. function Stall.deleteItem(actor, msgData)
  171. removeitemfrombag(actor, msgData[1], msgData[2], 0, 9999, '摆摊')
  172. end
  173. -- 获取摊位商品信息
  174. function Stall.getStallGoods(actor, rid)
  175. local stallActor = getactor(actor, rid)
  176. local stallInfo = Stall.getStallTimeInfo(stallActor)
  177. if table.isEmpty(stallInfo) then
  178. return
  179. end
  180. local listingGood = {}
  181. listingGood["rid"] = rid
  182. listingGood["title"] = stallInfo.title
  183. local allgoods = {}
  184. local allWorldGoods = getsysvar(stallActor, SYS_TRADE_WORLD_GOODS)
  185. if not allWorldGoods then
  186. allWorldGoods = {}
  187. listingGood["number"] = 0
  188. listingGood["allgoods"] = allWorldGoods
  189. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_GOODS, listingGood)
  190. return
  191. end
  192. local roleWorld = allWorldGoods[stallActor:toString()]
  193. if not roleWorld then
  194. roleWorld = {}
  195. listingGood["number"] = 0
  196. listingGood["allgoods"] = roleWorld
  197. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_GOODS, listingGood)
  198. return
  199. end
  200. local now = getbaseinfo("now")
  201. for index, good in pairs(roleWorld) do
  202. if good.itemcfgid ~= 30030305 then
  203. local publicityTime = good.listingtime + good.publicitytime
  204. local offShelfTime = publicityTime + good.upshelfduration
  205. if publicityTime > now then
  206. good["publicity"] = 1
  207. good["timename"] = "公示中"
  208. good["time"] = (publicityTime - now) / 1000
  209. elseif publicityTime < now and offShelfTime > now then
  210. good["publicity"] = 0
  211. good["timename"] = "下架时间"
  212. good["time"] = (offShelfTime - now) / 1000
  213. end
  214. table.insert(allgoods, good)
  215. end
  216. end
  217. if allgoods then
  218. Trade.sortGoods(allgoods, 0)
  219. end
  220. listingGood["number"] = #allgoods
  221. listingGood["allgoods"] = allgoods
  222. sendluamsg(actor, LuaMessageIdToClient.RES_STALL_GOODS, listingGood)
  223. end
  224. -- 在全局变量添加摊位信息
  225. --function this.allStall(actor)
  226. -- local allStall = getsysvar(actor, ALL_STALL_START)
  227. -- if not allStall or allStall == "" then
  228. -- allStall = {}
  229. -- end
  230. -- table.insert(allStall,actor:toString())
  231. -- setsysvar(actor, ALL_STALL_START,allStall)
  232. --end
  233. --function clearallstallinfo(actor)
  234. -- setsysvar(actor,ALL_STALL_START,{})
  235. --end
  236. -- 在全局变量减少摊位信息
  237. --function this.deleteAllStall(actor)
  238. -- local allStall = getsysvar(actor, ALL_STALL_START)
  239. -- if not allStall then
  240. -- return
  241. -- end
  242. -- local deleteIndex = 0
  243. -- for index, stallActor in pairs(allStall) do
  244. -- if actor:toString() == stallActor then
  245. -- deleteIndex = index
  246. -- end
  247. -- end
  248. -- allStall[deleteIndex] = nil
  249. -- setsysvar(actor, ALL_STALL_START,allStall)
  250. --end
  251. function Stall.flushStall()
  252. callonserial("stall_flush")
  253. end
  254. function stall_flush()
  255. local allStallInfo = getsysvar(ROLE_STALL_START)
  256. if table.isEmpty(allStallInfo) then
  257. return
  258. end
  259. local stallActor
  260. local now = getbaseinfo("nowsec")
  261. for index, stallInfo in pairs(allStallInfo) do
  262. stallActor = getactor(index)
  263. if not table.isEmpty(stallInfo) and stallActor then
  264. if now > stallInfo.endTime then
  265. allStallInfo[index] = nil
  266. -- this.deleteAllStall(actor)
  267. endstall(stallActor)
  268. -- Stall.endStall(stallActor)
  269. end
  270. end
  271. stallActor = nil
  272. end
  273. setsysvar(ROLE_STALL_START, allStallInfo)
  274. end