about summary refs log tree commit diff
diff options
context:
space:
mode:
authoroy <Tom_Adams@web.de>2011-03-26 17:44:34 +0100
committeroy <Tom_Adams@web.de>2011-03-26 17:44:34 +0100
commitfbeace52723509604e390f0a20e9f922aa26d1d6 (patch)
treeea60feca2bf3346ef62ce12339e40573dfea9762
parentb8f144ba811708bfac82faed438ce389f3d01863 (diff)
downloadzcatch-fbeace52723509604e390f0a20e9f922aa26d1d6.tar.gz
zcatch-fbeace52723509604e390f0a20e9f922aa26d1d6.zip
cleaned up some voting code
-rw-r--r--src/game/client/components/menus.h3
-rw-r--r--src/game/client/components/menus_ingame.cpp8
-rw-r--r--src/game/client/components/voting.cpp13
-rw-r--r--src/game/client/components/voting.h26
-rw-r--r--src/game/server/gamecontext.cpp39
-rw-r--r--src/game/server/gamecontext.h18
-rw-r--r--src/game/voting.h28
7 files changed, 80 insertions, 55 deletions
diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h
index a9921655..f9a0fea1 100644
--- a/src/game/client/components/menus.h
+++ b/src/game/client/components/menus.h
@@ -8,6 +8,7 @@
 
 #include <engine/demo.h>
 
+#include <game/voting.h>
 #include <game/client/component.h>
 #include <game/client/ui.h>
 
@@ -170,7 +171,7 @@ class CMenus : public CComponent
 	// for call vote
 	int m_CallvoteSelectedOption;
 	int m_CallvoteSelectedPlayer;
-	char m_aCallvoteReason[16];
+	char m_aCallvoteReason[VOTE_REASON_LENGTH];
 	
 	// demo
 	struct CDemoItem
