core/MenuVoting.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Jul 12 09:05:19 2008 +0000 (2008-07-12)
changeset 2201 d2b8bc01ccd8
parent 2056 f1fcde46ac7c
child 2289 44045ead61cd
permissions -rw-r--r--
Builds against latest OB SDK now
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <string.h>
ds@1952
    33
#include <stdlib.h>
ds@1952
    34
#include "MenuVoting.h"
ds@1952
    35
#include "PlayerManager.h"
ds@1952
    36
#include "sourcemm_api.h"
ds@1952
    37
ds@1952
    38
float g_next_vote = 0.0f;
ds@1952
    39
ds@1952
    40
#if defined ORANGEBOX_BUILD
ds@1952
    41
void OnVoteDelayChange(IConVar *cvar, const char *value, float flOldValue);
ds@1952
    42
#else
ds@1952
    43
void OnVoteDelayChange(ConVar *cvar, const char *value);
ds@1952
    44
#endif
ds@1952
    45
ConVar sm_vote_delay("sm_vote_delay",
ds@1952
    46
					 "30",
ds@1952
    47
					 0,
ds@1952
    48
					 "Sets the recommended time in between public votes",
ds@1952
    49
					 false,
ds@1952
    50
					 0.0,
ds@1952
    51
					 false,
ds@1952
    52
					 0.0,
ds@1952
    53
					 OnVoteDelayChange);
ds@1952
    54
ds@1952
    55
#if defined ORANGEBOX_BUILD
ds@1952
    56
void OnVoteDelayChange(IConVar *cvar, const char *value, float flOldValue)
ds@1952
    57
#else
ds@1952
    58
void OnVoteDelayChange(ConVar *cvar, const char *value)
ds@1952
    59
#endif
ds@1952
    60
{
ds@1952
    61
	/* See if the new vote delay isn't something we need to account for */
ds@1952
    62
	if (sm_vote_delay.GetFloat() < 1.0f)
ds@1952
    63
	{
ds@1952
    64
		g_next_vote = 0.0f;
ds@1952
    65
		return;
ds@1952
    66
	}
ds@1952
    67
ds@1952
    68
	/* If there was never a last vote, ignore this change */
ds@1952
    69
	if (g_next_vote < 0.1f)
ds@1952
    70
	{
ds@1952
    71
		return;
ds@1952
    72
	}
ds@1952
    73
ds@1952
    74
	/* Subtract the original value, then add the new one. */
ds@1952
    75
	g_next_vote -= (float)atof(value);
ds@1952
    76
	g_next_vote += sm_vote_delay.GetFloat();
ds@1952
    77
}
ds@1952
    78
ds@1952
    79
unsigned int VoteMenuHandler::GetRemainingVoteDelay()
ds@1952
    80
{
ds@1952
    81
	if (g_next_vote <= gpGlobals->curtime)
ds@1952
    82
	{
ds@1952
    83
		return 0;
ds@1952
    84
	}
ds@1952
    85
ds@1952
    86
	return (unsigned int)(g_next_vote - gpGlobals->curtime);
ds@1952
    87
}
ds@1952
    88
ds@1952
    89
void VoteMenuHandler::OnSourceModAllInitialized()
ds@1952
    90
{
ds@1952
    91
	g_Players.AddClientListener(this);
ds@1952
    92
}
ds@1952
    93
ds@1952
    94
void VoteMenuHandler::OnSourceModShutdown()
ds@1952
    95
{
ds@1952
    96
	g_Players.RemoveClientListener(this);
ds@1952
    97
}
ds@1952
    98
ds@1952
    99
void VoteMenuHandler::OnSourceModLevelChange(const char *mapName)
ds@1952
   100
{
ds@1952
   101
	g_next_vote = 0.0f;
ds@1952
   102
}
ds@1952
   103
ds@1952
   104
unsigned int VoteMenuHandler::GetMenuAPIVersion2()
ds@1952
   105
{
ds@1952
   106
	return m_pHandler->GetMenuAPIVersion2();
ds@1952
   107
}
ds@1952
   108
ds@1952
   109
