diff --git a/Makefile b/Makefile index 0795a35..1e6ebff 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,14 @@ -BINARIES = run-tanks designer.cgi +BINARIES = forftanks designer.cgi HTML = forf.html procs.html intro.html designer.html CFLAGS = -Wall all: $(BINARIES) $(HTML) -run-tanks: run-tanks.o ctanks.o forf.o -run-tanks: LDFLAGS = -lm +forftanks: forftanks.o ctanks.o forf.o +forftanks: LDFLAGS = -lm -run-tanks.o: forf.h ctanks.h +forftanks.o: forf.h ctanks.h forf.o: forf.c forf.h ctanks.o: ctanks.h diff --git a/designer.cgi.c b/designer.cgi.c index 01fd8df..f67a32c 100644 --- a/designer.cgi.c +++ b/designer.cgi.c @@ -6,7 +6,7 @@ #include #include -#define BASE_PATH "/tmp/tanks/" +char *BASE_PATH = ""; struct { char *name; @@ -96,7 +96,7 @@ copy_item(char *filename, size_t maxlen) size_t pos = 0; snprintf(path, sizeof(path), - BASE_PATH "%05d.%s", + "%s%05d.%s", BASE_PATH, getpid(), filename); f = fopen(path, "w"); if (! f) { @@ -134,7 +134,7 @@ croak(char *msg) for (i = 0; entries[i].name; i += 1) { snprintf(path, sizeof(path), - BASE_PATH "%05d.%s", + "%s%05d.%s", BASE_PATH getpid(), entries[i].name); unlink(path); } @@ -167,6 +167,11 @@ main(int argc, char *argv[]) memset(sensor, 0, sizeof(sensor)); token[0] = '\0'; + BASE_PATH = getenv("BASE_PATH"); + if (! BASE_PATH) { + BASE_PATH = ""; + } + { char *rm = getenv("REQUEST_METHOD"); @@ -254,15 +259,15 @@ main(int argc, char *argv[]) struct stat st; int i; - snprintf(path, sizeof(path), BASE_PATH "%s/", token); + snprintf(path, sizeof(path), "%s%s/", BASE_PATH, token); if (-1 == stat(path, &st)) return croak("Invalid token"); if (! S_ISDIR(st.st_mode)) return croak("Invalid token"); for (i = 0; entries[i].name; i += 1) { snprintf(path, sizeof(path), - BASE_PATH "%05d.%s", + "%s%05d.%s", BASE_PATH, getpid(), entries[i].name); snprintf(dest, sizeof(dest), - BASE_PATH "%s/%s", + "%s%s/%s", BASE_PATH, token, entries[i].name); rename(path, dest); } @@ -271,7 +276,7 @@ main(int argc, char *argv[]) FILE *f; snprintf(dest, sizeof(dest), - BASE_PATH "%s/sensor%d", + "%s%s/sensor%d", BASE_PATH, token, i); f = fopen(dest, "w"); if (! f) break; diff --git a/run-tanks.c b/forftanks.c similarity index 100% rename from run-tanks.c rename to forftanks.c diff --git a/rank.awk b/rank.awk deleted file mode 100755 index 45acf41..0000000 --- a/rank.awk +++ /dev/null @@ -1,94 +0,0 @@ -#! /usr/bin/awk -f - -## -## C doesn't have good string handling routines, but awk does. Figuring -## out rankings, who shot whom, and deciding on a "winner" is therefore -## handled with this awk script. -## -## Input looks like this: -## 0xbff81f28 players/sittingduckwithteeth shot 0xbff82138 0 None -## 0xbff82030 players/sandlion shot 0xbff82138 0 None -## 0xbff82138 players/chashtank (null) (nil) 0 None -## - -BEGIN { - FS = "\t"; -} - -function esc(s) { - gsub(/&/, "&", s); - gsub(//, ">", s); - return s; -} - -{ - id = $1; - ntanks += 1; - tanks[id] = id; - if ($4 == "(nil)") { - score[id] += 1; - } else { - reason[id] = $3; - killer[id] = $4; - kills[$4] += 1; - score[$4] += 1; - } - path[id] = $2; - if ($5) { - lasterr[id] = $6 " around char " $5; - } else { - lasterr[id] = $6; - } - - if (1 == getline < (path[id] "/name")) { - name[id] = esc($0); - } else { - name[id] = "Unnamed"; - } - - getline < (path[id] "/color"); - if (/^#[0-9A-Fa-f]+$/) { - color[id] = $0; - } else { - color[id] = "#c0c0c0"; - } -} - -END { - # Fill in who killed whom - for (id in tanks) { - if (score[id] > topscore) { - winner = id; - topscore = score[id]; - } - if (killer[id]) { - reason[id] = reason[id] " (" name[killer[id]] ")"; - } - print score[id] >> (path[id] "/points"); - } - - # Dole out points - - # Output the table - print ""; - print ""; - for (i = ntanks; i >= 0; i -= 1) { - for (me in tanks) { - if (score[me] == i) { - if (me == winner) { - style = "style=\"font-weight: bold; background-color: #666666\""; - } else { - style = ""; - } - printf(""); - printf(""); - printf(""); - printf(""); - printf(""); - printf("\n"); - } - } - } - print "
NameScoreCause of DeathLast Error
# " name[me] "" score[me] "" reason[me] "" lasterr[me] "
"; -} diff --git a/round.sh b/round.sh deleted file mode 100755 index 0d8734e..0000000 --- a/round.sh +++ /dev/null @@ -1,59 +0,0 @@ -#! /bin/sh - -if [ "$#" -gt 0 ]; then - tanks="$@" -else - echo "Usage: $0 tank1 tank2 [...]" - exit 1 -fi - - -if [ -f next-round ]; then - next=$(cat next-round) -else - next=0 -fi -expr $next + 1 > next-round - -fn=$(printf "round-%04d.html" $next) -rfn=results$$.txt - - -echo -n "Running round $next... " -cat <$fn - - - - Tanks Round $next - - - - - -

