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