void VoteMenuHandler::OnClientDisconnected(int client)
ds@1952
   110
{
ds@1952
   111
	if (!IsVoteInProgress())
ds@1952
   112
	{
ds@1952
   113
		return;
ds@1952
   114
	}
ds@1952
   115
dvander@2056
   116
	/* Wipe out their vote if they had one.  We have to make sure the the the
dvander@2056
   117
	 * newly connected client is not allowed to vote. 
dvander@2056
   118
	 */
ds@1952
   119
	int item;
dvander@2056
   120
	if ((item = m_ClientVotes[client]) >= -1)
ds@1952
   121
	{
dvander@2056
   122
		if (item >= 0)
dvander@2056
   123
		{
dvander@2056
   124
			assert((unsigned)item < m_Items);
dvander@2056
   125
			assert(m_Votes[item] > 0);
dvander@2056
   126
			m_Votes[item]--;
dvander@2056
   127
		}
dvander@2056
   128
		m_ClientVotes[client] = -2;
ds@1952
   129
	}
ds@1952
   130
}
ds@1952
   131
ds@1952
   132
bool VoteMenuHandler::IsVoteInProgress()
ds@1952
   133
{
ds@1952
   134
	return (m_pCurMenu != NULL);
ds@1952
   135
}
ds@1952
   136
dvander@2056
   137
bool VoteMenuHandler::StartVote(IBaseMenu *menu,
dvander@2056
   138
								unsigned int num_clients,
dvander@2056
   139
								int clients[],
dvander@2056
   140
								unsigned int max_time,
dvander@2056
   141
								unsigned int flags/* =0 */)
ds@1952
   142
{
ds@1952
   143
	if (!InitializeVoting(menu, menu->GetHandler(), max_time, flags))
ds@1952
   144
	{
ds@1952
   145
		return false;
ds@1952
   146
	}
ds@1952
   147
dvander@2056
   148
	/* Note: we can use game time and not universal time because 
dvander@2056
   149
	 * if we're voting then players are in-game.
dvander@2056
   150
	 */
dvander@2056
   151
ds@1952
   152
	float fVoteDelay = sm_vote_delay.GetFloat();
ds@1952
   153
	if (fVoteDelay < 1.0)
ds@1952
   154
	{
ds@1952
   155
		g_next_vote = 0.0;
dvander@2056
   156
	}
dvander@2056
   157
	else
dvander@2056
   158
	{
ds@1952
   159
		/* This little trick breaks for infinite votes!
ds@1952
   160
		 * However, we just ignore that since those 1) shouldn't exist and 
ds@1952
   161
		 * 2) people must be checking IsVoteInProgress() beforehand anyway.
ds@1952
   162
		 */
ds@1952
   163
		g_next_vote = gpGlobals->curtime + fVoteDelay + (float)max_time;
ds@1952
   164
	}
ds@1952
   165
dvander@2056
   166
	m_fStartTime = gpGlobals->curtime;
dvander@2056
   167
	m_nMenuTime = max_time;
dvander@2056
   168
ds@1952
   169
	for (unsigned int i=0; i<num_clients; i++)
ds@1952
   170
	{
dvander@2056
   171
		if (clients[i] < 1 || clients[i] > 256)
dvander@2056
   172
		{
dvander@2056
   173
			continue;
dvander@2056
   174
		}
ds@1952
   175
		menu->Display(clients[i], max_time, this);
ds@1952
   176
	}
ds@1952
   177
ds@1952
   178
	StartVoting();
ds@1952
   179
ds@1952
   180
	return true;
ds@1952
   181
}
ds@1952
   182
dvander@2056
   183
bool VoteMenuHandler::IsClientInVotePool(int client)
dvander@2056
   184
{
dvander@2056
   185
	if (client < 1 
dvander@2056
   186
		|| client > g_Players.MaxClients() 
dvander@2056
   187
		|| m_pCurMenu == NULL)
dvander@2056
   188
	{
dvander@2056
   189
		return false;
dvander@2056
   190
	}
dvander@2056
   191
dvander@2056
   192
	return (m_ClientVotes[client] > -2);
dvander@2056
   193
}
dvander@2056
   194
dvander@2056
   195
bool VoteMenuHandler::GetClientVoteChoice(int client, unsigned int *pItem)
dvander@2056
   196
{
dvander@2056
   197
	if (!IsClientInVotePool(client)
dvander@2056
   198
		|| m_ClientVotes[client] == -1)
dvander@2056
   199
	{
dvander@2056
   200
		return false;
dvander@2056
   201
	}
dvander@2056
   202
dvander@2056
   203
	*pItem = m_ClientVotes[client];
dvander@2056
   204
dvander@2056
   205
	return true;
dvander@2056
   206
}
dvander@2056
   207
dvander@2056
   208
