about summary refs log tree commit diff
path: root/src/game/client/components/voting.cpp
blob: dcf5c954ddabdf97b801f68c7613ab66187261d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <engine/e_client_interface.h>
#include <game/generated/g_protocol.hpp>
#include <base/vmath.hpp>
#include <game/client/render.hpp>
//#include <game/client/gameclient.hpp>
#include "voting.hpp"

void VOTING::con_callvote(void *result, void *user_data)
{
	VOTING *self = (VOTING*)user_data;
	self->callvote(console_arg_string(result, 0), console_arg_string(result, 1));
}

void VOTING::con_vote(void *result, void *user_data)
{
	VOTING *self = (VOTING *)user_data;
	if(str_comp_nocase(console_arg_string(result, 0), "yes") == 0)
		self->vote(1);
	else if(str_comp_nocase(console_arg_string(result, 0), "no") == 0)
		self->vote(-1);
}

void VOTING::callvote(const char *type, const char *value)
{
	NETMSG_CL_CALLVOTE msg = {0};
	msg.type = type;
	msg.value = value;
	msg.pack(MSGFLAG_VITAL);
	client_send_msg();
}

void VOTING::callvote_kick(int client_id)
{
	char buf[32];
	str_format(buf, sizeof(buf), "%d", client_id);
	callvote("kick", buf);
}

void VOTING::callvote_option(int option_id)
{
	VOTEOPTION *option = this->first;
	while(option && option_id >= 0)
	{
		if(option_id == 0)
		{
			callvote("option", option->command);
			break;
		}
		
		option_id--;
		option = option->next;
	}
}

void VOTING::vote(int v)
{
	NETMSG_CL_VOTE msg = {v};
	msg.pack(MSGFLAG_VITAL);
	client_send_msg();
}

VOTING::VOTING()
{
	heap = 0;
	clearoptions();
	on_reset();
}


void VOTING::clearoptions()
{
	if(heap)
		memheap_destroy(heap);
	heap = memheap_create();
	
	first = 0;
	last = 0;
}

void VOTING::on_reset()
{
	closetime = 0;
	description[0] = 0;
	command[0] = 0;
	yes = no = pass = total = 0;
	voted = 0;
}

void VOTING::on_console_init()
{
	MACRO_REGISTER_COMMAND("callvote", "sr", CFGFLAG_CLIENT, con_callvote, this, "Call vote");
	MACRO_REGISTER_COMMAND("vote", "r", CFGFLAG_CLIENT, con_vote, this, "Vote yes/no");
}

void VOTING::on_message(int msgtype, void *rawmsg)
{
	if(msgtype == NETMSGTYPE_SV_VOTE_SET)
	{
		NETMSG_SV_VOTE_SET *msg = (NETMSG_SV_VOTE_SET *)rawmsg;
		if(msg->timeout)
		{
			on_reset();
			str_copy(description, msg->description, sizeof(description));
			str_copy(command, msg->command, sizeof(description));
			closetime = time_get() + time_freq() * msg->timeout;
		}
		else
			on_reset();
	}
	else if(msgtype == NETMSGTYPE_SV_VOTE_STATUS)
	{
		NETMSG_SV_VOTE_STATUS *msg = (NETMSG_SV_VOTE_STATUS *)rawmsg;
		yes = msg->yes;
		no = msg->no;
		pass = msg->pass;
		total = msg->total;
	}	
	else if(msgtype == NETMSGTYPE_SV_VOTE_CLEAROPTIONS)
	{
		clearoptions();
	}
	else if(msgtype == NETMSGTYPE_SV_VOTE_OPTION)
	{
		NETMSG_SV_VOTE_OPTION *msg = (NETMSG_SV_VOTE_OPTION *)rawmsg;
		int len = str_length(msg->command);
	
		VOTEOPTION *option = (VOTEOPTION *)memheap_allocate(heap, sizeof(VOTEOPTION) + len);
		option->next = 0;
		option->prev = last;
		if(option->prev)
			option->prev->next = option;
		last = option;
		if(!first)
			first = option;
		
		mem_copy(option->command, msg->command, len+1);

	}
}

void VOTING::on_render()
{
}


void VOTING::render_bars(CUIRect bars, bool text)
{
	RenderTools()->DrawUIRect(&bars, vec4(0.8f,0.8f,0.8f,0.5f), CUI::CORNER_ALL, bars.h/3);
	
	CUIRect splitter = bars;
	splitter.x = splitter.x+splitter.w/2;
	splitter.w = splitter.h/2.0f;
	splitter.x -= splitter.w/2;
	RenderTools()->DrawUIRect(&splitter, vec4(0.4f,0.4f,0.4f,0.5f), CUI::CORNER_ALL, splitter.h/4);
			
	if(total)
	{
		CUIRect pass_area = bars;
		if(yes)
		{
			CUIRect yes_area = bars;
			yes_area.w *= yes/(float)total;
			RenderTools()->DrawUIRect(&yes_area, vec4(0.2f,0.9f,0.2f,0.85f), CUI::CORNER_ALL, bars.h/3);
			
			if(text)
			{
				char buf[256];
				str_format(buf, sizeof(buf), "%d", yes);
				UI()->DoLabel(&yes_area, buf, bars.h*0.75f, 0);
			}
			
			pass_area.x += yes_area.w;
			pass_area.w -= yes_area.w;
		}
		
		if(no)
		{
			CUIRect no_area = bars;
			no_area.w *= no/(float)total;
			no_area.x = (bars.x + bars.w)-no_area.w;
			RenderTools()->DrawUIRect(&no_area, vec4(0.9f,0.2f,0.2f,0.85f), CUI::CORNER_ALL, bars.h/3);
			
			if(text)
			{
				char buf[256];
				str_format(buf, sizeof(buf), "%d", no);
				UI()->DoLabel(&no_area, buf, bars.h*0.75f, 0);
			}

			pass_area.w -= no_area.w;
		}

		if(text && pass)
		{
			char buf[256];
			str_format(buf, sizeof(buf), "%d", pass);
			UI()->DoLabel(&pass_area, buf, bars.h*0.75f, 0);
		}
	}	
}