Freeciv-3.2: /fast/freeciv/nightly/S3_2/client/voteinfo.c Source File

Freeciv-3.2

Loading...
Searching...
No Matches
voteinfo.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <time.h>
19
20/* utility */
21#include "log.h"
22
23/* common */
24#include "packets.h"
25
26/* client/include */
27#include "voteinfo_bar_g.h"
28
29/* client */
30#include "client_main.h"
31#include "clinet.h"
32#include "options.h"
33
34#include "voteinfo.h"
35
36
37/* Define struct voteinfo_list type. */
38#define SPECLIST_TAG voteinfo
39#define SPECLIST_TYPE struct voteinfo
40#include "speclist.h"
41#define voteinfo_list_iterate(alist, pitem)\
42 TYPED_LIST_ITERATE(struct voteinfo, alist, pitem)
43#define voteinfo_list_iterate_end LIST_ITERATE_END
44
45 static struct voteinfo_list *voteinfo_queue = NULL;
46 static int voteinfo_queue_current_index = 0;
47
48
49/**********************************************************************/
53 void voteinfo_queue_delayed_remove(int vote_no)
54{
55 struct voteinfo *vi;
56
57 fc_assert_ret_msg(NULL != voteinfo_queue,
58 "%s() called before votinfo_queue_init()!",
59 __FUNCTION__);
60
61 vi = voteinfo_queue_find(vote_no);
62 if (vi == NULL) {
63 return;
64 }
65 vi->remove_time = time(NULL);
66}
67
68/**********************************************************************/
72 void voteinfo_queue_check_removed(void)
73{
74 time_t now;
75 struct voteinfo_list *removed;
76
77 if (voteinfo_queue == NULL) {
78 return;
79 }
80
81 now = time(NULL);
82 removed = voteinfo_list_new();
83 voteinfo_list_iterate(voteinfo_queue, vi) {
84 if (vi != NULL && vi->remove_time > 0 && now - vi->remove_time > 2) {
85 voteinfo_list_append(removed, vi);
86 }
87 } voteinfo_list_iterate_end;
88
89 voteinfo_list_iterate(removed, vi) {
90 voteinfo_queue_remove(vi->vote_no);
91 } voteinfo_list_iterate_end;
92
93 if (voteinfo_list_size(removed) > 0) {
94 voteinfo_gui_update();
95 }
96
97 voteinfo_list_destroy(removed);
98}
99
100/**********************************************************************/
103 void voteinfo_queue_remove(int vote_no)
104{
105 struct voteinfo *vi;
106
107 fc_assert_ret_msg(NULL != voteinfo_queue,
108 "%s() called before votinfo_queue_init()!",
109 __FUNCTION__);
110
111 vi = voteinfo_queue_find(vote_no);
112 if (vi == NULL) {
113 return;
114 }
115
116 voteinfo_list_remove(voteinfo_queue, vi);
117 free(vi);
118}
119
120/**********************************************************************/
123 void voteinfo_queue_add(int vote_no, const char *user, const char *desc,
124 int percent_required, int flags)
125{
126 struct voteinfo *vi;
127
128 fc_assert_ret_msg(NULL != voteinfo_queue,
129 "%s() called before votinfo_queue_init()!",
130 __FUNCTION__);
131
132 vi = fc_calloc(1, sizeof(struct voteinfo));
133 vi->vote_no = vote_no;
134 sz_strlcpy(vi->user, user);
135 sz_strlcpy(vi->desc, desc);
136 vi->percent_required = percent_required;
137 vi->flags = flags;
138
139 if (gui_options.voteinfo_bar_new_at_front) {
140 voteinfo_list_prepend(voteinfo_queue, vi);
141 voteinfo_queue_current_index = 0;
142 } else {
143 voteinfo_list_append(voteinfo_queue, vi);
144 }
145}
146
147/**********************************************************************/
150 struct voteinfo *voteinfo_queue_find(int vote_no)
151{
152 fc_assert_ret_val_msg(NULL != voteinfo_queue, NULL,
153 "%s() called before votinfo_queue_init()!",
154 __FUNCTION__);
155
156 voteinfo_list_iterate(voteinfo_queue, vi) {
157 if (vi->vote_no == vote_no) {
158 return vi;
159 }
160 } voteinfo_list_iterate_end;
161 return NULL;
162}
163
164/**********************************************************************/
167 void voteinfo_queue_init(void)
168{
169 if (voteinfo_queue != NULL) {
170 voteinfo_queue_free();
171 }
172 voteinfo_queue = voteinfo_list_new();
173 voteinfo_queue_current_index = 0;
174}
175
176/**********************************************************************/
179 void voteinfo_queue_free(void)
180{
181 if (voteinfo_queue == NULL) {
182 return;
183 }
184
185 voteinfo_list_iterate(voteinfo_queue, vi) {
186 if (vi != NULL) {
187 free(vi);
188 }
189 } voteinfo_list_iterate_end;
190
191 voteinfo_list_destroy(voteinfo_queue);
192 voteinfo_queue = NULL;
193 voteinfo_queue_current_index = 0;
194}
195
196/**********************************************************************/
201 struct voteinfo *voteinfo_queue_get_current(int *pindex)
202{
203 struct voteinfo *vi;
204 int size;
205
206 if (voteinfo_queue == NULL) {
207 return NULL;
208 }
209
210 size = voteinfo_list_size(voteinfo_queue);
211
212 if (size <= 0) {
213 return NULL;
214 }
215
216 if (!(0 <= voteinfo_queue_current_index
217 && voteinfo_queue_current_index < size)) {
218 voteinfo_queue_next();
219 }
220
221 vi = voteinfo_list_get(voteinfo_queue, voteinfo_queue_current_index);
222
223 if (vi != NULL && pindex != NULL) {
224 *pindex = voteinfo_queue_current_index;
225 }
226
227 return vi;
228}
229
230/**********************************************************************/
234 void voteinfo_do_vote(int vote_no, enum client_vote_type vote)
235{
236 struct voteinfo *vi;
237 struct packet_vote_submit packet;
238
239 if (!can_client_control()) {
240 return;
241 }
242
243 vi = voteinfo_queue_find(vote_no);
244 if (vi == NULL) {
245 return;
246 }
247
248 packet.vote_no = vi->vote_no;
249
250 switch (vote) {
251 case CVT_YES:
252 packet.value = 1;
253 break;
254 case CVT_NO:
255 packet.value = -1;
256 break;
257 case CVT_ABSTAIN:
258 packet.value = 0;
259 break;
260 default:
261 return;
262 break;
263 }
264
265 send_packet_vote_submit(&client.conn, &packet);
266 vi->client_vote = vote;
267}
268
269/**********************************************************************/
272 void voteinfo_queue_next(void)
273{
274 int size;
275
276 if (voteinfo_queue == NULL) {
277 return;
278 }
279
280 size = voteinfo_list_size(voteinfo_queue);
281
282 voteinfo_queue_current_index++;
283 if (voteinfo_queue_current_index >= size) {
284 voteinfo_queue_current_index = 0;
285 }
286}
287
288/**********************************************************************/
291 int voteinfo_queue_size(void)
292{
293 return (NULL != voteinfo_queue ? voteinfo_list_size(voteinfo_queue) : 0);
294}
295
296/**********************************************************************/
299 bool voteinfo_bar_can_be_shown(void)
300{
301 return (NULL != voteinfo_queue
302 && gui_options.voteinfo_bar_use
303 && (gui_options.voteinfo_bar_always_show
304 || (0 < voteinfo_list_size(voteinfo_queue)
305 && NULL != voteinfo_queue_get_current(NULL)))
306 && (!gui_options.voteinfo_bar_hide_when_not_player
307 || (client_has_player()
308 && !client_is_observer())));
309}
can_client_control
bool can_client_control(void)
Definition client_main.c:1253
client_is_observer
bool client_is_observer(void)
Definition client_main.c:1083
client
struct civclient client
Definition client_main.c:150
client_has_player
bool client_has_player(void)
Definition client_main.c:1364
client_main.h
clinet.h
incite_cost
char * incite_cost
Definition comments.c:74
voteinfo_gui_update
void voteinfo_gui_update(void)
Definition voteinfo_bar.c:214
log.h
fc_assert_ret_msg
#define fc_assert_ret_msg(condition, message,...)
Definition log.h:205
fc_assert_ret_val_msg
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:208
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
gui_options
struct client_options gui_options
Definition options.c:71
options.h
packets.h
send_packet_vote_submit
int send_packet_vote_submit(struct connection *pc, const struct packet_vote_submit *packet)
Definition packets_gen.c:74944
speclist.h
size
size_t size
Definition specvec.h:72
civclient::conn
struct connection conn
Definition client_main.h:96
client_options::voteinfo_bar_use
bool voteinfo_bar_use
Definition options.h:178
client_options::voteinfo_bar_hide_when_not_player
bool voteinfo_bar_hide_when_not_player
Definition options.h:180
client_options::voteinfo_bar_new_at_front
bool voteinfo_bar_new_at_front
Definition options.h:181
client_options::voteinfo_bar_always_show
bool voteinfo_bar_always_show
Definition options.h:179
packet_vote_submit
Definition packets_gen.h:1653
packet_vote_submit::vote_no
int vote_no
Definition packets_gen.h:1654
packet_vote_submit::value
int value
Definition packets_gen.h:1655
vote
Definition voting.h:46
voteinfo
Definition voteinfo.h:29
voteinfo::desc
char desc[512]
Definition voteinfo.h:33
voteinfo::flags
int flags
Definition voteinfo.h:35
voteinfo::vote_no
int vote_no
Definition voteinfo.h:31
voteinfo::percent_required
int percent_required
Definition voteinfo.h:34
voteinfo::user
char user[MAX_LEN_NAME]
Definition voteinfo.h:32
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:189
voteinfo_queue_init
void voteinfo_queue_init(void)
Definition voteinfo.c:167
voteinfo_queue_current_index
static int voteinfo_queue_current_index
Definition voteinfo.c:46
voteinfo_queue_delayed_remove
void voteinfo_queue_delayed_remove(int vote_no)
Definition voteinfo.c:53
voteinfo_do_vote
void voteinfo_do_vote(int vote_no, enum client_vote_type vote)
Definition voteinfo.c:234
voteinfo_bar_can_be_shown
bool voteinfo_bar_can_be_shown(void)
Definition voteinfo.c:299
voteinfo_queue_remove
void voteinfo_queue_remove(int vote_no)
Definition voteinfo.c:103
voteinfo_queue_find
struct voteinfo * voteinfo_queue_find(int vote_no)
Definition voteinfo.c:150
voteinfo_queue_get_current
struct voteinfo * voteinfo_queue_get_current(int *pindex)
Definition voteinfo.c:201
voteinfo_list_iterate
#define voteinfo_list_iterate(alist, pitem)
Definition voteinfo.c:41
voteinfo_queue_next
void voteinfo_queue_next(void)
Definition voteinfo.c:272
voteinfo_queue_size
int voteinfo_queue_size(void)
Definition voteinfo.c:291
voteinfo_queue_check_removed
void voteinfo_queue_check_removed(void)
Definition voteinfo.c:72
voteinfo_queue
static struct voteinfo_list * voteinfo_queue
Definition voteinfo.c:45
voteinfo_list_iterate_end
#define voteinfo_list_iterate_end
Definition voteinfo.c:43
voteinfo_queue_free
void voteinfo_queue_free(void)
Definition voteinfo.c:179
voteinfo_queue_add
void voteinfo_queue_add(int vote_no, const char *user, const char *desc, int percent_required, int flags)
Definition voteinfo.c:123
voteinfo.h
client_vote_type
client_vote_type
Definition voteinfo.h:22
CVT_YES
@ CVT_YES
Definition voteinfo.h:24
CVT_NO
@ CVT_NO
Definition voteinfo.h:25
CVT_ABSTAIN
@ CVT_ABSTAIN
Definition voteinfo.h:26
voteinfo_bar_g.h