#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <term.h>
#include <time.h>
#include <unistd.h>
#include "scores.h"
#include "screen.h"
#include "tetris.h"
#define NUMSPOTS (MAXHISCORES + 1)
#define NLEVELS (MAXLEVEL + 1)
static time_t now;
static int nscores;
static int gotscores;
static struct highscore scores[NUMSPOTS];
static int checkscores(struct highscore *, int);
static int cmpscores(const void *, const void *);
static void getscores(FILE **);
static void printem(int, int, struct highscore *, int, const char *);
static char *thisuser(void);
static void
getscores(FILE **fpp)
{
int sd, mint, i;
char *mstr, *human;
FILE *sf;
if (fpp != NULL) {
mint = O_RDWR | O_CREAT;
mstr = "r+";
human = "read/write";
*fpp = NULL;
} else {
mint = O_RDONLY;
mstr = "r";
human = "reading";
}
sd = open(scorepath, mint, 0666);
if (sd == -1) {
if (fpp == NULL) {
nscores = 0;
return;
}
err(1, "cannot open %s for %s", scorepath, human);
}
if ((sf = fdopen(sd, mstr)) == NULL)
err(1, "cannot fdopen %s for %s", scorepath, human);
nscores = fread(scores, sizeof(scores[0]), MAXHISCORES, sf);
if (ferror(sf))
err(1, "error reading %s", scorepath);
for (i = 0; i < nscores; i++)
if (scores[i].hs_level < MINLEVEL ||
scores[i].hs_level > MAXLEVEL)
errx(1, "scorefile %s corrupt", scorepath);
if (fpp)
*fpp = sf;
else
(void)fclose(sf);
}
void
savescore(int level)
{
struct highscore *sp;
int i;
int change;
FILE *sf;
const char *me;
getscores(&sf);
gotscores = 1;
(void)time(&now);
change = 0;
me = thisuser();
for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) {
if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0)
continue;
if (score > sp->hs_score) {
(void)printf("%s bettered %s %d score of %d!\n",
"\nYou", "your old level", level,
sp->hs_score * sp->hs_level);
sp->hs_score = score;
sp->hs_time = now;
change = 1;
} else if (score == sp->hs_score) {
(void)printf("%s tied %s %d high score.\n",
"\nYou", "your old level", level);
sp->hs_time = now;
change = 1;
}
break;
}
if (i >= nscores) {
strlcpy(sp->hs_name, me, sizeof sp->hs_name);
sp->hs_level = level;
sp->hs_score = score;
sp->hs_time = now;
nscores++;
change = 1;
}
if (change) {
nscores = checkscores(scores, nscores);
if (fseek(sf, 0L, SEEK_SET) == -1)
err(1, "fseek");
if (fwrite(scores, sizeof(*sp), nscores, sf) != nscores ||
fflush(sf) == EOF)
warnx("error writing scorefile: %s\n\t-- %s",
strerror(errno),
"high scores may be damaged");
}
(void)fclose(sf);
}
static char *
thisuser(void)
{
const char *p;
static char u[sizeof(scores[0].hs_name)];
if (u[0])
return (u);
p = getenv("LOGNAME");
if (p == NULL || *p == '\0')
p = getenv("USER");
if (p == NULL || *p == '\0')
p = getlogin();
if (p == NULL || *p == '\0')
p = " ???";
strlcpy(u, p, sizeof(u));
return (u);
}
static int
cmpscores(const void *x, const void *y)
{
const struct highscore *a, *b;
long l;
a = x;
b = y;
l = (long)b->hs_level * b->hs_score - (long)a->hs_level * a->hs_score;
if (l < 0)
return (-1);
if (l > 0)
return (1);
if (a->hs_time < b->hs_time)
return (-1);
if (a->hs_time > b->hs_time)
return (1);
return (0);
}
static int
checkscores(struct highscore *hs, int num)
{
struct highscore *sp;
int i, j, k, nrnames;
int levelfound[NLEVELS];
struct peruser {
char *name;
int times;
} count[NUMSPOTS];
struct peruser *pu;
qsort((void *)hs, nscores, sizeof(*hs), cmpscores);
for (i = MINLEVEL; i < NLEVELS; i++)
levelfound[i] = 0;
nrnames = 0;
for (i = 0, sp = hs; i < num;) {
for (j = 0, pu = count; j < nrnames; j++, pu++)
if (strcmp(sp->hs_name, pu->name) == 0)
break;
if (j == nrnames) {
pu->name = sp->hs_name;
pu->times = 1;
nrnames++;
} else {
if ((pu->times < MAXSCORES &&
sp->hs_time + EXPIRATION >= now) ||
levelfound[sp->hs_level] == 0)
pu->times++;
else {
num--;
for (k = i; k < num; k++)
hs[k] = hs[k + 1];
continue;
}
}
levelfound[sp->hs_level] = 1;
i++, sp++;
}
return (num > MAXHISCORES ? MAXHISCORES : num);
}
void
showscores(int level)
{
struct highscore *sp;
int i, n, c;
const char *me;
int levelfound[NLEVELS];
if (!gotscores)
getscores((FILE **)NULL);
(void)printf("\n\t\t Tetris High Scores\n");
me = level && SOstr ? thisuser() : NULL;
for (i = MINLEVEL; i < NLEVELS; i++)
levelfound[i] = 0;
for (i = 0, sp = scores; i < nscores; i++, sp++) {
if (levelfound[sp->hs_level])
sp->hs_time = 0;
else {
sp->hs_time = 1;
levelfound[sp->hs_level] = 1;
}
}
for (i = 0, sp = scores; i < nscores; sp += n) {
n = 20;
if (i + n > nscores)
n = nscores - i;
printem(level, i + 1, sp, n, me);
if ((i += n) < nscores) {
(void)printf("\nHit RETURN to continue.");
(void)fflush(stdout);
while ((c = getchar()) != '\n')
if (c == EOF)
break;
(void)printf("\n");
}
}
if (nscores == 0)
printf("\t\t\t - none to date.\n");
}
static void
printem(int level, int offset, struct highscore *hs, int n, const char *me)
{
struct highscore *sp;
int row, highlight, i;
char buf[100];
#define TITLE "Rank Score Name (points/level)"
#define TITL2 "=========================================================="
printf("%s\n%s\n", TITLE, TITL2);
highlight = 0;
for (row = 0; row < n; row++) {
sp = &hs[row];
(void)snprintf(buf, sizeof(buf),
"%3d%c %6d %-31s (%6d on %d)\n",
row + offset, sp->hs_time ? '*' : ' ',
sp->hs_score * sp->hs_level,
sp->hs_name, sp->hs_score, sp->hs_level);
if ((row + 1) % 3 == 0) {
for (i = 0; i < sizeof(buf); i++)
if (buf[i] == ' ')
buf[i] = '_';
}
if (me != NULL &&
sp->hs_level == level &&
sp->hs_score == score &&
strcmp(sp->hs_name, me) == 0) {
putpad(SOstr);
highlight = 1;
}
(void)printf("%s", buf);
if (highlight) {
putpad(SEstr);
highlight = 0;
}
}
}