diff options
| author | oy <Tom_Adams@web.de> | 2011-03-26 22:06:29 +0100 |
|---|---|---|
| committer | oy <Tom_Adams@web.de> | 2011-03-26 22:06:29 +0100 |
| commit | 5b328e54842b128881237ed608705b596f310ce8 (patch) | |
| tree | 6700438579549faf9295bd5622826145c2976284 /src | |
| parent | 6e1b6b102a623d1e9b93566491bd9dc525fdcbaa (diff) | |
| download | zcatch-5b328e54842b128881237ed608705b596f310ce8.tar.gz zcatch-5b328e54842b128881237ed608705b596f310ce8.zip | |
added a limit for the number of votes that can be added to the server and send options as lists when a client joins. Closes #459
Diffstat (limited to 'src')
| -rw-r--r-- | src/game/client/components/voting.cpp | 77 | ||||
| -rw-r--r-- | src/game/client/components/voting.h | 1 | ||||
| -rw-r--r-- | src/game/server/gamecontext.cpp | 79 | ||||
| -rw-r--r-- | src/game/server/gamecontext.h | 1 | ||||
| -rw-r--r-- | src/game/voting.h | 2 |
5 files changed, 133 insertions, 27 deletions
diff --git a/src/game/client/components/voting.cpp b/src/game/client/components/voting.cpp index 0eddf805..feeb96f7 100644 --- a/src/game/client/components/voting.cpp +++ b/src/game/client/components/voting.cpp @@ -123,6 +123,32 @@ CVoting::CVoting() OnReset(); } +void CVoting::AddOption(const char *pDescription) +{ + CVoteOptionClient *pOption; + if(m_pRecycleFirst) + { + pOption = m_pRecycleFirst; + m_pRecycleFirst = m_pRecycleFirst->m_pNext; + if(m_pRecycleFirst) + m_pRecycleFirst->m_pPrev = 0; + else + m_pRecycleLast = 0; + } + else + pOption = (CVoteOptionClient *)m_Heap.Allocate(sizeof(CVoteOptionClient)); + + pOption->m_pNext = 0; + pOption->m_pPrev = m_pLast; + if(pOption->m_pPrev) + pOption->m_pPrev->m_pNext = pOption; + m_pLast = pOption; + if(!m_pFirst) + m_pFirst = pOption; + + str_copy(pOption->m_aDescription, pDescription, sizeof(pOption->m_aDescription)); + ++m_NumVoteOptions; +} void CVoting::ClearOptions() { @@ -178,33 +204,36 @@ void CVoting::OnMessage(int MsgType, void *pRawMsg) { ClearOptions(); } - else if(MsgType == NETMSGTYPE_SV_VOTEOPTIONADD) + else if(MsgType == NETMSGTYPE_SV_VOTEOPTIONLISTADD) { - CNetMsg_Sv_VoteOptionAdd *pMsg = (CNetMsg_Sv_VoteOptionAdd *)pRawMsg; - - CVoteOptionClient *pOption; - if(m_pRecycleFirst) + CNetMsg_Sv_VoteOptionListAdd *pMsg = (CNetMsg_Sv_VoteOptionListAdd *)pRawMsg; + int NumOptions = pMsg->m_NumOptions; + for(int i = 0; i < NumOptions; ++i) { - pOption = m_pRecycleFirst; - m_pRecycleFirst = m_pRecycleFirst->m_pNext; - if(m_pRecycleFirst) - m_pRecycleFirst->m_pPrev = 0; - else - m_pRecycleLast = 0; + switch(i) + { + case 0: AddOption(pMsg->m_pDescription0); break; + case 1: AddOption(pMsg->m_pDescription1); break; + case 2: AddOption(pMsg->m_pDescription2); break; + case 3: AddOption(pMsg->m_pDescription3); break; + case 4: AddOption(pMsg->m_pDescription4); break; + case 5: AddOption(pMsg->m_pDescription5); break; + case 6: AddOption(pMsg->m_pDescription6); break; + case 7: AddOption(pMsg->m_pDescription7); break; + case 8: AddOption(pMsg->m_pDescription8); break; + case 9: AddOption(pMsg->m_pDescription9); break; + case 10: AddOption(pMsg->m_pDescription10); break; + case 11: AddOption(pMsg->m_pDescription11); break; + case 12: AddOption(pMsg->m_pDescription12); break; + case 13: AddOption(pMsg->m_pDescription13); break; + case 14: AddOption(pMsg->m_pDescription14); + } } - else - pOption = (CVoteOptionClient *)m_Heap.Allocate(sizeof(CVoteOptionClient)); - - pOption->m_pNext = 0; - pOption->m_pPrev = m_pLast; - if(pOption->m_pPrev) - pOption->m_pPrev->m_pNext = pOption; - m_pLast = pOption; - if(!m_pFirst) - m_pFirst = pOption; - - str_copy(pOption->m_aDescription, pMsg->m_pDescription, sizeof(pOption->m_aDescription)); - ++m_NumVoteOptions; + } + else if(MsgType == NETMSGTYPE_SV_VOTEOPTIONADD) + { + CNetMsg_Sv_VoteOptionAdd *pMsg = (CNetMsg_Sv_VoteOptionAdd *)pRawMsg; + AddOption(pMsg->m_pDescription); } else if(MsgType == NETMSGTYPE_SV_VOTEOPTIONREMOVE) { diff --git a/src/game/client/components/voting.h b/src/game/client/components/voting.h index cfba0605..1f8fb8cf 100644 --- a/src/game/client/components/voting.h +++ b/src/game/client/components/voting.h @@ -22,6 +22,7 @@ class CVoting : public CComponent int m_Voted; int m_Yes, m_No, m_Pass, m_Total; + void AddOption(const char *pDescription); void ClearOptions(); void Callvote(const char *pType, const char *pValue, const char *pReason); diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 5e3af09a..907542f2 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -32,6 +32,7 @@ void CGameContext::Construct(int Resetting) m_VoteCloseTime = 0; m_pVoteOptionFirst = 0; m_pVoteOptionLast = 0; + m_NumVoteOptions = 0; if(Resetting==NO_RESET) m_pVoteOptionHeap = new CHeap(); @@ -60,6 +61,7 @@ void CGameContext::Clear() CHeap *pVoteOptionHeap = m_pVoteOptionHeap; CVoteOptionServer *pVoteOptionFirst = m_pVoteOptionFirst; CVoteOptionServer *pVoteOptionLast = m_pVoteOptionLast; + int NumVoteOptions = m_NumVoteOptions; CTuningParams Tuning = m_Tuning; m_Resetting = true; @@ -70,6 +72,7 @@ void CGameContext::Clear() m_pVoteOptionHeap = pVoteOptionHeap; m_pVoteOptionFirst = pVoteOptionFirst; m_pVoteOptionLast = pVoteOptionLast; + m_NumVoteOptions = NumVoteOptions; m_Tuning = Tuning; } @@ -825,14 +828,73 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) // send vote options CNetMsg_Sv_VoteClearOptions ClearMsg; Server()->SendPackMsg(&ClearMsg, MSGFLAG_VITAL, ClientID); + + CNetMsg_Sv_VoteOptionListAdd OptionMsg; + int NumOptions = 0; + OptionMsg.m_pDescription1 = ""; + OptionMsg.m_pDescription2 = ""; + OptionMsg.m_pDescription3 = ""; + OptionMsg.m_pDescription4 = ""; + OptionMsg.m_pDescription5 = ""; + OptionMsg.m_pDescription6 = ""; + OptionMsg.m_pDescription7 = ""; + OptionMsg.m_pDescription8 = ""; + OptionMsg.m_pDescription9 = ""; + OptionMsg.m_pDescription10 = ""; + OptionMsg.m_pDescription11 = ""; + OptionMsg.m_pDescription12 = ""; + OptionMsg.m_pDescription13 = ""; + OptionMsg.m_pDescription14 = ""; CVoteOptionServer *pCurrent = m_pVoteOptionFirst; while(pCurrent) { - CNetMsg_Sv_VoteOptionAdd OptionMsg; - OptionMsg.m_pDescription = pCurrent->m_aDescription; - Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); + switch(NumOptions++) + { + case 0: OptionMsg.m_pDescription0 = pCurrent->m_aDescription; break; + case 1: OptionMsg.m_pDescription1 = pCurrent->m_aDescription; break; + case 2: OptionMsg.m_pDescription2 = pCurrent->m_aDescription; break; + case 3: OptionMsg.m_pDescription3 = pCurrent->m_aDescription; break; + case 4: OptionMsg.m_pDescription4 = pCurrent->m_aDescription; break; + case 5: OptionMsg.m_pDescription5 = pCurrent->m_aDescription; break; + case 6: OptionMsg.m_pDescription6 = pCurrent->m_aDescription; break; + case 7: OptionMsg.m_pDescription7 = pCurrent->m_aDescription; break; + case 8: OptionMsg.m_pDescription8 = pCurrent->m_aDescription; break; + case 9: OptionMsg.m_pDescription9 = pCurrent->m_aDescription; break; + case 10: OptionMsg.m_pDescription10 = pCurrent->m_aDescription; break; + case 11: OptionMsg.m_pDescription11 = pCurrent->m_aDescription; break; + case 12: OptionMsg.m_pDescription12 = pCurrent->m_aDescription; break; + case 13: OptionMsg.m_pDescription13 = pCurrent->m_aDescription; break; + case 14: + { + OptionMsg.m_pDescription14 = pCurrent->m_aDescription; + OptionMsg.m_NumOptions = NumOptions; + Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); + OptionMsg = CNetMsg_Sv_VoteOptionListAdd(); + NumOptions = 0; + OptionMsg.m_pDescription1 = ""; + OptionMsg.m_pDescription2 = ""; + OptionMsg.m_pDescription3 = ""; + OptionMsg.m_pDescription4 = ""; + OptionMsg.m_pDescription5 = ""; + OptionMsg.m_pDescription6 = ""; + OptionMsg.m_pDescription7 = ""; + OptionMsg.m_pDescription8 = ""; + OptionMsg.m_pDescription9 = ""; + OptionMsg.m_pDescription10 = ""; + OptionMsg.m_pDescription11 = ""; + OptionMsg.m_pDescription12 = ""; + OptionMsg.m_pDescription13 = ""; + OptionMsg.m_pDescription14 = ""; + } + } pCurrent = pCurrent->m_pNext; } + if(NumOptions > 0) + { + OptionMsg.m_NumOptions = NumOptions; + Server()->SendPackMsg(&OptionMsg, MSGFLAG_VITAL, ClientID); + NumOptions = 0; + } // send tuning parameters to client SendTuningParams(ClientID); @@ -994,6 +1056,12 @@ void CGameContext::ConAddVote(IConsole::IResult *pResult, void *pUserData) CGameContext *pSelf = (CGameContext *)pUserData; const char *pDescription = pResult->GetString(0); const char *pCommand = pResult->GetString(1); + + if(pSelf->m_NumVoteOptions == MAX_VOTE_OPTIONS) + { + pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "maximum number of vote options reached"); + return; + } // check for valid option if(!pSelf->Console()->LineIsValid(pCommand) || str_length(pCommand) >= VOTE_CMD_LENGTH) @@ -1026,6 +1094,7 @@ void CGameContext::ConAddVote(IConsole::IResult *pResult, void *pUserData) } // add the option + ++pSelf->m_NumVoteOptions; int Len = str_length(pCommand); pOption = (CVoteOptionServer *)pSelf->m_pVoteOptionHeap->Allocate(sizeof(CVoteOptionServer) + Len); @@ -1072,6 +1141,7 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData) // TODO: improve this // remove the option + --pSelf->m_NumVoteOptions; char aBuf[256]; str_format(aBuf, sizeof(aBuf), "removed option '%s' '%s'", pOption->m_aDescription, pOption->m_aCommand); pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); @@ -1079,6 +1149,7 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData) CHeap *pVoteOptionHeap = new CHeap(); CVoteOptionServer *pVoteOptionFirst = 0; CVoteOptionServer *pVoteOptionLast = 0; + int NumVoteOptions = pSelf->m_NumVoteOptions; for(CVoteOptionServer *pSrc = pSelf->m_pVoteOptionFirst; pSrc; pSrc = pSrc->m_pNext) { if(pSrc == pOption) @@ -1104,6 +1175,7 @@ void CGameContext::ConRemoveVote(IConsole::IResult *pResult, void *pUserData) pSelf->m_pVoteOptionHeap = pVoteOptionHeap; pSelf->m_pVoteOptionFirst = pVoteOptionFirst; pSelf->m_pVoteOptionLast = pVoteOptionLast; + pSelf->m_NumVoteOptions = NumVoteOptions; // inform clients about removed option CNetMsg_Sv_VoteOptionRemove OptionMsg; @@ -1188,6 +1260,7 @@ void CGameContext::ConClearVotes(IConsole::IResult *pResult, void *pUserData) pSelf->m_pVoteOptionHeap->Reset(); pSelf->m_pVoteOptionFirst = 0; pSelf->m_pVoteOptionLast = 0; + pSelf->m_NumVoteOptions = 0; } void CGameContext::ConVote(IConsole::IResult *pResult, void *pUserData) diff --git a/src/game/server/gamecontext.h b/src/game/server/gamecontext.h index 399e0058..38100a06 100644 --- a/src/game/server/gamecontext.h +++ b/src/game/server/gamecontext.h @@ -99,6 +99,7 @@ public: char m_aVoteDescription[VOTE_DESC_LENGTH]; char m_aVoteCommand[VOTE_CMD_LENGTH]; char m_aVoteReason[VOTE_REASON_LENGTH]; + int m_NumVoteOptions; int m_VoteEnforce; enum { diff --git a/src/game/voting.h b/src/game/voting.h index 8b31d93d..e1706caf 100644 --- a/src/game/voting.h +++ b/src/game/voting.h @@ -8,6 +8,8 @@ enum VOTE_DESC_LENGTH=64, VOTE_CMD_LENGTH=512, VOTE_REASON_LENGTH=16, + + MAX_VOTE_OPTIONS=128, }; struct CVoteOptionClient |