· 6 years ago · Sep 14, 2019, 11:04 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
12CREATE TABLE `auction` (
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
32local function error(uid, text)
33 local player = Player(uid)
34 if player then
35 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, text)
36 player:getPosition():sendMagicEffect(CONST_ME_POFF)
37 return false
38 end
39end
40
41function onSay(player, words, param)
42 local playerId = player:getId()
43 if param == '' then
44 error(playerId, "Command param required.")
45 elseif not Tile(player:getPosition()):hasFlag(TILESTATE_PROTECTIONZONE) then
46 error(playerId, "You must be in the protection zone to use these commands.")
47 end
48
49 local word = param:split(",")
50 if word[1] == "add" then
51 if not word[2] or not word[3] or not word[4] then
52 error(playerId, "Command param required. Ex: /offer add, ItemName, ItemPrice, ItemCount")
53 elseif not tonumber(word[3]) or not tonumber(word[4]) then
54 error(playerId, "You don't set valid price or items count.")
55 elseif tonumber(word[3]) < 1 or tonumber(word[4]) < 1 then
56 error(playerId, "You have to type a number higher than 0.")
57 elseif string.len(word[3]) > 3 or string.len(word[4]) > 7 then
58 error(playerId, "This price or item count is too high.")
59 elseif player:getLevel() < config.levelRequiredToAdd then
60 error(playerId, "You don't have required level.")
61 end
62
63 local offers = 0
64 local resultId = db.storeQuery("SELECT `id` FROM `auction` WHERE `player` = " .. playerId)
65 if resultId ~= false then
66 repeat
67 offers = offers + 1
68 until not result.next(resultId)
69 result.free(resultId)
70 end
71
72 if offers >= config.maxOffersPerPlayer then
73 error(playerId, "Sorry you can't add more offers (max. " .. config.maxOffersPerPlayer .. ")")
74 end
75
76 local itemId = ItemType(word[2]):getId()
77 if not itemId or itemId == 0 then
78 error(playerId, "Item wich such name does not exists.")
79 elseif table.contains(config.blockedItems, itemId) then
80 error(playerId, "This item is blocked.")
81 elseif player:getItemCount(itemId) < word[3] then
82 error(playerId, "Sorry, you don't have this item(s).")
83 end
84
85 local itemCount = math.floor(word[3]
86 if not player:removeItem(itemId, itemCount) then
87 error(playerId, "You do not have the necessary items!")
88 else
89 local itemValue = math.floor(word[4])
90 db.query("INSERT INTO `auction` (`player_id`, `item_name`, `item_id`, `count`, `value`, `date`) VALUES (" .. playerId .. ", \"" .. db.escapeString(itemName) .. "\", " .. itemId .. ", " .. itemCount .. ", " .. itemValue ..", " .. os.time() .. ")")
91 end
92
93 return false
94
95 elseif word[1] == "buy" then
96
97 if not word[2] then
98 error(playerId, "Command param required. Ex: /offer buy, AuctionID")
99 elseif not tonumber(word[2]) then
100 error(playerId, "Wrong ID.")
101 end
102
103 local id = word[2]
104 local resultId = db.storeQuery("SELECT * FROM `auction` WHERE `id` = " .. id)
105 if resultId == false then
106 return false
107 end
108
109 local playerId = result.getNumber(resultId, "player")
110 local itemValue = result.getNumber(resultId, "value")
111 local itemId = result.getNumber(resultId, "item_id")
112 local itemCount = result.getNumber(resultId, "count")
113 result.free(resultId)
114
115 if player:getFreeCapacity() < ItemType(itemId):getWeight() then
116 error(playerId, "You don't have capacity.")
117 elseif player:getId() == playerId then
118 error(playerId, "Sorry, you can't buy your own items.")
119 elseif not player:removeMoney(itemValue) then
120 error(playerId, "You don't have enoguh gold coins.")
121 else
122 player:addItem(itemId, itemCount)
123 Player(playerId):setBankBalance(player:getBankBalance() + itemValue)
124 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You bought " .. itemCount .. " ".. ItemType(itemId):getName() .. " for " .. itemValue .. " gold coins in auction system!")
125 db.query("DELETE FROM `auction` WHERE `id` = " .. id)
126 end
127
128 return false
129
130 elseif word[1] == "remove" then
131
132 if not word[2] then
133 error(playerId, "Command param required. Ex: /offer remove, AuctionID")
134 elseif not tonumber(word[2]) then
135 error(playerId, "Wrong ID.")
136 end
137
138 local id = word[2]
139 local resultId = db.storeQuery("SELECT * FROM `auction` WHERE `id` = " .. id)
140 if resultId == false then
141 return false
142 end
143
144 local playerId = result.getString(resultId, "player")
145 local itemId = result.getNumber(resultId, "item_id")
146 local itemCount = result.getNumber(resultId, "count")
147 result.free(resultId)
148
149 if player:getId() == playerId then
150 if player:getFreeCapacity() < ItemType(itemId):getWeight() then
151 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You don't have capacity.")
152 player:getPosition():sendMagicEffect(CONST_ME_POFF)
153 else
154 db.query("DELETE FROM `auction` WHERE `id` = " .. id)
155 player:addItem(itemId, itemCount)
156 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Your offert has been deleted from offerts database.")
157 end
158 else
159 player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "This is not your offert.")
160 end
161
162 return false
163 end
164
165 return false
166end