score.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* $NetBSD: score.c,v 1.17 2004/01/27 20:30:30 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: score.c,v 1.17 2004/01/27 20:30:30 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. # include "robots.h"
  39. # include "pathnames.h"
  40. const char *Scorefile = _PATH_SCORE;
  41. int Max_per_uid = MAX_PER_UID;
  42. static SCORE Top[MAXSCORES];
  43. static u_int32_t numscores, max_uid;
  44. static void read_score(int);
  45. static void write_score(int);
  46. /*
  47. * read_score:
  48. * Read the score file in MI format
  49. */
  50. static void
  51. read_score(inf)
  52. int inf;
  53. {
  54. SCORE *scp;
  55. if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid) {
  56. max_uid = ntohl(max_uid);
  57. read(inf, Top, sizeof Top);
  58. for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
  59. scp->s_uid = ntohl(scp->s_uid);
  60. scp->s_score = ntohl(scp->s_score);
  61. scp->s_auto = ntohl(scp->s_auto);
  62. scp->s_level = ntohl(scp->s_level);
  63. }
  64. }
  65. else {
  66. for (scp = Top; scp < &Top[MAXSCORES]; scp++)
  67. scp->s_score = 0;
  68. max_uid = Max_per_uid;
  69. }
  70. }
  71. /*
  72. * write_score:
  73. * Write the score file in MI format
  74. */
  75. static void
  76. write_score(inf)
  77. int inf;
  78. {
  79. SCORE *scp;
  80. lseek(inf, 0L, SEEK_SET);
  81. max_uid = htonl(max_uid);
  82. write(inf, &max_uid, sizeof max_uid);
  83. for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
  84. scp->s_uid = htonl(scp->s_uid);
  85. scp->s_score = htonl(scp->s_score);
  86. scp->s_auto = htonl(scp->s_auto);
  87. scp->s_level = htonl(scp->s_level);
  88. }
  89. write(inf, Top, sizeof Top);
  90. }
  91. /*
  92. * score:
  93. * Post the player's score, if reasonable, and then print out the
  94. * top list.
  95. */
  96. void
  97. score(score_wfd)
  98. int score_wfd;
  99. {
  100. int inf = score_wfd;
  101. SCORE *scp;
  102. u_int32_t uid;
  103. bool done_show = FALSE;
  104. Newscore = FALSE;
  105. if (inf < 0)
  106. return;
  107. read_score(inf);
  108. uid = getuid();
  109. if (Top[MAXSCORES-1].s_score <= Score) {
  110. numscores = 0;
  111. for (scp = Top; scp < &Top[MAXSCORES]; scp++)
  112. if ((scp->s_uid == uid && ++numscores == max_uid)) {
  113. if (scp->s_score > Score)
  114. break;
  115. scp->s_score = Score;
  116. scp->s_uid = uid;
  117. scp->s_auto = Auto_bot;
  118. scp->s_level = Level;
  119. set_name(scp);
  120. Newscore = TRUE;
  121. break;
  122. }
  123. if (scp == &Top[MAXSCORES]) {
  124. Top[MAXSCORES-1].s_score = Score;
  125. Top[MAXSCORES-1].s_uid = uid;
  126. Top[MAXSCORES-1].s_auto = Auto_bot;
  127. Top[MAXSCORES-1].s_level = Level;
  128. set_name(&Top[MAXSCORES-1]);
  129. Newscore = TRUE;
  130. }
  131. if (Newscore)
  132. qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
  133. }
  134. if (!Newscore) {
  135. Full_clear = FALSE;
  136. lseek(inf, 0, SEEK_SET);
  137. return;
  138. }
  139. else
  140. Full_clear = TRUE;
  141. move(1, 15);
  142. printw("%5.5s %5.5s %-9.9s %-8.8s %5.5s", "Rank", "Score", "User",
  143. " ", "Level");
  144. for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
  145. if (scp->s_score == 0)
  146. break;
  147. move((scp - Top) + 2, 15);
  148. if (!done_show && scp->s_uid == uid && scp->s_score == Score)
  149. standout();
  150. printw("%5ld %5d %-8.8s %-9.9s %5d",
  151. (long)(scp - Top) + 1, scp->s_score, scp->s_name,
  152. scp->s_auto ? "(autobot)" : "", scp->s_level);
  153. if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
  154. standend();
  155. done_show = TRUE;
  156. }
  157. }
  158. Num_scores = scp - Top;
  159. refresh();
  160. if (Newscore) {
  161. write_score(inf);
  162. }
  163. lseek(inf, 0, SEEK_SET);
  164. }
  165. void
  166. set_name(scp)
  167. SCORE *scp;
  168. {
  169. PASSWD *pp;
  170. static char unknown[] = "???";
  171. if ((pp = getpwuid(scp->s_uid)) == NULL)
  172. pp->pw_name = unknown;
  173. strncpy(scp->s_name, pp->pw_name, MAXNAME);
  174. }
  175. /*
  176. * cmp_sc:
  177. * Compare two scores.
  178. */
  179. int
  180. cmp_sc(s1, s2)
  181. const void *s1, *s2;
  182. {
  183. return ((const SCORE *)s2)->s_score - ((const SCORE *)s1)->s_score;
  184. }
  185. /*
  186. * show_score:
  187. * Show the score list for the '-s' option.
  188. */
  189. void
  190. show_score()
  191. {
  192. SCORE *scp;
  193. int inf;
  194. if ((inf = open(Scorefile, O_RDONLY)) < 0) {
  195. warn("opening `%s'", Scorefile);
  196. return;
  197. }
  198. read_score(inf);
  199. close(inf);
  200. inf = 1;
  201. printf("%5.5s %5.5s %-9.9s %-8.8s %5.5s\n", "Rank", "Score", "User",
  202. " ", "Level");
  203. for (scp = Top; scp < &Top[MAXSCORES]; scp++)
  204. if (scp->s_score > 0)
  205. printf("%5d %5d %-8.8s %-9.9s %5d\n",
  206. inf++, scp->s_score, scp->s_name,
  207. scp->s_auto ? "(autobot)" : "", scp->s_level);
  208. }