bool VoteMenuHandler::RedrawToClient(int client)
dvander@2056
   209
{
dvander@2056
   210
	unsigned int time_limit;
dvander@2056
   211
dvander@2056
   212
	if (!IsClientInVotePool(client))
dvander@2056
   213
	{
dvander@2056
   214
		return false;
dvander@2056
   215
	}
dvander@2056
   216
dvander@2056
   217
	if (m_nMenuTime == MENU_TIME_FOREVER)
dvander@2056
   218
	{
dvander@2056
   219
		time_limit = m_nMenuTime;
dvander@2056
   220
	}
dvander@2056
   221
	else
dvander@2056
   222
	{
dvander@2056
   223
		time_limit = (int)((float)m_nMenuTime - (gpGlobals->curtime - m_fStartTime));
dvander@2056
   224
		
dvander@2056
   225
		/* Make sure this doesn't round to zero */
dvander@2056
   226
		if (time_limit == MENU_TIME_FOREVER)
dvander@2056
   227
		{
dvander@2056
   228
			time_limit = 1;
dvander@2056
   229
		}
dvander@2056
   230
	}
dvander@2056
   231
dvander@2056
   232
	return m_pCurMenu->Display(client, time_limit, this);
dvander@2056
   233
}
dvander@2056
   234
ds@1952
   235
bool VoteMenuHandler::InitializeVoting(IBaseMenu *menu, 
ds@1952
   236
									   IMenuHandler *handler,
ds@1952
   237
									   unsigned int time,
ds@1952
   238
									   unsigned int flags)
ds@1952
   239
{
ds@1952
   240
	if (IsVoteInProgress())
ds@1952
   241
	{
ds@1952
   242
		return false;
ds@1952
   243
	}
ds@1952
   244
ds@1952
   245
	InternalReset();
ds@1952
   246
ds@1952
   247
	/* Mark all clients as not voting */
ds@1952
   248
	for (int i=1; i<=gpGlobals->maxClients; i++)
ds@1952
   249
	{
ds@1952
   250
		m_ClientVotes[i] = -2;
ds@1952
   251
	}
ds@1952
   252
ds@1952
   253
	m_Items = menu->GetItemCount();
ds@1952
   254
ds@1952
   255
	if (m_Votes.size() < (size_t)m_Items)
ds@1952
   256
	{
ds@1952
   257
		/* Only clear the items we need to... */
ds@1952
   258
		size_t size = m_Votes.size();
ds@1952
   259
		for (size_t i=0; i<size; i++)
ds@1952
   260
		{
ds@1952
   261
			m_Votes[i] = 0;
ds@1952
   262
		}
ds@1952
   263
		m_Votes.resize(m_Items, 0);
dvander@2056
   264
	}
dvander@2056
   265
	else
dvander@2056
   266
	{
ds@1952
   267
		for (unsigned int i=0; i<m_Items; i++)
ds@1952
   268
		{
ds@1952
   269
			m_Votes[i] = 0;
ds@1952
   270
		}
ds@1952
   271
	}
ds@1952
   272
ds@1952
   273
	m_pCurMenu = menu;
ds@1952
   274
	m_VoteTime = time;
ds@1952
   275
	m_VoteFlags = flags;
ds@1952
   276
	m_pHandler = handler;
ds@1952
   277
ds@1952
   278
	m_pHandler->OnMenuStart(m_pCurMenu);
ds@1952
   279
ds@1952
   280
	return true;
ds@1952
   281
}
ds@1952
   282
ds@1952
   283
void VoteMenuHandler::StartVoting()
ds@1952
   284
{
ds@1952
   285
	if (!m_pCurMenu)
ds@1952
   286
	{
ds@1952
   287
		return;
ds@1952
   288
	}
ds@1952
   289
ds@1952
   290
	m_bStarted = true;
ds@1952
   291
ds@1952
   292
	m_pHandler->OnMenuVoteStart(m_pCurMenu);
ds@1952
   293
ds@1952
   294
	/* By now we know how many clients were set.  
ds@1952
   295
	 * If there are none, we should end IMMEDIATELY.
ds@1952
   296
	 */
ds@1952
   297
	if (m_Clients == 0)
ds@1952
   298
	{
ds@1952
   299
		EndVoting();
ds@1952
   300
	}
ds@1952
   301
}
ds@1952
   302
ds@1952
   303
void VoteMenuHandler::DecrementPlayerCount()
ds@1952
   304
{
ds@1952
   305
	assert(m_Clients > 0);
ds@1952
   306
ds@1952
   307
	m_Clients--;
ds@1952
   308
ds@1952
   309
	if (m_bStarted && m_Clients == 0)
ds@1952
   310
	{
ds@1952
   311
		EndVoting();
ds@1952
   312
	}
ds@1952
   313
}
ds@1952
   314
ds@1952
   315
