diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-01-19 19:16:23 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-01-19 19:16:23 +0100 |
commit | 47d78e9ce3330d6c00d5d0f3c9021e838944ae60 (patch) | |
tree | 65adfaa236acdc85b2cbde8fda1717c13ee13a5b | |
parent | fcc0658730e541477fa60995d8bd82ff87163b92 (diff) |
Broadcast on join and leave
-rw-r--r-- | main.c | 20 | ||||
-rwxr-xr-x | test.py | 8 |
2 files changed, 26 insertions, 2 deletions
@@ -134,10 +134,26 @@ i64 list_find_d(void **d,i64 len,void *value){ #define LIST_FIND(_list,_value) list_find_d((void**)(_list)->d,(_list)->len,(void*)(_value)) +void room_broadcast_line(Room *room,const char *line){ + for(i64 i=0;i<room->members.len;i++){ + tcp_send_line(room->members.d[i]->sock,line); + } +} + +__attribute__((format (printf,2,3))) +void room_broadcast_line_f(Room *room,const char *format,...){ + va_list ap; + va_start(ap,format); + char *buf; + if(vasprintf(&buf,format,ap)<0)throw("vasprintf: allocation error"); + room_broadcast_line(room,buf); + free(buf); +} + //Returns false iff conn was already in specified room. bool room_join(Room *room,Connection *conn){ if(LIST_FIND(&room->members,conn)!=-1)return false; - //TODO: notify other members + room_broadcast_line_f(room,"room_join %s %s %" PRIi64,room->gameid,room->roomid,conn->id); clist_add(&room->members,conn); rlist_add(&conn->rooms,room); return true; @@ -151,7 +167,7 @@ bool room_leave(Room *room,Connection *conn){ if(idx==-1)return false; clist_remove(&room->members,conn); rlist_remove(&conn->rooms,room); - //TODO: notify other members + room_broadcast_line_f(room,"room_leave %s %s %" PRIi64,room->gameid,room->roomid,conn->id); if(room->members.len==0){ room_destroy(room); } @@ -124,18 +124,26 @@ def testprivateroom(s,t): @testfunction("Join room") def testjoin(s,t): sendlineok(s,"room_create game room 1 2") + sendline(s,"id"); sid=recvint(s) + sendline(t,"id"); tid=recvint(t) sendlineok(t,"room_join game room") + expect(s,"room_join game room "+str(tid)) sendlinelist(t,"room_query",["room"]) + sendlineok(t,"room_create game room2 1 1") sendlinelist(t,"room_query",["room","room2"]) sendlineerror(s,"room_join game room2","Room full") + sendlineok(s,"room_leave game room") + expect(t,"room_leave game room "+str(sid)) + @testfunction("Player list, messaging") def testid(s,t): sendline(s,"id"); sid=recvint(s) sendline(t,"id"); tid=recvint(t) sendlineok(s,"room_create game room 0 2") sendlineok(t,"room_join game room") + expect(s,"room_join game room "+str(tid)) sendlinelist(s,"room_player_list game room",[str(sid),str(tid)]) sendlinelist(t,"room_player_list game room",[str(sid),str(tid)]) |