Skip to content

Commit

Permalink
Add new account events: onAccountCreate/onAccountRemove (multitheftau…
Browse files Browse the repository at this point in the history
…to#3019)

New events: onAccountCreate, onAccountRemove
New function: getAccountType

* Refactored CAccountManager & CAccount a little
* Refactored internal SQL logic
  • Loading branch information
TracerDS authored May 25, 2024
1 parent 3d8bd50 commit 545f54b
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 149 deletions.
75 changes: 33 additions & 42 deletions Server/mods/deathmatch/logic/CAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void CAccount::EnsureLoadedSerialUsage()
}
}

bool CAccount::HasLoadedSerialUsage()
bool CAccount::HasLoadedSerialUsage() const
{
return m_bLoadedSerialUsage;
}
Expand Down Expand Up @@ -238,11 +238,7 @@ CAccount::SSerialUsage* CAccount::GetSerialUsage(const SString& strSerial)
bool CAccount::IsSerialAuthorized(const SString& strSerial)
{
SSerialUsage* pInfo = GetSerialUsage(strSerial);
if (pInfo)
{
return pInfo->IsAuthorized();
}
return false;
return pInfo ? pInfo->IsAuthorized() : false;
}

//
Expand All @@ -265,17 +261,13 @@ bool CAccount::IsIpAuthorized(const SString& strIp)
bool CAccount::AuthorizeSerial(const SString& strSerial, const SString& strWho)
{
SSerialUsage* pInfo = GetSerialUsage(strSerial);
if (pInfo)
{
if (!pInfo->IsAuthorized())
{
pInfo->tAuthDate = time(nullptr);
pInfo->strAuthWho = strWho;
m_pManager->MarkAsChanged(this);
return true;
}
}
return false;
if (!pInfo || pInfo->IsAuthorized())
return false;

pInfo->tAuthDate = time(nullptr);
pInfo->strAuthWho = strWho;
m_pManager->MarkAsChanged(this);
return true;
}

//
Expand Down Expand Up @@ -320,29 +312,28 @@ void CAccount::RemoveUnauthorizedSerials()
bool CAccount::AddSerialForAuthorization(const SString& strSerial, const SString& strIp)
{
SSerialUsage* pInfo = GetSerialUsage(strSerial);
if (!pInfo)
{
// Only one new serial at a time, so remove all other unauthorized serials for this account
RemoveUnauthorizedSerials();
if (pInfo)
return false;

SSerialUsage info;
info.strSerial = strSerial;
info.strAddedIp = strIp;
info.tAddedDate = time(nullptr);
info.tAuthDate = 0;
info.tLastLoginDate = 0;
info.tLastLoginHttpDate = 0;
// Only one new serial at a time, so remove all other unauthorized serials for this account
RemoveUnauthorizedSerials();

// First one doesn't require authorization
if (m_SerialUsageList.size() == 0)
{
info.tAuthDate = time(nullptr);
}
m_SerialUsageList.push_back(info);
m_pManager->MarkAsChanged(this);
return true;
SSerialUsage info;
info.strSerial = strSerial;
info.strAddedIp = strIp;
info.tAddedDate = time(nullptr);
info.tAuthDate = 0;
info.tLastLoginDate = 0;
info.tLastLoginHttpDate = 0;

// First one doesn't require authorization
if (m_SerialUsageList.size() == 0)
{
info.tAuthDate = time(nullptr);
}
return false;
m_SerialUsageList.push_back(info);
m_pManager->MarkAsChanged(this);
return true;
}

//
Expand Down Expand Up @@ -372,10 +363,10 @@ void CAccount::OnLoginHttpSuccess(const SString& strIp)
EnsureLoadedSerialUsage();
for (auto& info : m_SerialUsageList)
{
if (info.strLastLoginIp == strIp && info.IsAuthorized())
{
info.tLastLoginHttpDate = time(nullptr);
m_pManager->MarkAsChanged(this);
}
if (info.strLastLoginIp != strIp || !info.IsAuthorized())
continue;

info.tLastLoginHttpDate = time(nullptr);
m_pManager->MarkAsChanged(this);
}
}
32 changes: 17 additions & 15 deletions Server/mods/deathmatch/logic/CAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,27 @@ class CAccount
const std::string& strIP = "", const std::string& strSerial = "", const SString& strHttpPassAppend = "");
~CAccount();

bool IsRegistered() { return m_AccountType != EAccountType::Guest; }
bool IsConsoleAccount() { return m_AccountType == EAccountType::Console; }
bool IsRegistered() const { return m_AccountType != EAccountType::Guest; }
bool IsConsoleAccount() const { return m_AccountType == EAccountType::Console; }
void OnLoginSuccess(const SString& strSerial, const SString& strIp);
void OnLoginHttpSuccess(const SString& strIp);

const SString& GetName() { return m_strName; }
const SString& GetName() const { return m_strName; }
void SetName(const std::string& strName);

const EAccountType GetType() const { return m_AccountType; }

void SetPassword(const SString& strPassword);
bool IsPassword(const SString& strPassword, bool* pbUsedHttpPassAppend = nullptr);
SString GetPasswordHash();
const SString& GetHttpPassAppend() { return m_strHttpPassAppend; }
const SString& GetHttpPassAppend() const { return m_strHttpPassAppend; }
void SetHttpPassAppend(const SString& strHttpPassAppend);

const std::string& GetIP() { return m_strIP; }
const std::string& GetSerial() { return m_strSerial; }
int GetID() { return m_iUserID; }
const std::string& GetIP() const { return m_strIP; }
const std::string& GetSerial() const { return m_strSerial; }
const int GetID() const { return m_iUserID; }

bool HasLoadedSerialUsage();
bool HasLoadedSerialUsage() const;
void EnsureLoadedSerialUsage();
std::vector<SSerialUsage>& GetSerialUsageList();
SSerialUsage* GetSerialUsage(const SString& strSerial);
Expand All @@ -100,19 +102,19 @@ class CAccount
bool RemoveSerial(const SString& strSerial);
void RemoveUnauthorizedSerials();

CClient* GetClient() { return m_pClient; }
CClient* GetClient() const { return m_pClient; }
void SetClient(CClient* pClient);

void SetChanged(bool bChanged) { m_bChanged = bChanged; }
bool HasChanged() { return m_bChanged; }
bool HasChanged() const { return m_bChanged; }
uint GetScriptID() const { return m_uiScriptID; }

std::shared_ptr<CLuaArgument> GetData(const std::string& strKey);
bool SetData(const std::string& strKey, const std::string& strValue, int iType);
bool HasData(const std::string& strKey);
void RemoveData(const std::string& strKey);
std::map<SString, CAccountData>::iterator DataBegin() { return m_Data.begin(); }
std::map<SString, CAccountData>::iterator DataEnd() { return m_Data.end(); }
std::map<SString, CAccountData>::iterator begin() { return m_Data.begin(); }
std::map<SString, CAccountData>::iterator end() { return m_Data.end(); }

protected:
CAccountData* GetDataPointer(const std::string& strKey);
Expand Down Expand Up @@ -147,9 +149,9 @@ class CAccountData
m_iType = iType;
}

const std::string& GetKey() { return m_strKey; }
const std::string& GetStrValue() { return m_strValue; }
int GetType() { return m_iType; }
const std::string& GetKey() const { return m_strKey; }
const std::string& GetStrValue() const { return m_strValue; }
int GetType() const { return m_iType; }
void SetStrValue(const std::string& strValue) { m_strValue = strValue; }
void SetType(int iType) { m_iType = iType; }

Expand Down
Loading

0 comments on commit 545f54b

Please sign in to comment.