Skip to content

Commit 545f54b

Browse files
authored
Add new account events: onAccountCreate/onAccountRemove (#3019)
New events: onAccountCreate, onAccountRemove New function: getAccountType * Refactored CAccountManager & CAccount a little * Refactored internal SQL logic
1 parent 3d8bd50 commit 545f54b

File tree

6 files changed

+162
-149
lines changed

6 files changed

+162
-149
lines changed

Server/mods/deathmatch/logic/CAccount.cpp

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void CAccount::EnsureLoadedSerialUsage()
210210
}
211211
}
212212

213-
bool CAccount::HasLoadedSerialUsage()
213+
bool CAccount::HasLoadedSerialUsage() const
214214
{
215215
return m_bLoadedSerialUsage;
216216
}
@@ -238,11 +238,7 @@ CAccount::SSerialUsage* CAccount::GetSerialUsage(const SString& strSerial)
238238
bool CAccount::IsSerialAuthorized(const SString& strSerial)
239239
{
240240
SSerialUsage* pInfo = GetSerialUsage(strSerial);
241-
if (pInfo)
242-
{
243-
return pInfo->IsAuthorized();
244-
}
245-
return false;
241+
return pInfo ? pInfo->IsAuthorized() : false;
246242
}
247243

248244
//
@@ -265,17 +261,13 @@ bool CAccount::IsIpAuthorized(const SString& strIp)
265261
bool CAccount::AuthorizeSerial(const SString& strSerial, const SString& strWho)
266262
{
267263
SSerialUsage* pInfo = GetSerialUsage(strSerial);
268-
if (pInfo)
269-
{
270-
if (!pInfo->IsAuthorized())
271-
{
272-
pInfo->tAuthDate = time(nullptr);
273-
pInfo->strAuthWho = strWho;
274-
m_pManager->MarkAsChanged(this);
275-
return true;
276-
}
277-
}
278-
return false;
264+
if (!pInfo || pInfo->IsAuthorized())
265+
return false;
266+
267+
pInfo->tAuthDate = time(nullptr);
268+
pInfo->strAuthWho = strWho;
269+
m_pManager->MarkAsChanged(this);
270+
return true;
279271
}
280272

281273
//
@@ -320,29 +312,28 @@ void CAccount::RemoveUnauthorizedSerials()
320312
bool CAccount::AddSerialForAuthorization(const SString& strSerial, const SString& strIp)
321313
{
322314
SSerialUsage* pInfo = GetSerialUsage(strSerial);
323-
if (!pInfo)
324-
{
325-
// Only one new serial at a time, so remove all other unauthorized serials for this account
326-
RemoveUnauthorizedSerials();
315+
if (pInfo)
316+
return false;
327317

328-
SSerialUsage info;
329-
info.strSerial = strSerial;
330-
info.strAddedIp = strIp;
331-
info.tAddedDate = time(nullptr);
332-
info.tAuthDate = 0;
333-
info.tLastLoginDate = 0;
334-
info.tLastLoginHttpDate = 0;
318+
// Only one new serial at a time, so remove all other unauthorized serials for this account
319+
RemoveUnauthorizedSerials();
335320

336-
// First one doesn't require authorization
337-
if (m_SerialUsageList.size() == 0)
338-
{
339-
info.tAuthDate = time(nullptr);
340-
}
341-
m_SerialUsageList.push_back(info);
342-
m_pManager->MarkAsChanged(this);
343-
return true;
321+
SSerialUsage info;
322+
info.strSerial = strSerial;
323+
info.strAddedIp = strIp;
324+
info.tAddedDate = time(nullptr);
325+
info.tAuthDate = 0;
326+
info.tLastLoginDate = 0;
327+
info.tLastLoginHttpDate = 0;
328+
329+
// First one doesn't require authorization
330+
if (m_SerialUsageList.size() == 0)
331+
{
332+
info.tAuthDate = time(nullptr);
344333
}
345-
return false;
334+
m_SerialUsageList.push_back(info);
335+
m_pManager->MarkAsChanged(this);
336+
return true;
346337
}
347338

348339
//
@@ -372,10 +363,10 @@ void CAccount::OnLoginHttpSuccess(const SString& strIp)
372363
EnsureLoadedSerialUsage();
373364
for (auto& info : m_SerialUsageList)
374365
{
375-
if (info.strLastLoginIp == strIp && info.IsAuthorized())
376-
{
377-
info.tLastLoginHttpDate = time(nullptr);
378-
m_pManager->MarkAsChanged(this);
379-
}
366+
if (info.strLastLoginIp != strIp || !info.IsAuthorized())
367+
continue;
368+
369+
info.tLastLoginHttpDate = time(nullptr);
370+
m_pManager->MarkAsChanged(this);
380371
}
381372
}

