1. itemshop = {}
  2. itemshop.config = {}
  3. itemshop.paths = {
  4. get_locale_base_path().."/quest/source/itemshop/syserr",
  5. get_locale_base_path().."/quest/source/itemshop/ITEMSHOP_CONFIG"
  6. }
  7. itemshop.viewers = {}
  8. itemshop.item = {
  9. ['ITEMSHOP'] = {},
  10. ['DRSSHOP'] = {},
  11. ['3RDSHOP'] = {},
  12. ['MOSTBOUGHT'] = {}
  13. }
  14. itemshop.logs = {}
  15. function split(str, delim, maxNb)
  16. if str == nil then return str end
  17. if string.find(str, delim) == nil then return { str } end
  18. if maxNb == nil or maxNb < 1 then maxNb = 0 end
  19. local result = {}
  20. local pat = "(.-)" .. delim .. "()"
  21. local nb = 0
  22. local lastPos
  23. for part, pos in string.gfind(str, pat) do
  24. nb = nb + 1
  25. result[nb] = part
  26. lastPos = pos
  27. if nb == maxNb then break end
  28. end
  29. if nb ~= maxNb then result[nb + 1] = string.sub(str, lastPos) end
  30. return result
  31. end
  32. function itemshop.syserr(err)
  33. local syserr_ishop = io.open(itemshop.paths[1], "a+")
  34. syserr_ishop:write(err)
  35. syserr_ishop:close()
  36. end
  37. os.remove(itemshop.paths[1])
  38. if true then
  39. local config_path = io.open(itemshop.paths[2], "r")
  40. if config_path == nil then
  41. itemshop.syserr("Can't load config file. Contact the creator.")
  42. return false
  43. end
  44. end
  45. function itemshop.get_coins()
  46. local c,account = mysql_direct_query("SELECT "..itemshop.config.COINS_COLUMN.." as coins, "..itemshop.config.DRS_COLUMN.." as drs FROM "..itemshop.config.ACCOUNT_TABLE.." WHERE id = "..pc.get_account_id().." LIMIT 1;")
  47. cmdchat('ITEMSHOP SEND_COINS#'..account[1].coins..'#'..account[1].drs)
  48. return tonumber(account[1].coins)
  49. end
  50. function itemshop.get_drs()
  51. local c,account = mysql_direct_query("SELECT "..itemshop.config.DRS_COLUMN.." as drs FROM "..itemshop.config.ACCOUNT_TABLE.." WHERE id = "..pc.get_account_id().." LIMIT 1;")
  52. return tonumber(account[1].drs)
  53. end
  54. function itemshop.get_3rd()
  55. local info
  56. if string.find(itemshop.config.OTHER_SHOP_TABLE, 'account') then
  57. info = pc.get_account_id()
  58. elseif string.find(itemshop.config.OTHER_SHOP_TABLE, 'player') then
  59. info = pc.get_player_id()
  60. end
  61. local c,qry = mysql_direct_query("SELECT "..(itemshop.config.OTHER_COLUMN).." as other FROM "..(itemshop.config.OTHER_SHOP_TABLE).." WHERE id = "..info.." LIMIT 1;")
  62. cmdchat('ITEMSHOP SEND_3RD_SHOP_COIN#'..qry[1].other)
  63. return tonumber(qry[1].other)
  64. end
  65. function itemshop.create_string(tab)
  66. local str_c = ""
  67. for i = 1, table.getn(tab) do
  68. str_c = str_c..tab[i]..(i < table.getn(tab) and ',' or '')
  69. end
  70. return str_c
  71. end
  72. function itemshop.load_config()
  73. local cfg = io.open(itemshop.paths[2], 'r')
  74. local booleans = {'ENABLE_ADMIN_PANEL', 'WHEEL_COST_DRS'}
  75. local tables = {'WHEEL_BAD_ITEMS', 'WHEEL_GOOD_ITEMS', 'ADMIN_PANEL_MEMBERS'}
  76. for line in cfg:lines() do
  77. tokens = split(line, ': ')
  78. local key = tokens[1]
  79. local value
  80. if itemshop.config_is_in(key, booleans) then
  81. if tonumber(tokens[2]) == 1 then
  82. value = true
  83. elseif tonumber(tokens[2]) == 0 then
  84. value = false
  85. end
  86. elseif itemshop.config_is_in(key, tables) then
  87. value = split(tokens[2], ',')
  88. else
  89. if tonumber(tokens[2]) then
  90. value = tonumber(tokens[2])
  91. else
  92. value = tostring(tokens[2])
  93. end
  94. end
  95. itemshop.config[key] = value
  96. end
  97. end
  98. function itemshop.config_is_in(cfg_opt, tbl)
  99. for i = 1, table.getn(tbl) do
  100. if tbl[i] == cfg_opt then
  101. return true
  102. end
  103. end
  104. return false
  105. end
  106. itemshop.load_config()
  107. function itemshop.load_items_array()
  108. itemshop.item['ITEMSHOP'] = {}
  109. local c,items = mysql_direct_query("SELECT * FROM "..itemshop.config.ITEMSHOP_ITEMS_TABLE..";")
  110. if c > 0 then
  111. for i = 1,c do
  112. itemshop.item['ITEMSHOP'][i] = {items[i].category, items[i].id, items[i].vnum, items[i].price, items[i].attrtype0, items[i].attrvalue0, items[i].attrtype1, items[i].attrvalue1, items[i].attrtype2, items[i].attrvalue2, items[i].attrtype3, items[i].attrvalue3, items[i].attrtype4, items[i].attrvalue4, items[i].attrtype5, items[i].attrvalue5, items[i].attrtype6, items[i].attrvalue6, items[i].socket0, items[i].socket1, items[i].socket2, items[i].time, items[i].percent}
  113. end
  114. end
  115. itemshop.item['DRSSHOP'] = {}
  116. local c,items = mysql_direct_query("SELECT * FROM "..itemshop.config.DRSSHOP_ITEMS_TABLE..";")
  117. if c > 0 then
  118. for i = 1,c do
  119. itemshop.item['DRSSHOP'][i] = {items[i].category, items[i].id, items[i].vnum, items[i].price, items[i].attrtype0, items[i].attrvalue0, items[i].attrtype1, items[i].attrvalue1, items[i].attrtype2, items[i].attrvalue2, items[i].attrtype3, items[i].attrvalue3, items[i].attrtype4, items[i].attrvalue4, items[i].attrtype5, items[i].attrvalue5, items[i].attrtype6, items[i].attrvalue6, items[i].socket0, items[i].socket1, items[i].socket2, items[i].time, items[i].percent}
  120. end
  121. end
  122. itemshop.item['3RDSHOP'] = {}
  123. local c,items = mysql_direct_query("SELECT * FROM "..itemshop.config.OTHERSHOP_ITEMS_TABLE..";")
  124. if c > 0 then
  125. for i = 1,c do
  126. itemshop.item['3RDSHOP'][i] = {items[i].category, items[i].id, items[i].vnum, items[i].price, items[i].attrtype0, items[i].attrvalue0, items[i].attrtype1, items[i].attrvalue1, items[i].attrtype2, items[i].attrvalue2, items[i].attrtype3, items[i].attrvalue3, items[i].attrtype4, items[i].attrvalue4, items[i].attrtype5, items[i].attrvalue5, items[i].attrtype6, items[i].attrvalue6, items[i].socket0, items[i].socket1, items[i].socket2, items[i].time, items[i].percent}
  127. end
  128. end
  129. end
  130. itemshop.load_items_array()
  131. function itemshop.unset_coins(amount)
  132. mysql_direct_query("UPDATE "..itemshop.config.ACCOUNT_TABLE.." SET "..itemshop.config.COINS_COLUMN.." = "..itemshop.config.COINS_COLUMN.." - "..amount.." WHERE id = "..pc.get_account_id()..";")
  133. mysql_direct_query("UPDATE "..itemshop.config.ACCOUNT_TABLE.." SET "..itemshop.config.DRS_COLUMN.." = "..itemshop.config.DRS_COLUMN.." + "..amount.." WHERE id = "..pc.get_account_id()..";")
  134. itemshop.get_coins()
  135. end
  136. function itemshop.unset_drs(amount)
  137. mysql_direct_query("UPDATE "..itemshop.config.ACCOUNT_TABLE.." SET "..itemshop.config.DRS_COLUMN.." = "..itemshop.config.DRS_COLUMN.." - "..amount.." WHERE id = "..pc.get_account_id()..";")
  138. itemshop.get_coins()
  139. end
  140. function itemshop.unset_3rd(amount)
  141. local info
  142. if string.find(itemshop.config.OTHER_SHOP_TABLE, 'account') then
  143. info = pc.get_account_id()
  144. elseif string.find(itemshop.config.OTHER_SHOP_TABLE, 'player') then
  145. info = pc.get_player_id()
  146. end
  147. mysql_direct_query("UPDATE "..(itemshop.config.OTHER_SHOP_TABLE).." SET "..(itemshop.config.OTHER_COLUMN).." = "..(itemshop.config.OTHER_COLUMN).." - "..amount.." WHERE id = "..info..";")
  148. itemshop.get_3rd()
  149. end
  150. function itemshop.load_items()
  151. if table.getn(itemshop.item['ITEMSHOP']) > 0 then
  152. for k,items in pairs(itemshop.item['ITEMSHOP']) do
  153. cmdchat('ITEMSHOP SET_ITEM#itemshop#'..items[1]..'#'..items[2]..'#'..items[3]..'#'..items[4]..'#'..items[5]..'#'..items[6]..'#'..items[7]..'#'..items[8]..'#'..items[9]..'#'..items[10]..'#'..items[11]..'#'..items[12]..'#'..items[13]..'#'..items[14]..'#'..items[15]..'#'..items[16]..'#'..items[17]..'#'..items[18]..'#'..items[19]..'#'..items[20]..'#'..items[21]..'#'..items[22]..'#0#'..items[23])
  154. end
  155. end
  156. if table.getn(itemshop.item['DRSSHOP']) > 0 then
  157. for k,items in pairs(itemshop.item['DRSSHOP']) do
  158. cmdchat('ITEMSHOP SET_ITEM#drs_shop#'..items[1]..'#'..items[2]..'#'..items[3]..'#'..items[4]..'#'..items[5]..'#'..items[6]..'#'..items[7]..'#'..items[8]..'#'..items[9]..'#'..items[10]..'#'..items[11]..'#'..items[12]..'#'..items[13]..'#'..items[14]..'#'..items[15]..'#'..items[16]..'#'..items[17]..'#'..items[18]..'#'..items[19]..'#'..items[20]..'#'..items[21]..'#'..items[22]..'#0#'..items[23])
  159. end
  160. end
  161. if table.getn(itemshop.item['3RDSHOP']) > 0 then
  162. for k,items in pairs(itemshop.item['3RDSHOP']) do
  163. cmdchat('ITEMSHOP SET_ITEM#3rd_shop#'..items[1]..'#'..items[2]..'#'..items[3]..'#'..items[4]..'#'..items[5]..'#'..items[6]..'#'..items[7]..'#'..items[8]..'#'..items[9]..'#'..items[10]..'#'..items[11]..'#'..items[12]..'#'..items[13]..'#'..items[14]..'#'..items[15]..'#'..items[16]..'#'..items[17]..'#'..items[18]..'#'..items[19]..'#'..items[20]..'#'..items[21]..'#'..items[22]..'#0#'..items[23])
  164. end
  165. end
  166. end
  167. function itemshop.load_hotoffers()
  168. if table.getn(itemshop.item['ITEMSHOP']) > 0 then
  169. for k,items in pairs(itemshop.item['ITEMSHOP']) do
  170. if items[22] > get_time() then
  171. cmdchat('ITEMSHOP SET_ITEM_HOTOFFERS#'..items[2]..'#'..items[3]..'#'..items[4]..'#'..items[5]..'#'..items[6]..'#'..items[7]..'#'..items[8]..'#'..items[9]..'#'..items[10]..'#'..items[11]..'#'..items[12]..'#'..items[13]..'#'..items[14]..'#'..items[15]..'#'..items[16]..'#'..items[17]..'#'..items[18]..'#'..items[19]..'#'..items[20]..'#'..items[21]..'#'..items[22]..'#0#'..items[23])
  172. end
  173. end
  174. end
  175. end
  176. function itemshop.load_mostbought_array()
  177. itemshop.item['MOSTBOUGHT'] = {}
  178. local c,vnum = mysql_direct_query("SELECT vnum,count(vnum) c FROM "..itemshop.config.ITEMSHOP_LOGS_TABLE.." GROUP BY vnum ORDER BY c DESC LIMIT 10;")
  179. if c > 0 then
  180. for a = 1,c do
  181. local co,items = mysql_direct_query("SELECT * FROM "..itemshop.config.ITEMSHOP_ITEMS_TABLE.." WHERE vnum = "..tonumber(vnum[a].vnum)..";")
  182. for i = 1,co do
  183. local array = {items[i].category, items[i].id, items[i].vnum, items[i].price, items[i].attrtype0, items[i].attrvalue0, items[i].attrtype1, items[i].attrvalue1, items[i].attrtype2, items[i].attrvalue2, items[i].attrtype3, items[i].attrvalue3, items[i].attrtype4, items[i].attrvalue4, items[i].attrtype5, items[i].attrvalue5, items[i].attrtype6, items[i].attrvalue6, items[i].socket0, items[i].socket1, items[i].socket2, items[i].time, items[i].percent}
  184. table.insert(itemshop.item['MOSTBOUGHT'], array)
  185. end
  186. end
  187. end
  188. end
  189. itemshop.load_mostbought_array()
  190. function itemshop.load_mostbought()
  191. if table.getn(itemshop.item['MOSTBOUGHT']) > 0 then
  192. for k,items in pairs(itemshop.item['MOSTBOUGHT']) do
  193. cmdchat('ITEMSHOP SET_ITEM_MOSTBOUGHT#'..items[2]..'#'..items[3]..'#'..items[4]..'#'..items[5]..'#'..items[6]..'#'..items[7]..'#'..items[8]..'#'..items[9]..'#'..items[10]..'#'..items[11]..'#'..items[12]..'#'..items[13]..'#'..items[14]..'#'..items[15]..'#'..items[16]..'#'..items[17]..'#'..items[18]..'#'..items[19]..'#'..items[20]..'#'..items[21]..'#'..items[22]..'#0#'..items[23])
  194. end
  195. end
  196. end
  197. function itemshop.load_logs_array()
  198. itemshop.logs = {}
  199. local c,logs = mysql_direct_query("SELECT * FROM "..itemshop.config.ITEMSHOP_LOGS_TABLE..";")
  200. if c > 0 then
  201. for i = 1,c do
  202. if itemshop.logs[logs[i].pid] == nil then itemshop.logs[logs[i].pid] = {} end
  203. itemshop.logs[logs[i].pid][i] = {logs[i].vnum, logs[i].amount, logs[i].price, (string.gsub(logs[i].date, ' ', '[_]')), logs[i].attrtype0, logs[i].attrvalue0, logs[i].attrtype1, logs[i].attrvalue1, logs[i].attrtype2, logs[i].attrvalue2, logs[i].attrtype3, logs[i].attrvalue3, logs[i].attrtype4, logs[i].attrvalue4, logs[i].attrtype5, logs[i].attrvalue5, logs[i].attrtype6, logs[i].attrvalue6, logs[i].socket0, logs[i].socket1, logs[i].socket2}
  204. end
  205. end
  206. end
  207. itemshop.load_logs_array()
  208. function itemshop.load_logs(order)
  209. if itemshop.logs[pc.get_player_id()] ~= nil then
  210. if order == 'all' then
  211. if table.getn(itemshop.logs[pc.get_player_id()]) > 0 then
  212. for k,logs in pairs(itemshop.logs[pc.get_player_id()]) do
  213. cmdchat('ITEMSHOP SET_LOG#'..logs[1]..'#'..logs[2]..'#'..logs[3]..'#'..logs[4]..'#'..logs[5]..'#'..logs[6]..'#'..logs[7]..'#'..logs[8]..'#'..logs[9]..'#'..logs[10]..'#'..logs[11]..'#'..logs[12]..'#'..logs[13]..'#'..logs[14]..'#'..logs[15]..'#'..logs[16]..'#'..logs[17]..'#'..logs[18]..'#'..logs[19]..'#'..logs[20]..'#'..logs[21])
  214. end
  215. end
  216. elseif order == 'last' then
  217. if table.getn(itemshop.logs[pc.get_player_id()]) > 0 then
  218. logs = itemshop.logs[pc.get_player_id()][table.getn(itemshop.logs[pc.get_player_id()])]
  219. cmdchat('ITEMSHOP SET_LOG#'..logs[1]..'#'..logs[2]..'#'..logs[3]..'#'..logs[4]..'#'..logs[5]..'#'..logs[6]..'#'..logs[7]..'#'..logs[8]..'#'..logs[9]..'#'..logs[10]..'#'..logs[11]..'#'..logs[12]..'#'..logs[13]..'#'..logs[14]..'#'..logs[15]..'#'..logs[16]..'#'..logs[17]..'#'..logs[18]..'#'..logs[19]..'#'..logs[20]..'#'..logs[21])
  220. end
  221. end
  222. end
  223. end
  224. function itemshop.create_categorys()
  225. for i = 1, itemshop.config.ITEMSHOP_CATEGORY_COUNT do
  226. cmdchat('ITEMSHOP CREATE_CATEGORY#itemshop#'..i)
  227. end
  228. for i = 1, itemshop.config.DRSSHOP_CATEGORY_COUNT do
  229. cmdchat('ITEMSHOP CREATE_CATEGORY#drs_shop#'..i)
  230. end
  231. for i = 1, itemshop.config.OTHERSHOP_CATEGORY_COUNT do
  232. cmdchat('ITEMSHOP CREATE_CATEGORY#3rd_shop#'..i)
  233. end
  234. end
  235. function itemshop.open_shop()
  236. if pc.is_gm() and itemshop.config.ENABLE_ADMIN_PANEL and (itemshop.config_is_in(pc.get_name(), itemshop.config.ADMIN_PANEL_MEMBERS) or itemshop.config_is_in(pc.get_player_id(), itemshop.config.ADMIN_PANEL_MEMBERS)) then
  237. cmdchat('ITEMSHOP OPEN#1')
  238. else
  239. cmdchat('ITEMSHOP OPEN#0')
  240. end
  241. itemshop.add_viewer(pc.get_name())
  242. end
  243. function itemshop.close_shop()
  244. itemshop.delete_viewer(pc.get_name())
  245. end
  246. function itemshop.buy_item(shop, id, amount)
  247. if (amount < 1) then return end
  248. local shop_info = {}
  249. if shop == 'itemshop' then
  250. shop_info[1] = itemshop.config.ITEMSHOP_ITEMS_TABLE
  251. shop_info[2] = itemshop.get_coins()
  252. shop_info[3] = itemshop.config.TXT_NO_COINS_BUY_ITEM
  253. shop_info[4] = itemshop.config.TXT_COIN_NAME
  254. shop_info[5] = itemshop.unset_coins
  255. elseif shop == 'drs_shop' then
  256. shop_info[1] = itemshop.config.DRSSHOP_ITEMS_TABLE
  257. shop_info[2] = itemshop.get_drs()
  258. shop_info[3] = itemshop.config.TXT_NO_DRS_BUY_ITEM
  259. shop_info[4] = itemshop.config.TXT_DRS_NAME
  260. shop_info[5] = itemshop.unset_drs
  261. elseif shop == '3rd_shop' then
  262. shop_info[1] = itemshop.config.OTHERSHOP_ITEMS_TABLE
  263. shop_info[2] = itemshop.get_3rd()
  264. shop_info[3] = itemshop.config.TXT_NO_3RD_BUY_ITEM
  265. shop_info[4] = itemshop.config.TXT_3RD_NAME
  266. shop_info[5] = itemshop.unset_3rd
  267. end
  268. local c,itemInfo = mysql_direct_query("SELECT * FROM "..shop_info[1].." WHERE id = "..id.." LIMIT 1;")
  269. if c > 0 then
  270. local price if tonumber(itemInfo[1].time)-get_time() > 0 then
  271. price = tonumber(itemInfo[1].price)-(tonumber(itemInfo[1].price)/100)*tonumber(itemInfo[1].percent) else price = tonumber(itemInfo[1].price) end
  272. if price < 0 or (price*amount) < 0 then return end
  273. if shop_info[2] < price*amount then
  274. syschat(shop_info[3])
  275. return
  276. end
  277. if not pc.enough_inventory(tonumber(itemInfo[1].vnum)) then
  278. syschat(itemshop.config.TXT_NOT_ENOUGH_INVENTORY)
  279. return
  280. end
  281. local attributes = {{tonumber(itemInfo[1].attrtype0), tonumber(itemInfo[1].attrvalue0)},{tonumber(itemInfo[1].attrtype1), tonumber(itemInfo[1].attrvalue1)},{tonumber(itemInfo[1].attrtype2), tonumber(itemInfo[1].attrvalue2)},{tonumber(itemInfo[1].attrtype3), tonumber(itemInfo[1].attrvalue3)},{tonumber(itemInfo[1].attrtype4), tonumber(itemInfo[1].attrvalue4)},{tonumber(itemInfo[1].attrtype5), tonumber(itemInfo[1].attrvalue5)},{tonumber(itemInfo[1].attrtype6), tonumber(itemInfo[1].attrvalue6)}}
  282. local sockets = {tonumber(itemInfo[1].socket0),tonumber(itemInfo[1].socket1),tonumber(itemInfo[1].socket2)}
  283. pc.give_item2_select(tonumber(itemInfo[1].vnum), amount)
  284. for attr = 0,6 do
  285. item.set_value(attr, attributes[attr+1][1], attributes[attr+1][2])
  286. end
  287. for socket = 0,2 do
  288. if sockets[socket+1] >= (86000/2) then
  289. sockets[socket+1] = sockets[socket+1] + get_time()
  290. end
  291. item.set_socket(socket, sockets[socket+1])
  292. end
  293. if item.get_count() > 1 then
  294. chat(string.format(itemshop.config.TXT_BOUGHT_ITEM_W_AMOUNT, amount, item.get_name(), price, shop_info[4]))
  295. else
  296. chat(string.format(itemshop.config.TXT_BOUGHT_ITEM_WO_AMOUNT, item.get_name(), price, shop_info[4]))
  297. end
  298. if shop == 'itemshop' then
  299. itemshop.write_log(pc.get_player_id(), tonumber(itemInfo[1].vnum), amount, item.get_id(), attributes, sockets, price)
  300. end
  301. shop_info[5](price*amount)
  302. itemshop.load_mostbought_array()
  303. itemshop.send_global_data('MOSTBOUGHT')
  304. end
  305. end
  306. function itemshop.write_log(pid, vnum, amount, itemID, attrs, sockets, price)
  307. local s_attrs,s_sockets = '',''
  308. for i = 1, table.getn(attrs) do
  309. s_attrs = s_attrs..attrs[i][1]..','..attrs[i][2]..(i < 7 and ',' or '')
  310. end
  311. for i = 1, table.getn(sockets) do
  312. s_sockets = s_sockets..sockets[i]..(i < 3 and ',' or '')
  313. end
  314. mysql_direct_query("INSERT INTO "..itemshop.config.ITEMSHOP_LOGS_TABLE.." (pid,vnum,amount,item_id,attrtype0,attrvalue0,attrtype1,attrvalue1,attrtype2,attrvalue2,attrtype3,attrvalue3,attrtype4,attrvalue4,attrtype5,attrvalue5,attrtype6,attrvalue6,socket0,socket1,socket2,price,date) VALUES ("..pid..","..vnum..","..amount..","..itemID..","..s_attrs..","..s_sockets..","..price..",'"..os.date("%Y-%m-%d %H:%M:%S").."');")
  315. if itemshop.logs[pid] == nil then itemshop.logs[pid] = {} end
  316. itemshop.logs[pid][table.getn(itemshop.logs[pid]) + 1] = {vnum, amount, price, (string.gsub(os.date("%Y-%m-%d %H:%M:%S"), ' ', '[_]')), attrs[1][1], attrs[1][2], attrs[2][1], attrs[2][2], attrs[3][1], attrs[3][2], attrs[4][1], attrs[4][2], attrs[5][1], attrs[5][2], attrs[6][1], attrs[6][2], attrs[7][1], attrs[7][2], sockets[1], sockets[2], sockets[3]}
  317. itemshop.load_logs('last')
  318. end
  319. function itemshop.load_wheel_items()
  320. cmdchat('ITEMSHOP SET_WHEEL_PRIZE#G#'..itemshop.create_string(itemshop.config.WHEEL_GOOD_ITEMS))
  321. cmdchat('ITEMSHOP SET_WHEEL_PRIZE#B#'..itemshop.create_string(itemshop.config.WHEEL_BAD_ITEMS))
  322. end
  323. function itemshop.request_spin_wheel()
  324. local price = itemshop.config.WHEEL_COST_DRS and itemshop.get_drs() or itemshop.get_coins()
  325. if price < itemshop.config.WHEEL_COST then
  326. syschat(itemshop.config.TXT_NO_COINS_SPIN_WHEEL)
  327. return
  328. end
  329. if pc.getf('itemshop', 'spin_time') ~= 0 and pc.getf('itemshop', 'spin_time') < get_time() then
  330. pc.setf('itemshop', 'spin_time', 0)
  331. pc.setf('itemshop', 'wheel_spins', 0)
  332. end
  333. if pc.getf('itemshop', 'wheel_spins') >= itemshop.config.WHEEL_LIMIT_SPINS_TIME then
  334. if pc.getf('itemshop', 'spin_time') <= 0 then
  335. pc.setf('itemshop', 'spin_time', get_time()+60*itemshop.config.WHEEL_LIMIT_TIME_SPINS_MINUTES)
  336. end
  337. syschat(itemshop.config.TXT_NO_MORE_SPINS)
  338. local remain = pc.getf('itemshop', 'spin_time')-get_time()
  339. local hours = math.floor(remain / 3600)
  340. local minutes = 0
  341. if hours >= 1 then
  342. minutes = math.floor((remain - (hours*3600)) / 60)
  343. syschat(string.format(itemshop.config.TXT_WHEEL_WAIT_TIME_W_HOURS, hours, minutes))
  344. else
  345. minutes = math.floor(remain / 60)
  346. if minutes < 1 then
  347. syschat(string.format(itemshop.config.TXT_WHEEL_WAIT_TIME_W_SEC, remain))
  348. else
  349. syschat(string.format(itemshop.config.TXT_WHEEL_WAIT_TIME_WO_HOURS, minutes))
  350. end
  351. end
  352. return
  353. end
  354. pc.setf('itemshop', 'wheel_spins', pc.getf('itemshop', 'wheel_spins')+1)
  355. if pc.getf('itemshop', 'wheel_spins') >= itemshop.config.WHEEL_LIMIT_SPINS_TIME then
  356. if pc.getf('itemshop', 'spin_time') <= 0 then
  357. pc.setf('itemshop', 'spin_time', get_time()+60*itemshop.config.WHEEL_LIMIT_TIME_SPINS_MINUTES)
  358. end
  359. end
  360. pc.setf('itemshop_manager', 'enable_wheel', 1)
  361. cmdchat('ITEMSHOP ALLOW_SPIN_WHEEL#')
  362. if itemshop.config.WHEEL_COST_DRS then
  363. itemshop.unset_drs(itemshop.config.WHEEL_COST)
  364. else
  365. itemshop.unset_coins(itemshop.config.WHEEL_COST)
  366. end
  367. end
  368. function itemshop.admin_add_item_time(item_id, item_time, percent, shop)
  369. if itemshop.config.ENABLE_ADMIN_PANEL == false then return end
  370. if not pc.is_gm() then return end
  371. local shop_db
  372. if shop == 0 then
  373. shop_db = itemshop.config.ITEMSHOP_ITEMS_TABLE
  374. elseif shop == 1 then
  375. shop_db = itemshop.config.DRSSHOP_ITEMS_TABLE
  376. elseif shop == 2 then
  377. shop_db = itemshop.config.OTHERSHOP_ITEMS_TABLE
  378. end
  379. local realTime = item_time+get_time()
  380. mysql_direct_query("UPDATE "..shop_db.." SET time = "..realTime..", percent = "..percent.." WHERE id = "..item_id..";")
  381. local shop_name = {[0] = 'ITEMSHOP', [1] = 'DRSSHOP', [2] = '3RDSHOP'}
  382. for k,items in pairs(itemshop.item[shop_name[shop]]) do
  383. if items[2] == item_id then
  384. items[22] = realTime
  385. items[23] = percent
  386. break
  387. end
  388. end
  389. for k,items in pairs(itemshop.item['MOSTBOUGHT']) do
  390. if items[2] == item_id then
  391. items[22] = realTime
  392. items[23] = percent
  393. break
  394. end
  395. end
  396. chat(string.format(itemshop.config.TXT_SUCCESS_TIME_ITEM, percent, item_time))
  397. itemshop.send_global_data('DISCOUNT')
  398. end
  399. function itemshop.admin_add_category_time(category, category_time, percent, shop)
  400. if itemshop.config.ENABLE_ADMIN_PANEL == false then return end
  401. if not pc.is_gm() then return end
  402. local shop_db
  403. if shop == 0 then
  404. shop_db = itemshop.config.ITEMSHOP_ITEMS_TABLE
  405. elseif shop == 1 then
  406. shop_db = itemshop.config.DRSSHOP_ITEMS_TABLE
  407. elseif shop == 2 then
  408. shop_db = itemshop.config.OTHERSHOP_ITEMS_TABLE
  409. end
  410. local realTime = category_time+get_time()
  411. mysql_direct_query("UPDATE "..shop_db.." SET time = "..realTime..", percent = "..percent.." WHERE category = "..category..";")
  412. local shop_name = {[0] = 'ITEMSHOP', [1] = 'DRSSHOP', [2] = '3RDSHOP'}
  413. for k,items in pairs(itemshop.item[shop_name[shop]]) do
  414. if items[1] == category then
  415. items[22] = realTime
  416. items[23] = percent
  417. end
  418. end
  419. for k,items in pairs(itemshop.item['MOSTBOUGHT']) do
  420. if items[1] == category then
  421. items[22] = realTime
  422. items[23] = percent
  423. end
  424. end
  425. chat(string.format(itemshop.config.TXT_SUCCESS_TIME_CATEGORY, percent, category_time, category))
  426. itemshop.send_global_data('DISCOUNT')
  427. end
  428. function itemshop.admin_add_item(category, vnum, price, attributes, sockets, shop)
  429. if itemshop.config.ENABLE_ADMIN_PANEL == false then return end
  430. if not pc.is_gm() then return end
  431. local s_attrs,s_sockets = '',''
  432. for i = 1, table.getn(attributes) do
  433. s_attrs = s_attrs..attributes[i][1]..','..attributes[i][2]..(i < 7 and ',' or '')
  434. end
  435. for i = 1, table.getn(sockets) do
  436. s_sockets = s_sockets..sockets[i]..(i < 3 and ',' or '')
  437. end
  438. local shop_db
  439. if shop == 0 then
  440. shop_db = itemshop.config.ITEMSHOP_ITEMS_TABLE
  441. elseif shop == 1 then
  442. shop_db = itemshop.config.DRSSHOP_ITEMS_TABLE
  443. elseif shop == 2 then
  444. shop_db = itemshop.config.OTHERSHOP_ITEMS_TABLE
  445. end
  446. local shop_name = {[0] = 'ITEMSHOP', [1] = 'DRSSHOP', [2] = '3RDSHOP'}
  447. mysql_direct_query("INSERT INTO "..shop_db.." (category,vnum,price,attrtype0,attrvalue0,attrtype1,attrvalue1,attrtype2,attrvalue2,attrtype3,attrvalue3,attrtype4,attrvalue4,attrtype5,attrvalue5,attrtype6,attrvalue6,socket0,socket1,socket2) VALUES ("..category..","..vnum..","..price..","..s_attrs..","..s_sockets..");")
  448. itemshop.load_items_array()
  449. chat(itemshop.config.TXT_SUCCESS_ADDED_ITEM)
  450. itemshop.send_global_data('NEW_ITEM')
  451. end
  452. function itemshop.admin_delete_item(shop, item_id)
  453. if itemshop.config.ENABLE_ADMIN_PANEL == false then return end
  454. if not pc.is_gm() then return end
  455. if not (itemshop.config_is_in(pc.get_name(), itemshop.config.ADMIN_PANEL_MEMBERS) or itemshop.config_is_in(pc.get_player_id(), itemshop.config.ADMIN_PANEL_MEMBERS)) then return end
  456. local shop_db
  457. if shop == 'itemshop' then
  458. shop_db = itemshop.config.ITEMSHOP_ITEMS_TABLE
  459. elseif shop == 'drs_shop' then
  460. shop_db = itemshop.config.DRSSHOP_ITEMS_TABLE
  461. elseif shop == '3rd_shop' then
  462. shop_db = itemshop.config.OTHERSHOP_ITEMS_TABLE
  463. end
  464. mysql_direct_query("DELETE FROM "..shop_db.." WHERE id = "..item_id..";")
  465. itemshop.load_items_array()
  466. itemshop.load_mostbought_array()
  467. itemshop.load_items_array()
  468. chat(itemshop.config.TXT_SUCCESS_DELETED_ITEM)
  469. itemshop.send_global_data('NONE')
  470. end
  471. function itemshop.admin_edit_item_price(shop, item_id, new_price)
  472. if new_price == 0 then return end
  473. if itemshop.config.ENABLE_ADMIN_PANEL == false then return end
  474. if not pc.is_gm() then return end
  475. if not (itemshop.config_is_in(pc.get_name(), itemshop.config.ADMIN_PANEL_MEMBERS) or itemshop.config_is_in(pc.get_player_id(), itemshop.config.ADMIN_PANEL_MEMBERS)) then return end
  476. local shop_db
  477. if shop == 'itemshop' then
  478. shop_db = itemshop.config.ITEMSHOP_ITEMS_TABLE
  479. elseif shop == 'drs_shop' then
  480. shop_db = itemshop.config.DRSSHOP_ITEMS_TABLE
  481. elseif shop == '3rd_shop' then
  482. shop_db = itemshop.config.OTHERSHOP_ITEMS_TABLE
  483. end
  484. mysql_direct_query("UPDATE "..shop_db.." SET price = "..new_price.." WHERE id = "..item_id..";")
  485. local shop_name = {['itemshop'] = 'ITEMSHOP', ['drs_shop'] = 'DRSSHOP', ['3rd_shop'] = '3RDSHOP'}
  486. for k,items in pairs(itemshop.item[shop_name[shop]]) do
  487. if items[2] == item_id then
  488. items[4] = new_price
  489. break
  490. end
  491. end
  492. for k,items in pairs(itemshop.item['MOSTBOUGHT']) do
  493. if items[2] == item_id then
  494. items[4] = new_price
  495. break
  496. end
  497. end
  498. chat(itemshop.config.TXT_SUCCESS_DELETED_ITEM)
  499. itemshop.send_global_data('CHANGED_PRICE')
  500. end
  501. function itemshop.add_viewer(name)
  502. table.insert(itemshop.viewers, name)
  503. end
  504. function itemshop.delete_viewer(name)
  505. for k,pname in pairs(itemshop.viewers) do
  506. if pname == name then
  507. table.remove(itemshop.viewers, k)
  508. end
  509. end
  510. end
  511. function itemshop.send_global_data(data)
  512. local old_vid = pc.get_vid()
  513. for k,pname in pairs(itemshop.viewers) do
  514. local vid = find_pc_by_name(pname)
  515. if vid > 0 then
  516. pc.select(vid)
  517. cmdchat('ITEMSHOP CLEAR_CONTENT#')
  518. itemshop.create_categorys()
  519. itemshop.load_items()
  520. itemshop.load_hotoffers()
  521. itemshop.load_mostbought()
  522. if data == 'NEW_ITEM' then
  523. chat(itemshop.config.TXT_GLOBAL_NEW_ITEM)
  524. elseif data == 'DISCOUNT' then
  525. chat(itemshop.config.TXT_GLOBAL_DISCOUNT)
  526. elseif data == 'CHANGED_PRICE' then
  527. chat(itemshop.config.TXT_GLOBAL_EDITED_PRICE)
  528. end
  529. cmdchat('ITEMSHOP REFRESH_CONTENT#')
  530. end
  531. end
  532. pc.select(old_vid)
  533. end

itemshop_lib.lua