]> Pileus Git - ~andy/rhawk/blobdiff - spades.awk
Implement team names
[~andy/rhawk] / spades.awk
index 2e2261b08bcd9c0e1e94e11b1219f3a4d7140abb..a377ed5c415b6e6a6ac9a1e5c351e580e8f8b249 100644 (file)
@@ -56,6 +56,7 @@ function sp_reset(type)
                delete sp_share     # [c] Player teammates share["friend"] -> "name"
                delete sp_order     # [i] Player order order[i] -> "name"
                delete sp_scores    # [i] Teams score
+               delete sp_teams     # [i] Teams names
        }
 
        # Persistent
@@ -106,6 +107,7 @@ function sp_save(file,      game)
        json_copy(game, "share",   sp_share);
        json_copy(game, "order",   sp_order);
        json_copy(game, "scores",  sp_scores);
+       json_copy(game, "teams",   sp_teams);
 
        # Persistent
        game["channel"] = sp_channel;
@@ -150,6 +152,7 @@ function sp_load(file,      game)
        sp_acopy(sp_share,   game["share"]);
        sp_acopy(sp_order,   game["order"]);
        sp_acopy(sp_scores,  game["scores"]);
+       sp_acopy(sp_teams,   game["teams"]);
 
        # Persistent
        sp_channel = game["channel"];
@@ -159,10 +162,10 @@ function sp_load(file,    game)
 
 function sp_say(msg)
 {
-       print strftime("%Y-%m-%d %H:%M:%S | ") msg >> "logs/" sp_log
-       fflush("logs/" sp_log)
        say(sp_channel, msg)
        print msg |& sp_sock
+       print strftime("%Y-%m-%d %H:%M:%S | ") msg >> "logs/" sp_log
+       fflush("logs/" sp_log)
 }
 
 function sp_pretty(cards, who)
@@ -255,10 +258,13 @@ function sp_winner(       card, tmp)
        return tmp[1]
 }
 
-function sp_team(i)
+function sp_team(i, players)
 {
        #return "{" sp_order[i+0] "," sp_order[i+2] "}"
-       return sp_order[i+0] "/" sp_order[i+2]
+       if ((i in sp_teams) && !players)
+               return sp_teams[i]
+       else
+               return sp_order[i+0] "/" sp_order[i+2]
 }
 
 function sp_bags(i,    bags)
@@ -422,7 +428,7 @@ function sp_cur(list)
        return list[length(list)-1]
 }
 
-function sp_stats(file,   line, arr, time, user, turn, start, delay, extra)
+function sp_stats(file,   line, arr, time, user, turn, start, delay, short, extra)
 {
        # Process log file
        while ((stat = getline line < file) > 0) {
@@ -461,9 +467,10 @@ function sp_stats(file,   line, arr, time, user, turn, start, delay, extra)
 
        # Output statistics
        for (user in delay) {
+               short = length(user) <= 4 ? user : substr(user, 0, 4)
                extra = (user != turn) ? "" : \
                        ", " sp_delay(sp_cur(delay[user])) " (cur)";
-               say("latency for " user \
+               say("latency for " short \
                        ": " sp_delay(sp_avg(delay[user])) " (avg)" \
                        ", " sp_delay(sp_max(delay[user])) " (max)" extra)
        }
@@ -535,6 +542,7 @@ AUTH == OWNER &&
        say(".bid [n] -- bid for <n> tricks")
        say(".pass [card] -- pass a card to your partner")
        say(".play [card] -- play a card")
+       say(".team [name] -- set your team name")
        say(".last -- show who took the previous trick")
        say(".turn -- check whose turn it is")
        say(".bids -- check what everyone bid")
@@ -680,6 +688,28 @@ match($0, /^\.newgame ?([1-9][0-9]*) *- *([1-9][0-9]*)$/, _arr) {
        }
 }
 
+/^\.team/ {
+       gsub(/^\.team */, "")
+       _team = sp_from in sp_players ? sp_players[sp_from] % 2 : 0
+       if (sp_state ~ "new|join") {
+               reply("The game has not yet started")
+       }
+       else if (!(sp_from in sp_players)) {
+               reply("You are not playing")
+       }
+       else if ($0 ~ /^[^a-zA-Z0-9]/) {
+               reply("Invalid team name")
+       }
+       else if ($0 ~ /^./) {
+               sp_teams[_team] = substr($0, 0, 32)
+               sp_say(sp_team(_team,1) " are now known as " sp_team(_team))
+       }
+       else {
+               delete sp_teams[_team]
+               sp_say(sp_team(_team,1) " are boring")
+       }
+}
+
 /^\.whoami/ {
        if (!(sp_from in sp_players))
                reply("You are not playing")
@@ -718,7 +748,7 @@ sp_state ~ "(bid|pass|play)" &&
        for (_i in sp_share)
                _lines[sp_share[_i]] = _lines[sp_share[_i]] " " _i
        for (_i in _lines)
-               sp_say(_i " allowed:" _lines[_i])
+               say(_i " allowed:" _lines[_i])
 }
 
 !sp_valid &&
@@ -756,7 +786,7 @@ sp_state == "bid" &&
                        sp_state = "play"
                        for (i=0; i<2; i++) {
                                if (sp_passer(i)) {
-                                       sp_say(sp_team(i) ": select a card to pass " \
+                                       sp_say(sp_team(i,1) ": select a card to pass " \
                                            "(/msg " NICK " .pass <card>)")
                                        sp_state = "pass"
                                }
@@ -967,6 +997,6 @@ sp_state == "play" &&
        sp_stats("logs/" $2 ".log");
 }
 
-/^\.((new|end|load)game|join|look|bid|pass|play|notify)/ {
+/^\.((new|end|load)game|join|look|bid|pass|play|allow|deny|team|notify)/ {
        sp_save("var/sp_cur.json");
 }