As of now, only players and cells still use the slower saving method, and only when disconnecting or being fully unloaded respectively. Additionally, rename the Save() and Load() methods into SaveToDisk() and LoadFromDisk() to reduce ambiguity and prevent them from being used all the time by new scripters.
44 lines
1.1 KiB
Lua
44 lines
1.1 KiB
Lua
Database = require("database")
|
|
local BasePlayer = require("player.base")
|
|
|
|
local Player = class("Player", BasePlayer)
|
|
|
|
function Player:__init(pid, playerName)
|
|
BasePlayer.__init(self, pid, playerName)
|
|
|
|
if self.hasAccount == nil then
|
|
|
|
self.dbPid = self:GetDatabaseId()
|
|
|
|
if self.dbPid ~= nil then
|
|
self.hasAccount = true
|
|
else
|
|
self.hasAccount = false
|
|
end
|
|
end
|
|
end
|
|
|
|
function Player:CreateAccount()
|
|
Database:InsertRow("player_login", self.data.login)
|
|
self.dbPid = self:GetDatabaseId()
|
|
Database:SavePlayer(self.dbPid, self.data)
|
|
self.hasAccount = true
|
|
end
|
|
|
|
function Player:SaveToDisk()
|
|
if self.hasAccount and self.loggedIn then
|
|
Database:SavePlayer(self.dbPid, self.data)
|
|
end
|
|
end
|
|
|
|
function Player:LoadFromDisk()
|
|
self.data = Database:LoadPlayer(self.dbPid, self.data)
|
|
end
|
|
|
|
function Player:GetDatabaseId()
|
|
local escapedName = Database:Escape(self.accountName)
|
|
return Database:GetSingleValue("player_login", "dbPid", string.format("WHERE name = '%s'", escapedName))
|
|
end
|
|
|
|
return Player
|