Rev 3150 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3150 | Rev 4348 | ||
|---|---|---|---|
| Line 53... | Line 53... | ||
| 53 | /* #include <fcntl.h> */ |
53 | /* #include <fcntl.h> */ |
| 54 | /* #include <pwd.h> */ |
54 | /* #include <pwd.h> */ |
| 55 | #include <stdio.h> |
55 | #include <stdio.h> |
| 56 | /* #include <stdlib.h> */ |
56 | /* #include <stdlib.h> */ |
| 57 | #include <string.h> |
57 | #include <string.h> |
| - | 58 | #include <kbd/kbd.h> |
|
| - | 59 | #include <kbd/keycode.h> |
|
| - | 60 | #include <stdlib.h> |
|
| 58 | /* #include <time.h> */ |
61 | /* #include <time.h> */ |
| 59 | /* #include <term.h> */ |
62 | /* #include <term.h> */ |
| 60 | /* #include <unistd.h> */ |
63 | /* #include <unistd.h> */ |
| 61 | /* #include <sys/param.h> */ |
64 | /* #include <sys/param.h> */ |
| 62 | /* #include <sys/stat.h> */ |
65 | /* #include <sys/stat.h> */ |
| Line 112... | Line 115... | ||
| 112 | /** Copy from hiscore table score with index src to dest |
115 | /** Copy from hiscore table score with index src to dest |
| 113 | * |
116 | * |
| 114 | */ |
117 | */ |
| 115 | static void copyhiscore(int dest, int src) |
118 | static void copyhiscore(int dest, int src) |
| 116 | { |
119 | { |
| 117 | strcpy(scores[dest].hs_name, scores[src].hs_name); |
120 | str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, |
| - | 121 | scores[src].hs_name); |
|
| 118 | scores[dest].hs_score = scores[src].hs_score; |
122 | scores[dest].hs_score = scores[src].hs_score; |
| 119 | scores[dest].hs_level = scores[src].hs_level; |
123 | scores[dest].hs_level = scores[src].hs_level; |
| 120 | } |
124 | } |
| 121 | 125 | ||
| 122 | void insertscore(int score, int level) |
126 | void insertscore(int score, int level) |
| 123 | { |
127 | { |
| 124 | int i,j; |
128 | int i,j; |
| 125 | int key; |
129 | size_t off; |
| 126 | 130 | kbd_event_t ev; |
|
| 127 | 131 | ||
| 128 | clear_screen(); |
132 | clear_screen(); |
| 129 | moveto(10 , 10); |
133 | moveto(10 , 10); |
| 130 | puts("Insert your name: "); |
134 | puts("Insert your name: "); |
| 131 | strncpy(scores[NUMSPOTS - 1].hs_name, "Player", MAXLOGNAME); |
135 | str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, |
| - | 136 | "Player"); |
|
| 132 | i = 6; |
137 | i = 6; off = 6; |
| 133 | 138 | ||
| 134 | moveto(10 , 28); |
139 | moveto(10 , 28); |
| 135 | printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................"); |
140 | printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................"); |
| - | 141 | ||
| - | 142 | while (1) { |
|
| 136 | key = getchar(); |
143 | fflush(stdout); |
| - | 144 | if (kbd_get_event(&ev) != EOK) |
|
| - | 145 | exit(1); |
|
| - | 146 | ||
| 137 | while(key != '\n') { |
147 | if (ev.type == KE_RELEASE) |
| - | 148 | continue; |
|
| - | 149 | ||
| - | 150 | if (ev.key == KC_ENTER || ev.key == KC_NENTER) |
|
| - | 151 | break; |
|
| - | 152 | ||
| 138 | if (key == '\b') { |
153 | if (ev.key == KC_BACKSPACE) { |
| 139 | if (i > 0) |
154 | if (i > 0) { |
| - | 155 | wchar_t uc; |
|
| - | 156 | ||
| - | 157 | --i; |
|
| - | 158 | while (off > 0) { |
|
| - | 159 | --off; |
|
| - | 160 | size_t otmp = off; |
|
| - | 161 | uc = str_decode(scores[NUMSPOTS - 1].hs_name, |
|
| - | 162 | &otmp, STR_BOUNDS(MAXLOGNAME) + 1); |
|
| - | 163 | if (uc != U_SPECIAL) |
|
| - | 164 | break; |
|
| - | 165 | } |
|
| - | 166 | ||
| 140 | scores[NUMSPOTS - 1].hs_name[--i] = '\0'; |
167 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; |
| - | 168 | } |
|
| 141 | } else { |
169 | } else if (ev.c != '\0') { |
| 142 | if (i < (MAXLOGNAME - 1)) |
170 | if (i < (MAXLOGNAME - 1)) { |
| 143 | scores[NUMSPOTS - 1].hs_name[i++] = key; |
171 | if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name, |
| - | 172 | &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) { |
|
| - | 173 | ++i; |
|
| - | 174 | } |
|
| 144 | scores[NUMSPOTS - 1].hs_name[i] = '\0'; |
175 | scores[NUMSPOTS - 1].hs_name[off] = '\0'; |
| - | 176 | } |
|
| 145 | } |
177 | } |
| 146 | 178 | ||
| 147 | moveto(10 , 28); |
179 | moveto(10 , 28); |
| 148 | printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................"); |
180 | printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................"); |
| 149 | - | ||
| 150 | key = getchar(); |
- | |
| 151 | } |
181 | } |
| 152 | 182 | ||
| 153 | scores[NUMSPOTS - 1].hs_score = score; |
183 | scores[NUMSPOTS - 1].hs_score = score; |
| 154 | scores[NUMSPOTS - 1].hs_level = level; |
184 | scores[NUMSPOTS - 1].hs_level = level; |
| 155 | 185 | ||
| Line 165... | Line 195... | ||
| 165 | 195 | ||
| 166 | void initscores(void) |
196 | void initscores(void) |
| 167 | { |
197 | { |
| 168 | int i; |
198 | int i; |
| 169 | for(i = 0; i < NUMSPOTS; i++) { |
199 | for(i = 0; i < NUMSPOTS; i++) { |
| 170 | strncpy(scores[i].hs_name, "HelenOS Team", MAXLOGNAME); |
200 | str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team"); |
| 171 | scores[i].hs_score = (NUMSPOTS - i) * 200; |
201 | scores[i].hs_score = (NUMSPOTS - i) * 200; |
| 172 | scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1); |
202 | scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1); |
| 173 | } |
203 | } |
| 174 | } |
204 | } |
| 175 | 205 | ||
| Line 259... | Line 289... | ||
| 259 | /* * Otherwise add new score at end (there is always room). */ |
289 | /* * Otherwise add new score at end (there is always room). */ |
| 260 | /* *\/ */ |
290 | /* *\/ */ |
| 261 | /* change = 0; */ |
291 | /* change = 0; */ |
| 262 | /* me = thisuser(); */ |
292 | /* me = thisuser(); */ |
| 263 | /* for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) { */ |
293 | /* for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) { */ |
| 264 | /* if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0) */ |
294 | /* if (sp->hs_level != level || str_cmp(sp->hs_name, me) != 0) */ |
| 265 | /* continue; */ |
295 | /* continue; */ |
| 266 | /* if (score > sp->hs_score) { */ |
296 | /* if (score > sp->hs_score) { */ |
| 267 | /* (void)printf("%s bettered %s %d score of %d!\n", */ |
297 | /* (void)printf("%s bettered %s %d score of %d!\n", */ |
| 268 | /* "\nYou", "your old level", level, */ |
298 | /* "\nYou", "your old level", level, */ |
| 269 | /* sp->hs_score * sp->hs_level); */ |
299 | /* sp->hs_score * sp->hs_level); */ |
| Line 387... | Line 417... | ||
| 387 | /* for (i = 0, sp = hs; i < num;) { */ |
417 | /* for (i = 0, sp = hs; i < num;) { */ |
| 388 | /* /\* */ |
418 | /* /\* */ |
| 389 | /* * This is O(n^2), but do you think we care? */ |
419 | /* * This is O(n^2), but do you think we care? */ |
| 390 | /* *\/ */ |
420 | /* *\/ */ |
| 391 | /* for (j = 0, pu = count; j < numnames; j++, pu++) */ |
421 | /* for (j = 0, pu = count; j < numnames; j++, pu++) */ |
| 392 | /* if (strcmp(sp->hs_name, pu->name) == 0) */ |
422 | /* if (str_cmp(sp->hs_name, pu->name) == 0) */ |
| 393 | /* break; */ |
423 | /* break; */ |
| 394 | /* if (j == numnames) { */ |
424 | /* if (j == numnames) { */ |
| 395 | /* /\* */ |
425 | /* /\* */ |
| 396 | /* * Add new user, set per-user count to 1. */ |
426 | /* * Add new user, set per-user count to 1. */ |
| 397 | /* *\/ */ |
427 | /* *\/ */ |
| Line 524... | Line 554... | ||
| 524 | /* * we only get one score per level. */ |
554 | /* * we only get one score per level. */ |
| 525 | /* *\/ */ |
555 | /* *\/ */ |
| 526 | /* if (me != NULL && */ |
556 | /* if (me != NULL && */ |
| 527 | /* sp->hs_level == level && */ |
557 | /* sp->hs_level == level && */ |
| 528 | /* sp->hs_score == score && */ |
558 | /* sp->hs_score == score && */ |
| 529 | /* strcmp(sp->hs_name, me) == 0) { */ |
559 | /* str_cmp(sp->hs_name, me) == 0) { */ |
| 530 | /* putpad(SOstr); */ |
560 | /* putpad(SOstr); */ |
| 531 | /* highlight = 1; */ |
561 | /* highlight = 1; */ |
| 532 | /* } */ |
562 | /* } */ |
| 533 | /* (void)printf("%s", buf); */ |
563 | /* (void)printf("%s", buf); */ |
| 534 | /* if (highlight) { */ |
564 | /* if (highlight) { */ |