Advertisement
Guest User

Untitled

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