]> Pileus Git - ~andy/rhawk/blobdiff - spades.awk
Update Spades stats
[~andy/rhawk] / spades.awk
index 49c44cda8d2c350f68e0bf77e3424812a975ffe2..08f2776277aebd4cf895d10c44df417e09e91c62 100644 (file)
@@ -33,7 +33,7 @@ function sp_reset(type)
        if (type >= 1) {
                sp_state    = "bid" #     {new,join,bid,pass,play}
                sp_broken   = 0     #     Whether spades are broken
-               sp_last     = ""    #     The result of the last hand
+               delete sp_last      # [x] The result of the last hand
                delete sp_hands     # [p] Each players cards
                delete sp_looked    # [i] Whether a player has looked a their cards
                delete sp_bids      # [i] Each players bid
@@ -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
        }
@@ -85,7 +85,7 @@ function sp_save(file,        game)
        # Per round
        game["state"]   = sp_state;
        game["broken"]  = sp_broken;
-       game["last"]    = sp_last;
+       json_copy(game, "last",    sp_last);
        json_copy(game, "looked",  sp_looked);
        json_copy(game, "bids",    sp_bids);
        json_copy(game, "nil",     sp_nil);
@@ -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
@@ -127,7 +129,7 @@ function sp_load(file,      game)
        # Per round
        sp_state   = game["state"];
        sp_broken  = game["broken"];
-       sp_last    = game["last"];
+       sp_acopy(sp_last,    game["last"]);
        sp_acopy(sp_looked,  game["looked"]);
        sp_acopy(sp_bids,    game["bids"]);
        sp_acopy(sp_nil,     game["nil"]);
@@ -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"]);
 }
 
@@ -287,6 +291,15 @@ function sp_bidders(       i, turn, bid, bids)
        return bids
 }
 
+function sp_extra(     n, s)
+{
+       n = sp_bids[0] + sp_bids[1] + sp_bids[2] + sp_bids[3];
+       s = n == 12 || n == 14 ? "" : "s";
+
+       return n<13 ? "Playing with " 13-n " bag"   s "!" :
+              n>13 ? "Fighting for " n-13 " trick" s "!" : "No bags!";
+}
+
 function sp_score(     bids, times, tricks)
 {
        for (i=0; i<2; i++) {
@@ -347,7 +360,8 @@ function sp_play(card,      winner, pi)
                sp_tricks[pi]++
                sp_say(sp_pile[winner] " wins with " sp_pretty(winner, FROM) \
                       " (" sp_pretty(sp_piles, FROM) ")")
-               sp_last = sp_pile[winner] " took " sp_piles
+               sp_last["player"] = sp_pile[winner];
+               sp_last["pile"]   = sp_piles;
                sp_next(sp_pile[winner])
                sp_reset(0)
        }
@@ -369,7 +383,7 @@ function sp_play(card,      winner, pi)
                        sp_reset(2)
 
                } else {
-                       if (sp_scores[0] == sp_scores[1] && 
+                       if (sp_scores[0] == sp_scores[1] &&
                            sp_scores[0] >= sp_playto)
                                sp_say("It's tie! Playing an extra round!");
                        sp_reset(1)
@@ -378,6 +392,81 @@ 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_cur(list)
+{
+       return list[length(list)-1]
+}
+
+function sp_stats(file,   line, arr, time, user, turn, start, delay, extra)
+{
+       # 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|you .*(first|lead)!$)/, arr)) {
+                       turn  = user
+                       start = time
+               }
+       }
+       close(file)
+
+       # Add current latency
+       if (turn) {
+               delay[turn][length(delay[turn])] = systime() - start
+               debug("time: " (systime() - start))
+       }
+
+       # Check for error
+       if (stat < 0)
+               reply("File does not exist: " file);
+
+       # Output statistics
+       for (user in delay) {
+               extra = (user != turn) ? "" : \
+                       ", " sp_delay(sp_cur(delay[user])) " (cur)";
+               say("latency for " user \
+                       ": " sp_delay(sp_avg(delay[user])) " (avg)" \
+                       ", " sp_delay(sp_max(delay[user])) " (max)" extra)
+       }
+}
+
 # Misc
 BEGIN {
        cmd = "od -An -N4 -td4 /dev/random"
@@ -587,6 +676,15 @@ match($0, /^\.newgame ?([1-9][0-9]*) *- *([1-9][0-9]*)$/, _arr) {
        }
 }
 
+/^\.whoami/ {
+       if (!(sp_from in sp_players))
+               reply("You are not playing")
+       else if (sp_from == FROM)
+               say(FROM " has an existential crisis")
+       else
+               reply("You are playing for " sp_from);
+}
+
 /^\.notify$/ {
        if (sp_from in sp_notify)
                reply("Your address is " sp_notify[sp_from])
@@ -648,6 +746,7 @@ sp_state == "bid" &&
                if (sp_turn != sp_dealer) {
                        sp_say(sp_player ": it is your bid! (" sp_bidders() ")")
                } else {
+                       sp_say(sp_extra() " (" sp_bidders() ")")
                        for (p in sp_players)
                                say(p, "You have: " sp_hand(p, p))
                        sp_state = "play"
@@ -752,10 +851,11 @@ sp_state == "play" &&
 }
 
 /^\.last/ && sp_state == "play" {
-       if (!sp_last)
+       if (!isarray(sp_last))
                say("No tricks have been taken!");
        else
-               say(sp_pretty(sp_last, FROM));
+               say(sp_last["player"] " took " \
+                   sp_pretty(sp_last["pile"], FROM));
 }
 
 /^\.bids/ && sp_state == "bid" ||
@@ -837,11 +937,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");
 }