Cocain
28-03-10, 12:55 PM
A small voting system, which you can use ingame.
#include "StdAfx.h"
#define SKIP_ALLOCATOR_SHARING 1
#include <ScriptSetup.h>
void SetupVoteBallot(ScriptMgr * mgr);
void SendVoteBallotToAll();
void EndVote();
enum VOTES
{
VOTE_YES = 0,
VOTE_NO,
VOTE_BLANK
};
bool vote = false;
int voteamounts[4];
int originalamounts[4];
ostringstream stream;
string question;
string answer;
TimedEvent * te;
class VoteBallot : public GossipScript
{
void GossipHello(Object* pObject, Player* Plr, bool AutoSend)
{
if(vote)
{
GossipMenu * menu;
objmgr.CreateGossipMenuForPlayer(&menu, pObject->GetGUID(), 1, Plr);
menu->AddItem(0, "Yes", 1);
menu->AddItem(0, "No", 2);
menu->AddItem(0, "Blank vote", 3);
if(AutoSend)
menu->SendTo(Plr);
}
else
{
Plr->BroadcastMessage("No active vote at the moment.");
Plr->GetItemInterface()->RemoveItemAmt(90001, Plr->GetItemInterface()->GetItemCount(90001, true));
}
}
void GossipSelectOption(Object* pObject, Player* Plr, uint32 Id, uint32 IntId, const char * EnteredCode)
{
voteamounts[IntId - 1]++;
Plr->BroadcastMessage("Thanks for voting.");
Plr->GetItemInterface()->RemoveItemAmt(90001, Plr->GetItemInterface()->GetItemCount(90001, true));
Plr->Gossip_Complete();
}
void GossipEnd(Object * pObject, Player * pPlayer)
{
}
void Destroy()
{
delete this;
}
public:
void EndVote()
{
sWorld.SendWorldText("|cff00ff00[VoteSystem]|cff8aff00Vote ended.");
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Question was \'" << question << "\'.";
sWorld.SendWorldText(stream.str().c_str());
int voteamount = 0;
int temp = 0;
for (int i = 0; i < 4; i++)
{
originalamounts[i] = voteamounts[i];
}
//Sort array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if(voteamounts[j] < voteamounts[j + 1])
{
temp = voteamounts[j];
voteamounts[j] = voteamounts[j + 1];
voteamounts[j + 1] = temp;
}
}
}
//Check for ties with the winning option, and give answer if there are ties
if(voteamounts[0] == voteamounts[1])
{
if(voteamounts[1] == voteamounts[2])
answer = "undecided. All vote amounts are equal";
else
{
answer = "undecided. 2 vote amounts are equal";
}
}
else
{
if(voteamounts[0] == originalamounts[VOTE_YES])
answer = "\'Yes\'";
else if(voteamounts[0] == originalamounts[VOTE_NO])
answer = "\'No\'";
else if(voteamounts[0] == originalamounts[VOTE_BLANK])
answer = "\'Blank\'";
}
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Answer was " << answer << ".";
sWorld.SendWorldText(stream.str().c_str());
vote = false;
}
};
VoteBallot * voteballot;
void SetupVoteBallot(ScriptMgr * mgr)
{
voteballot = new VoteBallot();
GossipScript * gs = voteballot;
mgr->register_item_gossip_script(90001, gs);
}
bool OnChat(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc)
{
string msg(Message);
string cmd = msg.substr(0, 10);
if(strcmp(cmd.c_str(), "#startvote") == 0 && pPlayer->GetSession()->GetPermissionCount())
{
if(vote)
{
pPlayer->BroadcastMessage("|cff00ff00[VoteSystem]|cff8aff00There's already a vote active. Use #endvote to end the current vote manually.");
return false;
}
unsigned int questionfound1 = msg.find(" ");
if(questionfound1 != string::npos)
{
question = msg.substr(questionfound1 + 1, msg.length());
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00<GM>" << pPlayer->GetName() << " started a vote.";
sWorld.SendWorldText(stream.str().c_str());
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Question: " << question;
sWorld.SendWorldText(stream.str().c_str());
sWorld.SendWorldText("|cff00ff00[VoteSystem]|cff8aff00Voting will end in 1 minute.");
//clean up vote amounts
for(int i = 0; i < 3;i++)
{
voteamounts[i] = 0;
}
voteamounts[3] = -1;
vote = true;
SendVoteBallotToAll();
te = TimedEvent::Allocate(voteballot, new CallbackP0<VoteBallot>(voteballot, &VoteBallot::EndVote), 0, 60000, 1);
pPlayer->event_AddEvent(te);
}
else
{
pPlayer->BroadcastMessage("|cff00ff00[VoteSystem]|cff8aff00Please use the correct syntax. For example: #startvote question?.");
}
return false;
}
else if(strcmp(msg.c_str(), "#endvote") == 0 && pPlayer->GetSession()->GetPermissionCount())
{
pPlayer->event_RemoveByPointer(te);
voteballot->EndVote();
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Vote was stopped by <GM>" << pPlayer->GetName() << ".";
sWorld.SendWorldText(stream.str().c_str());
return false;
}
else
{
return true;
}
}
void SendVoteBallotToAll()
{
PlayerStorageMap::const_iterator itr;
objmgr._playerslock.AcquireReadLock();
for (itr = objmgr._players.begin(); itr != objmgr._players.end(); itr++)
{
Item * item = objmgr.CreateItem(90001, itr->second);
itr->second->GetItemInterface()->AddItemToFreeSlot(item);
item->SoulBind();
itr->second->BroadcastMessage("A vote ballot has been added to your backpack. Right-Click it to vote.");
}
objmgr._playerslock.ReleaseReadLock();
}
extern "C" SCRIPT_DECL uint32 _exp_get_script_type()
{
return SCRIPT_TYPE_MISC;
}
extern "C" SCRIPT_DECL void _exp_script_register(ScriptMgr * mgr)
{
SetupVoteBallot(mgr);
mgr->register_hook(SERVER_HOOK_EVENT_ON_CHAT, (void*)OnChat);
}
Credits to me & Banaanaz.
#include "StdAfx.h"
#define SKIP_ALLOCATOR_SHARING 1
#include <ScriptSetup.h>
void SetupVoteBallot(ScriptMgr * mgr);
void SendVoteBallotToAll();
void EndVote();
enum VOTES
{
VOTE_YES = 0,
VOTE_NO,
VOTE_BLANK
};
bool vote = false;
int voteamounts[4];
int originalamounts[4];
ostringstream stream;
string question;
string answer;
TimedEvent * te;
class VoteBallot : public GossipScript
{
void GossipHello(Object* pObject, Player* Plr, bool AutoSend)
{
if(vote)
{
GossipMenu * menu;
objmgr.CreateGossipMenuForPlayer(&menu, pObject->GetGUID(), 1, Plr);
menu->AddItem(0, "Yes", 1);
menu->AddItem(0, "No", 2);
menu->AddItem(0, "Blank vote", 3);
if(AutoSend)
menu->SendTo(Plr);
}
else
{
Plr->BroadcastMessage("No active vote at the moment.");
Plr->GetItemInterface()->RemoveItemAmt(90001, Plr->GetItemInterface()->GetItemCount(90001, true));
}
}
void GossipSelectOption(Object* pObject, Player* Plr, uint32 Id, uint32 IntId, const char * EnteredCode)
{
voteamounts[IntId - 1]++;
Plr->BroadcastMessage("Thanks for voting.");
Plr->GetItemInterface()->RemoveItemAmt(90001, Plr->GetItemInterface()->GetItemCount(90001, true));
Plr->Gossip_Complete();
}
void GossipEnd(Object * pObject, Player * pPlayer)
{
}
void Destroy()
{
delete this;
}
public:
void EndVote()
{
sWorld.SendWorldText("|cff00ff00[VoteSystem]|cff8aff00Vote ended.");
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Question was \'" << question << "\'.";
sWorld.SendWorldText(stream.str().c_str());
int voteamount = 0;
int temp = 0;
for (int i = 0; i < 4; i++)
{
originalamounts[i] = voteamounts[i];
}
//Sort array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if(voteamounts[j] < voteamounts[j + 1])
{
temp = voteamounts[j];
voteamounts[j] = voteamounts[j + 1];
voteamounts[j + 1] = temp;
}
}
}
//Check for ties with the winning option, and give answer if there are ties
if(voteamounts[0] == voteamounts[1])
{
if(voteamounts[1] == voteamounts[2])
answer = "undecided. All vote amounts are equal";
else
{
answer = "undecided. 2 vote amounts are equal";
}
}
else
{
if(voteamounts[0] == originalamounts[VOTE_YES])
answer = "\'Yes\'";
else if(voteamounts[0] == originalamounts[VOTE_NO])
answer = "\'No\'";
else if(voteamounts[0] == originalamounts[VOTE_BLANK])
answer = "\'Blank\'";
}
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Answer was " << answer << ".";
sWorld.SendWorldText(stream.str().c_str());
vote = false;
}
};
VoteBallot * voteballot;
void SetupVoteBallot(ScriptMgr * mgr)
{
voteballot = new VoteBallot();
GossipScript * gs = voteballot;
mgr->register_item_gossip_script(90001, gs);
}
bool OnChat(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc)
{
string msg(Message);
string cmd = msg.substr(0, 10);
if(strcmp(cmd.c_str(), "#startvote") == 0 && pPlayer->GetSession()->GetPermissionCount())
{
if(vote)
{
pPlayer->BroadcastMessage("|cff00ff00[VoteSystem]|cff8aff00There's already a vote active. Use #endvote to end the current vote manually.");
return false;
}
unsigned int questionfound1 = msg.find(" ");
if(questionfound1 != string::npos)
{
question = msg.substr(questionfound1 + 1, msg.length());
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00<GM>" << pPlayer->GetName() << " started a vote.";
sWorld.SendWorldText(stream.str().c_str());
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Question: " << question;
sWorld.SendWorldText(stream.str().c_str());
sWorld.SendWorldText("|cff00ff00[VoteSystem]|cff8aff00Voting will end in 1 minute.");
//clean up vote amounts
for(int i = 0; i < 3;i++)
{
voteamounts[i] = 0;
}
voteamounts[3] = -1;
vote = true;
SendVoteBallotToAll();
te = TimedEvent::Allocate(voteballot, new CallbackP0<VoteBallot>(voteballot, &VoteBallot::EndVote), 0, 60000, 1);
pPlayer->event_AddEvent(te);
}
else
{
pPlayer->BroadcastMessage("|cff00ff00[VoteSystem]|cff8aff00Please use the correct syntax. For example: #startvote question?.");
}
return false;
}
else if(strcmp(msg.c_str(), "#endvote") == 0 && pPlayer->GetSession()->GetPermissionCount())
{
pPlayer->event_RemoveByPointer(te);
voteballot->EndVote();
stream.str("");
stream << "|cff00ff00[VoteSystem]|cff8aff00Vote was stopped by <GM>" << pPlayer->GetName() << ".";
sWorld.SendWorldText(stream.str().c_str());
return false;
}
else
{
return true;
}
}
void SendVoteBallotToAll()
{
PlayerStorageMap::const_iterator itr;
objmgr._playerslock.AcquireReadLock();
for (itr = objmgr._players.begin(); itr != objmgr._players.end(); itr++)
{
Item * item = objmgr.CreateItem(90001, itr->second);
itr->second->GetItemInterface()->AddItemToFreeSlot(item);
item->SoulBind();
itr->second->BroadcastMessage("A vote ballot has been added to your backpack. Right-Click it to vote.");
}
objmgr._playerslock.ReleaseReadLock();
}
extern "C" SCRIPT_DECL uint32 _exp_get_script_type()
{
return SCRIPT_TYPE_MISC;
}
extern "C" SCRIPT_DECL void _exp_script_register(ScriptMgr * mgr)
{
SetupVoteBallot(mgr);
mgr->register_hook(SERVER_HOOK_EVENT_ON_CHAT, (void*)OnChat);
}
Credits to me & Banaanaz.