· 10 years ago · Sep 19, 2016, 01:26 AM
1-- Includes
2include("listener/playerListener.lua");
3include("listener/commandListener.lua");
4
5-- Global variables
6database = getDatabase();
7server = getServer();
8world = getWorld();
9wallets = {};
10shops = {};
11bankValues = {};
12defaultPrice = 0;
13defaultEarning = 1;
14walletLoaded = false;
15shopsLoaded = false;
16
17--- Script enable event.
18-- This event is triggered when the script is loaded.
19-- From this point on, the script gets notified about
20-- all events and is able to call serverside functions.
21function onEnable()
22 print("Money maker script started!");
23
24 -- Create database tables "areas" and "rights" if they don't exist already
25 database:queryupdate("CREATE TABLE IF NOT EXISTS 'wallets' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'playerID' INTEGER, 'walletAmount' INTEGER);");
26 database:queryupdate("CREATE TABLE IF NOT EXISTS 'accounts' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'playerID' INTEGER, 'pincode' varchar, 'amount' INTEGER);");
27 database:queryupdate("CREATE TABLE IF NOT EXISTS 'treasury' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'reserve' INTEGER, 'transactionFee' integer, 'intrest' integer, 'tax' integer, 'defaultPrice' integer, 'defaultEarnings' integer);");
28 database:queryupdate("CREATE TABLE IF NOT EXISTS 'earnings' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'type' VARCHAR, 'amount' INTEGER, 'blockID' INTEGER);");
29 database:queryupdate("CREATE TABLE IF NOT EXISTS 'prices' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'itemName' VARCHAR, 'price' INTEGER, 'perAmount' INTEGER, 'itemTypeID' INTEGER, 'TextureID' INTEGER);");
30 database:queryupdate("CREATE TABLE IF NOT EXISTS 'shopareas' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'type' VARCHAR, 'startChunkpositionX' INTEGER, 'startChunkpositionY' INTEGER, 'startChunkpositionZ' INTEGER, 'startBlockpositionX' INTEGER, 'startBlockpositionY' INTEGER, 'startBlockpositionZ' INTEGER,'endChunkpositionX' INTEGER, 'endChunkpositionY' INTEGER, 'endChunkpositionZ' INTEGER, 'endBlockpositionX' INTEGER, 'endBlockpositionY' INTEGER, 'endBlockpositionZ' INTEGER, 'playerID' INTEGER);");
31 database:queryupdate("CREATE TABLE IF NOT EXISTS 'rights' ('ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'shopareaID' INTEGER, 'playerID' INTEGER, 'group' VARCHAR);");
32 database:queryupdate("CREATE TABLE IF NOT EXISTS 'chests' ('ID' INTEGER , 'chunkOffsetX' INTEGER, 'chunkOffsetY' INTEGER, 'chunkOffsetZ' INTEGER, 'positionX' INTEGER, 'positionY' INTEGER, 'positionZ' INTEGER);");
33 -- Load all groups from property-files
34 --loadGroups();
35
36 -- Receive all saved chests from database and store them in a global table
37 --local result = database:query("SELECT * FROM chests;");
38 --while result:next() do
39 -- local chest = {};
40 -- chest["chunkOffsetX"] = result:getInt("chunkOffsetX");
41 -- chest["chunkOffsetY"] = result:getInt("chunkOffsetY");
42 -- chest["chunkOffsetZ"] = result:getInt("chunkOffsetZ");
43 -- chest["positionX"] = result:getFloat("positionX");
44 -- chest["positionY"] = result:getFloat("positionY");
45 -- chest["positionZ"] = result:getFloat("positionZ");
46 --
47 -- chests[result:getInt("ID")] = chest;
48 -- print("Load Chest ID:" .. result:getInt("ID"));
49 --end
50
51 -- Iterate the table "areas" and assign all related grouprights
52 --for key,value in pairs(areas) do
53 -- result = database:query("SELECT * FROM rights WHERE areaID='".. value["areaID"] .. "';");
54 -- while result:next() do
55 -- local group = getGroupByName(result:getString("Group"));
56 -- if group ~= nil then
57 -- value["rights"][result:getInt("playerID")] = group;
58 -- end
59 -- end
60 -- print("Loaded area \"".. value["areaName"] .."\" successfully");
61 --end
62 loadShops();
63 -- Set a flag indicating that all areas are loaded
64 --walletLoaded = true;
65 shopsLoaded = true;
66
67 readBankRates();
68 bank_timer = setTimer(function() readBankRates(); end, 3600, -1);
69end
70
71--- Script disable event.
72-- This event is triggered when the script is unloaded
73-- which causes the script to stop.
74function onDisable()
75 print("Money maker script stopped!");
76end
77
78--- Update tick event (frequently called event!)
79-- This event is triggered every tick. Use this event
80-- to update your script environment, as long as it requires frequent updates.
81-- @param event The update event object
82-- You can get the current tpf (event:getTpf()) or runningtime from it
83function onUpdate(event)
84 --print(server:getGameTimestamp());
85 --print("TPF:" .. event:getTpf());
86 --print("RUNNINGTIME:" .. event:getRunningTime());
87end
88addEvent("Update", onUpdate);
89
90----------------------------------------------------------
91function readBankRates()
92 local result1 = database:query("select * from treasury");
93 if result1:isAfterLast() == false then
94 bankValues["reserve"] = result1:getInt("reserve");
95 bankValues["transactionFee"] = result1:getInt("transactionFee");
96 bankValues["intrest"] = result1:getInt("intrest");
97 bankValues["tax"] = result1:getInt("tax");
98 bankValues["defaultPrice"] = result1:getInt("defaultPrice");
99 bankValues["defaultEarnings"] = result1:getInt("defaultEarnings");
100 else
101 database:query("insert into treasury (reserve, transactionFee, intrest, tax, defaultPrice, defaultEarnings) values (0, 5, 0, 1, 0, 1)");
102 readBankRates();
103 end
104end
105
106function readAccount(player)
107 local result2 = database:query("SELECT * FROM accounts where playerID = "..player:getDBID());
108 if result2:isAfterLast() == false then
109 local account = {};
110 account["accountID"] = result2:getInt("ID");
111 account["playerID"] = result2:getInt("playerID");
112 account["pincode"] = result2:getString("pincode");
113 account["amount"] = result2:getInt("amount");
114 player:setAttribute("account", account);
115 else
116 player:setAttribute("account", nil);
117 end
118end
119
120function readWallet(player)
121 local result3 = database:query("SELECT * FROM wallets where playerID = "..player:getDBID());
122 if result3:isAfterLast() == false then
123 local wallet = {};
124 wallet["walletID"] = result3:getInt("ID");
125 wallet["playerID"] = result3:getInt("playerID");
126 wallet["amount"] = result3:getInt("walletAmount");
127 player:setAttribute("wallet", wallet);
128 else
129 addPlayerToWallets(player);
130 readWallet(player);
131 end
132end
133
134function addPlayerToWallets(player)
135 local result4 = database:query("SELECT * FROM wallets WHERE playerID = "..player:getDBID());
136 if result4:isAfterLast() then
137 database:queryupdate("INSERT INTO wallets (playerID, walletAmount) VALUES ("..player:getDBID()..", 10)");
138
139 local insertID = database:getLastInsertID();
140 local wallet = player:getAttribute("wallet");
141 wallet["walletID"] = insertID;
142 wallet["playerID"] = player:getDBID();
143 wallet["amount"] = 10;
144 end
145end
146
147function loadShops()
148 -- Receive all saved areas from database and store them in a global table
149 local result5 = database:query("SELECT * FROM shopareas;");
150 while result5:next() do
151 local shop = {};
152 shop["shopID"] = result5:getInt("ID");
153 shop["playerID"] = result5:getInt("playerID");
154 shop["shopType"] = result5:getString("type");
155 shop["startChunkpositionX"] = result5:getInt("startChunkpositionX");
156 shop["startChunkpositionY"] = result5:getInt("startChunkpositionY");
157 shop["startChunkpositionZ"] = result5:getInt("startChunkpositionZ");
158 shop["startBlockpositionX"] = result5:getInt("startBlockpositionX");
159 shop["startBlockpositionY"] = result5:getInt("startBlockpositionY");
160 shop["startBlockpositionZ"] = result5:getInt("startBlockpositionZ");
161
162 shop["endChunkpositionX"] = result5:getInt("endChunkpositionX");
163 shop["endChunkpositionY"] = result5:getInt("endChunkpositionY");
164 shop["endChunkpositionZ"] = result5:getInt("endChunkpositionZ");
165 shop["endBlockpositionX"] = result5:getInt("endBlockpositionX");
166 shop["endBlockpositionY"] = result5:getInt("endBlockpositionY");
167 shop["endBlockpositionZ"] = result5:getInt("endBlockpositionZ");
168
169 shop["rights"] = {};
170
171 calculateGlobalAreaPosition(shop);
172 shops[result5:getInt("ID")] = shop;
173 end
174end
175
176--labelCL
177
178function createLabels(player)
179 if player ~= nil then
180 local label = Gui:createLabel("", 0.05, 0.01);
181 label:setFontsize(17);
182 label:setFontColor(0xFFFFFFFF);
183 label:setBackgroundColor(0x0000007f);
184
185 local topLabel = Gui:createLabel("Top 10 players!", 0.85, 0.140);
186 topLabel:setFontsize(14);
187 topLabel:setFontColor(0xFFFFFFFF);
188 topLabel:setBackgroundColor(0x0000007f);
189
190 player:setAttribute("walletLabel", label);
191 player:setAttribute("topLabel", topLabel);
192 player:addGuiElement(label);
193 player:addGuiElement(topLabel);
194 end
195end
196
197function updateWalletInfo(player)
198 -- Retrieve the label from the players attributes
199 if player:getAttribute("walletInfo") then
200 local wallet = player:getAttribute("wallet");
201 if wallet ~= nil and player:getAttribute("walletLabel") ~= nil then
202 player:getAttribute("walletLabel"):setText("Wallet amount: "..wallet["amount"]);
203 player:getAttribute("walletLabel"):setVisible(true);
204 end
205 updateTop10Players(player);
206 else
207 if player:getAttribute("walletLabel") ~= nil then
208 player:getAttribute("walletLabel"):setVisible(false);
209 end
210 updateTop10Players(player);
211 end
212end
213
214function addAmountToWallet(playerName, amount)
215 local wallet = nil;
216 local player = server:findPlayerByName(playerName);
217 if player ~= nil then
218 wallet = player:getAttribute("wallet");
219 if wallet ~= nil then
220 wallet["amount"] = wallet["amount"] + tonumber(amount);
221 end
222 end
223 local result6 = database:query("select * from wallets where playerID = "..player:getDBID());
224 local walletamount = 0;
225 if wallet ~= nil then
226 walletamount = wallet["amount"];
227 else
228 walletamount = result6:getInt("walletAmount") + tonumber(amount);
229 end
230 if result6:isAfterLast() == false then
231 database:queryupdate("UPDATE wallets SET walletAmount = "..walletamount.." WHERE playerID = "..player:getDBID());
232 end
233 if result6 == nil then
234 walletamount:close()
235 end
236end
237
238function substractAmountFromWallet(playerName, amount);
239 local wallet = nil;
240 local player = server:findPlayerByName(playerName);
241 if player ~= nil then
242 wallet = player:getAttribute("wallet");
243 if wallet ~= nil then
244 wallet["amount"] = wallet["amount"] - tonumber(amount);
245 end
246 end
247 local result7 = database:query("select * from wallets where playerID = "..player:getDBID());
248 local walletamount = 0;
249 if wallet ~= nil then
250 walletamount = wallet["amount"];
251 else
252 walletamount = result7:getInt("walletAmount") - tonumber(amount);
253 end
254 if result7:isAfterLast() == false then
255 database:queryupdate("UPDATE wallets SET walletAmount = "..walletamount.." WHERE playerID = "..player:getDBID());
256 end
257end
258
259function createAccount(player, pincode);
260 local account = player:getAttribute("account");
261 if account == nil then
262 database:queryupdate("insert into accounts (playerID, pincode) values ("..player:getDBID()..", "..pincode..")");
263 readAccount(player);
264 player:sendTextMessage("[#FFFF00]Welcome to 'Rising Citys Savings', your account is created.");
265 else
266 player:sendTextMessage("[#FFFF00]you already have an account with 'Rising Citys Savings'.");
267 end
268end
269
270function getTreasuryReserve()
271 local result8 = database:query("select reserve from treasury");
272 if result8:isAfterLast() == false then
273 return result8:getInt("reserve");
274 end
275end
276
277function addToAccount(player, amount)
278 local account = player:getAttribute("account");
279 if account ~= nil then
280 account["amount"] = account["amount"] + tonumber(amount);
281 database:queryupdate("update accounts set amount = "..account["amount"].." where playerID = "..player:getDBID());
282 end
283end
284
285function depositCredits(player, pincode, amount);
286 local account = player:getAttribute("account");
287 if account ~= nil then
288 if pincode == account["pincode"] then
289 local wallet = player:getAttribute("wallet");
290 if wallet ~= nil then
291 if wallet["amount"] >= (tonumber(amount) + bankValues["transactionFee"]) then
292 substractAmountFromWallet(player:getName(), tonumber(amount) + bankValues["transactionFee"]);
293 addToAccount(player, tonumber(amount));
294 local reserve = getTreasuryReserve();
295 reserve = reserve + bankValues["transactionFee"];
296 database:queryupdate("update treasury set reserve = "..reserve);
297 player:sendTextMessage("[#FFFF00]You now have "..account["amount"].." credits in your account.");
298 else
299 player:sendTextMessage("[#FFFF00]You do not have enough credits in your wallet to make this transaction.");
300 end
301 end
302 else
303 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
304 end
305 else
306 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
307 end
308end
309
310function subAmountFromAccount(player, amount)
311 local account = player:getAttribute("account");
312 if account ~= nil then
313 account["amount"] = account["amount"] - tonumber(amount);
314 database:queryupdate("update accounts set amount = "..account["amount"].." where playerID = "..player:getDBID());
315 end
316end
317
318function withdrawCredits(player, pincode, amount);
319 local account = player:getAttribute("account");
320 if account ~= nil then
321 if pincode == account["pincode"] then
322 local wallet = player:getAttribute("wallet");
323 if wallet ~= nil then
324 if account["amount"] >= (tonumber(amount) + bankValues["transactionFee"]) then
325 addAmountToWallet(player:getName(), amount);
326 subAmountFromAccount(player, amount + bankValues["transactionFee"]);
327 local reserve = getTreasuryReserve();
328 reserve = reserve + bankValues["transactionFee"];
329 database:queryupdate("update treasury set reserve = "..reserve);
330 player:sendTextMessage("[#FFFF00]You now have "..account["amount"].." credits in your account.");
331 else
332 player:sendTextMessage("[#FFFF00]You do not have enough credits in your account to make this transaction.");
333 end
334 end
335 else
336 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
337 end
338 else
339 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
340 end
341end
342
343
344
345
346function paywithCredits(player, pincode, playerName, amount);
347 local account = player:getAttribute("account");
348 if account ~= nil then
349 if pincode == account["pincode"] then
350 local wallet = player:getAttribute("wallet");
351 if wallet ~= nil then
352 if account["amount"] >= (tonumber(amount) + bankValues["transactionFee"]) then
353 addAmountToWallet(playerName, amount);
354 subAmountFromAccount(player, amount + bankValues["transactionFee"]);
355 local reserve = getTreasuryReserve();
356 reserve = reserve + bankValues["transactionFee"];
357 database:queryupdate("update treasury set reserve = "..reserve);
358 player:sendTextMessage("[#FFFF00]You now have "..account["amount"].." credits in your account.");
359 else
360 player:sendTextMessage("[#FFFF00]You do not have enough credits in your account to make this transaction.");
361 end
362 end
363 else
364 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
365 end
366 else
367 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
368 end
369end
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387function changePin(player, pincode, newpincode);
388 local account = player:getAttribute("account");
389 if account ~= nil then
390 if pincode == account["pincode"] then
391 if pincode ~= newpincode then
392 account["pincode"] = newpincode;
393 database:queryupdate("update accounts set pincode = "..newpincode.." where playerID = "..player:getDBID());
394 player:sendTextMessage("[#FFFF00]Your pincode has been changed.");
395 else
396 player:sendTextMessage("[#FFFF00]The new pincode can not be the same as the old pincode.");
397 end
398 else
399 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
400 end
401 else
402 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
403 end
404end
405
406function showAccount(player, pincode)
407 local account = player:getAttribute("account");
408 if account ~= nil then
409 if pincode == account["pincode"] then
410 player:sendTextMessage("[#FFFF00]you have "..account["amount"].." in your account.");
411 else
412 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
413 end
414 else
415 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
416 end
417end
418
419function updateTop10Players(player)
420 if player:getAttribute("top10info") then
421 local result9 = database:query("SELECT playerID, walletAmount FROM wallets ORDER BY walletAmount DESC LIMIT 10");
422 text = "\n Top 10 players \n";
423 local topPlayer = nil;
424
425 local i = 1;
426 while result9:next() do
427 local topPlayer = server:getPlayerInformationFromDB(result9:getInt("playerID"));
428
429
430 if topPlayer ~= nil then
431 text = text.." "..i..": "..topPlayer.name.." ";
432 if i < 10 then
433 text = text.."\n";
434 end
435 end
436 i = i + 1;
437 end
438 text = text.."\n";
439 if player:getAttribute("topLabel") ~= nil then
440 player:getAttribute("topLabel"):setText(text);
441 player:getAttribute("topLabel"):setVisible(true);
442 end
443 else
444 if player:getAttribute("topLabel") ~= nil then
445 player:getAttribute("topLabel"):setVisible(false);
446 end
447 end
448end
449
450--- Calculates the "global" start- and endposition of an area.
451-- @param area The area object
452function calculateGlobalAreaPosition(shop)
453 shop["globalStartPositionX"] = ChunkUtils:getGlobalBlockPositionX(shop["startChunkpositionX"], shop["startBlockpositionX"]);
454 shop["globalStartPositionY"] = ChunkUtils:getGlobalBlockPositionY(shop["startChunkpositionY"], shop["startBlockpositionY"]);
455 shop["globalStartPositionZ"] = ChunkUtils:getGlobalBlockPositionZ(shop["startChunkpositionZ"], shop["startBlockpositionZ"]);
456 shop["globalEndPositionX"] = ChunkUtils:getGlobalBlockPositionX(shop["endChunkpositionX"], shop["endBlockpositionX"]);
457 shop["globalEndPositionY"] = ChunkUtils:getGlobalBlockPositionY(shop["endChunkpositionY"], shop["endBlockpositionY"]);
458 shop["globalEndPositionZ"] = ChunkUtils:getGlobalBlockPositionZ(shop["endChunkpositionZ"], shop["endBlockpositionZ"]);
459end