Tanks Round $next

-
-

0 fps

-EOF -./rank.awk $rfn >>$fn -rm -f $rfn -cat nav.html.inc >>$fn -cat <>$fn - - -EOF - -./summary.awk $tanks > summary.html - -echo "done." - diff --git a/run-tanks b/run-tanks new file mode 100755 index 0000000..4b0b500 --- /dev/null +++ b/run-tanks @@ -0,0 +1,243 @@ +#! /bin/sh + +if [ "$#" -gt 0 ]; then + tanks="$@" +else + echo "Usage: $0 tank1 tank2 [...]" + exit 1 +fi + +TANKS_GAME=${TANKS_GAME:-./forftanks} +NAV_HTML_INC=${NAV_HTML_INC:-./nav.html.inc} export NAV_HTML_INC + +rank () { + awk 'BEGIN { + FS = "\t"; +} + +function esc(s) { + gsub(/&/, "&", s); + gsub(//, ">", s); + return s; +} + +{ + id = $1; + ntanks += 1; + tanks[id] = id; + if ($4 == "(nil)") { + score[id] += 1; + } else { + reason[id] = $3; + killer[id] = $4; + kills[$4] += 1; + score[$4] += 1; + } + path[id] = $2; + if ($5) { + lasterr[id] = $6 " around char " $5; + } else { + lasterr[id] = $6; + } + + if (1 == getline < (path[id] "/name")) { + name[id] = esc($0); + } else { + name[id] = "Unnamed"; + } + + getline < (path[id] "/color"); + if (/^#[0-9A-Fa-f]+$/) { + color[id] = $0; + } else { + color[id] = "#c0c0c0"; + } +} + +END { + # Fill in who killed whom + for (id in tanks) { + if (score[id] > topscore) { + winner = id; + topscore = score[id]; + } + if (killer[id]) { + reason[id] = reason[id] " (" name[killer[id]] ")"; + } + print score[id] >> (path[id] "/points"); + } + + # Dole out points + + # Output the table + print ""; + print ""; + for (i = ntanks; i >= 0; i -= 1) { + for (me in tanks) { + if (score[me] == i) { + if (me == winner) { + style = "style=\"font-weight: bold; background-color: #666666\""; + } else { + style = ""; + } + printf(""); + printf(""); + printf(""); + printf(""); + printf(""); + printf("\n"); + } + } + } + print "
NameScoreCause of DeathLast Error
# " name[me] "" score[me] "" reason[me] "" lasterr[me] "
"; +} +' "$@" +} + +summary () { + awk 'function esc(s) { + gsub(/&/, "&", s); + gsub(//, ">", s); + return s; +} + +BEGIN { + ngames = 20; + + print ""; + print ""; + print " "; + print " Dirtbags Tanks"; + print " "; + print " "; + print " "; + print "

