]> Pileus Git - ~andy/rhawk/blobdiff - spades.awk
Update Spades stats
[~andy/rhawk] / spades.awk
index 175ff9f5b264860bd360ae97b638ae54b45fbd1a..08f2776277aebd4cf895d10c44df417e09e91c62 100644 (file)
@@ -291,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++) {
@@ -374,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)
@@ -388,7 +397,7 @@ 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"      
+                                int(sec/60)%60    "m"
 }
 
 function sp_max(list,    i, max)
@@ -406,7 +415,12 @@ function sp_avg(list,    i, sum)
        return sum / length(list)
 }
 
-function sp_stats(file,   line, arr, time, user, turn, start, delay)
+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) {
@@ -426,22 +440,30 @@ function sp_stats(file,   line, arr, time, user, turn, start, delay)
                        delay[turn][length(delay[turn])] = time - start
                        turn  = 0
                }
-               if (match(arr[2], /^it is your.*$/, arr)) {
+               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)")
+                       ", " sp_delay(sp_max(delay[user])) " (max)" extra)
        }
 }
 
@@ -654,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])
@@ -715,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"