-- server/persistence.lua print("^2[turfwar]^7 persistence.lua loaded (MySQL await version)") TurfwarPersist = TurfwarPersist or {} local function getIdentifier(src) local id = GetPlayerIdentifierByType(src, "license2") if id and id ~= "" then return id end id = GetPlayerIdentifierByType(src, "license") if id and id ~= "" then return id end local ids = GetPlayerIdentifiers(src) return ids and ids[1] or nil end local function dbReady() return MySQL ~= nil end -- Call once on resource start to confirm DB + table exists CreateThread(function() Wait(500) if not dbReady() then print("^1[turfwar]^7 ERROR: MySQL is nil. Did you add '@oxmysql/lib/MySQL.lua' BEFORE this file?") return end -- Ensure table exists (auto-create) local ok, err = pcall(function() MySQL.query.await([[ CREATE TABLE IF NOT EXISTS turfwar_players ( identifier VARCHAR(64) NOT NULL PRIMARY KEY, gang_id INT NOT NULL DEFAULT 0, rank INT NOT NULL DEFAULT 0, last_seen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ]]) end) if ok then print("^2[turfwar]^7 DB ready: turfwar_players table ensured.") else print("^1[turfwar]^7 DB ERROR creating/ensuring table: " .. tostring(err)) end end) function TurfwarPersist.Load(src) if not dbReady() then return 0, 0 end local ident = getIdentifier(src) if not ident then print(("^3[turfwar]^7 Persist.Load: no identifier for src=%s"):format(src)) return 0, 0 end local row = MySQL.single.await("SELECT gang_id, rank FROM turfwar_players WHERE identifier = ?", { ident }) if row then return tonumber(row.gang_id) or 0, tonumber(row.rank) or 0 end -- first time: insert MySQL.insert.await("INSERT INTO turfwar_players (identifier, gang_id, rank) VALUES (?, 0, 0)", { ident }) return 0, 0 end function TurfwarPersist.SaveGang(src, gangId) if not dbReady() then print("^1[turfwar]^7 Persist.SaveGang: MySQL not ready (MySQL=nil)") return end local ident = getIdentifier(src) if not ident then print(("^1[turfwar]^7 Persist.SaveGang: no identifier for src=%s"):format(src)) return end gangId = tonumber(gangId) or 0 print(("^3[turfwar]^7 Persist.SaveGang: ident=%s gangId=%d"):format(ident, gangId)) local affected = MySQL.update.await([[ INSERT INTO turfwar_players (identifier, gang_id, rank) VALUES (?, ?, 0) ON DUPLICATE KEY UPDATE gang_id = VALUES(gang_id) ]], { ident, gangId }) print(("^2[turfwar]^7 Persist.SaveGang: DB affected=%s"):format(tostring(affected))) end