Server/mods/deathmatch/logic/CAccount.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,27 @@ class CAccount
7171
const std::string& strIP = "", const std::string& strSerial = "", const SString& strHttpPassAppend = "");
7272
~CAccount();
7373

74-
bool IsRegistered() { return m_AccountType != EAccountType::Guest; }
75-
bool IsConsoleAccount() { return m_AccountType == EAccountType::Console; }
74+
bool IsRegistered() const { return m_AccountType != EAccountType::Guest; }
75+
bool IsConsoleAccount() const { return m_AccountType == EAccountType::Console; }
7676
void OnLoginSuccess(const SString& strSerial, const SString& strIp);
7777
void OnLoginHttpSuccess(const SString& strIp);
7878

79-
const SString& GetName() { return m_strName; }
79+
const SString& GetName() const { return m_strName; }
8080
void SetName(const std::string& strName);
8181

82+
const EAccountType GetType() const { return m_AccountType; }
83+
8284
void SetPassword(const SString& strPassword);
8385
bool IsPassword(const SString& strPassword, bool* pbUsedHttpPassAppend = nullptr);
8486
SString GetPasswordHash();
85-
const SString& GetHttpPassAppend() { return m_strHttpPassAppend; }
87+
const SString& GetHttpPassAppend() const { return m_strHttpPassAppend; }
8688
void SetHttpPassAppend(const SString& strHttpPassAppend);
8789

88-
const std::string& GetIP() { return m_strIP; }
89-
const std::string& GetSerial() { return m_strSerial; }
90-
int GetID() { return m_iUserID; }
90+
const std::string& GetIP() const { return m_strIP; }
91+
const std::string& GetSerial() const { return m_strSerial; }
92+
const int GetID() const { return m_iUserID; }
9193

92-
bool HasLoadedSerialUsage();
94+
bool HasLoadedSerialUsage() const;
9395
void EnsureLoadedSerialUsage();
9496
std::vector<SSerialUsage>& GetSerialUsageList();
9597
SSerialUsage* GetSerialUsage(const SString& strSerial);
@@ -100,19 +102,19 @@ class CAccount
100102
bool RemoveSerial(const SString& strSerial);
101103
void RemoveUnauthorizedSerials();
102104

103-
CClient* GetClient() { return m_pClient; }
105+
CClient* GetClient() const { return m_pClient; }
104106
void SetClient(CClient* pClient);
105107

106108
void SetChanged(bool bChanged) { m_bChanged = bChanged; }
107-
bool HasChanged() { return m_bChanged; }
109+
bool HasChanged() const { return m_bChanged; }
108110
uint GetScriptID() const { return m_uiScriptID; }
109111

110112
std::shared_ptr<CLuaArgument> GetData(const std::string& strKey);
111113
bool SetData(const std::string& strKey, const std::string& strValue, int iType);
112114
bool HasData(const std::string& strKey);
113115
void RemoveData(const std::string& strKey);
114-
std::map<SString, CAccountData>::iterator DataBegin() { return m_Data.begin(); }
115-
std::map<SString, CAccountData>::iterator DataEnd() { return m_Data.end(); }
116+
std::map<SString, CAccountData>::iterator begin() { return m_Data.begin(); }
117+
std::map<SString, CAccountData>::iterator end() { return m_Data.end(); }
116118

117119
protected:
118120
CAccountData* GetDataPointer(const std::string& strKey);
@@ -147,9 +149,9 @@ class CAccountData
147149
m_iType = iType;
148150
}
149151

150-
const std::string& GetKey() { return m_strKey; }
151-
const std::string& GetStrValue() { return m_strValue; }
152-
int GetType() { return m_iType; }
152+
const std::string& GetKey() const { return m_strKey; }
153+
const std::string& GetStrValue() const { return m_strValue; }
154+
int GetType() const { return m_iType; }
153155
void SetStrValue(const std::string& strValue) { m_strValue = strValue; }
154156
void SetType(int iType) { m_iType = iType; }
155157

0 commit comments

Comments
 (0)