]> git.jsancho.org Git - lugaru.git/blob - Source/Account.cpp
bae2abda7bfcb45c29312e64da0c1ce6388f09ad
[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 void Account::add(const string& name)
53 {
54     accounts.push_back(new Account(name));
55     _active = accounts.back();
56 }
57
58 Account* Account::get(int i)
59 {
60     if ((i >= 0) && (i < int(accounts.size()))) {
61         return accounts[i];
62     } else
63         return NULL;
64 }
65
66 void Account::setActive(int i)
67 {
68     _active = get(i);
69 }
70
71 void Account::destroyActive()
72 {
73     for (unsigned i = 0; i < accounts.size(); i++) {
74         if (accounts[i] == _active) {
75             accounts.erase(accounts.begin() + i);
76             _active = nullptr;
77             return;
78         }
79     }
80     cerr << "Unexpected error : User " << _active->getName() << " not found" << endl;
81     if (accounts.empty()) {
82         _active = nullptr;
83     } else {
84         _active = accounts.front();
85     }
86 }
87
88 int Account::getDifficulty()
89 {
90     return difficulty;
91 }
92
93 void Account::endGame()
94 {
95     campaignProgress[currentCampaign].choices.clear();
96     campaignProgress[currentCampaign].score = 0;
97     campaignProgress[currentCampaign].time = 0;
98 }
99
100 void Account::winCampaignLevel(int choice, float score, float time)
101 {
102     campaignProgress[currentCampaign].choices.push_back(choice);
103     setCampaignScore(campaignProgress[currentCampaign].score + score);
104     campaignProgress[currentCampaign].time = time;
105 }
106
107 void Account::winLevel(int level, float score, float time)
108 {
109     if (!devtools) {
110         if (score > highscore[level])
111             highscore[level] = score;
112         if (time < fasttime[level] || fasttime[level] == 0)
113             fasttime[level] = time;
114     }
115     if (progress < level + 1)
116         progress = level + 1;
117 }
118
119 void Account::loadFile(string filename)
120 {
121     FILE *tfile;
122     int numaccounts;
123     int iactive;
124     errno = 0;
125
126     tfile = fopen(filename.c_str(), "rb" );
127
128     if (tfile) {
129         funpackf(tfile, "Bi", &numaccounts);
130         funpackf(tfile, "Bi", &iactive);
131         printf("number of accounts %d\n", numaccounts);
132         for (int i = 0; i < numaccounts; i++) {
133             printf("loading account %d/%d\n", i, numaccounts);
134             Account* acc = new Account();
135             funpackf(tfile, "Bi", &(acc->difficulty));
136             funpackf(tfile, "Bi", &(acc->progress));
137             int nbCampaigns;
138             funpackf(tfile, "Bi", &nbCampaigns);
139
140             for (int k = 0; k < nbCampaigns; ++k) {
141                 string campaignName = "";
142                 int t;
143                 char c;
144                 funpackf(tfile, "Bi",  &t);
145                 for (int j = 0; j < t; j++) {
146                     funpackf(tfile, "Bb",  &c);
147                     campaignName.append(1, c);
148                 }
149                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].time));
150                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].score));
151                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].fasttime));
152                 funpackf(tfile, "Bf", &(acc->campaignProgress[campaignName].highscore));
153                 int campaignchoicesmade, campaignchoice;
154                 funpackf(tfile, "Bi", &campaignchoicesmade);
155                 for (int j = 0; j < campaignchoicesmade; j++) {
156                     funpackf(tfile, "Bi", &campaignchoice);
157                     if (campaignchoice >= 10) { // what is that for?
158                         campaignchoice = 0;
159                     }
160                     acc->campaignProgress[campaignName].choices.push_back(campaignchoice);
161                 }
162             }
163
164             acc->currentCampaign = "";
165             int t;
166             char c;
167             funpackf(tfile, "Bi",  &t);
168             for (int i = 0; i < t; i++) {
169                 funpackf(tfile, "Bb",  &c);
170                 acc->currentCampaign.append(1, c);
171             }
172
173             funpackf(tfile, "Bf", &(acc->points));
174             for (int i = 0; i < 50; i++) {
175                 funpackf(tfile, "Bf", &(acc->highscore[i]));
176                 funpackf(tfile, "Bf", &(acc->fasttime[i]));
177             }
178             for (int i = 0; i < 60; i++) {
179                 funpackf(tfile, "Bb",  &(acc->unlocked[i]));
180             }
181             int temp;
182             char ctemp;
183             funpackf(tfile, "Bi",  &temp);
184             for (int i = 0; i < temp; i++) {
185                 funpackf(tfile, "Bb",  &ctemp);
186                 acc->name.append(1, ctemp);
187             }
188             if (!strcmp(acc->name.c_str(), ""))
189                 acc->name = "Lugaru Player"; // no empty player name security.
190             accounts.push_back(acc);
191         }
192
193         fclose(tfile);
194         _active = get(iactive);
195     } else {
196         perror(("Couldn't load users from " + filename).c_str());
197         _active = nullptr;
198     }
199 }
200
201 void Account::saveFile(string filename)
202 {
203     FILE *tfile;
204     errno = 0;
205
206     tfile = fopen(filename.c_str(), "wb" );
207     if (tfile) {
208         fpackf(tfile, "Bi", getNbAccounts());
209         fpackf(tfile, "Bi", indice(Account::_active));
210
211         for (int i = 0; i < getNbAccounts(); i++) {
212             Account* a = Account::get(i);
213             printf("writing account %d/%d (%s)\n", i + 1, getNbAccounts(), a->getName().c_str());
214             fpackf(tfile, "Bi", a->difficulty);
215             fpackf(tfile, "Bi", a->progress);
216             fpackf(tfile, "Bi", a->campaignProgress.size());
217
218             map<string, CampaignProgress>::const_iterator it;
219             for (it = a->campaignProgress.begin(); it != a->campaignProgress.end(); ++it) {
220                 fpackf(tfile, "Bi",  it->first.size());
221                 for (unsigned j = 0; j < it->first.size(); j++) {
222                     fpackf(tfile, "Bb",  it->first[j]);
223                 }
224                 fpackf(tfile, "Bf", it->second.time);
225                 fpackf(tfile, "Bf", it->second.score);
226                 fpackf(tfile, "Bf", it->second.fasttime);
227                 fpackf(tfile, "Bf", it->second.highscore);
228                 fpackf(tfile, "Bi", it->second.choices.size());
229                 for (unsigned j = 0; j < it->second.choices.size(); j++) {
230                     fpackf(tfile, "Bi", it->second.choices[j]);
231                 }
232             }
233
234             fpackf(tfile, "Bi", a->getCurrentCampaign().size());
235             for (unsigned j = 0; j < a->getCurrentCampaign().size(); j++) {
236                 fpackf(tfile, "Bb", a->getCurrentCampaign()[j]);
237             }
238
239             fpackf(tfile, "Bf", a->points);
240             for (unsigned j = 0; j < 50; j++) {
241                 fpackf(tfile, "Bf", a->highscore[j]);
242                 fpackf(tfile, "Bf", a->fasttime[j]);
243             }
244             for (unsigned j = 0; j < 60; j++) {
245                 fpackf(tfile, "Bb",  a->unlocked[j]);
246             }
247             fpackf(tfile, "Bi",  a->name.size());
248             for (unsigned j = 0; j < a->name.size(); j++) {
249                 fpackf(tfile, "Bb",  a->name[j]);
250             }
251         }
252
253         fclose(tfile);
254     } else {
255         perror(("Couldn't save users in " + filename).c_str());
256     }
257 }
258
259 int Account::indice(Account* a)
260 {
261     for (unsigned i = 0; i < accounts.size(); i++) {
262         if (accounts[i] == a)
263             return i;
264     }
265     return -1;
266 }