From 9d3cce9bf6cb1e26dde4710ed22212b264043090 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 4 Dec 2024 18:39:55 -0700 Subject: [PATCH] Add overall rankings. Playable now. --- forftanks.c | 12 +-- tanksd.go | 25 +++--- www/designer.mjs | 3 +- www/index.html | 68 ++++++++++++++- www/main.mjs | 5 ++ www/player.mjs | 3 +- www/replay.mjs | 217 +++++++++++++++++++++++++++++++++++++++++++++++ www/style.css | 6 +- 8 files changed, 313 insertions(+), 26 deletions(-) create mode 100644 www/main.mjs create mode 100644 www/replay.mjs diff --git a/forftanks.c b/forftanks.c index b415fbb..9bc305a 100644 --- a/forftanks.c +++ b/forftanks.c @@ -25,7 +25,7 @@ struct forftank { char color[8]; /* "#ff0088" */ char name[50]; char *path; - ino_t inode; + unsigned int uid; struct forf_stack _prog; struct forf_value _progvals[CSTACK_SIZE]; @@ -326,20 +326,20 @@ ft_read_tank(struct forftank *ftank, ftank->path = path; - /* Store inode */ + /* Store uid */ { struct stat s; if (-1 == stat(path, &s)) { - ftank->inode = -1; + ftank->uid = 0; } else { - ftank->inode = s.st_ino; + ftank->uid = (unsigned int)s.st_ino; } } /* What is your name? */ ret = ft_read_file(ftank->name, sizeof(ftank->name), path, "name"); if (! ret) { - snprintf(ftank->name, sizeof(ftank->name), "i:%lx", ftank->inode); + snprintf(ftank->name, sizeof(ftank->name), "i:%x", ftank->uid); } /* What is your quest? */ @@ -455,7 +455,7 @@ print_standings(FILE *f, fprintf(f, ",\n"); } fprintf(f, " {\n"); - fprintf(f, " \"inode\": %ld,\n", ftanks[i].inode); + fprintf(f, " \"uid\": %d,\n", ftanks[i].uid); fprintf(f, " \"color\": \"%s\",\n", ftanks[i].color); fprintf(f, " \"name\": \"%s\",\n", ftanks[i].name); fprintf(f, " \"death\": \"%s\",\n", tanks[i].cause_death); diff --git a/tanksd.go b/tanksd.go index b40dd5a..c362cc9 100644 --- a/tanksd.go +++ b/tanksd.go @@ -84,11 +84,18 @@ func (ts *TankState) ServeHTTP(w http.ResponseWriter, req *http.Request) { } func (ts *TankState) WriteRound(now time.Time, round []byte) error { + // Write new round + roundFn := fmt.Sprintf("%016x.json", now.Unix()) + roundPath := path.Join(ts.roundsdir, roundFn) + if err := os.WriteFile(roundPath, round, 0644); err != nil { + return err + } + + // Clean up and index all rounds dents, err := os.ReadDir(ts.roundsdir) if err != nil { return err } - for uint(len(dents)) > *maxrounds { fn := path.Join(ts.roundsdir, dents[0].Name()) if err := os.Remove(fn); err != nil { @@ -97,17 +104,15 @@ func (ts *TankState) WriteRound(now time.Time, round []byte) error { dents = dents[1:] } - roundFn := fmt.Sprintf("%016x.json", now.Unix()) - roundPath := path.Join(ts.roundsdir, roundFn) - if err := os.WriteFile(roundPath, round, 0644); err != nil { - return err - } - - rounds := make([]string, len(dents) + 1) + rounds := make([]string, 0, len(dents)) for i := 0; i < len(dents); i++ { - rounds[i] = dents[i].Name() + name := dents[i].Name() + switch name { + case "index.json": + continue + } + rounds = append(rounds, name) } - rounds[len(dents)] = roundFn roundsJs, err := json.Marshal(rounds) if err != nil { diff --git a/www/designer.mjs b/www/designer.mjs index 411c0f5..7cd2b7b 100644 --- a/www/designer.mjs +++ b/www/designer.mjs @@ -132,7 +132,7 @@ EOD } } -function init() { +export function init() { let canvas = document.querySelector("#design") let ctx = canvas.getContext("2d") canvas.width = 200 @@ -184,4 +184,3 @@ function init() { setInterval(() => update(ctx), Second / FPS) } -init() diff --git a/www/index.html b/www/index.html index 5ac6722..3b249ac 100644 --- a/www/index.html +++ b/www/index.html @@ -3,7 +3,7 @@ Tanks - +

Tanks

@@ -11,7 +11,7 @@

Upload a tank

- + Tank Designer
Program