Quantcast
Channel: RaGEZONE - MMO Development Forums
Viewing all articles
Browse latest Browse all 14690

[Release] Player Polling System

$
0
0
This release allows a GM to pose a question to the server. Players can then answer using a command (@vote <option number>, or whatever you are coding it as). Poll results will be broadcast every one minute until the poll is closed.

DO NOTE: I have no idea which source I coded it on. I doubt it's lithium-based. But, I'm referring to lithium to re-create the tutorial. And copy-pasting the old non-lithium code.

TL;DR version: Would work on lithium with minor edits, e.g. variable renaming etc.
Idiot version: Do not use this release.

Step 1
Head to ChannelServer.java, adding this chunk of code in:
PHP Code:

    private static String voteQuestion "";
    private static List<
StringvoteOptions = new ArrayList<>();
    private static List<
IntegervoteCounts = new ArrayList<>();
    private static List<
Stringvoters = new ArrayList<>();
    private static 
boolean voteStarted false;

    public 
boolean setVoteQuestion(String vQuestion) {
        if (
voteStarted) {
            return 
false;
        }
        
voteQuestion vQuestion;
        return 
true;
    }

    public 
String getVoteQuestion() {
        return 
voteQuestion;
    }

    public 
boolean addVoteOption(String newVOption) {
        if (
voteStarted) {
            return 
false;
        }
        
voteOptions.add(newVOption);
        
voteCounts.add(o);
        return 
true;
    }

    public 
boolean clearVoteOptions() {
        if (
voteStarted) {
            return 
false;
        }
        
voteOptions.clear();
        
voteCounts.clear();
        return 
true;
    }

    public 
String[] getAllVoteOptions() {
        return 
voteOptions.toArray(new String[voteOptions.size()]);
    }

    public 
int[] getAllVoteCounts() {
        
int result[] = new int[voteCounts.size()];
        for (
int i 0voteCounts.size(); i++) {
            
result[i] = voteCounts.get(i);
        }
        return 
result;
    }

    public 
boolean vote(int optionNumberMapleCharacter thisVoter) {
        if (!
voteStarted) {
            return 
false;
        }
        if (
voters.contains(thisVoter.getName())) {
            
thisVoter.dropMessage("You have already placed your vote!");
            return 
false;
        }
        
optionNumber -= 1;
        if (
optionNumber >= voteCounts.size()) {
            
thisVoter.dropMessage("There are only " String.valueOf(voteCounts.size()) + " option" + (voteCounts.size() == "" "s") + " for this 

poll."
);
            for (
int i 0voteOptions.size(); i++) {
                
thisVoter.dropMessage("[OPTION " String.valueOf(1) + "] " voteOptions.get(i));
            }
            return 
false;
        }
        
voteCounts.set(optionNumbervoteCounts.get(optionNumber) + 1);
        return 
true;
    }

    public 
boolean isVoteStarted() {
        return 
voteStarted;
    }

    public 
void startVote(int updateInterval) {
        if (
voteStarted) {
            return;
        }
        
voteStarted true;

        for (
MapleCharacter chr players.getAllCharacters()) {
            
chr.dropMessage("[POLL] " voteQuestion);
            for (
int i 0voteOptions.size(); i++) {
                
chr.dropMessage("[OPTION " String.valueOf(1) + "] " voteOptions.get(i));
            }
            
chr.dropMessage("Use the command @vote <option number> to place your votes.");
        }

        
/*TimerManager.getInstance().schedule(new Runnable() {
            [MENTION=2000004426]Override[/MENTION]
            public void run() {
                voteStarted = false;
            }
        }, updateInterval);*/
        
updateVote(updateInterval);
    }
    
    public 
void startVote() {
        
startVote(60 1000);
    }

    private 
void updateVote(final int updateInterval) {
        
TimerManager.getInstance().schedule(new Runnable() {
            [
MENTION=2000004426]Override[/MENTION]
            public 
void run() {
                if (
voteStarted == false) {
                    return;
                }
                for (
MapleCharacter chr players.getAllCharacters()) {
                    
chr.dropMessage("[POLL] " voteQuestion);
                    for (
int i 0voteOptions.size(); i++) {
                        
chr.dropMessage("[OPTION " String.valueOf(1) + "] " voteOptions.get(i) + " - Voted: " voteCounts.get(i));
                    }
                    
chr.dropMessage("Use the command @vote <option number> to place your votes.");
                }
                
updateVote();
            }
        }, 
updateInterval);
    }
    
    public 
void endVote() {
        for (
MapleCharacter chr players.getAllCharacters()) {
            
chr.dropMessage("[POLL] The poll has ended. The following are the results to the question: + voteQuestion");
            for (
int i 0voteOptions.size(); i++) {
                
chr.dropMessage("[OPTION " String.valueOf(1) + "] " voteOptions.get(i) + " - Voted: " voteCounts.get(i));
            }
        }
    }

    public 
