X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=Source%2FAccount.cpp;h=bae2abda7bfcb45c29312e64da0c1ce6388f09ad;hb=e08372a2095837a0b951ccb68c3499ef67c1a827;hp=b3d42c7d320eb55bb120848428338baa45d7e719;hpb=f66f0e0b961dd138e4a60554373de9e64abd973b;p=lugaru.git diff --git a/Source/Account.cpp b/Source/Account.cpp index b3d42c7..bae2abd 100644 --- a/Source/Account.cpp +++ b/Source/Account.cpp @@ -27,9 +27,10 @@ along with Lugaru. If not, see . using namespace std; -extern bool debugmode; +extern bool devtools; vector Account::accounts = vector(); +Account* Account::_active = nullptr; Account::Account(const string& name) : name(name), campaignProgress() { @@ -48,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() @@ -100,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) @@ -110,18 +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(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 +191,14 @@ Account* Account::loadFile(string filename) } fclose(tfile); - return get(accountactive); + _active = get(iactive); } else { perror(("Couldn't load users from " + filename).c_str()); - return NULL; + _active = nullptr; } } -void Account::saveFile(string filename, Account* accountactive) +void Account::saveFile(string filename) { FILE *tfile; errno = 0; @@ -200,7 +206,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", indice(Account::_active)); for (int i = 0; i < getNbAccounts(); i++) { Account* a = Account::get(i);