Prepare for packaging

This commit is contained in:
Neale Pickett 2010-07-27 13:17:44 -06:00
parent e932242cfa
commit d41b792eab
7 changed files with 259 additions and 252 deletions

View File

@ -1,14 +1,14 @@
BINARIES = run-tanks designer.cgi BINARIES = forftanks designer.cgi
HTML = forf.html procs.html intro.html designer.html HTML = forf.html procs.html intro.html designer.html
CFLAGS = -Wall CFLAGS = -Wall
all: $(BINARIES) $(HTML) all: $(BINARIES) $(HTML)
run-tanks: run-tanks.o ctanks.o forf.o forftanks: forftanks.o ctanks.o forf.o
run-tanks: LDFLAGS = -lm forftanks: LDFLAGS = -lm
run-tanks.o: forf.h ctanks.h forftanks.o: forf.h ctanks.h
forf.o: forf.c forf.h forf.o: forf.c forf.h
ctanks.o: ctanks.h ctanks.o: ctanks.h

View File

@ -6,7 +6,7 @@
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
#define BASE_PATH "/tmp/tanks/" char *BASE_PATH = "";
struct { struct {
char *name; char *name;
@ -96,7 +96,7 @@ copy_item(char *filename, size_t maxlen)
size_t pos = 0; size_t pos = 0;
snprintf(path, sizeof(path), snprintf(path, sizeof(path),
BASE_PATH "%05d.%s", "%s%05d.%s", BASE_PATH,
getpid(), filename); getpid(), filename);
f = fopen(path, "w"); f = fopen(path, "w");
if (! f) { if (! f) {
@ -134,7 +134,7 @@ croak(char *msg)
for (i = 0; entries[i].name; i += 1) { for (i = 0; entries[i].name; i += 1) {
snprintf(path, sizeof(path), snprintf(path, sizeof(path),
BASE_PATH "%05d.%s", "%s%05d.%s", BASE_PATH
getpid(), entries[i].name); getpid(), entries[i].name);
unlink(path); unlink(path);
} }
@ -167,6 +167,11 @@ main(int argc, char *argv[])
memset(sensor, 0, sizeof(sensor)); memset(sensor, 0, sizeof(sensor));
token[0] = '\0'; token[0] = '\0';
BASE_PATH = getenv("BASE_PATH");
if (! BASE_PATH) {
BASE_PATH = "";
}
{ {
char *rm = getenv("REQUEST_METHOD"); char *rm = getenv("REQUEST_METHOD");
@ -254,15 +259,15 @@ main(int argc, char *argv[])
struct stat st; struct stat st;
int i; 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 (-1 == stat(path, &st)) return croak("Invalid token");
if (! S_ISDIR(st.st_mode)) return croak("Invalid token"); if (! S_ISDIR(st.st_mode)) return croak("Invalid token");
for (i = 0; entries[i].name; i += 1) { for (i = 0; entries[i].name; i += 1) {
snprintf(path, sizeof(path), snprintf(path, sizeof(path),
BASE_PATH "%05d.%s", "%s%05d.%s", BASE_PATH,
getpid(), entries[i].name); getpid(), entries[i].name);
snprintf(dest, sizeof(dest), snprintf(dest, sizeof(dest),
BASE_PATH "%s/%s", "%s%s/%s", BASE_PATH,
token, entries[i].name); token, entries[i].name);
rename(path, dest); rename(path, dest);
} }
@ -271,7 +276,7 @@ main(int argc, char *argv[])
FILE *f; FILE *f;
snprintf(dest, sizeof(dest), snprintf(dest, sizeof(dest),
BASE_PATH "%s/sensor%d", "%s%s/sensor%d", BASE_PATH,
token, i); token, i);
f = fopen(dest, "w"); f = fopen(dest, "w");
if (! f) break; if (! f) break;

View File

@ -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(/&/, "&amp;", s);
gsub(/</, "&lt;", s);
gsub(/>/, "&gt;", 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] = "<i>Unnamed</i>";
}
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 "<table id=\"results\">";
print "<tr><th>Name</th><th>Score</th><th>Cause of Death</th><th>Last Error</th></tr>";
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("<tr " style ">");
printf("<td><span class=\"swatch\" style=\"background-color: " color[me] "\">#</span> " name[me] "</td>");
printf("<td>" score[me] "</td>");
printf("<td>" reason[me] "</td>");
printf("<td>" lasterr[me] "</td>");
printf("</tr>\n");
}
}
}
print "</table>";
}

View File

@ -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 <<EOF >$fn
<!DOCTYPE html>
<html>
<head>
<title>Tanks Round $next</title>
<script type="application/javascript" src="tanks.js"></script>
<link rel="stylesheet" href="dirtbags.css" type="text/css">
<script type="application/javascript">
function go() {
start("battlefield",
// Start JSON data
EOF
./run-tanks $tanks >>$fn 3>$rfn
cat <<EOF >>$fn
// end JSON data
);
}
window.onload = go;
</script>
</head>
<body>
<h1>Tanks Round $next</h1>
<div id="game_box"><canvas id="battlefield"></canvas></div>
<p><span id="fps">0</span> fps</p>
EOF
./rank.awk $rfn >>$fn
rm -f $rfn
cat nav.html.inc >>$fn
cat <<EOF >>$fn
</body>
</html>
EOF
./summary.awk $tanks > summary.html
echo "done."

243
run-tanks Executable file
View File

@ -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(/&/, "&amp;", s);
gsub(/</, "&lt;", s);
gsub(/>/, "&gt;", 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] = "<i>Unnamed</i>";
}
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 "<table id=\"results\">";
print "<tr><th>Name</th><th>Score</th><th>Cause of Death</th><th>Last Error</th></tr>";
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("<tr " style ">");
printf("<td><span class=\"swatch\" style=\"background-color: " color[me] "\">#</span> " name[me] "</td>");
printf("<td>" score[me] "</td>");
printf("<td>" reason[me] "</td>");
printf("<td>" lasterr[me] "</td>");
printf("</tr>\n");
}
}
}
print "</table>";
}
' "$@"
}
summary () {
awk 'function esc(s) {
gsub(/&/, "&amp;", s);
gsub(/</, "&lt;", s);
gsub(/>/, "&gt;", s);
return s;
}
BEGIN {
ngames = 20;
print "<!DOCTYPE html>";
print "<html>";
print " <head>";
print " <title>Dirtbags Tanks</title>";
print " <link rel=\"stylesheet\" href=\"dirtbags.css\" type=\"text/css\">";
print " </head>";
print " <body>";
print " <h1>Dirtbags Tanks</h1>";
print " <p>New here? Read the <a href=\"intro.html\">introduction</a>.</p>";
print " <h2>Rankings</h2>";
print " <p>Over the last 20 games only.</p>";
print " <ol>";
for (i = 1; i < ARGC; i += 1) {
id = ARGV[i];
if (1 == getline < (id "/name")) {
names[id] = esc($0);
} else {
names[id] = "<i>Unnamed</i>";
}
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("<li><span class=\"swatch\" style=\"background-color: %s;\">#</span> %s (%d points)</li>\n", color[id], names[id], points[id]);
}
}
}
print " </ol>";
print " <h2>Rounds</h2>";
print " <ul>";
getline rounds < "next-round";
for (i = rounds - 1; i >= 0; i -= 1) {
printf("<li><a href=\"round-%04d.html\">%04d</a></li>\n", i, i);
}
print " </ul>";
while (getline < ENVIRON["NAV_HTML_INC"]) {
print;
}
print " </body>";
print "</html>";
}
' "$@"
}
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 <<EOF >$fn
<!DOCTYPE html>
<html>
<head>
<title>Tanks Round $next</title>
<script type="application/javascript" src="tanks.js"></script>
<link rel="stylesheet" href="dirtbags.css" type="text/css">
<script type="application/javascript">
function go() {
start("battlefield",
// Start JSON data
EOF
$TANKS_GAME $tanks >>$fn 3>$rfn
cat <<EOF >>$fn
// end JSON data
);
}
window.onload = go;
</script>
</head>
<body>
<h1>Tanks Round $next</h1>
<div id="game_box"><canvas id="battlefield"></canvas></div>
<p><span id="fps">0</span> fps</p>
EOF
rank $rfn >>$fn
rm -f $rfn
cat $NAV_HTML_INC >>$fn
cat <<EOF >>$fn
</body>
</html>
EOF
summary $tanks > summary.html.$$
mv summary.html.$$ summary.html
echo "done."

