-
Notifications
You must be signed in to change notification settings - Fork 2
/
c4a.c
303 lines (288 loc) · 7.76 KB
/
c4a.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
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
Alternatively, the contents of this file may be used under the terms
of the GNU Lesser General Public license (the "LGPL License"), in which case the
provisions of LGPL License are applicable instead of those
above.
For feedback and questions about my Files and Projects please mail me,
Alexander Matthes (Ziz) , zizsdl_at_googlemail.com
*/
#include "c4a.h"
#include "settings.h"
#define TIME_OUT 15000
char c4aStatus[256] = "";
//int c4aFade;
spNetC4AScorePointer raceScore=NULL,pointsScore=NULL,survivalScore=NULL;
spNetC4AProfilePointer profile;
int c4aState = 0;
int c4aErrorTimeOut = 0;
void init_c4a()
{
spInitNet();
profile = spNetC4AGetProfile();
if (profile == NULL)
{
sprintf(c4aStatus,"C4A-Manager or Mini-C4A not installed.");
c4aErrorTimeOut = 20000;
c4aState = -3;
}
updateScores();
}
void updateScores()
{
if (profile == NULL)
return;
int status = spNetC4AGetStatus();
if (status > 0)
spNetC4ACancelTask();
spNetC4ADeleteScores(&pointsScore);
spNetC4ADeleteScores(&survivalScore);
spNetC4ADeleteScores(&raceScore);
c4aState = 1; //update
spNetC4AGetScore(&pointsScore,profile,"puzzletube_points",TIME_OUT);
}
int score_col = 0;
int score_row = 0;
void init_score_commit()
{
switch (score_col)
{
case 0:
if (get_highscore(0,1,score_row) == 0 || spNetC4ACommitScore(profile,"puzzletube_points",get_highscore(0,1,score_row),&pointsScore,TIME_OUT))
printf("Points score %i already at server\n",get_highscore(0,1,score_row));
break;
case 1:
if (get_highscore(1,1,score_row) == 0 || spNetC4ACommitScore(profile,"puzzletube_survival",get_highscore(1,1,score_row)*100,&survivalScore,TIME_OUT))
printf("Survival score %i already at server\n",get_highscore(1,1,score_row));
break;
case 2:
if (get_highscore(2,1,score_row) == 10000 || spNetC4ACommitScore(profile,"puzzletube_race",get_highscore(2,1,score_row)*100,&raceScore,TIME_OUT))
printf("Race score %i already at server\n",get_highscore(2,1,score_row));
break;
}
}
void init_one_score_commit(int gametype,int score)
{
if (settings_get_color() == 0)
return;
score_col = gametype;
switch (gametype)
{
case 0:
if (spNetC4ACommitScore(profile,"puzzletube_points",score,&pointsScore,TIME_OUT))
printf("Points score %i already at server\n",score);
else
c4aState = 5;
break;
case 1:
if (spNetC4ACommitScore(profile,"puzzletube_survival",score*100,&survivalScore,TIME_OUT))
printf("Survival score %i already at server\n",score);
else
c4aState = 5;
break;
case 2:
if (spNetC4ACommitScore(profile,"puzzletube_race",score*100,&raceScore,TIME_OUT))
printf("Race score %i already at server\n",score);
c4aState = 5;
break;
}
}
void send_c4a_scores(int timeout_sec,int timeout_dec)
{
switch (spNetC4AGetStatus())
{
case SP_C4A_PROGRESS:
switch (score_col)
{
case 0:
sprintf(c4aStatus,"Commit points scores. %i.%is",timeout_sec,timeout_dec);
break;
case 1:
sprintf(c4aStatus,"Commit survival scores. %i.%is",timeout_sec,timeout_dec);
break;
case 2:
sprintf(c4aStatus,"Commit race scores. %i.%is",timeout_sec,timeout_dec);
break;
}
break;
case SP_C4A_ERROR:
sprintf(c4aStatus,"Error with connection");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_TIMEOUT:
sprintf(c4aStatus,"Connection timeout");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_OK:
score_row++;
if (score_row >= 3)
{
score_row = 0;
score_col++;
if (score_col >= 3)
{
c4aState = 0;
break;
}
}
init_score_commit();
c4aState = 4;
break;
}
}
void send_one_c4a_scores(int timeout_sec,int timeout_dec)
{
switch (spNetC4AGetStatus())
{
case SP_C4A_PROGRESS:
switch (score_col)
{
case 0:
sprintf(c4aStatus,"Commit points scores. %i.%is",timeout_sec,timeout_dec);
break;
case 1:
sprintf(c4aStatus,"Commit survival scores. %i.%is",timeout_sec,timeout_dec);
break;
case 2:
sprintf(c4aStatus,"Commit race scores. %i.%is",timeout_sec,timeout_dec);
break;
}
break;
case SP_C4A_ERROR:
sprintf(c4aStatus,"Error with connection");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_TIMEOUT:
sprintf(c4aStatus,"Connection timeout");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_OK:
c4aState = 0;
break;
}
}
void calc_c4a(int steps)
{
int timeout_sec;
int timeout_dec;
if (c4aErrorTimeOut > 0)
c4aErrorTimeOut -= steps;
if (profile == NULL)
return;
if (c4aState > 0)
{
timeout_sec = spNetC4AGetTimeOut() / 1000;
timeout_dec = (spNetC4AGetTimeOut() / 100) % 10;
}
switch (c4aState)
{
case 1:
switch (spNetC4AGetStatus())
{
case SP_C4A_PROGRESS:
sprintf(c4aStatus,"Loading points highscore. %i.%is",timeout_sec,timeout_dec);
break;
case SP_C4A_ERROR:
sprintf(c4aStatus,"Error with connection");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_TIMEOUT:
sprintf(c4aStatus,"Connection timeout");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_OK:
spNetC4AGetScore(&survivalScore,profile,"puzzletube_survival",TIME_OUT);
c4aState = 2;
break;
}
break;
case 2:
switch (spNetC4AGetStatus())
{
case SP_C4A_PROGRESS:
sprintf(c4aStatus,"Loading survival highscore. %i.%is",timeout_sec,timeout_dec);
break;
case SP_C4A_ERROR:
sprintf(c4aStatus,"Error with connection");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_TIMEOUT:
sprintf(c4aStatus,"Connection timeout");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_OK:
spNetC4AGetScore(&raceScore,profile,"puzzletube_race",TIME_OUT);
c4aState = 3;
break;
}
break;
case 3:
switch (spNetC4AGetStatus())
{
case SP_C4A_PROGRESS:
sprintf(c4aStatus,"Loading race highscore. %i.%is",timeout_sec,timeout_dec);
break;
case SP_C4A_ERROR:
sprintf(c4aStatus,"Error with connection");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_TIMEOUT:
sprintf(c4aStatus,"Connection timeout");
c4aErrorTimeOut = 20000;
c4aState = -1;
break;
case SP_C4A_OK:
score_row = 0;
score_col = 0;
init_score_commit();
c4aState = 4;
break;
}
break;
case 4:
send_c4a_scores(timeout_sec,timeout_dec);
break;
case 5:
send_one_c4a_scores(timeout_sec,timeout_dec);
break;
}
}
void draw_c4a()
{
if (c4aState == 0)
return;
if (spNetC4AGetTimeOut() <= 0 && c4aErrorTimeOut <= 0)
return;
spFontPointer small_font = settings_get_small_font();
int engineWindowX=spGetWindowSurface()->w;
int engineWindowY=spGetWindowSurface()->h;
spFontDraw(1,engineWindowY-(small_font->maxheight),-1,c4aStatus,small_font);
}
void quit_c4a()
{
int status = spNetC4AGetStatus();
if (status != SP_C4A_OK && status != SP_C4A_ERROR)
spNetC4ACancelTask();
if (profile == NULL)
return;
spNetC4AFreeProfile(profile);
spNetC4ADeleteScores(&pointsScore);
spNetC4ADeleteScores(&survivalScore);
spNetC4ADeleteScores(&raceScore);
}