forked from DusteDdk/Wizznic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.c
416 lines (350 loc) · 10.3 KB
/
stats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/************************************************************************
* This file is part of Wizznic. *
* Copyright 2009-2011 Jimmy Christensen <[email protected]> *
* Wizznic is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* Wizznic is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Foobar. If not, see <http://www.gnu.org/licenses/>. *
************************************************************************/
#include "strings.h"
#include "stats.h"
#include "pack.h"
#include "defs.h"
#include "player.h"
#include "list.h"
#include "settings.h"
#include "userfiles.h"
static stats_t st;
//Return ptr to stats_t
inline stats_t* stats()
{
return(&st);
}
//Sets some ptrs 0, that's all.
void statsInit()
{
memset( &st, 0, sizeof(stats_t) );
printf("statsInit(); Stats ready to be refreshed.\n");
}
//Tries to load stats
void statsLoad()
{
FILE* f;
char* buf = malloc(sizeof(char)*128);
char* bufb = malloc(sizeof(char)*128);
int i;
listItem* it;
hsEntry_t* hs;
hsEntry_t ths;
statsFileHeader_t sfh;
//Set progress -1 before reading the real progress from file (in case there is no file yet)
// -1 because progress is updated to current-level after the completion of that level, but
// at the same time, it is used (with +2) to set maxY in menu.
st.progress=-1;
//Free levelStats if allready filled
if(st.levelStats)
{
it = st.levelStats;
while( (it = it->next) )
{
free(it->data);
}
freeList( st.levelStats );
}
//Create new list for levelStats
st.levelStats = initList();
for(i=0; i < packState()->cp->numLevels; i++)
{
hs = malloc(sizeof(hsEntry_t));
//Set name 0, we're not going to use it for levelStats
memset( hs->name, 0, 11 );
//Set default "best"
hs->levelNum=i;
hs->score=0;
hs->time=9999;
hs->moves=9999;
hs->combos=0;
//Add to list
listAddData(st.levelStats, (void*)hs);
}
//Remove packwide highscores if they exist
if(st.packHsTable)
{
it=st.packHsTable;
while( (it=it->next) )
{
free(it->data);
}
freeList(st.packHsTable);
}
//Create new list for packWide highscores
st.packHsTable = initList();
//Cut pack name out of pack-path from last /
strcpy( buf, packState()->cp->path+charrpos(packState()->cp->path, '/')+1 );
//prepend the highscore path
sprintf(bufb, "%s/%s.hig", getHighscoreDir(), buf);
//Copy name to st.
if(!st.hsFn)
free(st.hsFn);
st.hsFn = malloc(sizeof(char)*strlen(bufb)+1);
strcpy(st.hsFn, bufb);
f=fopen(st.hsFn, "r");
if(f)
{
//Read the int and check that it's the version we expect.
fread( &i, sizeof(int),1,f);
if(i == STATS_FILE_FORMAT_VERSION )
{
//Jump to start of file again
rewind(f);
//Read the header
fread( (void*)(&sfh), sizeof(statsFileHeader_t), 1, f);
//Set progress
st.progress = sfh.progress;
//Override defaults for levels found in file
for(i=0; i < sfh.numLevelEntries; i++)
{
fread( (void*)(&ths), sizeof(hsEntry_t), 1, f );
//Find the list to put it in
hs = (hsEntry_t*)listGetItemData(st.levelStats,ths.levelNum);
//If it was found, copy it
if(hs)
{
memcpy( hs, &ths, sizeof(hsEntry_t) );
}
}
//Fill in the pack-wide highscore list
for(i=0; i < sfh.numHsEntries; i++)
{
hs=malloc(sizeof(hsEntry_t));
fread(hs,sizeof(hsEntry_t),1,f);
listAddData(st.packHsTable, (void*)hs);
}
} else {
printf("File '%s' is version %i but current version is %i, delete the file.\n", st.hsFn, i, STATS_FILE_FORMAT_VERSION);
}
fclose(f);
}
free(buf);
free(bufb);
}
void statsSave()
{
listItem* it;
hsEntry_t* hs;
//Check that there's a filename to write to (st is all 0's if not)
if(!st.hsFn)
{
printf("statsSave(); Fatal error; Called to save stats, but there's no filename to save.\n");
return;
}
//Open file
FILE* f = fopen(st.hsFn, "w");
if(f)
{
//Fill in header
statsFileHeader_t sfh;
sfh.hsFileVersion = STATS_FILE_FORMAT_VERSION;
sfh.progress = st.progress;
sfh.numLevelEntries = listSize( st.levelStats );
sfh.numHsEntries = listSize( st.packHsTable );
//Write header
fwrite( (void*)(&sfh), sizeof(statsFileHeader_t),1, f );
//Write levelstats
it=st.levelStats;
while( (it = it->next) )
{
hs=(hsEntry_t*)it->data;
//Write entry to file.
fwrite( (void*)(hs), sizeof(hsEntry_t), 1, f);
}
//Write packWide hstable
it = st.packHsTable;
while( (it = it->next) )
{
hs = (hsEntry_t*)it->data;
fwrite( (void*)(hs), sizeof(hsEntry_t), 1, f);
}
fclose(f);
} else {
printf("Error, couldn't open '%s' for writing.\n", st.hsFn);
}
}
//Figure out if this deserves an entry in the highscore table, update number of games played
void statsSubmitBest()
{
hsEntry_t* lvlHs = &player()->hsEntry;
statsSetLevel(lvlHs->levelNum);
lvlHs->time /= 1000;
//Update the campain-wide scores
player()->campStats.levelNum = player()->level;
player()->campStats.score += player()->hsEntry.score;
player()->campStats.time += lvlHs->time;
player()->campStats.moves += lvlHs->moves;
player()->campStats.combos += lvlHs->combos;
statsUpload(player()->level, lvlHs->time, lvlHs->moves,lvlHs->combos,lvlHs->score, "complete",0, NULL);
//Update progress, if any.
if( lvlHs->levelNum > st.progress )
st.progress=lvlHs->levelNum;
/* printf("----\nstatsSubmitBest; Current best stats for level %i\n"
"Score: %i\n"
"Time: %i\n"
"Moves: %i\n"
"Combos: %i\n", st.cl->levelNum,st.cl->score,
st.cl->time,
st.cl->moves,
st.cl->combos);*/
//Now override the stats that are better in hs.
if(lvlHs->score > st.cl->score)
{
st.cl->score = lvlHs->score;
}
if(lvlHs->time < st.cl->time)
{
st.cl->time=lvlHs->time;
}
if(lvlHs->moves < st.cl->moves)
{
st.cl->moves = lvlHs->moves;
}
if(lvlHs->combos > st.cl->combos)
{
st.cl->combos = lvlHs->combos;
}
statsSave();
}
void statsSetLevel(int l)
{
st.cl=listGetItemData(st.levelStats, l);
}
void statsDrawHs(SDL_Surface* screen)
{
listItem* it = stats()->packHsTable;
hsEntry_t* h;
int i=0;
char buf[64];
txtWriteCenter(screen, FONTMEDIUM,"Highscores!", HSCREENW, HSCREENH-105);
txtWriteCenter(screen, FONTSMALL, packState()->cp->name, HSCREENW, HSCREENH-80);
txtWriteCenter(screen, FONTSMALL, "L Name Score Time Move Combo", HSCREENW,HSCREENH-50);
txtWriteCenter(screen, FONTSMALL, "+-+---------+------+-----+----+----", HSCREENW,HSCREENH-40);
while( (it=it->next) )
{
h= (hsEntry_t*)it->data;
sprintf(buf, "%-2i%-10s%-7i%2i:%-3i%-5i%-5i", h->levelNum, h->name,h->score, h->time/60, h->time%60, h->moves, h->combos);
txtWriteCenter(screen, FONTSMALL, buf, HSCREENW,(HSCREENH-30)+(i*12));
i++;
}
}
//Return score place (1 = first) or 0 if score place > 10
int statsIsHighScore()
{
int place=1;
hsEntry_t* hs;
listItem* it = st.packHsTable;
if( player()->campStats.score < 1000 ) return(0); //No highscores if under 10k
while( (it=it->next) )
{
hs=(hsEntry_t*)it->data;
if( player()->campStats.score > hs->score )
{
return(place);
}
place++;
}
if(place>10)
place=0;
return(place);
}
//Add to list
void statsSaveHighScore()
{
int place = statsIsHighScore();
int p=0;
listItem* it = st.packHsTable;
hsEntry_t* hs;
//Copy from player struct.
hs = malloc(sizeof(hsEntry_t));
memcpy( hs, &(player()->campStats), sizeof(hsEntry_t) );
listInsertData(st.packHsTable, (void*)hs, place-1); //-1 since listInsert expects 0 to be the first position, but statsIsHigh returns >0 to be a valid highscore.
//Trim the list if there are more than 10.
while( (it=it->next) )
{
p++;
if(p>10)
{
it=listRemoveItem(st.packHsTable,it);
}
}
//Save to file.
statsSave();
//Clear player.
initPlayer();
}
#if !defined (GP2X) || !defined (WIZ)
#include <SDL/SDL_thread.h>
static char curlbuf[2048];
typedef struct thrDat
{ char* cmd;
int* ret;
} thrDat_t;
int upStatsThread(void * d)
{
char pBuf[128];
memset(pBuf, 0, sizeof(pBuf));
FILE *pipe;
thrDat_t* dat = (thrDat_t*)d;
char* cmd = dat->cmd;
int* ret = dat->ret;
if( (pipe = popen( cmd, "r" )) != NULL )
{
setting()->online=1;
if( fgets( pBuf, 127, pipe)!= NULL )
{
printf("Server: (%p) ='%s'\n",ret,pBuf);
if(ret)
{
*ret=atoi(pBuf);
}
}
pclose(pipe);
} else {
setting()->online=0;
}
free(dat->cmd);
free(dat);
return(0);
}
#endif
void statsUpload(int level, int time, int moves, int combos, int score, const char* action, int ignoreIfOnline, int* retVal)
{
#if !defined (GP2X) || !defined (WIZ)
if( (setting()->online || ignoreIfOnline) )
{
printf("StatsUpload with session = %i\n",setting()->session );
int b = sprintf( curlbuf, "%s\"version=%s&pack=%s&level=%i&time=%i&moves=%i&combos=%i&score=%i&action=%s&session=%i&platform=%s\"",\
CURLBIN, VERSION_STRING, packState()->cp->path,\
level,time,moves,combos,score,action, setting()->session, PLATFORM );
if(b > 0 && b < 2048)
{
thrDat_t* thrData=malloc(sizeof(thrDat_t));
thrData->cmd=malloc(strlen(curlbuf)+1);
strcpy(thrData->cmd,curlbuf);
thrData->ret=retVal;
if( SDL_CreateThread( upStatsThread, (void*)thrData ) == NULL )
{
printf("Warning: Coulnd't start thread: %s\n", SDL_GetError());
}
} else {
printf("Errir; printf returned %i\n", b);
}
}
#endif
}