X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=Source%2FAccount.cpp;h=560592f62ebf7a0809b579d8b0b1a9757dcbd0e7;hb=f83c7bc4ef12b0bbdc1cfa6bdce35d9665510e6c;hp=d132c2123d602945454e79d7f87e7d96c3315e9e;hpb=3de67c0db8c3c74f5fb487579936a07770d253a1;p=lugaru.git diff --git a/Source/Account.cpp b/Source/Account.cpp index d132c21..560592f 100644 --- a/Source/Account.cpp +++ b/Source/Account.cpp @@ -30,7 +30,7 @@ using namespace std; extern bool devtools; vector Account::accounts = vector(); -Account* Account::active = nullptr; +int Account::i_active = -1; Account::Account(const string& name) : name(name), campaignProgress() { @@ -49,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() @@ -111,7 +127,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 +202,10 @@ Account* Account::loadFile(string filename) } fclose(tfile); - return get(iactive); + setActive(iactive); } else { perror(("Couldn't load users from " + filename).c_str()); - return NULL; + i_active = -1; } } @@ -201,7 +217,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", i_active); for (int i = 0; i < getNbAccounts(); i++) { Account* a = Account::get(i); @@ -250,12 +266,3 @@ void Account::saveFile(string filename) 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; -}