Dirtbags Tanks

"; + + print "

New here? Read the introduction.

"; + print "

Rankings

"; + print "

Over the last 20 games only.

"; + print "
    "; + for (i = 1; i < ARGC; i += 1) { + id = ARGV[i]; + + if (1 == getline < (id "/name")) { + names[id] = esc($0); + } else { + names[id] = "Unnamed"; + } + + getline < (id "/color"); + if (/^#[0-9A-Fa-f]+$/) { + color[id] = $0; + } else { + color[id] = "#c0c0c0"; + } + + + for (j = 0; 1 == getline < (id "/points"); j += 1) { + pts[id, j % ngames] = int($0); + } + total = 0; + for (j = 0; j < ngames; j += 1) { + total += pts[id, j]; + } + scores[total] = total; + points[id] = total; + } + while (1) { + # Find highest score + maxscore = -1; + for (p in scores) { + if (int(p) > maxscore) { + maxscore = int(p); + } + } + if (maxscore == -1) { + break; + } + delete scores[maxscore]; + + for (id in points) { + if (points[id] == maxscore) { + printf("
  1. # %s (%d points)
  2. \n", color[id], names[id], points[id]); + } + } + } + print "
"; + + print "

Rounds

"; + print "
    "; + getline rounds < "next-round"; + for (i = rounds - 1; i >= 0; i -= 1) { + printf("
  • %04d
  • \n", i, i); + } + print "
"; + + while (getline < ENVIRON["NAV_HTML_INC"]) { + print; + } + + print " "; + print ""; +} +' "$@" +} + + + + + + + +if [ -f next-round ]; then + next=$(cat next-round) +else + next=0 +fi +expr $next + 1 > next-round + +fn=$(printf "round-%04d.html" $next) +rfn=results$$.txt + + +echo -n "Running round $next... " +cat <$fn + + + + Tanks Round $next + + + + + +

Tanks Round $next

+
+

0 fps

+EOF +rank $rfn >>$fn +rm -f $rfn +cat $NAV_HTML_INC >>$fn +cat <>$fn + + +EOF + +summary $tanks > summary.html.$$ +mv summary.html.$$ summary.html + +echo "done." + diff --git a/summary.awk b/summary.awk deleted file mode 100755 index d3267ef..0000000 --- a/summary.awk +++ /dev/null @@ -1,88 +0,0 @@ -#! /usr/bin/awk -f - -function esc(s) { - gsub(/&/, "&", s); - gsub(//, ">", s); - return s; -} - -BEGIN { - ngames = 20; - - print ""; - print ""; - print " "; - print " Dirtbags Tanks"; - print " "; - print " "; - print " "; - print "

Dirtbags Tanks

"; - - print "

New here? Read the introduction.

"; - print "

Rankings

"; - print "

Over the last 20 games only.

"; - print "
    "; - for (i = 1; i < ARGC; i += 1) { - id = ARGV[i]; - - if (1 == getline < (id "/name")) { - names[id] = esc($0); - } else { - names[id] = "Unnamed"; - } - - getline < (id "/color"); - if (/^#[0-9A-Fa-f]+$/) { - color[id] = $0; - } else { - color[id] = "#c0c0c0"; - } - - - for (j = 0; 1 == getline < (id "/points"); j += 1) { - pts[id, j % ngames] = int($0); - } - total = 0; - for (j = 0; j < ngames; j += 1) { - total += pts[id, j]; - } - scores[total] = total; - points[id] = total; - } - while (1) { - # Find highest score - maxscore = -1; - for (p in scores) { - if (int(p) > maxscore) { - maxscore = int(p); - } - } - if (maxscore == -1) { - break; - } - delete scores[maxscore]; - - for (id in points) { - if (points[id] == maxscore) { - printf("
  1. # %s (%d points)
  2. \n", color[id], names[id], points[id]); - } - } - } - print "
"; - - print "

Rounds

"; - print "
    "; - getline rounds < "next-round"; - for (i = rounds - 1; i >= 0; i -= 1) { - printf("
  • %04d
  • \n", i, i); - } - print "
"; - - while (getline < "nav.html.inc") { - print; - } - - print " "; - print ""; -}