1 /*
2     XorGramana Copyright 2009 James W. Morris, james@jwm-art.net
3 
4     This file is part of XorGramana.
5 
6     XorGramana is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     XorGramana is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with XorGramana.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "scores.h"
20 #include "options.h"
21 #include "player.h"
22 
23 #include <stdio.h>
24 
25 ctr_t* scores=0;
26 
create_scores()27 void create_scores()
28 {
29     su_t n;
30     if(scores)
31         destroy_scores();
32     if((scores=malloc(sizeof(ctr_t)*MAX_LEVEL)))
33         for(n=0;n<MAX_LEVEL;n++)
34             scores[n]=(n<6?1000:2000);
35 }
36 
destroy_scores()37 void destroy_scores()
38 {
39     if(scores)
40         free(scores);
41 }
42 
load_scores()43 void load_scores()
44 {
45     FILE* fp=0;
46     char* fname=0;
47     if(!scores)
48         return;
49     if(!options->user_dir)
50         return;
51     if((!(fname=options_file_path(".xorcurses",options->user_dir))))
52         return;
53     if(!(fp=fopen(fname,"r"))){
54         free(fname);
55         write_scores();
56         return;
57     }
58     fread(scores,sizeof(ctr_t),MAX_LEVEL,fp);
59     fclose(fp);
60     free(fname);
61 }
62 
save_score(lvl_t level,ctr_t moves)63 void save_score(lvl_t level,ctr_t moves)
64 {
65     if(!scores)
66         return;
67     if(level<MIN_LEVEL||level>=MAX_LEVEL)
68         return;
69     if(moves>MAX_MOVES)
70         return;
71     if(scores[level]>moves){
72         scores[level]=moves;
73         write_scores();
74     }
75 }
76 
write_scores()77 void write_scores()
78 {
79     FILE* fp=0;
80     char* fname=0;
81     if(!scores)
82         return;
83     if(!options->user_dir)
84         return;
85     if((!(fname=options_file_path(".xorcurses",options->user_dir))))
86         return;
87     if(!(fp=fopen(fname,"w")))
88         return;
89     fwrite(scores,sizeof(ctr_t),MAX_LEVEL,fp);
90     fclose(fp);
91     free(fname);
92     return;
93 }
94