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