mirror of
https://github.com/CFC-Servers/cfc_time.git
synced 2025-03-04 03:03:20 -05:00
Simplify column typing
This commit is contained in:
parent
77cda559da
commit
6eae1ba998
@ -9,8 +9,7 @@ config.setDefaults{
|
||||
MYSQL_HOST = "127.0.0.1",
|
||||
MYSQL_USERNAME = "",
|
||||
MYSQL_PASSWORD = "",
|
||||
MYSQL_DATABASE = "cfc_time",
|
||||
MYSQL_SESSION_DURATION_COLUMN_TYPE = "MEDIUMINT UNSIGNED"
|
||||
MYSQL_DATABASE = "cfc_time"
|
||||
}
|
||||
|
||||
storage.database = mysqloo.connect(
|
||||
@ -30,7 +29,6 @@ function storage.database:onConnected()
|
||||
transaction:addQuery( storage:SessionCleanupQuery() )
|
||||
|
||||
transaction.onSuccess = function()
|
||||
storage:CacheMaxSessionDuration()
|
||||
storage:PrepareStatements()
|
||||
end
|
||||
|
||||
@ -84,8 +82,7 @@ end
|
||||
|
||||
function storage:CreateSession( steamID, sessionStart, sessionDuration )
|
||||
local maxDuration = self.MAX_SESSION_DURATION
|
||||
local sessionsCount = math.ceil( sessionDuration / maxDuration )
|
||||
sessionsCount = math.max( 1, sessionsCount )
|
||||
local sessionsCount = math.max( 1, math.ceil( sessionDuration / maxDuration ) )
|
||||
|
||||
logger:debug(
|
||||
string.format(
|
||||
@ -106,6 +103,8 @@ function storage:CreateSession( steamID, sessionStart, sessionDuration )
|
||||
)
|
||||
)
|
||||
|
||||
-- Because we use the same prepared query repeatedly, we need to run them in individual
|
||||
-- transactions or they'll all use the values given to the last one. This is a weird bug in MySQLOO
|
||||
local newSessionTransaction = self:InitTransaction()
|
||||
local newSession = self:Prepare( "newSession", nil, steamID, newStart, newEnd, duration )
|
||||
|
||||
@ -124,12 +123,6 @@ function storage:CreateSession( steamID, sessionStart, sessionDuration )
|
||||
end
|
||||
end
|
||||
|
||||
-- Takes a player, a session start timestamp, and a callback, then:
|
||||
-- - Creates a new user (if needed)
|
||||
-- - Creates a new session with given values
|
||||
-- Calls callback with a structure containing:
|
||||
-- - isFirstVisit (boolean describing if this is the player's first visit to the server)
|
||||
-- - sessionID (the id of the newly created session)
|
||||
function storage:PlayerInit( ply, sessionStart, callback )
|
||||
local steamID = ply:SteamID64()
|
||||
|
||||
@ -146,12 +139,12 @@ function storage:PlayerInit( ply, sessionStart, callback )
|
||||
logger:debug( "PlayerInit transaction successful!" )
|
||||
|
||||
local isFirstVisit = newUser:lastInsert() ~= 0
|
||||
local sessionIDResult = newSession:lastInsert()
|
||||
local sessionID = newSession:lastInsert()
|
||||
logger:debug( "NewUser last inserted index: " .. tostring( newUser:lastInsert() ) )
|
||||
|
||||
local data = {
|
||||
isFirstVisit = isFirstVisit,
|
||||
sessionID = sessionIDResult
|
||||
sessionID = sessionID
|
||||
}
|
||||
|
||||
callback( data )
|
||||
|
@ -2,34 +2,10 @@ local storage = CFCTime.Storage
|
||||
local logger = CFCTime.Logger
|
||||
local config = CFCTime.Config
|
||||
|
||||
storage.MAX_SESSION_DURATION = nil
|
||||
storage.preparedQueries = {}
|
||||
|
||||
local MAX_SESSION_DURATIONS = {
|
||||
tinyint = {
|
||||
signed = 127,
|
||||
unsigned = 255
|
||||
},
|
||||
|
||||
smallint = {
|
||||
signed = 32767,
|
||||
unsigned = 65535
|
||||
},
|
||||
|
||||
mediumint = {
|
||||
signed = 8388607,
|
||||
unsigned = 16777215
|
||||
},
|
||||
|
||||
int = {
|
||||
signed = 2147483647,
|
||||
unsigned = 4294967295
|
||||
},
|
||||
|
||||
bigint = {
|
||||
signed = math.huge,
|
||||
unsigned = math.huge
|
||||
}
|
||||
}
|
||||
-- Maximum unsigned integer for a mysql mediumint
|
||||
storage.MAX_SESSION_DURATION = 16777215
|
||||
|
||||
function storage:InitTransaction()
|
||||
local transaction = self.database:createTransaction()
|
||||
@ -51,65 +27,6 @@ function storage:InitQuery( rawQuery )
|
||||
return query
|
||||
end
|
||||
|
||||
storage.preparedQueries = {}
|
||||
|
||||
function storage:GetMaxSessionTime( callback )
|
||||
local queryStr = [[
|
||||
SELECT COLUMN_TYPE
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_NAME = "sessions"
|
||||
AND COLUMN_NAME = "duration"
|
||||
]]
|
||||
|
||||
local query = self:InitQuery( queryStr )
|
||||
|
||||
local maxSessionTime = function( data )
|
||||
-- e.g.
|
||||
-- "mediumint(8)"
|
||||
-- "mediumint(8) unsigned"
|
||||
local columnData = data[1].COLUMN_TYPE
|
||||
|
||||
-- e.g.
|
||||
-- { [1] = "mediumint(8)" }
|
||||
-- { [1] = "mediumint(8)", [2] = "unsigned" }
|
||||
local spl = string.Split( columnData, " " )
|
||||
|
||||
-- e.g.
|
||||
-- "mediumint"
|
||||
-- "mediumint"
|
||||
local column = string.Split( spl[1], "(" )[1]
|
||||
|
||||
-- e.g.
|
||||
-- true
|
||||
-- false
|
||||
local signed = spl[2] ~= "unsigned"
|
||||
|
||||
logger:debug( "Getting max session duration for column: [" .. column .. "] (signed: " .. tostring( signed ) .. ")" )
|
||||
return MAX_SESSION_DURATIONS[column][signed and "signed" or "unsigned"]
|
||||
end
|
||||
|
||||
query.onSuccess = function( _, data )
|
||||
if data then
|
||||
logger:debug( "Session duration query result", data )
|
||||
end
|
||||
|
||||
local maxTime = maxSessionTime( data )
|
||||
|
||||
callback( maxTime )
|
||||
end
|
||||
|
||||
query:start()
|
||||
end
|
||||
|
||||
function storage:CacheMaxSessionDuration()
|
||||
self:GetMaxSessionTime(
|
||||
function( maxSessionTime )
|
||||
logger:debug( "Setting max session duration to: " .. maxSessionTime )
|
||||
storage.MAX_SESSION_DURATION = maxSessionTime
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
function storage:CreateUsersQuery()
|
||||
local createUsers = [[
|
||||
CREATE TABLE IF NOT EXISTS users(
|
||||
@ -130,7 +47,7 @@ function storage:CreateSessionsQuery()
|
||||
user_id VARCHAR(20) NOT NULL,
|
||||
joined INT UNSIGNED NOT NULL,
|
||||
departed INT UNSIGNED,
|
||||
duration %s NOT NULL DEFAULT 0,
|
||||
duration MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES users (steam_id) ON DELETE CASCADE
|
||||
)
|
||||
]], config.get( "MYSQL_SESSION_DURATION_COLUMN_TYPE" ) )
|
||||
|
Loading…
Reference in New Issue
Block a user