1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU General Public License |
|
7 * as published by the Free Software Foundation; either version 2 |
|
8 * of the License, or (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 */ |
|
19 |
|
20 #include "roomlist.h" |
|
21 |
|
22 #include "../util/util.h" |
|
23 #include "../util/list.h" |
|
24 #include "../util/logging.h" |
|
25 |
|
26 #include <stdlib.h> |
|
27 #include <string.h> |
|
28 |
|
29 flib_roomlist *flib_roomlist_create() { |
|
30 return flib_calloc(1, sizeof(flib_roomlist)); |
|
31 } |
|
32 |
|
33 static void flib_roomlist_room_destroy(flib_room *room) { |
|
34 if(room) { |
|
35 free(room->map); |
|
36 free(room->name); |
|
37 free(room->owner); |
|
38 free(room->scheme); |
|
39 free(room->weapons); |
|
40 free(room); |
|
41 } |
|
42 } |
|
43 |
|
44 void flib_roomlist_destroy(flib_roomlist *list) { |
|
45 if(list) { |
|
46 for(int i=0; i<list->roomCount; i++) { |
|
47 flib_roomlist_room_destroy(list->rooms[i]); |
|
48 } |
|
49 free(list->rooms); |
|
50 free(list); |
|
51 } |
|
52 } |
|
53 |
|
54 static flib_room *fillRoomFromParams(char **params) { |
|
55 flib_room *result = NULL; |
|
56 flib_room *tmpRoom = flib_calloc(1, sizeof(flib_room)); |
|
57 if(tmpRoom) { |
|
58 tmpRoom->inProgress = !strcmp(params[0], "True"); |
|
59 tmpRoom->name = flib_strdupnull(params[1]); |
|
60 tmpRoom->playerCount = atoi(params[2]); |
|
61 tmpRoom->teamCount = atoi(params[3]); |
|
62 tmpRoom->owner = flib_strdupnull(params[4]); |
|
63 tmpRoom->map = flib_strdupnull(params[5]); |
|
64 tmpRoom->scheme = flib_strdupnull(params[6]); |
|
65 tmpRoom->weapons = flib_strdupnull(params[7]); |
|
66 if(tmpRoom->name && tmpRoom->owner && tmpRoom->map && tmpRoom->scheme && tmpRoom->weapons) { |
|
67 result = tmpRoom; |
|
68 tmpRoom = NULL; |
|
69 } |
|
70 } |
|
71 flib_roomlist_room_destroy(tmpRoom); |
|
72 return result; |
|
73 } |
|
74 |
|
75 GENERATE_STATIC_LIST_INSERT(insertRoom, flib_room*) |
|
76 GENERATE_STATIC_LIST_DELETE(deleteRoom, flib_room*) |
|
77 |
|
78 static int findRoom(const flib_roomlist *list, const char *name) { |
|
79 for(int i=0; i<list->roomCount; i++) { |
|
80 if(!strcmp(name, list->rooms[i]->name)) { |
|
81 return i; |
|
82 } |
|
83 } |
|
84 return -1; |
|
85 } |
|
86 |
|
87 int flib_roomlist_add(flib_roomlist *list, char **params) { |
|
88 int result = -1; |
|
89 if(!log_badargs_if2(list==NULL, params==NULL)) { |
|
90 flib_room *tmpRoom = fillRoomFromParams(params); |
|
91 if(tmpRoom) { |
|
92 if(!insertRoom(&list->rooms, &list->roomCount, tmpRoom, 0)) { |
|
93 tmpRoom = NULL; |
|
94 result = 0; |
|
95 } |
|
96 } |
|
97 flib_roomlist_room_destroy(tmpRoom); |
|
98 } |
|
99 return result; |
|
100 } |
|
101 |
|
102 int flib_roomlist_delete(flib_roomlist *list, const char *name) { |
|
103 int result = -1; |
|
104 if(!log_badargs_if2(list==NULL, name==NULL)) { |
|
105 int roomid = findRoom(list, name); |
|
106 if(roomid<0) { |
|
107 flib_log_w("Attempt to delete unknown room %s", name); |
|
108 } else { |
|
109 flib_room *room = list->rooms[roomid]; |
|
110 if(!deleteRoom(&list->rooms, &list->roomCount, roomid)) { |
|
111 flib_roomlist_room_destroy(room); |
|
112 result = 0; |
|
113 } |
|
114 } |
|
115 } |
|
116 return result; |
|
117 } |
|
118 |
|
119 int flib_roomlist_update(flib_roomlist *list, const char *name, char **params) { |
|
120 int result = -1; |
|
121 if(!log_badargs_if3(list==NULL, name==NULL, params==NULL)) { |
|
122 flib_room *tmpRoom = fillRoomFromParams(params); |
|
123 int roomid = findRoom(list, name); |
|
124 if(tmpRoom && roomid>=0) { |
|
125 flib_roomlist_room_destroy(list->rooms[roomid]); |
|
126 list->rooms[roomid] = tmpRoom; |
|
127 tmpRoom = NULL; |
|
128 result = 0; |
|
129 } |
|
130 flib_roomlist_room_destroy(tmpRoom); |
|
131 } |
|
132 return result; |
|
133 } |
|
134 |
|
135 flib_room *flib_roomlist_find(const flib_roomlist *list, const char *name) { |
|
136 flib_room *result = NULL; |
|
137 if(!log_badargs_if2(list==NULL, name==NULL)) { |
|
138 int roomid = findRoom(list, name); |
|
139 if(roomid>=0) { |
|
140 result = list->rooms[roomid]; |
|
141 } |
|
142 } |
|
143 return result; |
|
144 } |
|
145 |
|
146 void flib_roomlist_clear(flib_roomlist *list) { |
|
147 if(!log_badargs_if(list==NULL)) { |
|
148 for(int i=0; i<list->roomCount; i++) { |
|
149 flib_roomlist_room_destroy(list->rooms[i]); |
|
150 } |
|
151 free(list->rooms); |
|
152 list->rooms = NULL; |
|
153 list->roomCount = 0; |
|
154 } |
|
155 } |
|