· 5 years ago · Apr 12, 2020, 05:58 PM
1DB = nil
2eliminarDatos = true
3refreshDataWhowas = timeDeleteInfo ---Tiempo en el que se ejecuta la verificación de datos
4if timeDeleteInfo and timeDeleteInfo == 0 then
5 eliminarDatos = false
6elseif not timeDeleteInfo or timeDeleteInfo <= 100 then
7 timeDeleteInfo = 259200000 --Tiempo minimo para eliminar los datos (3 días)
8end
9
10if refreshDataWhowas and refreshDataWhowas > 86400000 then
11 refreshDataWhowas = 86400000 --Si la verificación se ejecuta mas de cada 24 hrs entonces se le asigna a 24 hrs para verificar
12end
13
14if refreshUserInGame == nil then
15 refreshUserInGame = false
16end
17
18addEventHandler("onResourceStart", resourceRoot, function()
19 SQLSentence = "CREATE TABLE IF NOT EXISTS WHOWAS (USER TEXT, ACCOUNT TEXT, IP TEXT, SERIAL TEXT, LASTCONEXION INTEGER, DATELASTCON TEXT, VERSION TEXT )"
20 if DBType == "mysql" then
21 DB = dbConnect("mysql", "dbname="..(DBname or "") ..";host="..(DBhost or ""), (DBuser or ""), (DBpassword or ""), "autoreconnect=1")
22 SQLSentence = "create table if not exists WHOWAS (ID int not null AUTO_INCREMENT, USER text,ACCOUNT text,IP text,SERIAL text, LASTCONEXION bigint, DATELASTCON text, VERSION text, primary key (ID))"
23 else
24 DB = dbConnect("sqlite", ":/Whowas.DB")
25 end
26 if not DB then
27 outputDebugString('[Whowas] Error al conectar a la base de datos! Apagando recurso...')
28 cancelEvent()
29 return
30 else
31 outputDebugString('[Whowas] Conectado con éxito a la base de datos.')
32 end
33 dbExec(DB,SQLSentence)
34 outputChatBox('[Whowas]Sistema whowas iniciado',root,255,178,0)
35 outputChatBox('[Whowas]Programado por scripters de la comunidad GTAChile.com',root,255,178,0)
36 if refreshUserInGame then
37 for k, pl in ipairs(getElementsByType('player')) do insertWhowas(pl) end
38 end
39end)
40
41function getWhowasUser()
42 insertWhowas(source)
43end
44addEventHandler("onPlayerJoin", getRootElement(), getWhowasUser )
45addEventHandler("onPlayerLogin", root,getWhowasUser)
46addEventHandler("onPlayerChangeNick", getRootElement(), getWhowasUser)
47addEventHandler("onPlayerQuit", root, getWhowasUser )
48
49
50function insertWhowas(jugador)
51 if jugador and isElement(jugador) and getElementType(jugador) == 'player' then
52 local ip = jugador:getIP()
53 local serial = jugador:getSerial()
54 local version = jugador:getVersion()
55 local cuenta = ""
56 if not isGuestAccount(jugador.account) then cuenta = jugador.account.name end
57 local nombre = jugador.name
58 local realTime = getRealTime()
59 local lastConn = realTime.timestamp
60 if realTime.hour <10 then realTime.hour = "0"..realTime.hour end
61 if realTime.minute <10 then realTime.minute = "0"..realTime.minute end
62 if realTime.second <10 then realTime.second = "0"..realTime.second end
63 realTime.month = realTime.month+1
64 if realTime.month <10 then realTime.month = "0"..realTime.month end
65 if realTime.monthday <10 then realTime.monthday = "0"..realTime.monthday end
66
67 local dateLastCon = realTime.monthday.."/"..realTime.month.."/"..(realTime.year+1900).." - "..realTime.hour..":"..realTime.minute..":"..realTime.second
68 local QH = dbQuery(DB, "SELECT * FROM WHOWAS WHERE USER = ? AND ACCOUNT = ? AND IP = ? AND SERIAL = ?",nombre, cuenta,ip, serial)
69 local Resultado = dbPoll(QH, -1)
70 if #Resultado > 0 then
71 dbExec(DB, "UPDATE WHOWAS set LASTCONEXION = ?, DATELASTCON = ? where USER = ? AND ACCOUNT = ? AND IP = ? AND SERIAL = ?", lastConn,dateLastCon, nombre, cuenta,ip, serial)
72 else
73 dbExec(DB, "INSERT INTO WHOWAS (USER, ACCOUNT, IP, SERIAL, LASTCONEXION,DATELASTCON, VERSION) VALUES (?,?,?,?,?,?,?)", nombre, cuenta,ip, serial, lastConn,dateLastCon,version)
74 end
75 end
76end
77
78
79function whowasFindPlayer ( jugador, cmd, findPlayer)
80 local typeElement = getElementType(jugador)
81 if not findPlayer or findPlayer == "" then outputChatBox("/whowas [name]",jugador,250,20,97) return end
82 findPlayer = "%"..string.lower(findPlayer).."%"
83 if jugador and isElement(jugador) and ( typeElement == "console" or typeElement == "player") then
84 local QH = dbQuery(DB, "SELECT * FROM WHOWAS where LOWER(user) like ? or LOWER(account) like ?",findPlayer,findPlayer)
85 local Resultado = dbPoll(QH, -1)
86 if typeElement == "console" then
87 if #Resultado >0 then
88 for k, data in ipairs(Resultado) do
89 if data['USER'] and data['USER'] ~= "" then
90 local mensaje = data['USER']
91 if data['ACCOUNT'] and data['ACCOUNT'] ~= "" then mensaje = mensaje.." ("..data['ACCOUNT']..")" end
92 mensaje = mensaje .. " - "
93 if data['IP'] and data['IP'] ~= "" then mensaje = mensaje.."IP: "..data['IP'] end
94 if data['SERIAL'] and data['SERIAL'] ~= "" then mensaje = mensaje.." serial: "..data['SERIAL'] end
95 if data['VERSION'] and data['VERSION'] ~= "" then mensaje = mensaje.." version:"..data['VERSION'] end
96 outputServerLog(mensaje)
97 if data['DATELASTCON'] and data['DATELASTCON'] ~= "" then
98 outputServerLog("Ultima conexion: "..data['DATELASTCON'])
99 end
100 end
101 end
102 else
103 outputServerLog('[Whowas]No se encontraron resultados!')
104 end
105 elseif typeElement == "player" then
106 if #Resultado >0 then
107 for k, data in ipairs(Resultado) do
108 if data['USER'] and data['USER'] ~= "" then
109 local mensaje = data['USER']
110 if data['ACCOUNT'] and data['ACCOUNT'] ~= "" then mensaje = mensaje.." ("..data['ACCOUNT']..")" end
111 mensaje = mensaje .. " - "
112 if data['IP'] and data['IP'] ~= "" then mensaje = mensaje.."IP: "..data['IP'] end
113 if data['SERIAL'] and data['SERIAL'] ~= "" then mensaje = mensaje.." serial: "..data['SERIAL'] end
114 if data['VERSION'] and data['VERSION'] ~= "" then mensaje = mensaje.." version:"..data['VERSION'] end
115 outputChatBox(mensaje,jugador,255,178,0)
116 if data['DATELASTCON'] and data['DATELASTCON'] ~= "" then
117 outputChatBox("Ultima conexion: "..data['DATELASTCON'],jugador,255,178,0)
118 end
119 end
120 end
121 else
122 outputChatBox('[Whowas]No se encontraron resultados!',jugador,255,0,0)
123 end
124 end
125 end
126end
127addCommandHandler ("whowas", whowasFindPlayer ,true)
128
129
130function deleteWhowasForTimeOut(Player,Editor)
131 local timeNow = getRealTime().timestamp
132 outputServerLog('[Whowas]Buscando y eliminando datos no deseados... ')
133 dbExec(DB, "DELETE FROM whowas WHERE ("..timeNow.." - whowas.LASTCONEXION) > "..(timeDeleteInfo/1000))
134
135end
136
137if eliminarDatos then
138 outputServerLog('[Whowas]Eliminación de datos no deseados activada')
139 setTimer(deleteWhowasForTimeOut,refreshDataWhowas,0)
140end