lua: (xpsave) clean up

This commit is contained in:
Radegast 2013-11-17 22:45:25 +00:00
parent 5862386ca4
commit ea55f531d3
2 changed files with 33 additions and 34 deletions

View file

@ -15,7 +15,7 @@ Lua scripts for the Legacy mod
## xpsave
* In development and not intended to be used for public servers yet *
*In development and not intended to be used for public servers yet*
* This script is intended for Legacy mod, but may work in NoQuarter too
* luasql module with sqlite3 driver is required, but other database drivers may be used instead with a small change

View file

@ -48,44 +48,46 @@ function et_InitGame(levelTime, randomSeed, restart)
--cur = assert (con:execute( "DROP TABLE users" ))
cur = assert ( con:execute[[
CREATE TABLE IF NOT EXISTS users(
name VARCHAR(64),
guid VARCHAR(64),
last_seen VARCHAR(64),
xp_battlesense REAL,
xp_engineering REAL,
xp_medic REAL,
xp_fieldops REAL,
xp_lightweapons REAL,
xp_heavyweapons REAL,
xp_covertops REAL,
UNIQUE (guid)
)
CREATE TABLE IF NOT EXISTS users(
guid VARCHAR(64),
last_seen VARCHAR(64),
xp_battlesense REAL,
xp_engineering REAL,
xp_medic REAL,
xp_fieldops REAL,
xp_lightweapons REAL,
xp_heavyweapons REAL,
xp_covertops REAL,
UNIQUE (guid)
)
]])
end -- et_InitGame
function et_ShutdownGame(restart) -- clean up cur:close() con:close() env:close() end -- et_ShutdownGame
function et_ShutdownGame(restart)
-- clean up
cur:close()
con:close()
env:close()
end -- et_ShutdownGame
-- called when a client enters the game world
function et_ClientBegin(cno)
name = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "name" )
guid = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "cl_guid" )
--et.G_Print(name .. " with guid " .. guid .. " just connected\n")
cur = assert (con:execute(string.format("SELECT * FROM users WHERE guid='%s'", guid)))
player = cur:fetch({}, 'a')
if not player then
-- First time we see this player
et.G_Print ("Welcome, " .. name .. "!\n")
cur = assert (con:execute(string.format("INSERT INTO users VALUES ('%s', '%s', '%s', '', '', '', '', '', '', '')", name, guid, os.date("%Y-%m-%d %H:%M:%S"))))
-- First time we see this player
et.trap_SendServerCommand (cno, "cpm \"" .. "Welcome, " .. name .. "!\n\"")
cur = assert (con:execute(string.format("INSERT INTO users VALUES ('%s', '%s', 0, 0, 0, 0, 0, 0, 0)", guid, os.date("%Y-%m-%d %H:%M:%S"))))
else
et.G_Print ("Welcome back, " .. name .. "! You last played on " .. player.last_seen .. "\n") -- in db: player.name
et.trap_SendServerCommand (cno, "cpm \"" .. "Welcome back, " .. name .. "! Your last connection was on " .. player.last_seen .. "\n\"") -- in db: player.name
et.G_Print ("Loading XP from database: " .. player.xp_battlesense .. " | " .. player.xp_engineering .. " | " .. player.xp_medic .. " | " .. player.xp_fieldops .. " | " .. player.xp_lightweapons .. " | " .. player.xp_heavyweapons .. " | " .. player.xp_covertops .. "\n\n")
--et.G_Print ("Loading XP from database: " .. player.xp_battlesense .. " | " .. player.xp_engineering .. " | " .. player.xp_medic .. " | " .. player.xp_fieldops .. " | " .. player.xp_lightweapons .. " | " .. player.xp_heavyweapons .. " | " .. player.xp_covertops .. "\n\n")
et.G_XP_Set (cno, player.xp_battlesense, 0, 0)
et.G_XP_Set (cno, player.xp_engineering, 1, 0)
@ -95,9 +97,9 @@ function et_ClientBegin(cno)
et.G_XP_Set (cno, player.xp_heavyweapons, 5, 0)
et.G_XP_Set (cno, player.xp_covertops, 6, 0)
et.G_Print ("Current XP:\n")
for id, name in pairs(skills) do
et.G_Print ("\t" .. name .. ": " .. et.gentity_get (cno, "sess.skillpoints", id) .. " XP\n")
et.G_Print (name .. "'s current XP levels:\n")
for id, skill in pairs(skills) do
et.G_Print ("\t" .. skill .. ": " .. et.gentity_get (cno, "sess.skillpoints", id) .. " XP\n")
end
end
@ -111,18 +113,16 @@ function et_ClientDisconnect(cno)
name = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "name" )
guid = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "cl_guid" )
--et.G_Print(name .. " with guid " .. guid .. " just disconnected\n")
cur = assert (con:execute(string.format("SELECT * FROM users WHERE guid='%s'", guid)))
cur = assert (con:execute(string.format("SELECT * FROM users WHERE guid='%s' LIMIT 1", guid)))
player = cur:fetch({}, 'a')
if not player then
-- This should not happen
et.G_Print ("ERROR: user was not found in the XP Save database!\n")
et.G_Print ("^1ERROR: (XP Save) user was not found in the database!\n")
return
else
et.G_Print ("See you again soon, " .. name .. "\n")
for id, name in pairs(skills) do et.G_Print (name .. ": " .. et.gentity_get (cno, "sess.skillpoints", id) .. " XP\n") end
et.trap_SendServerCommand (cno, "cpm \"" .. "See you again soon, " .. name .. "\n\"")
--for id, name in pairs(skills) do et.G_Print (name .. ": " .. et.gentity_get (cno, "sess.skillpoints", id) .. " XP\n") end
cur = assert (con:execute(string.format([[UPDATE users SET
last_seen='%s',
@ -145,5 +145,4 @@ function et_ClientDisconnect(cno)
guid
)))
end
end -- et_ClientDisconnect