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