1 /*
2 * GNUjump
3 * =======
4 *
5 * Copyright (C) 2005-2008, Juan Pedro Bolivar Puente
6 *
7 * GNUjump 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 3 of the License, or
10 * (at your option) any later version.
11 *
12 * GNUjump 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 this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "gnujump.h"
22 #include "records.h"
23 #include "tools.h"
24
25 extern L_gblOptions gblOps;
26
27 int
loadRecords(char * fname,records_t * rtab)28 loadRecords (char *fname, records_t * rtab)
29 {
30 FILE *fh = NULL;
31 int i;
32
33 if ((fh = fopen (fname, "r")) == NULL)
34 {
35 printf (_
36 ("\nWARNING: Could not open records file (%s). I will create a new one later. \n"),
37 fname);
38 return FALSE;
39 }
40
41 for (i = 0; i < MAX_RECORDS; i++)
42 {
43 rtab[i].pname = getValue_charp (fh, "player_name");
44 rtab[i].floor = getValue_int (fh, "floor");
45 rtab[i].mode = getValue_charp (fh, "mode");
46 rtab[i].time = getValue_int (fh, "time");
47 getValue_str (fh, "date", rtab[i].date, NULL);
48 }
49
50 fclose (fh);
51 return TRUE;
52 }
53
54 int
writeRecords(char * fname,records_t * rtab)55 writeRecords (char *fname, records_t * rtab)
56 {
57 FILE *fh;
58 int i;
59
60 if ((fh = fopen (fname, "w")) == NULL)
61 {
62 printf (_
63 ("\nERROR: Error while opening records file (%s) for writing. \n"),
64 fname);
65 return FALSE;
66 }
67
68 putComment (fh, "This file has been automatically generated by SDLjump");
69 putLine (fh);
70
71 for (i = 0; i < MAX_RECORDS; i++)
72 {
73 putValue_str (fh, "player_name", rtab[i].pname);
74 putValue_int (fh, "floor", rtab[i].floor);
75 putValue_str (fh, "mode", rtab[i].mode);
76 putValue_int (fh, "time", rtab[i].time);
77 putValue_str (fh, "date", rtab[i].date);
78 putLine (fh);
79 }
80
81 fclose (fh);
82 return TRUE;
83 }
84
85 int
addRecord(records_t * rtab,records_t * rec,int pos)86 addRecord (records_t * rtab, records_t * rec, int pos)
87 {
88 int j;
89
90 pos--;
91 free (rtab[MAX_RECORDS - 1].pname);
92 free (rtab[MAX_RECORDS - 1].mode);
93 for (j = MAX_RECORDS - 1; j >= pos; j--)
94 {
95 rtab[j] = rtab[j - 1];
96 }
97
98 rtab[pos] = *rec;
99
100 return FALSE;
101 }
102
103 int
checkRecord(records_t * rtab,int floor,int time)104 checkRecord (records_t * rtab, int floor, int time)
105 {
106 int i;
107 for (i = 0; i < MAX_RECORDS; i++)
108 {
109 if (rtab[i].floor < floor)
110 {
111 return i + 1;
112 }
113 if (rtab[i].floor == floor)
114 {
115 if (time <= rtab[i].time)
116 return i + 1;
117 else
118 return i + 2;
119 }
120 }
121 return FALSE;
122 }
123
124 void
makeRecord(records_t * rec,char * name,int floor,int len)125 makeRecord (records_t * rec, char *name, int floor, int len)
126 {
127 struct tm *tims;
128 time_t timt;
129
130 rec->pname = NULL;
131 rec->pname = malloc (sizeof (char) * (strlen (name) + 1));
132 strcpy (rec->pname, name);
133 rec->floor = floor;
134
135 rec->mode = NULL;
136 rec->mode = malloc (sizeof (char) * 4);
137 if (gblOps.fps == FPS40)
138 {
139 rec->mode[0] = 'x';
140 }
141 else
142 rec->mode[0] = 's';
143
144 switch (gblOps.rotMode)
145 {
146 case ROTNONE:
147 rec->mode[1] = 'n';
148 break;
149 case ROTORIG:
150 rec->mode[1] = 'x';
151 break;
152 case ROTFULL:
153 rec->mode[1] = 's';
154 break;
155 default:
156 break;
157 }
158
159 switch (gblOps.scrollMode)
160 {
161 case HARDSCROLL:
162 rec->mode[2] = 'x';
163 break;
164 case SOFTSCROLL:
165 rec->mode[2] = 's';
166 default:
167 break;
168 }
169 rec->mode[3] = '\0';
170
171 rec->time = len;
172
173 timt = time (0);
174 tims = localtime (&timt);
175 strftime (rec->date, 64, "%H:%M %d/%m/%y", tims);
176 }
177
178 void
defaultRecords(records_t * rtab)179 defaultRecords (records_t * rtab)
180 {
181 int i;
182 for (i = 0; i < MAX_RECORDS; i++)
183 {
184 makeRecord (&rtab[i], "default", (MAX_RECORDS - i) * 3, rnd (100));
185 }
186 }
187
188 void
freeRecords(records_t * rtab)189 freeRecords (records_t * rtab)
190 {
191 int i;
192 for (i = 0; i < MAX_RECORDS; i++)
193 {
194 free (rtab[i].pname);
195 free (rtab[i].mode);
196 }
197 }
198