2013-11-17 21:13:11 +00:00
|
|
|
--[[
|
|
|
|
Author: Jan Šimek [Radegast]
|
2013-11-23 00:22:14 +00:00
|
|
|
Version 0.2
|
2013-11-17 21:13:11 +00:00
|
|
|
License: MIT
|
2013-11-23 00:22:14 +00:00
|
|
|
Released on 23.11.2013
|
2013-11-17 21:13:11 +00:00
|
|
|
Website: http://www.etlegacy.com
|
2013-11-18 12:06:51 +00:00
|
|
|
Mod: intended for Legacy, but might also work in NoQuarter
|
2013-11-17 21:13:11 +00:00
|
|
|
|
|
|
|
Description: this script saves users' experience points into
|
|
|
|
a database and thus preserves them between connections
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- load sqlite driver (or mysql..)
|
2013-11-18 12:06:51 +00:00
|
|
|
local luasql = require "luasql.sqlite3"
|
2013-11-17 21:13:11 +00:00
|
|
|
|
|
|
|
local env -- environment object
|
|
|
|
local con -- database connection
|
|
|
|
local cur -- cursor
|
|
|
|
|
2013-11-18 12:06:51 +00:00
|
|
|
-- skill identifiers
|
|
|
|
local BATTLESENSE = 0
|
|
|
|
local ENGINEERING = 1
|
|
|
|
local MEDIC = 2
|
|
|
|
local FIELDOPS = 3
|
|
|
|
local LIGHTWEAPONS = 4
|
|
|
|
local HEAVYWEAPONS = 5
|
|
|
|
local COVERTOPS = 6
|
|
|
|
|
2013-11-17 21:13:11 +00:00
|
|
|
local skills = {}
|
2013-11-18 12:06:51 +00:00
|
|
|
skills[BATTLESENSE] = "Battlesense"
|
|
|
|
skills[ENGINEERING] = "Engineering"
|
|
|
|
skills[MEDIC] = "Medic"
|
|
|
|
skills[FIELDOPS] = "Field ops"
|
|
|
|
skills[LIGHTWEAPONS] = "Light weapons"
|
|
|
|
skills[HEAVYWEAPONS] = "Heavy weapons"
|
|
|
|
skills[COVERTOPS] = "Covert ops"
|
2013-11-17 21:13:11 +00:00
|
|
|
|
|
|
|
-- database helper function
|
|
|
|
-- returns database rows matching sql_statement
|
|
|
|
function rows(connection, sql_statement)
|
|
|
|
local cursor = assert (connection:execute (sql_statement))
|
|
|
|
return function ()
|
|
|
|
return cursor:fetch()
|
|
|
|
end
|
|
|
|
end -- rows
|
|
|
|
|
2013-11-18 12:06:51 +00:00
|
|
|
-- con:prepare with bind_names should be used to prevent sql injections
|
|
|
|
-- but it doesn't work on my version of luasql
|
|
|
|
function validateGUID(cno, guid)
|
|
|
|
-- allow only alphanumeric characters in guid
|
|
|
|
if(string.match(guid, "%W")) then
|
|
|
|
-- Invalid characters detected. We should probably drop this client
|
|
|
|
et.G_Print("^3WARNING: (XP Save) user with id " .. cno .. " has an invalid GUID: " .. guid .. "\n")
|
|
|
|
et.trap_SendServerCommand (cno, "cpm \"" .. "Your XP won't be saved because you have an invalid cl_guid.\n\"")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-11-23 00:22:14 +00:00
|
|
|
-- saves XP values of a player with id 'cno' into sqlite database
|
|
|
|
function saveXP(cno)
|
|
|
|
local name = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "name" )
|
|
|
|
local guid = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "cl_guid" )
|
|
|
|
|
|
|
|
if not validateGUID(cno, guid) then return end
|
|
|
|
|
|
|
|
cur = assert (con:execute(string.format("SELECT * FROM users WHERE guid='%s' LIMIT 1", guid)))
|
|
|
|
local player = cur:fetch({}, 'a')
|
|
|
|
|
|
|
|
if not player then
|
|
|
|
-- This should not happen
|
|
|
|
et.G_Print ("^1ERROR: (XP Save) user was not found in the database!\n")
|
|
|
|
return
|
|
|
|
else
|
|
|
|
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',
|
|
|
|
xp_battlesense='%s',
|
|
|
|
xp_engineering='%s',
|
|
|
|
xp_medic='%s',
|
|
|
|
xp_fieldops='%s',
|
|
|
|
xp_lightweapons='%s',
|
|
|
|
xp_heavyweapons='%s',
|
|
|
|
xp_covertops='%s'
|
|
|
|
WHERE guid='%s']],
|
|
|
|
os.date("%Y-%m-%d %H:%M:%S"),
|
|
|
|
et.gentity_get (cno, "sess.skillpoints", BATTLESENSE),
|
|
|
|
et.gentity_get (cno, "sess.skillpoints", ENGINEERING),
|
|
|
|
et.gentity_get (cno, "sess.skillpoints", MEDIC),
|
|
|
|
et.gentity_get (cno, "sess.skillpoints", FIELDOPS),
|
|
|
|
et.gentity_get (cno, "sess.skillpoints", LIGHTWEAPONS),
|
|
|
|
et.gentity_get (cno, "sess.skillpoints", HEAVYWEAPONS),
|
|
|
|
et.gentity_get (cno, "sess.skillpoints", COVERTOPS),
|
|
|
|
guid
|
|
|
|
)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-17 21:13:11 +00:00
|
|
|
-- init db on game start
|
|
|
|
function et_InitGame(levelTime, randomSeed, restart)
|
|
|
|
-- name of this module
|
|
|
|
et.RegisterModname ( "XP Save player database" )
|
|
|
|
|
|
|
|
-- create environement object
|
|
|
|
env = assert ( luasql.sqlite3() )
|
|
|
|
|
|
|
|
-- connect to database
|
|
|
|
con = assert ( env:connect( "xpsave.sqlite" ) )
|
|
|
|
|
|
|
|
--cur = assert (con:execute( "DROP TABLE users" ))
|
|
|
|
|
|
|
|
cur = assert ( con:execute[[
|
2013-11-17 22:45:25 +00:00
|
|
|
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)
|
|
|
|
)
|
2013-11-17 21:13:11 +00:00
|
|
|
]])
|
2013-11-18 12:06:51 +00:00
|
|
|
|
|
|
|
cur = assert ( con:execute("SELECT COUNT(*) FROM users") )
|
|
|
|
|
|
|
|
et.G_Print("There are " .. tonumber(cur:fetch(row, 'a')) .. " users in the XP save database.\n")
|
|
|
|
|
|
|
|
--et.G_Print ("^4List of users in XP Save database:\n")
|
|
|
|
--for guid, date in rows (con, "SELECT * FROM users") do
|
|
|
|
-- et.G_Print (string.format ("\tGUID %s was last seen on %s\n", guid, date))
|
|
|
|
--end
|
2013-11-17 21:13:11 +00:00
|
|
|
end -- et_InitGame
|
|
|
|
|
2013-11-23 00:22:14 +00:00
|
|
|
function et_ShutdownGame(restart)
|
|
|
|
local cno = 0
|
2013-12-22 21:42:28 +00:00
|
|
|
local maxclients = tonumber(et.trap_Cvar_Get("sv_maxclients"))
|
|
|
|
|
2013-11-23 00:22:14 +00:00
|
|
|
-- iterate through clients and save their XP
|
2013-12-22 21:42:28 +00:00
|
|
|
while cno < maxclients do
|
2014-10-07 19:19:43 +00:00
|
|
|
local cs = et.trap_GetConfigstring(tonumber(et.CS_PLAYERS) + cno)
|
2013-11-23 00:22:14 +00:00
|
|
|
|
|
|
|
if not cs or cs == "" then break end
|
|
|
|
|
|
|
|
saveXP(cno)
|
|
|
|
cno = cno + 1
|
|
|
|
end
|
|
|
|
|
2013-11-17 22:45:25 +00:00
|
|
|
-- clean up
|
|
|
|
cur:close()
|
|
|
|
con:close()
|
|
|
|
env:close()
|
|
|
|
end -- et_ShutdownGame
|
2013-11-17 21:13:11 +00:00
|
|
|
|
|
|
|
-- called when a client enters the game world
|
|
|
|
function et_ClientBegin(cno)
|
2013-11-18 12:06:51 +00:00
|
|
|
local name = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "name" )
|
|
|
|
local guid = et.Info_ValueForKey( et.trap_GetUserinfo( cno ), "cl_guid" )
|
2013-11-17 21:13:11 +00:00
|
|
|
|
2013-11-18 12:06:51 +00:00
|
|
|
if not validateGUID(cno, guid) then return end
|
|
|
|
|
2013-11-17 21:13:11 +00:00
|
|
|
cur = assert (con:execute(string.format("SELECT * FROM users WHERE guid='%s'", guid)))
|
2013-11-18 12:06:51 +00:00
|
|
|
local player = cur:fetch({}, 'a')
|
2013-11-17 21:13:11 +00:00
|
|
|
|
|
|
|
if not player then
|
2013-11-17 22:45:25 +00:00
|
|
|
-- First time we see this player
|
2013-11-22 22:46:51 +00:00
|
|
|
et.trap_SendServerCommand (cno, "cpm \"" .. "Welcome, " .. name .. "^7! You are playing on an XP save server.\n\"")
|
2013-11-17 22:45:25 +00:00
|
|
|
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"))))
|
2013-11-17 21:13:11 +00:00
|
|
|
else
|
2013-11-22 22:46:51 +00:00
|
|
|
et.trap_SendServerCommand (cno, "cpm \"" .. "Welcome back, " .. name .. "^7! Your last connection was on " .. player.last_seen .. "\n\"") -- in db: player.name
|
2013-11-17 21:13:11 +00:00
|
|
|
|
2013-11-17 22:45:25 +00:00
|
|
|
--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")
|
2013-11-18 12:06:51 +00:00
|
|
|
|
|
|
|
et.G_XP_Set (cno, player.xp_battlesense, BATTLESENSE, 0)
|
|
|
|
et.G_XP_Set (cno, player.xp_engineering, ENGINEERING, 0)
|
|
|
|
et.G_XP_Set (cno, player.xp_medic, MEDIC, 0)
|
|
|
|
et.G_XP_Set (cno, player.xp_fieldops, FIELDOPS, 0)
|
|
|
|
et.G_XP_Set (cno, player.xp_lightweapons, LIGHTWEAPONS, 0)
|
|
|
|
et.G_XP_Set (cno, player.xp_heavyweapons, HEAVYWEAPONS, 0)
|
|
|
|
et.G_XP_Set (cno, player.xp_covertops, COVERTOPS, 0)
|
2013-11-17 21:13:11 +00:00
|
|
|
|
2013-11-17 22:45:25 +00:00
|
|
|
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")
|
2013-11-17 21:13:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end -- et_ClientBegin
|
|
|
|
|
|
|
|
function et_ClientDisconnect(cno)
|
2013-11-23 00:22:14 +00:00
|
|
|
saveXP(cno)
|
2013-11-17 21:13:11 +00:00
|
|
|
end -- et_ClientDisconnect
|