View File

@ -1,88 +0,0 @@
#! /usr/bin/awk -f
function esc(s) {
gsub(/&/, "&amp;", s);
gsub(/</, "&lt;", s);
gsub(/>/, "&gt;", s);
return s;
}
BEGIN {
ngames = 20;
print "<!DOCTYPE html>";
print "<html>";
print " <head>";
print " <title>Dirtbags Tanks</title>";
print " <link rel=\"stylesheet\" href=\"dirtbags.css\" type=\"text/css\">";
print " </head>";
print " <body>";
print " <h1>Dirtbags Tanks</h1>";
print " <p>New here? Read the <a href=\"intro.html\">introduction</a>.</p>";
print " <h2>Rankings</h2>";
print " <p>Over the last 20 games only.</p>";
print " <ol>";
for (i = 1; i < ARGC; i += 1) {
id = ARGV[i];
if (1 == getline < (id "/name")) {
names[id] = esc($0);
} else {
names[id] = "<i>Unnamed</i>";
}
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("<li><span class=\"swatch\" style=\"background-color: %s;\">#</span> %s (%d points)</li>\n", color[id], names[id], points[id]);
}
}
}
print " </ol>";
print " <h2>Rounds</h2>";
print " <ul>";
getline rounds < "next-round";
for (i = rounds - 1; i >= 0; i -= 1) {
printf("<li><a href=\"round-%04d.html\">%04d</a></li>\n", i, i);
}
print " </ul>";
while (getline < "nav.html.inc") {
print;
}
print " </body>";
print "</html>";
}