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