summaryrefslogtreecommitdiffstats
path: root/highscore.c
blob: 9c523743f93d8c76a501da43d70a2129198e0204 (plain)
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
/*
 * highscore.c
 *
 * Handle uploads and downloads to the highscore database
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "grv.h"
#include "highscore.h"
#include "network.h"

static const char score_server[] = "grvscore.zytor.com";
static int score_dl_port = 22392;
static int score_ul_port = 22393;

static const char score_file[] = "grvscore.dat";

void highscore_load(void)
{
  FILE *f;

  f = fopen(score_file, "r");
  if ( !f )
    return;

  highscore_parse(f, 1);
  fclose(f);
}

void highscore_download(void)
{
  FILE *n;

  if ( opt.nonet )
    return;

  n = fopen_network(score_server, score_dl_port, 0);
  if ( !n )
    return;

  highscore_parse(n, 0);
  fclose(n);

  highscore_save();
}

void highscore_save(void)
{
  FILE *f;
  
  f = fopen(score_file, "w");
  if ( !f )
    return;

  highscore_write(f, 1);
  fclose(f);
}

void highscore_upload(void)
{
  FILE *n;

  if ( opt.nonet )
    return;

  
  if ( !have_upload_scores() )
    return;

  
  n = fopen_network(score_server, score_ul_port, 1);
  if ( !n )
    return;

  highscore_write(n, 0);
  fclose(n);
}

/*
 * Allow the user to enter the name for the high score
 */
static void enter_name(int rank, char *player)
{
  static const unsigned char *symbols[3] = {
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ\x8f\x8e\x99""0123456789?",
    "\x01\x02\x0e\x0f\x10\x11\x18\x19._-/:;+$%&^!*@=,'`~<>()\xae\xaf\"\x80\x90\x92\x9a\xed\xa5",
    "\x13\xe4\xea\xef\xb1\xf9\xcd\xfe\xeb\xf1\xec\xe5  SPACE    DELETE    ENTER  "
  };
  int x, y, x1, y1;
  int ly, ry, vy;
  SDL_KeyboardEvent *ke;
  int len = 0;
  char sbuf[2];
  char *s;

  sbuf[1] = '\0';

  SDL_EnableKeyRepeat(250, 33);

  color(0,gp.c);
  lprint(15,11,"ENTER YOUR INITIALS!");

  color(0,gp.c2);
  lprint(17,1,symbols[0]);
  lprint(18,1,symbols[1]);
  lprint(19,1,symbols[2]);

  x = 0;
  y = 0;
  while ( 1 ) {
    if ( x == 2 && y >= 12 ) {
      if ( y >= 12 && y <= 20 ) {
	y = 12; ly = 11; vy = 16; ry = 21;
	s = "  SPACE  ";
	sbuf[0] = ' ';
      } else if ( y >= 21 && y <= 30 ) {
	y = 21; ly = 12; vy = 25; ry = 31;
	s = "  DELETE  ";
      } else {
	y = 31; ly = 21; vy = 35; ry = 31;
	s = "  ENTER  ";
      }
    } else {
      ly = max(0,y-1); vy = y; ry = min(39,y+1);
      sbuf[0] = symbols[x][y];
      s = sbuf;
    }

    color(15,gp.c2);
    lprint(x+17,y+1,s);

    x1 = x;  y1 = y;

    do {
      ke = get_key();

      if ( ke->keysym.sym == kbd_keys[0] ) {
	x = max(0,x-1);
	if ( x != x1 ) y = vy;
      } else if ( ke->keysym.sym == kbd_keys[1] ) {
	y = ly;
      } else if ( ke->keysym.sym == kbd_keys[2] ) {
	y = ry;
      } else if ( ke->keysym.sym == kbd_keys[3] ) {
	x = min(2,x+1);
	if ( x != x1 ) y = vy;
      } else if ( ke->keysym.sym == SDLK_RETURN ) {
	if ( x == 2 && y == 21 ) {
	  if ( len > 0 ) {
	    len--;
	    color(0,gp.c2);
	    lprint(rank+3,len+23,".");
	  }
	} else if ( x == 2 && y == 31 ) {
	  if ( len == 9 ) {
	    player[len] = '\0';
	    color(0,gp.c2);
	    lprint(rank+3,23,player);
	    return;
	  }
	} else {
	  if ( len < 9 ) {
	    player[len] = sbuf[0];
	    color(15,gp.c2);
	    lprint(rank+3,len+23,sbuf);
	    len++;
	  }
	}
      }
    } while ( x == x1 && y == y1 );

    color(0,gp.c2);
    lprint(x1+17,y1+1,s);
  }
}


/*
 * Display highscore screen; optionally show the user rank
 * and (if <= 10) let the user enter name
 */

void highscore_show(int myrank, int save)
{
  int i, x, y;

  levelscreen();

  color(0,gp.c);
  lprint(1,15,"THE TOP TEN");

  for ( i = 0 ; i < 10 ; i++ ) {
    if ( bests.total[i].score )
      lprintf(i+4,4,"%2d  %-13" PRId64 "  %-9.9s  (%2d)",
	      i+1, bests.total[i].score,
	      bests.total[i].player,
	      bests.total[i].endlvl+1);

    if ( i+1 == myrank ) {
      color(0,gp.c2);
      lprint(i+4,23,".........");
      color(0,gp.c);
    }
  }

  if ( myrank > 0 && myrank <= 10 )
    enter_name(myrank, bests.total[myrank-1].player);

  /* This can't be done until we have let the user enter name,
     and this is as good of a place as any, especially since
     there is no blink on the screen right now... */
  if (save)
    highscore_save();

  highscore_upload();

  color(0,gp.c);
  lprint(21,8,"Please press any key . . .");
  get_key();

  initscreen(80);

  x = 1; y = 1;
  for ( i = 10 ; i < 100 ; i++ ) {
    if ( i+1 == myrank )
      color(16,gp.c2);
    else
      color(0,gp.c);

    /* Don't use %3d; we only want the highlight bar to wrap the text,
       not spill over.  Therefore we need to adjust the starting position
       to make the numbers line up... */
    if ( bests.total[i].score )
      lprintf(x,y+(i < 99), " %d  %" PRId64 " ", i+1, bests.total[i].score);

    x++;
    if ( x > 23 ) {
      x = 1;
      y += 20;
    }
  }

  color(0,gp.c);
  lprintf(25,28,"Please press any key . . .");
  get_key();
}

/*
 * Call at end of game to show and possibly enter highscore
 * Note: always upload even if this wasn't a highscore -- we might have
 * entries in our database from old games
 */
void highscore_endgame(void)
{
  int myrank;

  highscore_download();

  if ( gp.Cheat )
    myrank = 0;
  else
    myrank = highscore_add_total(gp.gameid, gp.Sc, gp.MLev, NULL, 1);

  highscore_show(myrank, 1);
}