25 void flib_roomlist_destroy(flib_roomlist *list) { |
25 void flib_roomlist_destroy(flib_roomlist *list) { |
26 if(list) { |
26 if(list) { |
27 for(int i=0; i<list->roomCount; i++) { |
27 for(int i=0; i<list->roomCount; i++) { |
28 flib_roomlist_room_destroy(list->rooms[i]); |
28 flib_roomlist_room_destroy(list->rooms[i]); |
29 } |
29 } |
|
30 free(list->rooms); |
30 free(list); |
31 free(list); |
31 } |
32 } |
32 } |
33 } |
33 |
34 |
34 static flib_roomlist_room *fillRoomFromParams(char **params) { |
35 static flib_room *fillRoomFromParams(char **params) { |
35 flib_roomlist_room *result = NULL; |
36 flib_room *result = NULL; |
36 flib_roomlist_room *tmpRoom = flib_calloc(1, sizeof(flib_roomlist_room)); |
37 flib_room *tmpRoom = flib_calloc(1, sizeof(flib_room)); |
37 if(tmpRoom) { |
38 if(tmpRoom) { |
38 tmpRoom->inProgress = !strcmp(params[0], "True"); |
39 tmpRoom->inProgress = !strcmp(params[0], "True"); |
39 tmpRoom->name = flib_strdupnull(params[1]); |
40 tmpRoom->name = flib_strdupnull(params[1]); |
40 tmpRoom->playerCount = atoi(params[2]); |
41 tmpRoom->playerCount = atoi(params[2]); |
41 tmpRoom->teamCount = atoi(params[3]); |
42 tmpRoom->teamCount = atoi(params[3]); |
50 } |
51 } |
51 flib_roomlist_room_destroy(tmpRoom); |
52 flib_roomlist_room_destroy(tmpRoom); |
52 return result; |
53 return result; |
53 } |
54 } |
54 |
55 |
|
56 GENERATE_STATIC_LIST_INSERT(insertRoom, flib_room*) |
|
57 GENERATE_STATIC_LIST_DELETE(deleteRoom, flib_room*) |
|
58 |
|
59 static int findRoom(const flib_roomlist *list, const char *name) { |
|
60 for(int i=0; i<list->roomCount; i++) { |
|
61 if(!strcmp(name, list->rooms[i]->name)) { |
|
62 return i; |
|
63 } |
|
64 } |
|
65 return -1; |
|
66 } |
|
67 |
55 int flib_roomlist_add(flib_roomlist *list, char **params) { |
68 int flib_roomlist_add(flib_roomlist *list, char **params) { |
56 int result = -1; |
69 int result = -1; |
57 if(!list || !params) { |
70 if(!list || !params) { |
58 flib_log_e("null parameter in flib_roomlist_add"); |
71 flib_log_e("null parameter in flib_roomlist_add"); |
59 } else { |
72 } else { |
60 flib_roomlist_room *tmpRoom = fillRoomFromParams(params); |
73 flib_room *tmpRoom = fillRoomFromParams(params); |
61 if(tmpRoom) { |
74 if(tmpRoom) { |
62 flib_roomlist_room **rooms = flib_list_insert(list->rooms, &list->roomCount, sizeof(*list->rooms), &tmpRoom, 0); |
75 if(!insertRoom(&list->rooms, &list->roomCount, tmpRoom, 0)) { |
63 if(rooms) { |
|
64 list->rooms = rooms; |
|
65 tmpRoom = NULL; |
76 tmpRoom = NULL; |
66 result = 0; |
77 result = 0; |
67 } |
78 } |
68 } |
79 } |
69 flib_roomlist_room_destroy(tmpRoom); |
80 flib_roomlist_room_destroy(tmpRoom); |
70 } |
81 } |
71 return result; |
82 return result; |
72 } |
83 } |
73 |
84 |
74 static int findRoom(flib_roomlist *list, const char *name) { |
85 int flib_roomlist_delete(flib_roomlist *list, const char *name) { |
75 for(int i=0; i<list->roomCount; i++) { |
86 int result = -1; |
76 if(!strcmp(name, list->rooms[i]->name)) { |
87 if(!list || !name) { |
77 return i; |
88 flib_log_e("null parameter in flib_roomlist_delete"); |
|
89 } else { |
|
90 int roomid = findRoom(list, name); |
|
91 if(roomid<0) { |
|
92 flib_log_w("Attempt to delete unknown room %s", name); |
|
93 } else { |
|
94 flib_room *room = list->rooms[roomid]; |
|
95 if(!deleteRoom(&list->rooms, &list->roomCount, roomid)) { |
|
96 flib_roomlist_room_destroy(room); |
|
97 result = 0; |
|
98 } |
78 } |
99 } |
79 } |
100 } |
80 return -1; |
101 return result; |
81 } |
102 } |
82 |
103 |
83 int flib_roomlist_update(flib_roomlist *list, const char *name, char **params) { |
104 int flib_roomlist_update(flib_roomlist *list, const char *name, char **params) { |
84 int result = -1; |
105 int result = -1; |
85 if(!list || !name || !params) { |
106 if(!list || !name || !params) { |
86 flib_log_e("null parameter in flib_roomlist_update"); |
107 flib_log_e("null parameter in flib_roomlist_update"); |
87 } else { |
108 } else { |
88 flib_roomlist_room *tmpRoom = fillRoomFromParams(params); |
109 flib_room *tmpRoom = fillRoomFromParams(params); |
89 int roomid = findRoom(list, name); |
110 int roomid = findRoom(list, name); |
90 if(tmpRoom && roomid>=0) { |
111 if(tmpRoom && roomid>=0) { |
91 flib_roomlist_room_destroy(list->rooms[roomid]); |
112 flib_roomlist_room_destroy(list->rooms[roomid]); |
92 list->rooms[roomid] = tmpRoom; |
113 list->rooms[roomid] = tmpRoom; |
93 tmpRoom = NULL; |
114 tmpRoom = NULL; |
96 flib_roomlist_room_destroy(tmpRoom); |
117 flib_roomlist_room_destroy(tmpRoom); |
97 } |
118 } |
98 return result; |
119 return result; |
99 } |
120 } |
100 |
121 |
101 flib_roomlist_room *flib_roomlist_find(flib_roomlist *list, const char *name) { |
122 flib_room *flib_roomlist_find(const flib_roomlist *list, const char *name) { |
102 flib_roomlist_room *result = NULL; |
123 flib_room *result = NULL; |
103 if(!list || !name) { |
124 if(!list || !name) { |
104 flib_log_e("null parameter in flib_roomlist_find"); |
125 flib_log_e("null parameter in flib_roomlist_find"); |
105 } else { |
126 } else { |
106 int roomid = findRoom(list, name); |
127 int roomid = findRoom(list, name); |
107 if(roomid>=0) { |
128 if(roomid>=0) { |