· 10 years ago · Feb 26, 2016, 12:24 PM
1----------------------------------------->>
2-- Grand Theft International (GTi)
3-- Author: JT Pennington (JTPenn)
4-- Date: 29 Aug 2014
5-- Resource: GTIbank/database.slua
6-- Version: 1.0
7----------------------------------------->>
8
9local ipairs = ipairs
10local type = type
11local tonumber = tonumber
12
13local SERVER_PORT = 22003 -- Only Save Data on Dev Server
14
15db_name = "gti_9"
16host = "85.10.205.173"
17user = "gti_9"
18password = "123456789gti"
19
20local db = dbConnect( "mysql", "dbname="..db_name..";host="..host, user, password )
21if db then
22 outputChatBox("Database connected !", root, 0, 255, 0)
23else
24 outputChatBox("Database failed !", root, 255, 0, 0)
25end
26
27dbExec(db, "CREATE TABLE IF NOT EXISTS `banking`(`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id))")
28
29local database_online -- Is banking Database Online?
30local banking = {} -- 'banking' Database Cache
31
32addEvent("onDatabaseLoad", true) -- Triggers when gti database is ready
33addEvent("onLogDatabaseLoad", true) -- Triggers when log database is ready
34
35-- Database Cache
36------------------>>
37
38addEventHandler("onResourceStart", resourceRoot, function()
39 dbQuery(cacheDatabase, db, "SELECT * FROM `banking`")
40end)
41
42function cacheDatabase(qh)
43 local result = dbPoll(qh, 0)
44 banking["Console"] = {}
45 for i,row in ipairs(result) do
46 banking[row.name] = {}
47 for column,value in pairs(row) do
48 if (column ~= "id" or column ~= "name") then
49 banking["Console"][column] = true
50 if (value == "true") then value = true end
51 if (value == "false") then value = false end
52 banking[row.name][column] = value
53 end
54 end
55 end
56 database_online = true
57 triggerEvent("onDatabaseLoad", resourceRoot, "banking")
58end
59
60-- Banking Data Exports
61------------------------>>
62
63function setBankData(account, key, value)
64 if (not database_online) then return false end
65 if (not account or not key) then return false end
66 if (isGuestAccount(account) or type(key) ~= "string") then return false end
67 local account = getAccountName(account)
68
69 if (type(banking[account]) ~= "table") then
70 banking[account] = {}
71 if (getServerPort() == SERVER_PORT) then
72 dbExec(db, "INSERT INTO `banking`(name) VALUES(?)", account)
73 end
74 end
75
76 if (banking["Console"][key] == nil) then
77 if (getServerPort() == SERVER_PORT) then
78 dbExec(db, "ALTER TABLE `banking` ADD `??` text", key)
79 end
80 banking["Console"][key] = true
81 end
82
83 banking[account][key] = value
84 if (getServerPort() == SERVER_PORT) then
85 if (value ~= nil) then
86 dbExec(db, "UPDATE `banking` SET `??`=? WHERE name=?", key, tostring(value), account)
87 else
88 dbExec(db, "UPDATE `banking` SET `??`=NULL WHERE name=?", key, account)
89 end
90 end
91 return true
92end
93
94function getBankData(account, key)
95 if (not database_online) then return end
96 if (not account or not key) then return end
97 if (isGuestAccount(account) or type(key) ~= "string") then return end
98
99 local account = getAccountName(account)
100 if (banking[account] == nil) then return end
101 if (banking[account][key] == nil or banking[account][key] == "nil") then return end
102
103 if (key == "pin" or key == "acc_number") then
104 return banking[account][key]
105 end
106 return tonumber(banking[account][key]) or banking[account][key]
107end
108
109addEvent("onAccountDelete")
110addEventHandler("onAccountDelete", root, function(account)
111 dbExec(db, "DELETE FROM `banking` WHERE name=?", account)
112 banking[account] = nil
113end)
114
115-- Transaction Log
116------------------->>
117
118local dbLogs = dbConnect("sqlite", "group.db")
119
120function addLogToDatabase(category, timestamp, text, cash, balance, player, account)
121 dbExec(dbLogs, "CREATE TABLE IF NOT EXISTS `log_"..category.."`(id INT NOT NULL AUTO_INCREMENT, timestamp INT, text TEXT, cash INT, balance INT, player TEXT, account TEXT, ip TEXT, serial TEXT, PRIMARY KEY(id))")
122
123 local serial, ip
124 if (isElement(player)) then
125 serial = getPlayerSerial(player)
126 ip = getPlayerIP(player)
127 player = getPlayerName(player)
128 end
129 dbExec(dbLogs, "INSERT INTO `log_"..category.."`(timestamp, text, cash, balance, player, account, ip, serial) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", timestamp, text, cash, balance, player, account, ip, serial)
130 return true
131end
132
133function getAccountLogs(player, account, data_table, recovery)
134 if (not player or not account or not data_table) then return false end
135 if (data_table ~= "bank" and data_table ~= "cash" and data_table ~= "groupbank") then return false end
136 if ( recovery ) then
137 dbQuery(recoveryCallback, {player, data_table}, dbLogs, "SELECT * FROM `log_"..data_table.."` WHERE `account`=? ORDER BY `timestamp` DESC LIMIT 500", account)
138 else
139 dbQuery(returnDatabase, {player, data_table}, dbLogs, "SELECT * FROM `log_"..data_table.."` WHERE `account`=? ORDER BY `timestamp` DESC LIMIT 250", account)
140 end
141end
142
143function returnDatabase(qh, player, data_table)
144 local result = dbPoll(qh, 0)
145 triggerEvent("onLogDatabaseLoad", player, data_table, result)
146end
147
148addEvent("onAccountDelete")
149addEventHandler("onAccountDelete", root, function(account)
150 dbExec(dbLogs, "DELETE FROM `log_bank` WHERE account=?", account)
151 dbExec(dbLogs, "DELETE FROM `log_cash` WHERE account=?", account)
152end)
153
154function getDatabaseLogs()
155 return dbLogs
156end