]> Pileus Git - ~andy/rhawk/blobdiff - spades.awk
Allow stats on previous games
[~andy/rhawk] / spades.awk
index a824464a7a6d6e42b7c99cb008d36d2b94e58c16..175ff9f5b264860bd360ae97b638ae54b45fbd1a 100644 (file)
@@ -44,7 +44,6 @@ function sp_reset(type)
 
        # Per game
        if (type >= 2) {
-               sp_channel  = ""    #     channel to play in
                sp_state    = "new" #     {new,join,bid,pass,play}
                sp_owner    = ""    #     Who started the game
                sp_playto   = 0     #     Score the game will go to
@@ -61,6 +60,7 @@ function sp_reset(type)
 
        # Persistent
        if (type >= 3) {
+               sp_channel  = ""    #     channel to play in
                sp_log      = ""    #     Log file name
                delete sp_notify    # [p] E-mail notification address
        }
@@ -93,20 +93,22 @@ function sp_save(file,      game)
        json_copy(game, "tricks",  sp_tricks);
 
        # Per game
-       game["channel"] = sp_channel;
        game["owner"]   = sp_owner;
        game["playto"]  = sp_playto;
        game["dealer"]  = sp_dealer;
        game["turn"]    = sp_turn;
        game["player"]  = sp_player;
        game["limit"]   = sp_limit;
-       game["log"]     = sp_log;
        json_copy(game, "hands",   sp_hands);
        json_copy(game, "players", sp_players);
        json_copy(game, "auths",   sp_auths);
        json_copy(game, "share",   sp_share);
        json_copy(game, "order",   sp_order);
        json_copy(game, "scores",  sp_scores);
+
+       # Persistent
+       game["channel"] = sp_channel;
+       game["log"]     = sp_log;
        json_copy(game, "notify",  sp_notify);
 
        # Save
@@ -135,20 +137,22 @@ function sp_load(file,    game)
        sp_acopy(sp_tricks,  game["tricks"]);
 
        # Per game
-       sp_channel = game["channel"];
        sp_owner   = game["owner"];
        sp_playto  = game["playto"];
        sp_dealer  = game["dealer"];
        sp_turn    = game["turn"];
        sp_player  = game["player"];
        sp_limit   = game["limit"];
-       sp_log     = game["log"];
        sp_acopy(sp_hands,   game["hands"]);
        sp_acopy(sp_players, game["players"]);
        sp_acopy(sp_auths,   game["auths"]);
        sp_acopy(sp_share,   game["share"]);
        sp_acopy(sp_order,   game["order"]);
        sp_acopy(sp_scores,  game["scores"]);
+
+       # Persistent
+       sp_channel = game["channel"];
+       sp_log     = game["log"];
        sp_acopy(sp_notify,  game["notify"]);
 }
 
@@ -379,6 +383,68 @@ function sp_play(card,     winner, pi)
        }
 }
 
+# Statistics
+function sp_delay(sec)
+{
+       return (sec > 60*60*24 ? int(sec/60/60/24) "d " : "") \
+              (sec > 60*60    ? int(sec/60/60)%24 "h " : "") \
+                                int(sec/60)%60    "m"      
+}
+
+function sp_max(list,    i, max)
+{
+       for (i=0; i<length(list); i++)
+               if (max == "" || list[i] > max)
+                       max = list[i]
+       return max
+}
+
+function sp_avg(list,    i, sum)
+{
+       for (i=0; i<length(list); i++)
+               sum += list[i]
+       return sum / length(list)
+}
+
+function sp_stats(file,   line, arr, time, user, turn, start, delay)
+{
+       # Process log file
+       while ((stat = getline line < file) > 0) {
+               # Parse date
+               if (!match(line, /^([0-9\- \:]*) \| (.*)$/, arr))
+                       continue
+               gsub(/[:-]/, " ", arr[1])
+               time = mktime(arr[1])
+
+               # Parse user
+               if (!match(arr[2], /^([^:]*): (.*)$/, arr))
+                       continue
+               user = arr[1]
+
+               # Record user latency
+               if (turn) {
+                       delay[turn][length(delay[turn])] = time - start
+                       turn  = 0
+               }
+               if (match(arr[2], /^it is your.*$/, arr)) {
+                       turn  = user
+                       start = time
+               } 
+       }
+       close(file)
+
+       # Check for error
+       if (stat < 0)
+               reply("File does not exist: " file);
+
+       # Output statistics
+       for (user in delay) {
+               say("latency for " user \
+                       ": " sp_delay(sp_avg(delay[user])) " (avg)" \
+                       ", " sp_delay(sp_max(delay[user])) " (max)")
+       }
+}
+
 # Misc
 BEGIN {
        cmd = "od -An -N4 -td4 /dev/random"
@@ -839,11 +905,22 @@ sp_state == "play" &&
        }
 }
 
-(DST == sp_channel) &&
+(TO == NICK || DST == sp_channel) &&
 /^\.log/ {
        say("http://pileus.org/andy/spades/" sp_log)
 }
 
+(TO == NICK || DST == sp_channel) &&
+/^\.stats$/ {
+       sp_stats("logs/" sp_log);
+}
+
+(TO == NICK || DST == sp_channel) &&
+/^\.stats ([0-9]+_[0-9]+)(\.log)$/ {
+       gsub(/\.log$/, "", $2);
+       sp_stats("logs/" $2 ".log");
+}
+
 /^\.((new|end|load)game|join|look|bid|pass|play|notify)/ {
        sp_save("var/sp_cur.json");
 }