int SortVoteItems(const void *item1, const void *item2)
ds@1952
   316
{
ds@1952
   317
	return ((menu_vote_result_t::menu_item_vote_t *)item2)->count 
ds@1952
   318
		- ((menu_vote_result_t::menu_item_vote_t *)item1)->count;
ds@1952
   319
}
ds@1952
   320
ds@1952
   321
void VoteMenuHandler::EndVoting()
ds@1952
   322
{
ds@1952
   323
	/* Set when the next delay ends.  We ignore cancellation because a menu
ds@1952
   324
	 * was, at one point, displayed, which is all that counts.  However, we
ds@1952
   325
	 * do re-calculate the time just in case the menu had no time limit.
ds@1952
   326
	 */
ds@1952
   327
	float fVoteDelay = sm_vote_delay.GetFloat();
ds@1952
   328
	if (fVoteDelay < 1.0)
ds@1952
   329
	{
ds@1952
   330
		g_next_vote = 0.0;
dvander@2056
   331
	}
dvander@2056
   332
	else
dvander@2056
   333
	{
ds@1952
   334
		g_next_vote = gpGlobals->curtime + fVoteDelay;
ds@1952
   335
	}
ds@1952
   336
ds@1952
   337
	if (m_bCancelled)
ds@1952
   338
	{
ds@1952
   339
		/* If we were cancelled, don't bother tabulating anything.
ds@1952
   340
		 * Reset just in case someone tries to redraw, which means
ds@1952
   341
		 * we need to save our states.
ds@1952
   342
		 */
ds@1952
   343
		IBaseMenu *menu = m_pCurMenu;
ds@1952
   344
		IMenuHandler *handler = m_pHandler;
ds@1952
   345
		InternalReset();
ds@1952
   346
		handler->OnMenuVoteCancel(menu, VoteCancel_Generic);
ds@1952
   347
		handler->OnMenuEnd(menu, MenuEnd_VotingCancelled);
ds@1952
   348
		return;
ds@1952
   349
	}
ds@1952
   350
ds@1952
   351
	menu_vote_result_t vote;
ds@1952
   352
	menu_vote_result_t::menu_client_vote_t client_vote[256];
ds@1952
   353
	menu_vote_result_t::menu_item_vote_t item_vote[256];
ds@1952
   354
ds@1952
   355
	memset(&vote, 0, sizeof(vote));
ds@1952
   356
ds@1952
   357
	/* Build the item list */
ds@1952
   358
	for (unsigned int i=0; i<m_Items; i++)
ds@1952
   359
	{
ds@1952
   360
		if (m_Votes[i] > 0)
ds@1952
   361
		{
ds@1952
   362
			item_vote[vote.num_items].count = m_Votes[i];
ds@1952
   363
			item_vote[vote.num_items].item = i;
ds@1952
   364
			vote.num_votes += m_Votes[i];
ds@1952
   365
			vote.num_items++;
ds@1952
   366
		}
ds@1952
   367
	}
ds@1952
   368
	vote.item_list = item_vote;
ds@1952
   369
ds@1952
   370
	if (!vote.num_votes)
ds@1952
   371
	{
ds@1952
   372
		IBaseMenu *menu = m_pCurMenu;
ds@1952
   373
		IMenuHandler *handler = m_pHandler;
ds@1952
   374
		InternalReset();
ds@1952
   375
		handler->OnMenuVoteCancel(menu, VoteCancel_NoVotes);
ds@1952
   376
		handler->OnMenuEnd(menu, MenuEnd_VotingCancelled);
ds@1952
   377
		return;
ds@1952
   378
	}
ds@1952
   379
ds@1952
   380
	/* Build the client list */
ds@1952
   381
	for (int i=1; i<=gpGlobals->maxClients; i++)
ds@1952
   382
	{
ds@1952
   383
		if (m_ClientVotes[i] >= -1)
ds@1952
   384
		{
ds@1952
   385
			client_vote[vote.num_clients].client = i;
ds@1952
   386
			client_vote[vote.num_clients].item = m_ClientVotes[i];
ds@1952
   387
			vote.num_clients++;
ds@1952
   388
		}
ds@1952
   389
	}
ds@1952
   390
	vote.client_list = client_vote;
ds@1952
   391
ds@1952
   392
	/* Sort the item list descending like we promised */
ds@1952
   393
	qsort(item_vote,
ds@1952
   394
		vote.num_items,
ds@1952
   395
		sizeof(menu_vote_result_t::menu_item_vote_t),
ds@1952
   396
		SortVoteItems);
ds@1952
   397
ds@1952
   398
	/* Save states, then clear what we've saved.
ds@1952
   399
	 * This makes us re-entrant, which is always the safe way to go.
ds@1952
   400
	 */
ds@1952
   401
	IBaseMenu *menu = m_pCurMenu;
ds@1952
   402
	IMenuHandler *handler = m_pHandler;
ds@1952
   403
	InternalReset();
ds@1952
   404
ds@1952
   405
	/* Send vote info */
ds@1952
   406
	handler->OnMenuVoteResults(menu, &vote);
ds@1952
   407
	handler->OnMenuEnd(menu, MenuEnd_VotingDone);
ds@1952
   408
}
ds@1952
   409
