· 5 years ago · Apr 12, 2020, 01:36 AM
1DBType = "mysql" -- Puede ser mysql o sqlite
2DBhost = ""
3DBuser = "";
4DBpassword = "";
5DBname = "";
6DB = nil
7
8addEventHandler("onResourceStart", resourceRoot, function()
9 SQLSentence = "CREATE TABLE IF NOT EXISTS Reproductor (USER TEXT, ACCOUNT TEXT, IP TEXT, SERIAL TEXT, LASTCONEXION INTEGER, DATELASTCON TEXT, VERSION TEXT )"
10 if DBType == "mysql" then
11 DB = dbConnect("mysql", "dbname="..DBname..";host="..DBhost, DBuser, DBpassword, "autoreconnect=1")
12 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))"
13 else
14 DB = dbConnect("sqlite", ":/Whowas.DB")
15 end
16 if not DB then
17 outputDebugString('[Whowas] Error al conectar a la base de datos! Apagando recurso...')
18 cancelEvent()
19 return
20 else
21 outputDebugString('[Whowas] Conectado con éxito a la base de datos.')
22 end
23 dbExec(DB,SQLSentence)
24end)
25
26function getWhowasUser()
27 insertWhowas(source)
28end
29addEventHandler("onPlayerJoin", getRootElement(), getWhowasUser )
30addEventHandler("onPlayerLogin", root,getWhowasUser)
31addEventHandler("onPlayerChangeNick", getRootElement(), getWhowasUser)
32addEventHandler("onPlayerQuit", root, getWhowasUser )
33
34
35function insertWhowas(jugador)
36 if jugador and isElement(jugador) and getElementType(jugador) == 'player' then
37 local ip = jugador:getIP()
38 local serial = jugador:getSerial()
39 local version = jugador:getVersion()
40 local cuenta = ""
41 if not isGuestAccount(jugador.account) then cuenta = jugador.account.name end
42 local nombre = jugador.name
43 local realTime = getRealTime()
44 local lastConn = realTime.timestamp
45 local dateLastCon = realTime.monthday.."/"..(realTime.month+1).."/"..realTime.year.." - "..realTime.hour..":"..realTime.minute..":"..realTime.second
46 local QH = dbQuery(DB, "SELECT * FROM WHOWAS WHERE USER = ? AND ACCOUNT = ? AND IP = ? AND SERIAL = ?",nombre, cuenta,ip, serial)
47 local Resultado = dbPoll(QH, -1)
48 if #Resultado > 0 then
49 dbExec(DB, "UPDATE WHOWAS set LASTCONEXION = ?, DATELASTCON = ? where USER = ? AND ACCOUNT = ? AND IP = ? AND SERIAL = ?", lastConn,dateLastCon, nombre, cuenta,ip, serial)
50 else
51 dbExec(DB, "INSERT INTO WHOWAS (USER, ACCOUNT, IP, SERIAL, LASTCONEXION,DATELASTCON, VERSION) VALUES (?,?,?,?,?,?,?)", nombre, cuenta,ip, serial, lastConn,dateLastCon,version)
52 end
53 end
54end
55
56
57function whowasFindPlayer ( jugador, cmd, findPlayer)
58 local typeElement = getElementType(jugador)
59 findPlayer = "%"..string.lower(findPlayer).."%"
60 if jugador and isElement(jugador) and ( typeElement == "console" or typeElement == "player") then
61 local QH = dbQuery(DB, "SELECT * FROM WHOWAS where LOWER(user) like ? or LOWER(account) like ?",findPlayer,findPlayer)
62 local Resultado = dbPoll(QH, -1)
63 if typeElement == "console" then
64 if #Resultado >0 then
65 for k, data in ipairs(Resultado) do
66 if data['USER'] and data['USER'] ~= "" then
67 local mensaje = data['USER']
68 if data['ACCOUNT'] and data['ACCOUNT'] ~= "" then mensaje = mensaje.." ("..data['ACCOUNT']..")" end
69 mensaje = mensaje .. " - "
70 if data['IP'] and data['IP'] ~= "" then mensaje = mensaje.."IP: "..data['IP'] end
71 if data['SERIAL'] and data['SERIAL'] ~= "" then mensaje = mensaje.." serial: "..data['SERIAL'] end
72 if data['VERSION'] and data['VERSION'] ~= "" then mensaje = mensaje.." version:"..data['VERSION'] end
73 outputServerLog(mensaje)
74 if data['DATELASTCON'] and data['DATELASTCON'] ~= "" then
75 outputServerLog("Ultima conexion: "..data['DATELASTCON'])
76 end
77 end
78 end
79 else
80 outputServerLog('[Whowas]No se encontraron resultados!')
81 end
82 elseif typeElement == "player" then
83 if #Resultado >0 then
84 for k, data in ipairs(Resultado) do
85 if data['USER'] and data['USER'] ~= "" then
86 local mensaje = data['USER']
87 if data['ACCOUNT'] and data['ACCOUNT'] ~= "" then mensaje = mensaje.." ("..data['ACCOUNT']..")" end
88 mensaje = mensaje .. " - "
89 if data['IP'] and data['IP'] ~= "" then mensaje = mensaje.."IP: "..data['IP'] end
90 if data['SERIAL'] and data['SERIAL'] ~= "" then mensaje = mensaje.." serial: "..data['SERIAL'] end
91 if data['VERSION'] and data['VERSION'] ~= "" then mensaje = mensaje.." version:"..data['VERSION'] end
92 outputChatBox(mensaje,jugador,255,178,0)
93 if data['DATELASTCON'] and data['DATELASTCON'] ~= "" then
94 outputChatBox("Ultima conexion: "..data['DATELASTCON'],jugador,255,178,0)
95 end
96 end
97 end
98 else
99 outputChatBox('[Whowas]No se encontraron resultados!',jugador,255,0,0)
100 end
101 end
102 end
103end
104addCommandHandler ("whowas", whowasFindPlayer ,true)
105
106
107function deleteWhowasForTimeOut(Player,Editor)
108 local timeNow = getRealTime().timestamp
109 dbExec(DB, "DELETE FROM whowas WHERE ? > (whowas.LASTCONEXION + 259200000)", timeNow)
110
111end
112setTimer(deleteWhowasForTimeOut,259200000,0) ---Se elimina los datos de usuarios cada 3 días