Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added whowas command #161

Closed
wants to merge 18 commits into from
1 change: 1 addition & 0 deletions [admin]/admin2/conf/commands.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<command handler="blendweather" call="blendweather" args="i" />
<command handler="setgamespeed" call="setgamespeed" args="i" />
<command handler="setgravity" call="setgravity" args="i" />
<command handler="whowas" call="whowas" args="s" />
</server>
<bans>
<command handler="banip" call="banip" args="s" />
Expand Down
111 changes: 111 additions & 0 deletions [admin]/admin2/server/admin_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,117 @@ aFunctions = {
end,
["shutdown"] = function(reason)
shutdown(iif(reason, tostring(reason), nil))
end,
["whowas"] = function(nick)
if not nick then
outputChatBox("A nickname or serial is needed.", source, 255, 0, 0)
return false
end
local printF = get("whowasprint")
local serial = false
local qh;

if printF == "chat" then
printF = outputChatBox
else
printF = outputConsole
end

if nick:len() == 32 then
PlatinMTA marked this conversation as resolved.
Show resolved Hide resolved
serial = true
elseif nick:len() > 22 then
outputChatBox("That nickname or serial is too long/short.", source, 255, 0, 0)
return false
end

if #aWhowas > 0 then
local selected = {}
for k,data in ipairs(aWhowas) do
if (serial and data[2] == nick) or (data[1] == nick) then
table.insert(selected, data)
end
end

if #selected > 0 then
if printF == outputConsole then
outputChatBox("Info printed on console (F8).", source, 255, 141, 0)
end
if serial then
printF("Last serial: "..selected[#selected][2], source, 255, 141, 0)
else
printF("Last nickname: "..selected[#selected][1], source, 255, 141, 0)
end

local realTime = getRealTime().timestamp
for item,data in ipairs(selected) do
local sec = realTime-data[4]
local last = "Unknown"
if sec > 86400 then
last = math.floor(sec/86400).." day(s)"
elseif sec > 3600 then
last = math.floor(sec/3600).." hour(s)"
elseif sec > 60 then
last = math.floor(sec/60).." minute(s)"
else
last = math.floor(sec).." second(s)"
end
if serial then
printF("Last nickname #"..item..": "..data[1], source, 255, 141, 0)
else
printF("Last serial #"..item..": "..data[2], source, 255, 141, 0)
end

printF("Last ip #"..item..": "..data[3], source, 255, 141, 0)
printF("Last connection #"..item..": "..last, source, 255, 141, 0)
end
return true
end
end

if serial then
qh = dbQuery(db.connection, "SELECT * FROM whowas WHERE serial=?;", nick)
else
qh = dbQuery(db.connection, "SELECT * FROM whowas WHERE name=?;", nick)
end

local whowas = dbPoll(qh, 200)
if #whowas > 0 then
if printF == outputConsole then
outputChatBox("Info printed on console (F8).", source, 255, 141, 0)
end
if serial then
printF("Last serial: "..whowas[#whowas].serial, source, 255, 141, 0)
else
printF("Last nickname: "..whowas[#whowas].name, source, 255, 141, 0)
end

local realTime = getRealTime().timestamp
for item,row in ipairs(whowas) do
local sec = realTime-row.time
local last = "Unknown"
if sec > 86400 then
last = math.floor(sec/86400).." day(s)"
elseif sec > 3600 then
last = math.floor(sec/3600).." hour(s)"
elseif sec > 60 then
last = math.floor(sec/60).." minute(s)"
else
last = math.floor(sec).." second(s)"
end
if serial then
printF("Last nickname #"..item..": "..row.name, source, 255, 141, 0)
else
printF("Last serial #"..item..": "..row.serial, source, 255, 141, 0)
end

printF("Last ip #"..item..": "..row.ip, source, 255, 141, 0)
printF("Last connection #"..item..": "..last, source, 255, 141, 0)
end
return true
else
outputChatBox("We couldn't collect any info about \""..nick.."\".", source, 255, 141, 0)
return false
end
end
},
admin = {},
Expand Down
70 changes: 69 additions & 1 deletion [admin]/admin2/server/admin_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,67 @@ aInteriors = {}
aStats = {}
aReports = {}
aWeathers = {}
aWhowas = {}

function saveToDatabaseWhowas(nick, serial, ip, timestamp)
if not nick or not serial or not ip then
return
end
if not timestamp then
timestamp = getRealTime().timestamp
end
db.exec("DELETE FROM whowas WHERE name=? AND serial=?;", nick, serial)
db.exec("INSERT INTO whowas(name,serial,ip,time) VALUES(?,?,?,?);", nick, serial, ip, timestamp)
end

function clearRowsWhowas()
for k,data in ipairs(aWhowas) do
saveToDatabaseWhowas(data[1], data[2], data[3], data[4])
end
aWhowas = {}
end

function insertIntoWhowas(nick, serial, ip, timestamp)
if not nick or not serial or not ip then
return
end
if not timestamp then
timestamp = getRealTime().timestamp
end
table.insert(aWhowas, {nick, serial, ip, timestamp})
end

-- Ensures that the player's information is up to date, it also ensures the integrity of the database
function persistPlayerWhowasInfo(player)
if not player or not isElement(player) then
return
end
if player == root then
for _,plr in ipairs(getElementsByType("player")) do
persistPlayerWhowasInfo(plr)
end
return
end

local nick = getPlayerName(player)
PlatinMTA marked this conversation as resolved.
Show resolved Hide resolved
local serial = getPlayerSerial(player)
local ip = getPlayerIP(player)
local timestamp = getRealTime().timestamp
if get("whowassave") == "both" then
local rows = tonumber(get("whowasmax")) or 100
if rows > 500 then
rows = 500
elseif rows < 5 then
rows = 5
end
if #aWhowas > rows then
clearRowsWhowas()
end
insertIntoWhowas(nick, serial, ip, timestamp)
else
saveToDatabaseWhowas(nick, serial, ip, timestamp)
end
end

addEventHandler(
"onResourceStart",
Expand All @@ -27,6 +88,8 @@ addEventHandler(
end
end
return
else
persistPlayerWhowasInfo(root)
end

aSetupACL()
Expand All @@ -51,6 +114,7 @@ addEventHandler(
end
else
aReleaseStorage()
persistPlayerWhowasInfo(root)
end
aclSave()
end
Expand All @@ -76,6 +140,8 @@ addEventHandler(
end
end
setPedGravity(source, getGravity())

persistPlayerWhowasInfo(source)
end
)

Expand All @@ -84,6 +150,8 @@ addEventHandler(
root,
function()
aPlayers[source] = nil

persistPlayerWhowasInfo(source)
end
)

Expand Down Expand Up @@ -246,7 +314,7 @@ addEventHandler(
"aServer",
root,
function(action, ...)
if (hasObjectPermissionTo(source, "command." .. action)) then
if (hasObjectPermissionTo(source, "command."..action)) then
local func = aFunctions.server[action]
if (func) then
local result, mdata1, mdata2 = func(...)
Expand Down
Loading