ds@1952
   410
void VoteMenuHandler::OnMenuStart(IBaseMenu *menu)
ds@1952
   411
{
ds@1952
   412
	m_Clients++;
ds@1952
   413
}
ds@1952
   414
ds@1952
   415
void VoteMenuHandler::OnMenuEnd(IBaseMenu *menu, MenuEndReason reason)
ds@1952
   416
{
ds@1952
   417
	DecrementPlayerCount();
ds@1952
   418
}
ds@1952
   419
ds@1952
   420
void VoteMenuHandler::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
ds@1952
   421
{
ds@1952
   422
	m_pHandler->OnMenuCancel(menu, client, reason);
ds@1952
   423
}
ds@1952
   424
ds@1952
   425
void VoteMenuHandler::OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
ds@1952
   426
{
ds@1952
   427
	m_ClientVotes[client] = -1;
ds@1952
   428
	m_pHandler->OnMenuDisplay(menu, client, display);
ds@1952
   429
}
ds@1952
   430
ds@1952
   431
unsigned int VoteMenuHandler::OnMenuDisplayItem(IBaseMenu *menu, int client, IMenuPanel *panel, unsigned int item, const ItemDrawInfo &dr)
ds@1952
   432
{
ds@1952
   433
	return m_pHandler->OnMenuDisplayItem(menu, client, panel, item, dr);
ds@1952
   434
}
ds@1952
   435
ds@1952
   436
void VoteMenuHandler::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
ds@1952
   437
{
ds@1952
   438
	m_pHandler->OnMenuDrawItem(menu, client, item, style);
ds@1952
   439
}
ds@1952
   440
ds@1952
   441
void VoteMenuHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
ds@1952
   442
{
ds@1952
   443
	/* Check by our item count, NOT the vote array size */
ds@1952
   444
	if (item < m_Items)
ds@1952
   445
	{
ds@1952
   446
		m_ClientVotes[client] = item;
ds@1952
   447
		m_Votes[item]++;
ds@1952
   448
		m_NumVotes++;
ds@1952
   449
	}
ds@1952
   450
ds@1952
   451
	m_pHandler->OnMenuSelect(menu, client, item);
ds@1952
   452
}
ds@1952
   453
ds@1952
   454
void VoteMenuHandler::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
ds@1952
   455
{
ds@1952
   456
	if (m_pHandler->GetMenuAPIVersion2() >= 13)
ds@1952
   457
	{
ds@1952
   458
		m_pHandler->OnMenuSelect2(menu, client, item, item_on_page);
ds@1952
   459
	}
ds@1952
   460
}
ds@1952
   461
ds@1952
   462
void VoteMenuHandler::InternalReset()
ds@1952
   463
{
ds@1952
   464
	m_Clients = 0;
ds@1952
   465
	m_Items = 0;
ds@1952
   466
	m_bStarted = false;
ds@1952
   467
	m_pCurMenu = NULL;
ds@1952
   468
	m_NumVotes = 0;
ds@1952
   469
	m_bCancelled = false;
ds@1952
   470
	m_pHandler = NULL;
ds@1952
   471
}
ds@1952
   472
ds@1952
   473
void VoteMenuHandler::CancelVoting()
ds@1952
   474
{
ds@1952
   475
	if (m_bCancelled || !m_pCurMenu)
ds@1952
   476
	{
ds@1952
   477
		return;
ds@1952
   478
	}
ds@1952
   479
	m_bCancelled = true;
ds@1952
   480
	m_pCurMenu->Cancel();
ds@1952
   481
}
ds@1952
   482
ds@1952
   483
IBaseMenu *VoteMenuHandler::GetCurrentMenu()
ds@1952
   484
{
ds@1952
   485
	return m_pCurMenu;
ds@1952
   486
}
ds@1952
   487
ds@1952
   488
bool VoteMenuHandler::IsCancelling()
ds@1952
   489
{
ds@1952
   490
	return m_bCancelled;
ds@1952
   491
}