· 10 years ago · Sep 21, 2016, 01:22 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
104 result1:next() do end result1:close()
105end
106
107function readAccount(player)
108 local result2 = database:query("SELECT * FROM accounts where playerID = "..player:getDBID());
109 if result2:isAfterLast() == false then
110 local account = {};
111 account["accountID"] = result2:getInt("ID");
112 account["playerID"] = result2:getInt("playerID");
113 account["pincode"] = result2:getString("pincode");
114 account["amount"] = result2:getInt("amount");
115 player:setAttribute("account", account);
116 else
117 player:setAttribute("account", nil);
118 end
119 result2:next() do end result2:close()
120end
121
122function readWallet(player)
123 local result3 = database:query("SELECT * FROM wallets where playerID = "..player:getDBID());
124 if result3:isAfterLast() == false then
125 local wallet = {};
126 wallet["walletID"] = result3:getInt("ID");
127 wallet["playerID"] = result3:getInt("playerID");
128 wallet["amount"] = result3:getInt("walletAmount");
129 player:setAttribute("wallet", wallet);
130 else
131 addPlayerToWallets(player);
132 readWallet(player);
133 end
134 result3:next() do end result3:close()
135end
136
137function addPlayerToWallets(player)
138 local result4 = database:query("SELECT * FROM wallets WHERE playerID = "..player:getDBID());
139 if result4:isAfterLast() then
140 database:queryupdate("INSERT INTO wallets (playerID, walletAmount) VALUES ("..player:getDBID()..", 10)");
141
142 local insertID = database:getLastInsertID();
143 local wallet = player:getAttribute("wallet");
144 wallet["walletID"] = insertID;
145 wallet["playerID"] = player:getDBID();
146 wallet["amount"] = 10;
147 end
148 result4:next() do end result4:close()
149end
150
151function loadShops()
152 -- Receive all saved areas from database and store them in a global table
153 local result5 = database:query("SELECT * FROM shopareas;");
154 while result5:next() do
155 local shop = {};
156 shop["shopID"] = result5:getInt("ID");
157 shop["playerID"] = result5:getInt("playerID");
158 shop["shopType"] = result5:getString("type");
159 shop["startChunkpositionX"] = result5:getInt("startChunkpositionX");
160 shop["startChunkpositionY"] = result5:getInt("startChunkpositionY");
161 shop["startChunkpositionZ"] = result5:getInt("startChunkpositionZ");
162 shop["startBlockpositionX"] = result5:getInt("startBlockpositionX");
163 shop["startBlockpositionY"] = result5:getInt("startBlockpositionY");
164 shop["startBlockpositionZ"] = result5:getInt("startBlockpositionZ");
165
166 shop["endChunkpositionX"] = result5:getInt("endChunkpositionX");
167 shop["endChunkpositionY"] = result5:getInt("endChunkpositionY");
168 shop["endChunkpositionZ"] = result5:getInt("endChunkpositionZ");
169 shop["endBlockpositionX"] = result5:getInt("endBlockpositionX");
170 shop["endBlockpositionY"] = result5:getInt("endBlockpositionY");
171 shop["endBlockpositionZ"] = result5:getInt("endBlockpositionZ");
172
173 shop["rights"] = {};
174
175 calculateGlobalAreaPosition(shop);
176 shops[result5:getInt("ID")] = shop;
177 end
178 result5:next() do end result5:close()
179end
180
181--labelCL
182
183function createLabels(player)
184 if player ~= nil then
185 local label = Gui:createLabel("", 0.05, 0.01);
186 label:setFontsize(17);
187 label:setFontColor(0xFFFFFFFF);
188 label:setBackgroundColor(0x0000007f);
189
190 local topLabel = Gui:createLabel("Top 10 players!", 0.85, 0.140);
191 topLabel:setFontsize(14);
192 topLabel:setFontColor(0xFFFFFFFF);
193 topLabel:setBackgroundColor(0x0000007f);
194
195 player:setAttribute("walletLabel", label);
196 player:setAttribute("topLabel", topLabel);
197 player:addGuiElement(label);
198 player:addGuiElement(topLabel);
199 end
200end
201
202function updateWalletInfo(player)
203 -- Retrieve the label from the players attributes
204 if player:getAttribute("walletInfo") then
205 local wallet = player:getAttribute("wallet");
206 if wallet ~= nil and player:getAttribute("walletLabel") ~= nil then
207 player:getAttribute("walletLabel"):setText("Wallet amount: "..wallet["amount"]);
208 player:getAttribute("walletLabel"):setVisible(true);
209 end
210 updateTop10Players(player);
211 else
212 if player:getAttribute("walletLabel") ~= nil then
213 player:getAttribute("walletLabel"):setVisible(false);
214 end
215 updateTop10Players(player);
216 end
217end
218
219function addAmountToWallet(playerName, amount)
220 local wallet = nil;
221 local player = server:findPlayerByName(playerName);
222 if player ~= nil then
223 wallet = player:getAttribute("wallet");
224 if wallet ~= nil then
225 wallet["amount"] = wallet["amount"] + tonumber(amount);
226 end
227 end
228 local result6 = database:query("select * from wallets where playerID = "..player:getDBID());
229 local walletamount = 0;
230 if wallet ~= nil then
231 walletamount = wallet["amount"];
232 else
233 walletamount = result6:getInt("walletAmount") + tonumber(amount);
234 end
235 if result6:isAfterLast() == false then
236 database:queryupdate("UPDATE wallets SET walletAmount = "..walletamount.." WHERE playerID = "..player:getDBID());
237 end
238 result6:next() do end result6:close()
239end
240
241function substractAmountFromWallet(playerName, amount);
242 local wallet = nil;
243 local player = server:findPlayerByName(playerName);
244 if player ~= nil then
245 wallet = player:getAttribute("wallet");
246 if wallet ~= nil then
247 wallet["amount"] = wallet["amount"] - tonumber(amount);
248 end
249 end
250 local result7 = database:query("select * from wallets where playerID = "..player:getDBID());
251 local walletamount = 0;
252 if wallet ~= nil then
253 walletamount = wallet["amount"];
254 else
255 walletamount = result7:getInt("walletAmount") - tonumber(amount);
256 end
257 if result7:isAfterLast() == false then
258 database:queryupdate("UPDATE wallets SET walletAmount = "..walletamount.." WHERE playerID = "..player:getDBID());
259 end
260 result7:next() do end result7:close()
261end
262
263function createAccount(player, pincode);
264 local account = player:getAttribute("account");
265 if account == nil then
266 database:queryupdate("insert into accounts (playerID, pincode) values ("..player:getDBID()..", "..pincode..")");
267 readAccount(player);
268 player:sendTextMessage("[#FFFF00]Welcome to 'Rising Citys Savings', your account is created.");
269 else
270 player:sendTextMessage("[#FFFF00]you already have an account with 'Rising Citys Savings'.");
271 end
272end
273
274function getTreasuryReserve()
275 local result8 = database:query("select reserve from treasury");
276 if result8:isAfterLast() == false then
277 return result8:getInt("reserve");
278 end
279 result8:next() do end result8:close()
280end
281
282function addToAccount(player, amount)
283 local account = player:getAttribute("account");
284 if account ~= nil then
285 account["amount"] = account["amount"] + tonumber(amount);
286 database:queryupdate("update accounts set amount = "..account["amount"].." where playerID = "..player:getDBID());
287 end
288end
289
290function depositCredits(player, pincode, amount);
291 local account = player:getAttribute("account");
292 if account ~= nil then
293 if pincode == account["pincode"] then
294 local wallet = player:getAttribute("wallet");
295 if wallet ~= nil then
296 if wallet["amount"] >= (tonumber(amount) + bankValues["transactionFee"]) then
297 substractAmountFromWallet(player:getName(), tonumber(amount) + bankValues["transactionFee"]);
298 addToAccount(player, tonumber(amount));
299 local reserve = getTreasuryReserve();
300 reserve = reserve + bankValues["transactionFee"];
301 database:queryupdate("update treasury set reserve = "..reserve);
302 player:sendTextMessage("[#FFFF00]You now have "..account["amount"].." credits in your account.");
303 else
304 player:sendTextMessage("[#FFFF00]You do not have enough credits in your wallet to make this transaction.");
305 end
306 end
307 else
308 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
309 end
310 else
311 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
312 end
313end
314
315function subAmountFromAccount(player, amount)
316 local account = player:getAttribute("account");
317 if account ~= nil then
318 account["amount"] = account["amount"] - tonumber(amount);
319 database:queryupdate("update accounts set amount = "..account["amount"].." where playerID = "..player:getDBID());
320 end
321end
322
323function withdrawCredits(player, pincode, amount);
324 local account = player:getAttribute("account");
325 if account ~= nil then
326 if pincode == account["pincode"] then
327 local wallet = player:getAttribute("wallet");
328 if wallet ~= nil then
329 if account["amount"] >= (tonumber(amount) + bankValues["transactionFee"]) then
330 addAmountToWallet(player:getName(), amount);
331 subAmountFromAccount(player, amount + bankValues["transactionFee"]);
332 local reserve = getTreasuryReserve();
333 reserve = reserve + bankValues["transactionFee"];
334 database:queryupdate("update treasury set reserve = "..reserve);
335 player:sendTextMessage("[#FFFF00]You now have "..account["amount"].." credits in your account.");
336 else
337 player:sendTextMessage("[#FFFF00]You do not have enough credits in your account to make this transaction.");
338 end
339 end
340 else
341 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
342 end
343 else
344 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
345 end
346end
347
348
349
350
351function paywithCredits(player, pincode, playerName, amount);
352 local account = player:getAttribute("account");
353 if account ~= nil then
354 if pincode == account["pincode"] then
355 local wallet = player:getAttribute("wallet");
356 if wallet ~= nil then
357 if account["amount"] >= (tonumber(amount) + bankValues["transactionFee"]) then
358 addAmountToWallet(playerName, amount);
359 subAmountFromAccount(player, amount + bankValues["transactionFee"]);
360 local reserve = getTreasuryReserve();
361 reserve = reserve + bankValues["transactionFee"];
362 database:queryupdate("update treasury set reserve = "..reserve);
363 player:sendTextMessage("[#FFFF00]You now have "..account["amount"].." credits in your account.");
364 else
365 player:sendTextMessage("[#FFFF00]You do not have enough credits in your account to make this transaction.");
366 end
367 end
368 else
369 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
370 end
371 else
372 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
373 end
374end
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392function changePin(player, pincode, newpincode);
393 local account = player:getAttribute("account");
394 if account ~= nil then
395 if pincode == account["pincode"] then
396 if pincode ~= newpincode then
397 account["pincode"] = newpincode;
398 database:queryupdate("update accounts set pincode = "..newpincode.." where playerID = "..player:getDBID());
399 player:sendTextMessage("[#FFFF00]Your pincode has been changed.");
400 else
401 player:sendTextMessage("[#FFFF00]The new pincode can not be the same as the old pincode.");
402 end
403 else
404 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
405 end
406 else
407 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
408 end
409end
410
411function showAccount(player, pincode)
412 local account = player:getAttribute("account");
413 if account ~= nil then
414 if pincode == account["pincode"] then
415 player:sendTextMessage("[#FFFF00]you have "..account["amount"].." in your account.");
416 else
417 player:sendTextMessage("[#FFFF00]You did not provide the right pincode.");
418 end
419 else
420 player:sendTextMessage("[#FFFF00]You do not have an account with 'Rising Citys Savings'.");
421 end
422end
423
424function updateTop10Players(player)
425 if player:getAttribute("top10info") then
426 local result9 = database:query("SELECT playerID, walletAmount FROM wallets ORDER BY walletAmount DESC LIMIT 10");
427 text = "\n Top 10 players \n";
428 local topPlayer = nil;
429
430 local i = 1;
431 while result9:next() do
432 local topPlayer = server:getPlayerInformationFromDB(result9:getInt("playerID"));
433 if topPlayer ~= nil then
434 text = text.." "..i..": "..topPlayer.name.." ";
435 if i < 10 then
436 text = text.."\n";
437 end
438 end
439 i = i + 1;
440 end
441 result9:next() do end result9:close()
442 text = text.."\n";
443 if player:getAttribute("topLabel") ~= nil then
444 player:getAttribute("topLabel"):setText(text);
445 player:getAttribute("topLabel"):setVisible(true);
446 end
447 else
448 if player:getAttribute("topLabel") ~= nil then
449 player:getAttribute("topLabel"):setVisible(false);
450 end
451 end
452end
453
454--- Calculates the "global" start- and endposition of an area.
455-- @param area The area object
456function calculateGlobalAreaPosition(shop)
457 shop["globalStartPositionX"] = ChunkUtils:getGlobalBlockPositionX(shop["startChunkpositionX"], shop["startBlockpositionX"]);
458 shop["globalStartPositionY"] = ChunkUtils:getGlobalBlockPositionY(shop["startChunkpositionY"], shop["startBlockpositionY"]);
459 shop["globalStartPositionZ"] = ChunkUtils:getGlobalBlockPositionZ(shop["startChunkpositionZ"], shop["startBlockpositionZ"]);
460 shop["globalEndPositionX"] = ChunkUtils:getGlobalBlockPositionX(shop["endChunkpositionX"], shop["endBlockpositionX"]);
461 shop["globalEndPositionY"] = ChunkUtils:getGlobalBlockPositionY(shop["endChunkpositionY"], shop["endBlockpositionY"]);
462 shop["globalEndPositionZ"] = ChunkUtils:getGlobalBlockPositionZ(shop["endChunkpositionZ"], shop["endBlockpositionZ"]);
463end