]> 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 1ee3242bf0ec3779dc03ed369899bfb0cef681fd..bae2abda7bfcb45c29312e64da0c1ce6388f09ad 100644 (file)
@@ -1,6 +1,6 @@
 /*
 Copyright (C) 2003, 2010 - Wolfire Games
-Copyright (C) 2010 - Côme <MCMic> BERNIGAUD
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
 
 This file is part of Lugaru.
 
@@ -23,12 +23,14 @@ along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 #include <fstream>
 #include "MacCompatibility.h"
 #include "string.h"
+#include <iostream>
 
 using namespace std;
 
-extern bool debugmode;
+extern bool devtools;
 
 vector<Account*> Account::accounts = vector<Account*>();
+Account* Account::_active = nullptr;
 
 Account::Account(const string& name) : name(name), campaignProgress()
 {
@@ -47,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());
-    return accounts.front();
+    cerr << "Unexpected error : User " << _active->getName() << " not found" << endl;
+    if (accounts.empty()) {
+        _active = nullptr;
+    } else {
+        _active = accounts.front();
+    }
 }
 
 int Account::getDifficulty()
@@ -99,7 +106,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)
@@ -109,17 +116,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(ConvertFileName(filename.c_str()), "rb" );
+    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);
@@ -128,7 +136,6 @@ Account* Account::loadFile(string filename)
             funpackf(tfile, "Bi", &(acc->progress));
             int nbCampaigns;
             funpackf(tfile, "Bi", &nbCampaigns);
-            //~ printf("loading %d campaign progress info\n",nbCampaigns);
 
             for (int k = 0; k < nbCampaigns; ++k) {
                 string campaignName = "";
@@ -139,7 +146,6 @@ Account* Account::loadFile(string filename)
                     funpackf(tfile, "Bb",  &c);
                     campaignName.append(1, c);
                 }
-                //~ printf("loading %s campaign progress info\n",campaignName.c_str());
                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].time));
                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].score));
                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].fasttime));
@@ -185,26 +191,26 @@ Account* Account::loadFile(string filename)
         }
 
         fclose(tfile);
-        return get(accountactive);
+        _active = get(iactive);
     } else {
-        printf("filenotfound\n");
-        return NULL;
+        perror(("Couldn't load users from " + filename).c_str());
+        _active = nullptr;
     }
 }
 
-void Account::saveFile(string filename, Account* accountactive)
+void Account::saveFile(string filename)
 {
     FILE *tfile;
+    errno = 0;
 
-    tfile = fopen(ConvertFileName(filename.c_str(), "wb"), "wb" );
+    tfile = fopen(filename.c_str(), "wb" );
     if (tfile) {
-        printf("writing %d accounts :\n", getNbAccounts());
         fpackf(tfile, "Bi", getNbAccounts());
-        fpackf(tfile, "Bi", indice(accountactive));
+        fpackf(tfile, "Bi", indice(Account::_active));
 
         for (int i = 0; i < getNbAccounts(); i++) {
             Account* a = Account::get(i);
-            printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), a->getName());
+            printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), a->getName().c_str());
             fpackf(tfile, "Bi", a->difficulty);
             fpackf(tfile, "Bi", a->progress);
             fpackf(tfile, "Bi", a->campaignProgress.size());
@@ -245,6 +251,8 @@ void Account::saveFile(string filename, Account* accountactive)
         }
 
         fclose(tfile);
+    } else {
+        perror(("Couldn't save users in " + filename).c_str());
     }
 }