]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Account.cpp
Replaced all uses of Account::active outside of Account by call to active() method
[lugaru.git] / Source / Account.cpp
index d132c2123d602945454e79d7f87e7d96c3315e9e..bae2abda7bfcb45c29312e64da0c1ce6388f09ad 100644 (file)
@@ -30,7 +30,7 @@ using namespace std;
 extern bool devtools;
 
 vector<Account*> Account::accounts = vector<Account*>();
-Account* Account::active = nullptr;
+Account* Account::_active = nullptr;
 
 Account::Account(const string& name) : name(name), campaignProgress()
 {
@@ -49,35 +49,40 @@ void Account::setCurrentCampaign(const string& name)
     currentCampaign = name;
 }
 
-Account* Account::add(const string& name)
+void Account::add(const string& name)
 {
     accounts.push_back(new Account(name));
-    return accounts.back();
+    _active = accounts.back();
 }
 
 Account* Account::get(int i)
 {
-
     if ((i >= 0) && (i < int(accounts.size()))) {
         return accounts[i];
     } else
         return NULL;
 }
 
-void Account::destroy(int i)
+void Account::setActive(int i)
 {
-    accounts.erase(accounts.begin() + i);
+    _active = get(i);
 }
-Account* Account::destroy(Account* a)
+
+void Account::destroyActive()
 {
     for (unsigned i = 0; i < accounts.size(); i++) {
-        if (accounts[i] == a) {
+        if (accounts[i] == _active) {
             accounts.erase(accounts.begin() + i);
-            return NULL;
+            _active = nullptr;
+            return;
         }
     }
-    printf("Unexpected error : User %s not found\n", a->getName().c_str());
-    return accounts.front();
+    cerr << "Unexpected error : User " << _active->getName() << " not found" << endl;
+    if (accounts.empty()) {
+        _active = nullptr;
+    } else {
+        _active = accounts.front();
+    }
 }
 
 int Account::getDifficulty()
@@ -111,7 +116,7 @@ void Account::winLevel(int level, float score, float time)
         progress = level + 1;
 }
 
-Account* Account::loadFile(string filename)
+void Account::loadFile(string filename)
 {
     FILE *tfile;
     int numaccounts;
@@ -186,10 +191,10 @@ Account* Account::loadFile(string filename)
         }
 
         fclose(tfile);
-        return get(iactive);
+        _active = get(iactive);
     } else {
         perror(("Couldn't load users from " + filename).c_str());
-        return NULL;
+        _active = nullptr;
     }
 }
 
@@ -201,7 +206,7 @@ void Account::saveFile(string filename)
     tfile = fopen(filename.c_str(), "wb" );
     if (tfile) {
         fpackf(tfile, "Bi", getNbAccounts());
-        fpackf(tfile, "Bi", indice(Account::active));
+        fpackf(tfile, "Bi", indice(Account::_active));
 
         for (int i = 0; i < getNbAccounts(); i++) {
             Account* a = Account::get(i);