00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include <errno.h>
00053 #include <stdio.h>
00054 #include <str.h>
00055 #include <io/console.h>
00056 #include <io/keycode.h>
00057 #include <vfs/vfs.h>
00058 #include <stdlib.h>
00059 #include <fcntl.h>
00060 #include <err.h>
00061 #include <time.h>
00062
00063 #include "screen.h"
00064 #include "tetris.h"
00065 #include "scores.h"
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 #define NUMSPOTS (MAXHISCORES + 1)
00077 #define NLEVELS (MAXLEVEL + 1)
00078
00079 static struct highscore scores[NUMSPOTS];
00080
00084 static void copyhiscore(int dest, int src)
00085 {
00086 str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
00087 scores[src].hs_name);
00088 scores[dest].hs_score = scores[src].hs_score;
00089 scores[dest].hs_level = scores[src].hs_level;
00090 }
00091
00092 void showscores(int firstgame)
00093 {
00094 int i;
00095
00096 clear_screen();
00097 moveto(10, 0);
00098 printf("\tRank \tLevel \tName\t points\n");
00099 printf("\t========================================================\n");
00100
00101 for (i = 0; i < NUMSPOTS - 1; i++)
00102 printf("\t%6d %6d %-16s %20d\n",
00103 i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score);
00104
00105 if (!firstgame) {
00106 printf("\t========================================================\n");
00107 printf("\t Last %6d %-16s %20d\n",
00108 scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
00109 }
00110
00111 printf("\n\n\n\n\tPress any key to return to main menu.");
00112 getchar();
00113 }
00114
00115 void insertscore(int score, int level)
00116 {
00117 int i;
00118 int j;
00119 size_t off;
00120 console_event_t ev;
00121
00122 clear_screen();
00123 moveto(10, 10);
00124 puts("Insert your name: ");
00125 str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
00126 "Player");
00127 i = 6;
00128 off = 6;
00129
00130 moveto(10 , 28);
00131 printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i,
00132 "........................................");
00133
00134 while (1) {
00135 fflush(stdout);
00136 if (!console_get_event(fphone(stdin), &ev))
00137 exit(1);
00138
00139 if (ev.type == KEY_RELEASE)
00140 continue;
00141
00142 if (ev.key == KC_ENTER || ev.key == KC_NENTER)
00143 break;
00144
00145 if (ev.key == KC_BACKSPACE) {
00146 if (i > 0) {
00147 wchar_t uc;
00148
00149 --i;
00150 while (off > 0) {
00151 --off;
00152 size_t otmp = off;
00153 uc = str_decode(scores[NUMSPOTS - 1].hs_name,
00154 &otmp, STR_BOUNDS(MAXLOGNAME) + 1);
00155 if (uc != U_SPECIAL)
00156 break;
00157 }
00158
00159 scores[NUMSPOTS - 1].hs_name[off] = '\0';
00160 }
00161 } else if (ev.c != '\0') {
00162 if (i < (MAXLOGNAME - 1)) {
00163 if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name,
00164 &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
00165 ++i;
00166 }
00167 scores[NUMSPOTS - 1].hs_name[off] = '\0';
00168 }
00169 }
00170
00171 moveto(10, 28);
00172 printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
00173 "........................................");
00174 }
00175
00176 scores[NUMSPOTS - 1].hs_score = score;
00177 scores[NUMSPOTS - 1].hs_level = level;
00178
00179 i = NUMSPOTS - 1;
00180 while ((i > 0) && (scores[i - 1].hs_score < score))
00181 i--;
00182
00183 for (j = NUMSPOTS - 2; j > i; j--)
00184 copyhiscore(j, j-1);
00185
00186 copyhiscore(i, NUMSPOTS - 1);
00187 }
00188
00189 void initscores(void)
00190 {
00191 int i;
00192 for (i = 0; i < NUMSPOTS; i++) {
00193 str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
00194 scores[i].hs_score = (NUMSPOTS - i) * 200;
00195 scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1);
00196 }
00197 }
00198
00199 int loadscores(void)
00200 {
00201 FILE *f;
00202 size_t cnt;
00203 int rc;
00204
00205 f = fopen("/data/tetris.sco", "rb");
00206 if (f == NULL)
00207 return ENOENT;
00208
00209 cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f);
00210 rc = fclose(f);
00211
00212 if (cnt != NUMSPOTS || rc != 0)
00213 return EIO;
00214
00215 return EOK;
00216 }
00217
00218 void savescores(void)
00219 {
00220 FILE *f;
00221 size_t cnt;
00222 int rc;
00223
00224 f = fopen("/data/tetris.sco", "wb");
00225 cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f);
00226 rc = fclose(f);
00227
00228 if (cnt != NUMSPOTS || rc != 0)
00229 printf("Error saving score table\n");
00230 }
00231