]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Account.cpp
Account active is now stored as an integer to avoid weird loops to find the right id
[lugaru.git] / Source / Account.cpp
index b3d42c7d320eb55bb120848428338baa45d7e719..560592f62ebf7a0809b579d8b0b1a9757dcbd0e7 100644 (file)
@@ -27,9 +27,10 @@ along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 
 using namespace std;
 
-extern bool debugmode;
+extern bool devtools;
 
 vector<Account*> Account::accounts = vector<Account*>();
+int Account::i_active = -1;
 
 Account::Account(const string& name) : name(name), campaignProgress()
 {
@@ -48,35 +49,51 @@ 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();
+    i_active = accounts.size() - 1;
 }
 
 Account* Account::get(int i)
 {
+    return accounts.at(i);
+}
 
-    if ((i >= 0) && (i < int(accounts.size()))) {
-        return accounts[i];
-    } else
-        return NULL;
+int Account::getNbAccounts()
+{
+    return accounts.size();
 }
 
-void Account::destroy(int i)
+bool Account::hasActive()
 {
-    accounts.erase(accounts.begin() + i);
+    return (i_active >= 0);
 }
-Account* Account::destroy(Account* a)
+
+Account& Account::active()
 {
-    for (unsigned i = 0; i < accounts.size(); i++) {
-        if (accounts[i] == a) {
-            accounts.erase(accounts.begin() + i);
-            return NULL;
-        }
+    return *(accounts.at(i_active));
+}
+
+void Account::setActive(int i)
+{
+    if ((i >= 0) && (i < int(accounts.size()))) {
+        i_active = i;
+    } else {
+        cerr << "Tried to set active account to " << i << " but there is not such account" << endl;
+        i_active = -1;
+    }
+}
+
+void Account::destroyActive()
+{
+    if ((i_active >= 0) && (i_active < int(accounts.size()))) {
+        accounts.erase(accounts.begin() + i_active);
+        i_active = -1;
+    } else {
+        cerr << "Tried to destroy active account " << i_active << " but there is not such account" << endl;
+        i_active = -1;
     }
-    printf("Unexpected error : User %s not found\n", a->getName().c_str());
-    return accounts.front();
 }
 
 int Account::getDifficulty()
@@ -100,7 +117,7 @@ void Account::winCampaignLevel(int choice, float score, float time)
 
 void Account::winLevel(int level, float score, float time)
 {
-    if (!debugmode) {
+    if (!devtools) {
         if (score > highscore[level])
             highscore[level] = score;
         if (time < fasttime[level] || fasttime[level] == 0)
@@ -110,18 +127,18 @@ 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;
-    int accountactive;
+    int iactive;
     errno = 0;
 
     tfile = fopen(filename.c_str(), "rb" );
 
     if (tfile) {
         funpackf(tfile, "Bi", &numaccounts);
-        funpackf(tfile, "Bi", &accountactive);
+        funpackf(tfile, "Bi", &iactive);
         printf("number of accounts %d\n", numaccounts);
         for (int i = 0; i < numaccounts; i++) {
             printf("loading account %d/%d\n", i, numaccounts);
@@ -185,14 +202,14 @@ Account* Account::loadFile(string filename)
         }
 
         fclose(tfile);
-        return get(accountactive);
+        setActive(iactive);
     } else {
         perror(("Couldn't load users from " + filename).c_str());
-        return NULL;
+        i_active = -1;
     }
 }
 
-void Account::saveFile(string filename, Account* accountactive)
+void Account::saveFile(string filename)
 {
     FILE *tfile;
     errno = 0;
@@ -200,7 +217,7 @@ void Account::saveFile(string filename, Account* accountactive)
     tfile = fopen(filename.c_str(), "wb" );
     if (tfile) {
         fpackf(tfile, "Bi", getNbAccounts());
-        fpackf(tfile, "Bi", indice(accountactive));
+        fpackf(tfile, "Bi", i_active);
 
         for (int i = 0; i < getNbAccounts(); i++) {
             Account* a = Account::get(i);
@@ -249,12 +266,3 @@ void Account::saveFile(string filename, Account* accountactive)
         perror(("Couldn't save users in " + filename).c_str());
     }
 }
-
-int Account::indice(Account* a)
-{
-    for (unsigned i = 0; i < accounts.size(); i++) {
-        if (accounts[i] == a)
-            return i;
-    }
-    return -1;
-}