void clearVoting() {
        
voteQuestion "";
        
voteOptions = new ArrayList<>();
        
voteCounts = new ArrayList<>();
        
voters = new ArrayList<>();
        
voteStarted false;
    } 

Step 2
Add the following player command:

@vote
PHP Code:

int option Integer.parseInt(sub[1]);
ChannelServer.vote(optionchr); 

Step 3
Add the following GM commands:

!spquestion
PHP Code:

            String newQuestion joinStringFrom(sub1);
            if (
ChannelServer.setVoteQuestion(newQuestion)) {
                
player.dropMessage("Vote question successfully changed to " newQuestion +  "'");
            } else {
                
player.dropMessage("Voting in progress. Unable to change vote question.");
            } 

!spoption
PHP Code:

            String newOption joinStringFrom(sub1);
            if (
ChannelServer.addVoteOption(newOption)) {
                
player.dropMessage("Vote option successfully added.");
                for (
int i 0ChannelServer.getAllVoteOptions().lengthi++) {
                    
player.dropMessage("Option " String.valueOf(1) + ": " ChannelServer.getAllVoteOptions()[i]);
                }
            } else {
                
player.dropMessage("Voting in progress. Unable to change vote options.");
            } 

!spclear
PHP Code:

            if (ChannelServer.isVoteStarted()) {
               
player.dropMessage("Voting in progress. Unable to clear vote settings."); 
            } else {
               
ChannelServer.clearVoting();
            } 

!startpoll
PHP Code:

            if (ChannelServer.isVoteStarted()) {
               
player.dropMessage("Voting in progress. Unable to start a new vote"); 
            } else {
                if (
sub.length 1) {
                    
ChannelServer.startVote(Integer.parseInt(sub[1]));
                } else {
                    
ChannelServer.startVote();
                }
            } 

!endpoll
PHP Code:

            if (ChannelServer.isVoteStarted()) {
               
ChannelServer.endVote();
            } else {
                
player.dropMessage("There is no voting in progress"); 
            } 

How to use (If I remember correctly)

Spoiler:
Let's say you're a GM, and you wish to set up a poll to ask the question "Which event should I host?". You want to give the options "Hide and Seek", "Don't forget the lyrics" and "Megaphone Trivia". After 5 minutes, you want to see the results of the poll. This is how you would do it:

1) Use "!spclear" to clear the previous poll's settings (Just in case there are any settings still uncleared)
2) Use "!spquestion Which event should I host?" (This sets the question)
3) Use "!spoption Hide and Seek"
4) Use "!spoption Don't forget the lyrics"
5) Use "!spoption Megaphone Trivia" (Step 3-5 adds all 3 options to the poll)
6) Use "!startpoll" (This starts the poll, making it public to the server.)

A notice will be broadcast to the server in the following format:
[POLL] Which event should I host?
[OPTION 1] Hide and Seek
[OPTION 2] Don't forget the lyrics
[OPTION 3] Megaphone Trivia
Use the command @vote <option number> to place your votes.


Every minute, a notice will also be announced:
[POLL] Which event should I host?
[OPTION 1] Hide and Seek - Voted: 32
[OPTION 2] Don't forget the lyrics - Voted: 20
[OPTION 3] Megaphone Trivia - Voted: 56
Use the command @vote <option number> to place your votes.


Players can vote by using "@vote <option number>" (Surprise surprise.)
So for instance, if I want to vote for "Megaphone Trivia", I would use "@vote 3"

Once you are done with the poll,
7) Use "!endpoll"

This would broadcast:
[POLL] The poll has ended. The following are the results to the question: Which event should I host?
[OPTION 1] Hide and Seek - Voted: 41
[OPTION 2] Don't forget the lyrics - Voted: 23
[OPTION 3] Megaphone Trivia - Voted: 72


That's essentially how this polling system works.


"Features" to note:
  • Polling settings keep between polls. Meaning if the previous poll messed up, a simple !startvote will let you do it again without having to set all the options again. But this also means that if you create a new poll without using !spclear, you may end up with the previous poll's options in your poll.
  • The polling questions are public to all GMs. This means that anyone can change the question and the options anytime. If two or more people try to set up a poll at once, the results will be fairly disastrous, and not to mention seriously hilarious.
  • Interval between the notices can be changed. Or taken away entirely to make it a secret poll
  • There are no commands to update the GM on the poll results, though that can be easily arranged.
  • Starting/ending the poll is manual, so if the response is poor, you can always end the poll early, or leave it on longer for more people to vote. I leave it to you.


Uses:
  • Event hosting
  • Vote for a winner of a beauty/popularity contest
  • Polling for major decisions on the server
  • Spamming players with useless attention-whoring polls that have no use whatsoever other than to advertise the polling GM's presence and sense of obnoxious "popularity"


Enjoy, if you can get it to work :P

Viewing all articles
Browse latest Browse all 14690

Trending Articles