diff options
author | rodri <rgl@antares-labs.eu> | 2023-09-29 19:36:02 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-09-29 19:36:02 +0000 |
commit | 9c377e4f25344a908f18f5154ac9143ffb9577e6 (patch) | |
tree | 63e2a9d0e3f3c2dc2ba3165d98b57642deaec792 | |
parent | ecee83f1b2184f8443d8fc4a32176dcbfa0bd7b0 (diff) | |
download | battleship-9c377e4f25344a908f18f5154ac9143ffb9577e6.tar.gz battleship-9c377e4f25344a908f18f5154ac9143ffb9577e6.tar.bz2 battleship-9c377e4f25344a908f18f5154ac9143ffb9577e6.zip |
send the matches list upon first connection. changed the syntax a bit.
-rw-r--r-- | bts.c | 14 | ||||
-rw-r--r-- | btsd.c | 40 |
2 files changed, 34 insertions, 20 deletions
@@ -35,9 +35,9 @@ enum { CMwemiss, CMtheyhit, CMtheymiss, - CMmatchesb, /* list header */ + CMmatches, /* list opening */ CMmatch, /* list entry */ - CMmatchese, /* list tail */ + CMendmatches, /* list closure */ CMwatching, CMwin, CMlose, @@ -58,9 +58,9 @@ Cmdtab svcmd[] = { CMwemiss, "miss", 1, CMtheyhit, "hit", 2, CMtheymiss, "miss", 2, - CMmatchesb, "matches", 1, + CMmatches, "matches", 1, CMmatch, "m", 4, - CMmatchese, "end", 1, + CMendmatches, "endmatches", 1, CMwatching, "watching", 4, CMwin, "win", 1, CMlose, "lose", 1, @@ -829,12 +829,12 @@ processcmd(char *cmd) else if(ct->index == CMqueued){ game.state = Ready; csetcursor(mctl, &patrolcursor); - }else if(!matches->filling && ct->index == CMmatchesb){ + }else if(ct->index == CMmatches && !matches->filling){ matches->clear(matches); matches->filling = 1; - }else if(matches->filling && ct->index == CMmatch) + }else if(ct->index == CMmatch && matches->filling) matches->add(matches, strtoul(cb->f[1], nil, 10), smprint("%s vs %s", cb->f[2], cb->f[3])); - else if(matches->filling && ct->index == CMmatchese) + else if(ct->index == CMendmatches && matches->filling) matches->filling = 0; else if(ct->index == CMwatching){ match.id = strtoul(cb->f[1], nil, 10); @@ -174,6 +174,19 @@ freeseats(Stands *s) } void +sendmatches(Channel *c) +{ + Match *m; + + rlock(&theaterlk); + chanprint(c, "matches\n"); + for(m = theater.next; m != &theater; m = m->next) + chanprint(c, "m %d %s %s\n", m->id, m->pl[0]->name, m->pl[1]->name); + chanprint(c, "endmatches\n"); + runlock(&theaterlk); +} + +void broadcast(Stands *s, char *fmt, ...) { va_list arg; @@ -271,9 +284,10 @@ playerproc(void *arg) goto Nocmd; if(my->name[0] == 0){ - if(ct->index == CMid && strlen(cb->f[1]) > 0) + if(ct->index == CMid && strlen(cb->f[1]) > 0){ snprint(my->name, sizeof my->name, "%s", cb->f[1]); - else + sendmatches(my->io.out); + }else chanprint(my->io.out, "id\n"); }else switch(my->state){ @@ -281,12 +295,7 @@ playerproc(void *arg) if(ct->index == CMplay) sendp(playerq, my); else if(ct->index == CMgetmatches){ - rlock(&theaterlk); - chanprint(my->io.out, "matches\n"); - for(m = theater.next; m != &theater; m = m->next) - chanprint(my->io.out, "m %d %s %s\n", m->id, m->pl[0]->name, m->pl[1]->name); - chanprint(my->io.out, "end\n"); - runlock(&theaterlk); + sendmatches(my->io.out); }else if(ct->index == CMwatch){ mid = strtoul(cb->f[1], nil, 10); m = getmatch(mid); @@ -333,6 +342,11 @@ End: } void +aiproc(void *) +{ +} + +void battleproc(void *arg) { Msg *msg; @@ -402,11 +416,11 @@ battleproc(void *arg) } n0 = truerand(); if(debug) - fprint(2, "let the game begin: %s plays, %s waits\n", m->pl[n0%2]->name, m->pl[(n0+1)%2]->name); - chanprint(m->pl[n0%2]->io.out, "play\n"); - m->pl[n0%2]->state = Playing; - chanprint(m->pl[(n0+1)%2]->io.out, "wait\n"); - broadcast(&stands, "plays %d\n", n0%2); + fprint(2, "let the game begin: %s plays, %s waits\n", m->pl[n0&1]->name, m->pl[(n0+1)&1]->name); + chanprint(m->pl[n0&1]->io.out, "play\n"); + m->pl[n0&1]->state = Playing; + chanprint(m->pl[(n0+1)&1]->io.out, "wait\n"); + broadcast(&stands, "plays %d\n", n0&1); } } break; |