Advertisement
Guest User

Untitled

a guest
Jan 16th, 2021
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1. local storeweb = GlobalEvent("storeweb")
  2.  
  3. local messageType = MESSAGE_EVENT_ADVANCE
  4. local displayExecutionTime = true -- how time script took in console (true/false)
  5.  
  6. -- don't edit anything below this line
  7. if(displayExecutionTime) then
  8. function doSecondsFormat(i)
  9. local str, found = string.gsub(i, "(%d)(%d%d%d)$", "%1.%2", 1), 0
  10. repeat
  11. str, found = string.gsub(str, "(%d)(%d%d%d),", "%1.%2,", 1)
  12. until found == 0
  13. return str
  14. end
  15. end
  16.  
  17. if(not messageType) then
  18. messageType = MESSAGE_EVENT_ADVANCE
  19. if(not messageType) then
  20. messageType = MESSAGE_INFO_DESCR
  21. end
  22. end
  23.  
  24. if(not getPlayerByName) then
  25. function getPlayerByName(name) local p = Player(name) return p ~= nil and p:getId() or false end
  26. end
  27.  
  28.  
  29. if(not isPlayer) then
  30. function isPlayer(cid) return Player(cid) ~= nil end
  31. end
  32.  
  33. if(not doPlayerSave) then
  34. function doPlayerSave(cid)
  35. if(Player and type(Player) == "table" and Player.save and type(Player.save) == "function") then
  36. local player = Player(cid)
  37. if(player) then
  38. player:save()
  39. end
  40. end
  41.  
  42. return true
  43. end
  44. end
  45.  
  46. function getResults()
  47. if(db.storeQuery ~= nil and result.free ~= nil) then -- TFS 1.0+
  48. local resultId = db.storeQuery("SELECT * FROM z_ots_comunication;")
  49. if(resultId == false) then
  50. return false
  51. end
  52.  
  53. local results = {}
  54. repeat
  55. local tmp = {}
  56. tmp.name = result.getDataString(resultId, "name")
  57.  
  58. -- better performance when no player found
  59. tmp.exist = false
  60. tmp.cid = getPlayerByName(tmp.name)
  61. if(tmp.cid and isPlayer(tmp.cid)) then
  62. tmp.exist = true
  63.  
  64. tmp.id = result.getDataInt(resultId, "id")
  65. tmp.action = result.getDataString(resultId, "action")
  66. tmp.delete_it = result.getDataInt(resultId, "delete_it")
  67.  
  68. tmp.param1 = result.getDataInt(resultId, "param1")
  69. tmp.param2 = result.getDataInt(resultId, "param2")
  70. tmp.param3 = result.getDataInt(resultId, "param3")
  71. tmp.param4 = result.getDataInt(resultId, "param4")
  72. tmp.param5 = result.getDataString(resultId, "param5")
  73. tmp.param6 = result.getDataString(resultId, "param6")
  74. end
  75.  
  76. table.insert(results, tmp)
  77. until not result.next(resultId)
  78. result.free(resultId)
  79.  
  80. return results
  81. else -- TFS 0.3
  82. if(db.getResult ~= nil) then
  83. local result_plr = db.getResult("SELECT * FROM z_ots_comunication;")
  84. if(result_plr:getID() == -1) then
  85. return false
  86. end
  87.  
  88. local results = {}
  89. repeat
  90. local tmp = {}
  91. tmp.name = tostring(result_plr:getDataString("name"))
  92.  
  93. -- better performance when no player found
  94. tmp.exist = false
  95. tmp.cid = getPlayerByName(tmp.name)
  96. if(tmp.cid and isPlayer(tmp.cid)) then
  97. tmp.exist = true
  98.  
  99. tmp.id = tonumber(result_plr:getDataInt("id"))
  100. tmp.action = tostring(result_plr:getDataString("action"))
  101. tmp.delete_it = tonumber(result_plr:getDataInt("delete_it"))
  102.  
  103. tmp.param1 = tonumber(result_plr:getDataInt("param1"))
  104. tmp.param2 = tonumber(result_plr:getDataInt("param2"))
  105. tmp.param3 = tonumber(result_plr:getDataInt("param3"))
  106. tmp.param4 = tonumber(result_plr:getDataInt("param4"))
  107. tmp.param5 = tostring(result_plr:getDataString("param5"))
  108. tmp.param6 = tostring(result_plr:getDataString("param6"))
  109. end
  110.  
  111. table.insert(results, tmp)
  112. until not(result_plr:next())
  113.  
  114. result_plr:free()
  115. return results
  116. else
  117. print('[ERROR - gesior-shop-system.lua] Your distribution is not supported')
  118. end
  119. end
  120.  
  121. return false
  122. end
  123.  
  124. function doQuery(query)
  125. if(db.asyncQuery ~= nil) then
  126. db.asyncQuery(query)
  127. elseif(db.query ~= nil) then
  128. db.query(query)
  129. elseif(db.executeQuery ~= nil) then
  130. db.executeQuery(query)
  131. else
  132. return false
  133. end
  134.  
  135. return true
  136. end
  137.  
  138. if(not getItemWeightById) then
  139. getItemWeightById = getItemWeight
  140. end
  141.  
  142. if(not doCreateItemEx) then
  143. function doCreateItemEx(itemid, count)
  144. if(Game and type(Game) == "table" and Game.createItem and type(Game.createItem) == "function") then
  145. local item = Game.createItem(itemid, count)
  146. if item then
  147. return item:getUniqueId()
  148. end
  149. return false
  150. else
  151. print("[ERROR - gesior-shop-system] Error code: 1. Please contact slawkens at www.otland.net")
  152. end
  153. end
  154. end
  155.  
  156. function storeweb.onThink(interval)
  157. if(interval > 1000) then
  158. interval = interval / 1000
  159. end
  160.  
  161. local started = os.mtime and os.mtime() or os.time()
  162. local addedItems, waitingItems = 0, 0
  163. local added = false
  164.  
  165. local results = getResults()
  166. if(not results) then
  167. return true
  168. end
  169.  
  170. for i, v in ipairs(results) do
  171. added = false
  172. local id = v.id
  173. local action = v.action
  174. local delete = v.delete_it
  175.  
  176. if(v.exist) then
  177. local cid = v.cid
  178. local param1, param2, param3, param4 = v.param1, v.param2, v.param3, v.param4
  179. local add_item_type = v.param5
  180. local add_item_name = v.param6
  181. local received_item, full_weight, items_weight, item_weigth = 0, 0, 0, 0
  182. local item_doesnt_exist = false
  183.  
  184. if(add_item_type == 'container' or add_item_type == 'item') then
  185. local item_weigth = getItemWeightById(param1, 1)
  186. if(type(item_weigth) == 'boolean') then -- item doesn't exist
  187. print("[ERROR - gesior-shop-system] Invalid item id: " .. param1 .. ". Change/Fix `itemid1` in `z_shop_offers` then delete it from `z_ots_comunication`")
  188. item_doesnt_exist = true
  189. else
  190. if(add_item_type == 'container') then
  191. container_weight = getItemWeightById(param3, 1)
  192. if(type(container_weight) == 'boolean') then -- container item doesn't exist
  193. print("[ERROR - gesior-shop-system] Invalid container id: " .. param3 .. ". Change/Fix `itemid2` in `z_shop_offers` then delete it from `z_ots_comunication`")
  194. item_doesnt_exist = true
  195. else
  196. if(isItemRune(param1)) then
  197. items_weight = param4 * item_weigth
  198. else
  199. items_weight = param4 * getItemWeightById(param1, param2)
  200. end
  201.  
  202. full_weight = items_weight + container_weight
  203. end
  204. elseif(add_item_type == 'item') then
  205. full_weight = getItemWeightById(param1, param2)
  206. if(isItemRune(param1)) then
  207. full_weight = getItemWeightById(param1, 1)
  208. end
  209. end
  210. end
  211.  
  212. if(not item_doesnt_exist) then
  213. local free_cap = getPlayerFreeCap(cid)
  214. if(full_weight <= free_cap) then
  215. if(add_item_type == 'container') then
  216. local new_container = doCreateItemEx(param3, 1)
  217. for x = 1, param4 do
  218. doAddContainerItem(new_container, param1, param2)
  219. end
  220. received_item = doPlayerAddItemEx(cid, new_container)
  221. else
  222. local new_item = doCreateItemEx(param1, param2)
  223. received_item = doPlayerAddItemEx(cid, new_item)
  224. end
  225.  
  226. if(received_item == RETURNVALUE_NOERROR) then
  227. doPlayerSendTextMessage(cid, messageType, "You received >> ".. add_item_name .." << from OTS shop.")
  228. doQuery("DELETE FROM `z_ots_comunication` WHERE `id` = " .. id .. ";")
  229. doQuery("UPDATE `z_shop_history` SET `trans_state`='realized', `trans_real`=" .. os.time() .. " WHERE comunication_id = " .. id .. ";")
  230. doPlayerSave(cid)
  231. added = true
  232. else
  233. doPlayerSendTextMessage(cid, messageType, '>> '.. add_item_name ..' << from OTS shop is waiting for you. Please make place for this item in your backpack/hands and wait about '.. interval ..' seconds to get it.')
  234. end
  235. else
  236. doPlayerSendTextMessage(cid, messageType, '>> '.. add_item_name ..' << from OTS shop is waiting for you. It weight is '.. full_weight ..' oz., you have only '.. free_cap ..' oz. free capacity. Put some items in depot and wait about '.. interval ..' seconds to get it.')
  237. end
  238. end
  239. elseif(add_item_type == 'addon') then
  240. doPlayerSendTextMessage(cid, messageType, "You received >> ".. add_item_name .." << from OTS shop.")
  241. doSendMagicEffect(getCreaturePosition(cid), CONST_ME_GIFT_WRAPS)
  242. doPlayerAddOutfit(cid, param1, param3)
  243. doPlayerAddOutfit(cid, param2, param4)
  244. doQuery("DELETE FROM `z_ots_comunication` WHERE `id` = " .. id .. ";")
  245. doQuery("UPDATE `z_shop_history` SET `trans_state`='realized', `trans_real`=" .. os.time() .. " WHERE comunication_id = " .. id .. ";")
  246. doPlayerSave(cid)
  247. added = true
  248. elseif(add_item_type == 'mount') then
  249. if(not doPlayerAddMount) then
  250. print("[ERROR - gesior-shop-system] Your server doesn't support mounts. Remove all items in database from your `z_shop_offers` table where `offer_type` = mount and also in `z_ots_comunication` where `param5` = mount.")
  251. else
  252. doPlayerAddMount(cid, param1)
  253. doPlayerSendTextMessage(cid, messageType, "You received >> ".. add_item_name .." << from OTS shop.")
  254. doSendMagicEffect(getCreaturePosition(cid), CONST_ME_GIFT_WRAPS)
  255.  
  256. doQuery("DELETE FROM `z_ots_comunication` WHERE `id` = " .. id .. ";")
  257. doQuery("UPDATE `z_shop_history` SET `trans_state`='realized', `trans_real`=" .. os.time() .. " WHERE comunication_id = " .. id .. ";")
  258. doPlayerSave(cid)
  259. added = true
  260. end
  261. end
  262. end
  263.  
  264. if(added) then
  265. addedItems = addedItems + 1
  266. else
  267. waitingItems = waitingItems + 1
  268. end
  269. end
  270.  
  271. local message = ">> Shopsystem, added " .. addedItems .. " items. Still waiting with " .. waitingItems .. " items."
  272.  
  273. if(displayExecutionTime) then
  274. local done, str = os.time() - started, ""
  275. if(os.mtime) then
  276. done = os.mtime() - started
  277. if(done < 100) then
  278. str = "0.0" .. done
  279. elseif(done < 1000) then
  280. str = "0." .. done
  281. else
  282. str = doSecondsFormat(done)
  283. if(str:len() == 0) then str = "0.0" end
  284. end
  285. end
  286.  
  287. message = message .. " Done in: " .. str .. "s."
  288. end
  289.  
  290. print(message)
  291. return true
  292. end
  293. storeweb:interval(1000)
  294. storeweb:register()
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement