· 6 years ago · Sep 15, 2019, 10:30 PM
1--[[
2
3!offer add, itemName, itemCount, itemPrice
4!offer add, plate armor, 1, 500
5
6!offer buy, AuctionID
7!offer buy, 1943
8
9!offer remove, AuctionID
10!offer remove, 1943
11
12!offer list
13
14CREATE TABLE `auction_system` (
15 `id` int(11) NOT NULL AUTO_INCREMENT,
16 `player_id` int(11) NOT NULL,
17 `item_name` varchar(255) NOT NULL,
18 `item_id` smallint(6) NOT NULL,
19 `count` smallint(5) NOT NULL,
20 `value` int(7) NOT NULL,
21 `date` int(11) NOT NULL,
22 PRIMARY KEY (`id`),
23 FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
24) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
25
26]]--
27
28local config = {
29 levelRequiredToAdd = 20,
30 maxOffersPerPlayer = 5,
31 valuePerOffer = 500,
32 blockedItems = {2165, 2152, 2148, 2160, 2166, 2167, 2168, 2169, 2202, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212, 2213, 2214, 2215, 2343, 2433, 2640, 6132, 6300, 6301, 9932, 9933}
33}
34
35function onSay(player, words, param)
36 if param == '' then
37 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
38 player:getPosition():sendMagicEffect(CONST_ME_POFF)
39 return false
40 elseif not Tile(player:getPosition()):hasFlag(TILESTATE_PROTECTIONZONE) then
41 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You must be in the protection zone to use these commands.")
42 player:getPosition():sendMagicEffect(CONST_ME_POFF)
43 return false
44 elseif player:getExhaustion(Storage.exhaustion) > 0 then
45 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You need to wait a time.")
46 player:getPosition():sendMagicEffect(CONST_ME_POFF)
47 return false
48 end
49
50 local word = param:split(",")
51 if word[1] == "add" then
52 if not word[2] or not word[3] or not word[4] then
53 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Command param required. Ex: !offer add, ItemName, ItemCount, ItemPrice")
54 player:getPosition():sendMagicEffect(CONST_ME_POFF)
55 return false
56 end
57
58 local itemCount = tonumber(word[3])
59 local itemValue = tonumber(word[4])
60 if not itemCount or not itemValue then
61 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You don't set valid price or items count.")
62 player:getPosition():sendMagicEffect(CONST_ME_POFF)
63 return false
64 elseif itemCount < 1 or itemValue < 1 then
65 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have to type a number higher than 0.")
66 player:getPosition():sendMagicEffect(CONST_ME_POFF)
67 return false
68 elseif string.len(itemCount) > 3 or string.len(itemValue) > 7 then
69 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "This price or item count is too high.")
70 player:getPosition():sendMagicEffect(CONST_ME_POFF)
71 return false
72 elseif player:getLevel() < config.levelRequiredToAdd then
73 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You don't have required level ".. levelRequiredToAdd ..".")
74 player:getPosition():sendMagicEffect(CONST_ME_POFF)
75 return false
76 end
77
78 player:setExhaustion(Storage.exhaustion, 5)
79
80 local offers = 0
81 local playerId = player:getGuid()
82 local resultId = db.storeQuery("SELECT `id` FROM `auction_system` WHERE `player_id` = " .. playerId)
83 if resultId ~= false then
84 repeat
85 offers = offers + 1
86 until not result.next(resultId)
87 result.free(resultId)
88 end
89
90 if offers >= config.maxOffersPerPlayer then
91 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Sorry you can't add more offers (max. " .. config.maxOffersPerPlayer .. ")")
92 player:getPosition():sendMagicEffect(CONST_ME_POFF)
93 return false
94 end
95
96 -- Trim left
97 word[2] = word[2]:gsub("^%s*(.-)$", "%1")
98
99 local itemId = ItemType(word[2]):getId()
100 itemCount = math.floor(itemCount)
101 if itemId == 0 then
102 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Item wich such name does not exists.")
103 player:getPosition():sendMagicEffect(CONST_ME_POFF)
104 return false
105 elseif table.contains(config.blockedItems, itemId) then
106 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "This item is blocked.")
107 player:getPosition():sendMagicEffect(CONST_ME_POFF)
108 return false
109 elseif player:getItemCount(itemId) < itemCount then
110 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you don't have this item(s).")
111 player:getPosition():sendMagicEffect(CONST_ME_POFF)
112 return false
113 end
114
115 if player:getMoney() >= config.valuePerOffer then
116 if not player:removeItem(itemId, itemCount) then
117 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You do not have the necessary items!")
118 player:getPosition():sendMagicEffect(CONST_ME_POFF)
119 return false
120 elseif not player:removeMoney(config.valuePerOffer) then
121 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You need ".. config.valuePerOffer .." gold coins to add an offer in auction system.")
122 player:getPosition():sendMagicEffect(CONST_ME_POFF)
123 return false
124 else
125 local itemName = ItemType(itemId):getName()
126 itemValue = math.floor(itemValue)
127 db.query("INSERT INTO `auction_system` (`player_id`, `item_name`, `item_id`, `count`, `value`, `date`) VALUES (" .. playerId .. ", \"" .. db.escapeString(itemName) .. "\", " .. itemId .. ", " .. itemCount .. ", " .. itemValue ..", " .. os.time() .. ")")
128 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You successfully add " .. itemCount .." " .. itemName .." for " .. itemValue .. " gold coins to auction system.")
129 end
130 else
131 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You need ".. config.valuePerOffer .." gold coins to add an offer in auction system.")
132 player:getPosition():sendMagicEffect(CONST_ME_POFF)
133 end
134
135 return false
136
137 elseif word[1] == "buy" then
138
139 if not word[2] then
140 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Command param required. Ex: /offer buy, AuctionID")
141 player:getPosition():sendMagicEffect(CONST_ME_POFF)
142 return false
143 end
144
145 local id = tonumber(word[2])
146 if not id then
147 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
148 player:getPosition():sendMagicEffect(CONST_ME_POFF)
149 return false
150 end
151
152 player:setExhaustion(Storage.exhaustion, 5)
153
154 local resultId = db.storeQuery("SELECT * FROM `auction_system` WHERE `id` = " .. id)
155 if resultId == false then
156 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "This offer does not exist.")
157 player:getPosition():sendMagicEffect(CONST_ME_POFF)
158 return false
159 end
160
161 local playerId = result.getNumber(resultId, "player_id")
162 local itemValue = result.getNumber(resultId, "value")
163 local itemId = result.getNumber(resultId, "item_id")
164 local itemCount = result.getNumber(resultId, "count")
165 result.free(resultId)
166
167 if player:getGuid() == playerId then
168 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you can't buy your own items.")
169 player:getPosition():sendMagicEffect(CONST_ME_POFF)
170 return false
171 elseif player:getFreeCapacity() < ItemType(itemId):getWeight() then
172 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You don't have capacity.")
173 player:getPosition():sendMagicEffect(CONST_ME_POFF)
174 return false
175 elseif not player:removeMoney(itemValue) then
176 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You don't have enoguh gold coins.")
177 player:getPosition():sendMagicEffect(CONST_ME_POFF)
178 return false
179 else
180 player:addItem(itemId, itemCount)
181 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You bought " .. itemCount .. " ".. ItemType(itemId):getName() .. " for " .. itemValue .. " gold coins in auction system!")
182 db.query("DELETE FROM `auction_system` WHERE `id` = " .. id)
183 local seller = Player(playerId)
184 if seller then
185 seller:setBankBalance(seller:getBankBalance() + itemValue)
186 else
187 db.query('UPDATE `players` SET `balance` = `balance` + ' .. itemValue .. ' WHERE `id` = ' .. playerId)
188 end
189 end
190
191 return false
192
193 elseif word[1] == "remove" then
194
195 if not word[2] then
196 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Command param required. Ex: /offer remove, AuctionID")
197 player:getPosition():sendMagicEffect(CONST_ME_POFF)
198 return false
199 end
200
201 local id = tonumber(word[2])
202 if not id then
203 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Wrong ID.")
204 player:getPosition():sendMagicEffect(CONST_ME_POFF)
205 return false
206 end
207
208 player:setExhaustion(Storage.exhaustion, 5)
209
210 local resultId = db.storeQuery("SELECT * FROM `auction_system` WHERE `id` = " .. id)
211 if resultId == false then
212 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "This offer does not exist.")
213 player:getPosition():sendMagicEffect(CONST_ME_POFF)
214 return false
215 end
216
217 local playerId = result.getNumber(resultId, "player_id")
218 local itemId = result.getNumber(resultId, "item_id")
219 local itemCount = result.getNumber(resultId, "count")
220 result.free(resultId)
221
222 if player:getGuid() == playerId then
223 if player:getFreeCapacity() < ItemType(itemId):getWeight() then
224 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You don't have capacity.")
225 player:getPosition():sendMagicEffect(CONST_ME_POFF)
226 else
227 db.query("DELETE FROM `auction_system` WHERE `id` = " .. id)
228 player:addItem(itemId, itemCount)
229 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Your offert has been deleted from offerts database.")
230 end
231 else
232 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "This is not your offert.")
233 end
234
235 return false
236
237 elseif word[1] == "list" then
238
239 player:setExhaustion(Storage.exhaustion, 5)
240
241 local message = "Trade Offline:\n\n!offer add, ItemName, ItemCount, ItemPrice\n!offer buy, AuctionID\n!offer remove, AuctionID\n\n"
242 local resultId = db.storeQuery("SELECT * FROM `auction_system` ORDER BY `id` ASC")
243 if resultId ~= false then
244 repeat
245 local auctionId = result.getNumber(resultId, "id")
246 local itemId = result.getNumber(resultId, "item_id")
247 local itemCount = result.getNumber(resultId, "count")
248 local itemValue = result.getNumber(resultId, "value")
249 message = ""..message.."ID: ".. auctionId .." - ".. itemCount .." ".. ItemType(itemId):getName() .." for ".. itemValue .." gold coins.\n"
250 until not result.next(resultId)
251 result.free(resultId)
252 player:popupFYI(message)
253 else
254 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is not offer in the system.")
255 end
256 end
257
258 return false
259end