]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Account.cpp
Moved Account::active in Account class. Maybe it should be changed for a method and...
[lugaru.git] / Source / Account.cpp
index a42e2f70e8cf76e7857586d974e8756a19e838ae..d132c2123d602945454e79d7f87e7d96c3315e9e 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,16 +23,17 @@ 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(string n) : campaignProgress()
+Account::Account(const string& name) : name(name), campaignProgress()
 {
-    name = string(n);
     difficulty = 0;
     progress = 0;
     points = 0;
@@ -43,12 +44,12 @@ Account::Account(string n) : campaignProgress()
     setCurrentCampaign("main");
 }
 
-void Account::setCurrentCampaign(string name)
+void Account::setCurrentCampaign(const string& name)
 {
     currentCampaign = name;
 }
 
-Account* Account::add(string name)
+Account* Account::add(const string& name)
 {
     accounts.push_back(new Account(name));
     return accounts.back();
@@ -75,7 +76,7 @@ Account* Account::destroy(Account* a)
             return NULL;
         }
     }
-    printf("Unexpected error : User %s not found\n", a->getName());
+    printf("Unexpected error : User %s not found\n", a->getName().c_str());
     return accounts.front();
 }
 
@@ -100,7 +101,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)
@@ -114,13 +115,14 @@ Account* 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);
@@ -129,7 +131,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 = "";
@@ -140,7 +141,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));
@@ -186,35 +186,34 @@ Account* Account::loadFile(string filename)
         }
 
         fclose(tfile);
-        return get(accountactive);
+        return get(iactive);
     } else {
-        printf("filenotfound\n");
+        perror(("Couldn't load users from " + filename).c_str());
         return NULL;
     }
 }
 
-void Account::saveFile(string filename, Account* accountactive)
+void Account::saveFile(string filename)
 {
     FILE *tfile;
-    unsigned j;
+    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());
 
             map<string, CampaignProgress>::const_iterator it;
-            for ( it = a->campaignProgress.begin(); it != a->campaignProgress.end(); ++it) {
+            for (it = a->campaignProgress.begin(); it != a->campaignProgress.end(); ++it) {
                 fpackf(tfile, "Bi",  it->first.size());
-                for (j = 0; j < it->first.size(); j++) {
+                for (unsigned j = 0; j < it->first.size(); j++) {
                     fpackf(tfile, "Bb",  it->first[j]);
                 }
                 fpackf(tfile, "Bf", it->second.time);
@@ -222,31 +221,33 @@ void Account::saveFile(string filename, Account* accountactive)
                 fpackf(tfile, "Bf", it->second.fasttime);
                 fpackf(tfile, "Bf", it->second.highscore);
                 fpackf(tfile, "Bi", it->second.choices.size());
-                for (j = 0; j < it->second.choices.size(); j++) {
+                for (unsigned j = 0; j < it->second.choices.size(); j++) {
                     fpackf(tfile, "Bi", it->second.choices[j]);
                 }
             }
 
             fpackf(tfile, "Bi", a->getCurrentCampaign().size());
-            for (j = 0; j < a->getCurrentCampaign().size(); j++) {
+            for (unsigned j = 0; j < a->getCurrentCampaign().size(); j++) {
                 fpackf(tfile, "Bb", a->getCurrentCampaign()[j]);
             }
 
             fpackf(tfile, "Bf", a->points);
-            for (j = 0; j < 50; j++) {
+            for (unsigned j = 0; j < 50; j++) {
                 fpackf(tfile, "Bf", a->highscore[j]);
                 fpackf(tfile, "Bf", a->fasttime[j]);
             }
-            for (j = 0; j < 60; j++) {
+            for (unsigned j = 0; j < 60; j++) {
                 fpackf(tfile, "Bb",  a->unlocked[j]);
             }
             fpackf(tfile, "Bi",  a->name.size());
-            for (j = 0; j < a->name.size(); j++) {
+            for (unsigned j = 0; j < a->name.size(); j++) {
                 fpackf(tfile, "Bb",  a->name[j]);
             }
         }
 
         fclose(tfile);
+    } else {
+        perror(("Couldn't save users in " + filename).c_str());
     }
 }