diff --git a/src/game/client/components/menus_ingame.cpp b/src/game/client/components/menus_ingame.cpp
index 4b593989..01efda0a 100644
--- a/src/game/client/components/menus_ingame.cpp
+++ b/src/game/client/components/menus_ingame.cpp
@@ -325,16 +325,12 @@ void CMenus::RenderServerInfo(CUIRect MainView)
 
 void CMenus::RenderServerControlServer(CUIRect MainView)
 {
-	int NumOptions = 0;
-	for(CVoting::CVoteOption *pOption = m_pClient->m_pVoting->m_pFirst; pOption; pOption = pOption->m_pNext)
-		NumOptions++;
-
 	static int s_VoteList = 0;
 	static float s_ScrollValue = 0;
 	CUIRect List = MainView;
-	UiDoListboxStart(&s_VoteList, &List, 24.0f, Localize("Settings"), "", NumOptions, 1, m_CallvoteSelectedOption, s_ScrollValue);
+	UiDoListboxStart(&s_VoteList, &List, 24.0f, Localize("Settings"), "", m_pClient->m_pVoting->m_NumVoteOptions, 1, m_CallvoteSelectedOption, s_ScrollValue);
 	
-	for(CVoting::CVoteOption *pOption = m_pClient->m_pVoting->m_pFirst; pOption; pOption = pOption->m_pNext)
+	for(CVoteOptionClient *pOption = m_pClient->m_pVoting->m_pFirst; pOption; pOption = pOption->m_pNext)
 	{
 		CListboxItem Item = UiDoListboxNextItem(pOption);
 		
diff --git a/src/game/client/components/voting.cpp b/src/game/client/components/voting.cpp
index d8c87855..55343313 100644
--- a/src/game/client/components/voting.cpp
+++ b/src/game/client/components/voting.cpp
@@ -65,7 +65,7 @@ void CVoting::CallvoteKick(int ClientID, const char *pReason, bool ForceVote)
 
 void CVoting::CallvoteOption(int OptionId, const char *pReason, bool ForceVote)
 {
-	CVoteOption *pOption = m_pFirst;
+	CVoteOptionClient *pOption = m_pFirst;
 	while(pOption && OptionId >= 0)
 	{
 		if(OptionId == 0)
@@ -102,7 +102,8 @@ CVoting::CVoting()
 void CVoting::ClearOptions()
 {
 	m_Heap.Reset();
-	
+
+	m_NumVoteOptions = 0;	
 	m_pFirst = 0;
 	m_pLast = 0;
 
@@ -156,7 +157,7 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg)
 	{
 		CNetMsg_Sv_VoteOptionAdd *pMsg = (CNetMsg_Sv_VoteOptionAdd *)pRawMsg;
 	
-		CVoteOption *pOption;
+		CVoteOptionClient *pOption;
 		if(m_pRecycleFirst)
 		{
 			pOption = m_pRecycleFirst;
@@ -167,7 +168,7 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg)
 				m_pRecycleLast = 0;
 		}
 		else
-			pOption = (CVoteOption *)m_Heap.Allocate(sizeof(CVoteOption));
+			pOption = (CVoteOptionClient *)m_Heap.Allocate(sizeof(CVoteOptionClient));
 
 		pOption->m_pNext = 0;
 		pOption->m_pPrev = m_pLast;
@@ -178,12 +179,13 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg)
 			m_pFirst = pOption;
 		
 		str_copy(pOption->m_aDescription, pMsg->m_pDescription, sizeof(pOption->m_aDescription));
+		++m_NumVoteOptions;
 	}
 	else if(MsgType == NETMSGTYPE_SV_VOTEOPTIONREMOVE)
 	{
 		CNetMsg_Sv_VoteOptionRemove *pMsg = (CNetMsg_Sv_VoteOptionRemove *)pRawMsg;
 	
-		for(CVoteOption *pOption = m_pFirst; pOption; pOption = pOption->m_pNext)
+		for(CVoteOptionClient *pOption = m_pFirst; pOption; pOption = pOption->m_pNext)
 		{
 			if(str_comp(pOption->m_aDescription, pMsg->m_pDescription) == 0)
 			{
@@ -196,6 +198,7 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg)
 					pOption->m_pPrev->m_pNext = pOption->m_pNext;
 				if(pOption->m_pNext)
 					pOption->m_pNext->m_pPrev = pOption->m_pPrev;
+				--m_NumVoteOptions;
 
 				// add it to recycle list
 				pOption->m_pNext = 0;
diff --git a/src/game/client/components/voting.h b/src/game/client/components/voting.h
index 9e7da2dc..d2a88883 100644
--- a/src/game/client/components/voting.h
+++ b/src/game/client/components/voting.h
@@ -2,9 +2,12 @@
 /* If you are missing that file, acquire a complete release at teeworlds.com.                */
 #ifndef GAME_CLIENT_COMPONENTS_VOTING_H
 #define GAME_CLIENT_COMPONENTS_VOTING_H
+
+#include <engine/shared/memheap.h>
+
+#include <game/voting.h>
 #include <game/client/component.h>
 #include <game/client/ui.h>
-#include <engine/shared/memheap.h>
 
 class CVoting : public CComponent
 {
@@ -14,8 +17,8 @@ class CVoting : public CComponent
 	static void ConVote(IConsole::IResult *pResult, void *pUserData);
 	
 	int64 m_Closetime;
-	char m_aDescription[64];
-	char m_aReason[16];
+	char m_aDescription[VOTE_DESC_LENGTH];
+	char m_aReason[VOTE_REASON_LENGTH];
 	int m_Voted;
 	int m_Yes, m_No, m_Pass, m_Total;
 	
@@ -23,19 +26,12 @@ class CVoting : public CComponent
 	void Callvote(const char *pType, const char *pValue, const char *pReason);
 	
 public:
+	int m_NumVoteOptions;	
+	CVoteOptionClient *m_pFirst;
+	CVoteOptionClient *m_pLast;
 
-	struct CVoteOption
-	{
-		CVoteOption *m_pNext;
-		CVoteOption *m_pPrev;
-		char m_aDescription[64];
-	};
-	
-	CVoteOption *m_pFirst;
-	CVoteOption *m_pLast;
-
-	CVoteOption *m_pRecycleFirst;
-	CVoteOption *m_pRecycleLast;
+	CVoteOptionClient *m_pRecycleFirst;
+	CVoteOptionClient *m_pRecycleLast;
 
 	CVoting();
 	virtual void OnReset();
diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp
index 8245d508..5e3af09a 100644
--- a/src/game/server/gamecontext.cpp
+++ b/src/game/server/gamecontext.cpp
@@ -58,8 +58,8 @@ CGameContext::~CGameContext()
 void CGameContext::Clear()
 {
 	CHeap *pVoteOptionHeap = m_pVoteOptionHeap;
-	CVoteOption *pVoteOptionFirst = m_pVoteOptionFirst;
-	CVoteOption *pVoteOptionLast = m_pVoteOptionLast;
+	CVoteOptionServer *pVoteOptionFirst = m_pVoteOptionFirst;
+	CVoteOptionServer *pVoteOptionLast = m_pVoteOptionLast;
 	CTuningParams Tuning = m_Tuning;
 
 	m_Resetting = true;
@@ -628,14 +628,14 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID)
 		}
 		
 		char aChatmsg[512] = {0};
-		char aDesc[64] = {0};
-		char aCmd[512] = {0};
+		char aDesc[VOTE_DESC_LENGTH] = {0};
+		char aCmd[VOTE_CMD_LENGTH] = {0};
 		CNetMsg_Cl_CallVote *pMsg = (CNetMsg_Cl_CallVote *)pRawMsg;
 		const char *pReason = pMsg->m_Reason[0] ? pMsg->m_Reason : "No reason given";
 
 		if(str_comp_nocase(pMsg->m_Type, "option") == 0)
 		{
-			CVoteOption *pOption = m_pVoteOptionFirst;
+			CVoteOptionServer *pOption = m_pVoteOptionFirst;
 			while(pOption)
 			{
 				if(str_comp_nocase(pMsg->m_Value, pOption->m_aDescription) == 0)
@@ -825,7 +825,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID)
 		// send vote options
 		CNetMsg_Sv_VoteClearOptions ClearMsg;
 		Server()->SendPackMsg(&ClearMsg, MSGFLAG_VITAL, ClientID);
-		CVoteOption *pCurrent = m_pVoteOptionFirst;
+		CVoteOptionServer *pCurrent = m_pVoteOptionFirst;
 		while(pCurrent)
 		{
 			CNetMsg_Sv_VoteOptionAdd OptionMsg;
@@ -996,16 +996,23 @@ void CGameContext::ConAddVote(IConsole::IResult *pResult, void *pUserData)
 	const char *pCommand = pResult->GetString(1);
 	
 	// check for valid option
-	if(!pSelf->Console()->LineIsValid(pCommand))
+	if(!pSelf->Console()->LineIsValid(pCommand) || str_length(pCommand) >= VOTE_CMD_LENGTH)
 	{
 		char aBuf[256];
-		str_format(aBuf, sizeof(aBuf), "skipped invalid option '%s'", pCommand);
+		str_format(aBuf, sizeof(aBuf), "skipped invalid command '%s'", pCommand);
+		pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
+		return;
+	}
+	if(str_length(pDescription) >= VOTE_DESC_LENGTH)
+	{
+		char aBuf[256];
+		str_format(aBuf, sizeof(aBuf), "skipped invalid option '%s'", pDescription);
 		pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
 		return;
 	}
 	
 	// check for duplicate entry
-	CGameContext::CVoteOption *pOption = pSelf->m_pVoteOptionFirst;
+	CVoteOptionServer *pOption = pSelf->m_pVoteOptionFirst;
 	while(pOption)
 	{
 		if(str_comp_nocase(pDescription, pOption->m_aDescription) == 0)
@@ -1021,7 +1028,7 @@ void CGameContext::ConAddVote(IConsole::IResult *pResult, void *pUserData)
 	// add the option
 	int Len = str_length(pCommand);
 	
-	pOption = (CGameContext::CVoteOption *)pSelf->m_pVoteOptionHeap->Allocate(sizeof(CGameContext::CVoteOption) + Len);
+	pOption = (CVoteOptionServer *)pSelf->m_pVoteOptionHeap->Allocate(sizeof(CVoteOptionServer) + Len);
 	pOption->m_pNext = 0;
 	pOption->m_pPrev = pSelf->m_pVoteOptionLast;
 	if(pOption->m_pPrev)
@@ -1048,7 +1055,7 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData)
 	const char *pDescription = pResult->GetString(0);
 	
 	// check for valid option
-	CGameContext::CVoteOption *pOption = pSelf->m_pVoteOptionFirst;
+	CVoteOptionServer *pOption = pSelf->m_pVoteOptionFirst;
 	while(pOption)
 	{
 		if(str_comp_nocase(pDescription, pOption->m_aDescription) == 0)
@@ -1070,16 +1077,16 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData)
 	pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
 
 	CHeap *pVoteOptionHeap = new CHeap();
-	CVoteOption *pVoteOptionFirst = 0;
-	CVoteOption *pVoteOptionLast = 0;
-	for(CVoteOption *pSrc = pSelf->m_pVoteOptionFirst; pSrc; pSrc = pSrc->m_pNext)
+	CVoteOptionServer *pVoteOptionFirst = 0;
+	CVoteOptionServer *pVoteOptionLast = 0;
+	for(CVoteOptionServer *pSrc = pSelf->m_pVoteOptionFirst; pSrc; pSrc = pSrc->m_pNext)
 	{
 		if(pSrc == pOption)
 			continue;
 
 		// copy option
 		int Len = str_length(pSrc->m_aCommand);
-		CVoteOption *pDst = (CGameContext::CVoteOption *)pVoteOptionHeap->Allocate(sizeof(CGameContext::CVoteOption) + Len);
+		CVoteOptionServer *pDst = (CVoteOptionServer *)pVoteOptionHeap->Allocate(sizeof(CVoteOptionServer) + Len);
 		pDst->m_pNext = 0;
 		pDst->m_pPrev = pVoteOptionLast;
 		if(pDst->m_pPrev)
@@ -1114,7 +1121,7 @@ void CGameContext::ConForceVote(IConsole::IResult *pResult, void *pUserData)
 
 	if(str_comp_nocase(pType, "option") == 0)
 	{
-		CVoteOption *pOption = pSelf->m_pVoteOptionFirst;
+		CVoteOptionServer *pOption = pSelf->m_pVoteOptionFirst;
 		while(pOption)
 		{
 			if(str_comp_nocase(pValue, pOption->m_aDescription) == 0)
diff --git a/src/game/server/gamecontext.h b/src/game/server/gamecontext.h
index bd8140c3..399e0058 100644
--- a/src/game/server/gamecontext.h
+++ b/src/game/server/gamecontext.h
@@ -8,6 +8,7 @@
 #include <engine/shared/memheap.h>
 
 #include <game/layers.h>
+#include <game/voting.h>
 
 #include "eventhandler.h"
 #include "gamecontroller.h"
@@ -95,9 +96,9 @@ public:
 	int64 m_VoteCloseTime;
 	bool m_VoteUpdate;
 	int m_VotePos;
-	char m_aVoteDescription[64];
-	char m_aVoteCommand[512];
-	char m_aVoteReason[16];
+	char m_aVoteDescription[VOTE_DESC_LENGTH];
+	char m_aVoteCommand[VOTE_CMD_LENGTH];
+	char m_aVoteReason[VOTE_REASON_LENGTH];
 	int m_VoteEnforce;
 	enum
 	{
@@ -105,16 +106,9 @@ public:
 		VOTE_ENFORCE_NO,
 		VOTE_ENFORCE_YES,
 	};
-	struct CVoteOption
-	{
-		CVoteOption *m_pNext;
-		CVoteOption *m_pPrev;
-		char m_aDescription[64];
-		char m_aCommand[1];
-	};
 	CHeap *m_pVoteOptionHeap;
-	CVoteOption *m_pVoteOptionFirst;
-	CVoteOption *m_pVoteOptionLast;
+	CVoteOptionServer *m_pVoteOptionFirst;
+	CVoteOptionServer *m_pVoteOptionLast;
 
 	// helper functions
 	void CreateDamageInd(vec2 Pos, float AngleMod, int Amount);
diff --git a/src/game/voting.h b/src/game/voting.h
new file mode 100644
index 00000000..8b31d93d
--- /dev/null
+++ b/src/game/voting.h
@@ -0,0 +1,28 @@
+/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
+/* If you are missing that file, acquire a complete release at teeworlds.com.                */
+#ifndef GAME_VOTING_H
+#define GAME_VOTING_H
+
+enum
+{
+	VOTE_DESC_LENGTH=64,
+	VOTE_CMD_LENGTH=512,
+	VOTE_REASON_LENGTH=16,
+};
+
+struct CVoteOptionClient
+{
+	CVoteOptionClient *m_pNext;
+	CVoteOptionClient *m_pPrev;
+	char m_aDescription[VOTE_DESC_LENGTH];
+};
+
+struct CVoteOptionServer
+{
+	CVoteOptionServer *m_pNext;
+	CVoteOptionServer *m_pPrev;
+	char m_aDescription[VOTE_DESC_LENGTH];
+	char m_aCommand[1];
+};
+
+#endif