]> git.jsancho.org Git - lugaru.git/blob - Source/Account.cpp
Moved Account::active in Account class. Maybe it should be changed for a method and...
[lugaru.git] / Source / Account.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Account.h"
22 #include "binio.h"
23 #include <fstream>
24 #include "MacCompatibility.h"
25 #include "string.h"
26 #include <iostream>
27
28 using namespace std;
29
30 extern bool devtools;
31
32 vector<Account*> Account::accounts = vector<Account*>();
33 Account* Account::active = nullptr;
34
35 Account::Account(const string& name) : name(name), campaignProgress()
36 {
37     difficulty = 0;
38     progress = 0;
39     points = 0;
40     memset(highscore, 0, sizeof(highscore));
41     memset(fasttime, 0, sizeof(fasttime));
42     memset(unlocked, 0, sizeof(unlocked));
43
44     setCurrentCampaign("main");
45 }
46
47 void Account::setCurrentCampaign(const string& name)
48 {
49     currentCampaign = name;
50 }
51
52 Account* Account::add(const string& name)
53 {
54     accounts.push_back(new Account(name));
55     return accounts.back();
56 }
57
58 Account* Account::get(int i)
59 {
60
61     if ((i >= 0) && (i < int(accounts.size()))) {
62         return accounts[i];
63     } else
64         return NULL;
65 }
66
67 void Account::destroy(int i)
68 {
69     accounts.erase(accounts.begin() + i);
70 }
71 Account* Account::destroy(Account* a)
72 {
73     for (unsigned i = 0; i < accounts.size(); i++) {
74         if (accounts[i] == a) {
75             accounts.erase(accounts.begin() + i);
76             return NULL;
77         }
78     }
79     printf("Unexpected error : User %s not found\n", a->getName().c_str());
80     return accounts.front();
81 }
82
83 int Account::getDifficulty()
84 {
85     return difficulty;
86 }
87
88 void Account::endGame()
89 {
90     campaignProgress[currentCampaign].choices.clear();
91     campaignProgress[currentCampaign].score = 0;
92     campaignProgress[currentCampaign].time = 0;
93 }
94
95 void Account::winCampaignLevel(int choice, float score, float time)
96 {
97     campaignProgress[currentCampaign].choices.push_back(choice);
98     setCampaignScore(campaignProgress[currentCampaign].score + score);
99     campaignProgress[currentCampaign].time = time;
100 }
101
102 void Account::winLevel(int level, float score, float time)
103 {
104     if (!devtools) {
105         if (score > highscore[level])
106             highscore[level] = score;
107         if (time < fasttime[level] || fasttime[level] == 0)
108             fasttime[level] = time;
109     }
110     if (progress < level + 1)
111         progress = level + 1;
112 }
113
114 Account* Account::loadFile(string filename)
115 {
116     FILE *tfile;
117     int numaccounts;
118     int iactive;
119     errno = 0;
120
121     tfile = fopen(filename.c_str(), "rb" );
122
123     if (tfile) {
124         funpackf(tfile, "Bi", &numaccounts);
125         funpackf(tfile, "Bi", &iactive);
126         printf("number of accounts %d\n", numaccounts);
127         for (int i = 0; i < numaccounts; i++) {
128             printf("loading account %d/%d\n", i, numaccounts);
129             Account* acc = new Account();
130             funpackf(tfile, "Bi", &(acc->difficulty));
131             funpackf(tfile, "Bi", &(acc->progress));
132             int nbCampaigns;
133             funpackf(tfile, "Bi", &nbCampaigns);
134
135             for (int k = 0; k < nbCampaigns; ++k) {
136                 string campaignName = "";
137                 int t;
138                 char c;
139                 funpackf(tfile, "Bi",  &t);
140                 for (int j = 0; j < t; j++) {
141                     funpackf(tfile, "Bb",  &c);
142                     campaignName.append(1, c);
143                 }
144                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].time));
145                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].score));
146                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].fasttime));
147                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].highscore));
148                 int campaignchoicesmade, campaignchoice;
149                 funpackf(tfile, "Bi", &campaignchoicesmade);
150                 for (int j = 0; j < campaignchoicesmade; j++) {
151                     funpackf(tfile, "Bi", &campaignchoice);
152                     if (campaignchoice >= 10) { // what is that for?
153                         campaignchoice = 0;
154                     }
155                     acc->campaignProgress[campaignName].choices.push_back(campaignchoice);
156                 }
157             }
158
159             acc->currentCampaign = "";
160             int t;
161             char c;
162             funpackf(tfile, "Bi",  &t);
163             for (int i = 0; i < t; i++) {
164                 funpackf(tfile, "Bb",  &c);
165                 acc->currentCampaign.append(1, c);
166             }
167
168             funpackf(tfile, "Bf", &(acc->points));
169             for (int i = 0; i < 50; i++) {
170                 funpackf(tfile, "Bf", &(acc->highscore[i]));
171                 funpackf(tfile, "Bf", &(acc->fasttime[i]));
172             }
173             for (int i = 0; i < 60; i++) {
174                 funpackf(tfile, "Bb",  &(acc->unlocked[i]));
175             }
176             int temp;
177             char ctemp;
178             funpackf(tfile, "Bi",  &temp);
179             for (int i = 0; i < temp; i++) {
180                 funpackf(tfile, "Bb",  &ctemp);
181                 acc->name.append(1, ctemp);
182             }
183             if (!strcmp(acc->name.c_str(), ""))
184                 acc->name = "Lugaru Player"; // no empty player name security.
185             accounts.push_back(acc);
186         }
187
188         fclose(tfile);
189         return get(iactive);
190     } else {
191         perror(("Couldn't load users from " + filename).c_str());
192         return NULL;
193     }
194 }
195
196 void Account::saveFile(string filename)
197 {
198     FILE *tfile;
199     errno = 0;
200
201     tfile = fopen(filename.c_str(), "wb" );
202     if (tfile) {
203         fpackf(tfile, "Bi", getNbAccounts());
204         fpackf(tfile, "Bi", indice(Account::active));
205
206         for (int i = 0; i < getNbAccounts(); i++) {
207             Account* a = Account::get(i);
208             printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), a->getName().c_str());
209             fpackf(tfile, "Bi", a->difficulty);
210             fpackf(tfile, "Bi", a->progress);
211             fpackf(tfile, "Bi", a->campaignProgress.size());
212
213             map<string, CampaignProgress>::const_iterator it;
214             for (it = a->campaignProgress.begin(); it != a->campaignProgress.end(); ++it) {
215                 fpackf(tfile, "Bi",  it->first.size());
216                 for (unsigned j = 0; j < it->first.size(); j++) {
217                     fpackf(tfile, "Bb",  it->first[j]);
218                 }
219                 fpackf(tfile, "Bf", it->second.time);
220                 fpackf(tfile, "Bf", it->second.score);
221                 fpackf(tfile, "Bf", it->second.fasttime);
222                 fpackf(tfile, "Bf", it->second.highscore);
223                 fpackf(tfile, "Bi", it->second.choices.size());
224                 for (unsigned j = 0; j < it->second.choices.size(); j++) {
225                     fpackf(tfile, "Bi", it->second.choices[j]);
226                 }
227             }
228
229             fpackf(tfile, "Bi", a->getCurrentCampaign().size());
230             for (unsigned j = 0; j < a->getCurrentCampaign().size(); j++) {
231                 fpackf(tfile, "Bb", a->getCurrentCampaign()[j]);
232             }
233
234             fpackf(tfile, "Bf", a->points);
235             for (unsigned j = 0; j < 50; j++) {
236                 fpackf(tfile, "Bf", a->highscore[j]);
237                 fpackf(tfile, "Bf", a->fasttime[j]);
238             }
239             for (unsigned j = 0; j < 60; j++) {
240                 fpackf(tfile, "Bb",  a->unlocked[j]);
241             }
242             fpackf(tfile, "Bi",  a->name.size());
243             for (unsigned j = 0; j < a->name.size(); j++) {
244                 fpackf(tfile, "Bb",  a->name[j]);
245             }
246         }
247
248         fclose(tfile);
249     } else {
250         perror(("Couldn't save users in " + filename).c_str());
251     }
252 }
253
254 int Account::indice(Account* a)
255 {
256     for (unsigned i = 0; i < accounts.size(); i++) {
257         if (accounts[i] == a)
258             return i;
259     }
260     return -1;
261 }