core/MenuVoting.cpp
changeset 2056 f1fcde46ac7c
parent 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- a/core/MenuVoting.cpp	Sat May 17 05:10:36 2008 +0000
     1.2 +++ b/core/MenuVoting.cpp	Sat May 17 05:12:52 2008 +0000
     1.3 @@ -113,14 +113,19 @@
     1.4  		return;
     1.5  	}
     1.6  
     1.7 -	/* Wipe out their vote if they had one */
     1.8 +	/* Wipe out their vote if they had one.  We have to make sure the the the
     1.9 +	 * newly connected client is not allowed to vote. 
    1.10 +	 */
    1.11  	int item;
    1.12 -	if ((item = m_ClientVotes[client]) >= 0)
    1.13 +	if ((item = m_ClientVotes[client]) >= -1)
    1.14  	{
    1.15 -		assert((unsigned)item < m_Items);
    1.16 -		assert(m_Votes[item] > 0);
    1.17 -		m_Votes[item]--;
    1.18 -		m_ClientVotes[client] = -1;
    1.19 +		if (item >= 0)
    1.20 +		{
    1.21 +			assert((unsigned)item < m_Items);
    1.22 +			assert(m_Votes[item] > 0);
    1.23 +			m_Votes[item]--;
    1.24 +		}
    1.25 +		m_ClientVotes[client] = -2;
    1.26  	}
    1.27  }
    1.28  
    1.29 @@ -129,18 +134,28 @@
    1.30  	return (m_pCurMenu != NULL);
    1.31  }
    1.32  
    1.33 -bool VoteMenuHandler::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags/* =0 */)
    1.34 +bool VoteMenuHandler::StartVote(IBaseMenu *menu,
    1.35 +								unsigned int num_clients,
    1.36 +								int clients[],
    1.37 +								unsigned int max_time,
    1.38 +								unsigned int flags/* =0 */)
    1.39  {
    1.40  	if (!InitializeVoting(menu, menu->GetHandler(), max_time, flags))
    1.41  	{
    1.42  		return false;
    1.43  	}
    1.44  
    1.45 +	/* Note: we can use game time and not universal time because 
    1.46 +	 * if we're voting then players are in-game.
    1.47 +	 */
    1.48 +
    1.49  	float fVoteDelay = sm_vote_delay.GetFloat();
    1.50  	if (fVoteDelay < 1.0)
    1.51  	{
    1.52  		g_next_vote = 0.0;
    1.53 -	} else {
    1.54 +	}
    1.55 +	else
    1.56 +	{
    1.57  		/* This little trick breaks for infinite votes!
    1.58  		 * However, we just ignore that since those 1) shouldn't exist and 
    1.59  		 * 2) people must be checking IsVoteInProgress() beforehand anyway.
    1.60 @@ -148,8 +163,15 @@
    1.61  		g_next_vote = gpGlobals->curtime + fVoteDelay + (float)max_time;
    1.62  	}
    1.63  
    1.64 +	m_fStartTime = gpGlobals->curtime;
    1.65 +	m_nMenuTime = max_time;
    1.66 +
    1.67  	for (unsigned int i=0; i<num_clients; i++)
    1.68  	{
    1.69 +		if (clients[i] < 1 || clients[i] > 256)
    1.70 +		{
    1.71 +			continue;
    1.72 +		}
    1.73  		menu->Display(clients[i], max_time, this);
    1.74  	}
    1.75  
    1.76 @@ -158,6 +180,58 @@
    1.77  	return true;
    1.78  }
    1.79  
    1.80 +bool VoteMenuHandler::IsClientInVotePool(int client)
    1.81 +{
    1.82 +	if (client < 1 
    1.83 +		|| client > g_Players.MaxClients() 
    1.84 +		|| m_pCurMenu == NULL)
    1.85 +	{
    1.86 +		return false;
    1.87 +	}
    1.88 +
    1.89 +	return (m_ClientVotes[client] > -2);
    1.90 +}
    1.91 +
    1.92 +bool VoteMenuHandler::GetClientVoteChoice(int client, unsigned int *pItem)
    1.93 +{
    1.94 +	if (!IsClientInVotePool(client)
    1.95 +		|| m_ClientVotes[client] == -1)
    1.96 +	{
    1.97 +		return false;
    1.98 +	}
    1.99 +
   1.100 +	*pItem = m_ClientVotes[client];
   1.101 +
   1.102 +	return true;
   1.103 +}
   1.104 +
   1.105 +bool VoteMenuHandler::RedrawToClient(int client)
   1.106 +{
   1.107 +	unsigned int time_limit;
   1.108 +
   1.109 +	if (!IsClientInVotePool(client))
   1.110 +	{
   1.111 +		return false;
   1.112 +	}
   1.113 +
   1.114 +	if (m_nMenuTime == MENU_TIME_FOREVER)
   1.115 +	{
   1.116 +		time_limit = m_nMenuTime;
   1.117 +	}
   1.118 +	else
   1.119 +	{
   1.120 +		time_limit = (int)((float)m_nMenuTime - (gpGlobals->curtime - m_fStartTime));
   1.121 +		
   1.122 +		/* Make sure this doesn't round to zero */
   1.123 +		if (time_limit == MENU_TIME_FOREVER)
   1.124 +		{
   1.125 +			time_limit = 1;
   1.126 +		}
   1.127 +	}
   1.128 +
   1.129 +	return m_pCurMenu->Display(client, time_limit, this);
   1.130 +}
   1.131 +
   1.132  bool VoteMenuHandler::InitializeVoting(IBaseMenu *menu, 
   1.133  									   IMenuHandler *handler,
   1.134  									   unsigned int time,
   1.135 @@ -187,7 +261,9 @@
   1.136  			m_Votes[i] = 0;
   1.137  		}
   1.138  		m_Votes.resize(m_Items, 0);
   1.139 -	} else {
   1.140 +	}
   1.141 +	else
   1.142 +	{
   1.143  		for (unsigned int i=0; i<m_Items; i++)
   1.144  		{
   1.145  			m_Votes[i] = 0;
   1.146 @@ -252,7 +328,9 @@
   1.147  	if (fVoteDelay < 1.0)
   1.148  	{
   1.149  		g_next_vote = 0.0;
   1.150 -	} else {
   1.151 +	}
   1.152 +	else
   1.153 +	{
   1.154  		g_next_vote = gpGlobals->curtime + fVoteDelay;
   1.155  	}
   1.156