Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void poll()
- {
- if(msgArray[1].equalsIgnoreCase("poll") && msgArray[2].equals("help"))
- pollHelp();
- if(msgArray[1].equalsIgnoreCase("polls"))
- listPolls();
- if(msgArray[1].equalsIgnoreCase("poll") && !msgArray[2].equals("help"))
- pollInfo();
- if(msgArray[1].equalsIgnoreCase("vote"))
- vote();
- if(msgArray[1].equalsIgnoreCase("newPoll"))
- newPoll();
- if(msgArray[1].equalsIgnoreCase("addOption"))
- addOption();
- if(msgArray[1].equalsIgnoreCase("results"))
- pollResults();
- if(msgArray[1].equalsIgnoreCase("endPoll"))
- endPoll();
- }
- public void pollHelp()
- {
- Reply("I'll pm you info about polls");
- sendMessage(sender, Bot.botName+" polls --list open polls");
- sendMessage(sender, Bot.botName+" poll [poll_name] --see a poll");
- sendMessage(sender, Bot.botName+" vote [poll_name] [option #] --vote in a poll");
- sendMessage(sender, Bot.botName+" newPoll [poll_name] [question] --create a poll");
- sendMessage(sender, Bot.botName+" addOption [poll_name] [option] --add options to your poll");
- sendMessage(sender, Bot.botName+" results [poll_name] --see the results of a poll");
- sendMessage(sender, Bot.botName+" endPoll [poll_name] --end a poll (deletes results!)");
- }
- public void listPolls()
- {
- String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\";
- File f = new File(path);
- String[] polls = f.list();
- String openPolls = "";
- for(int x=0;x<polls.length;x++)
- {
- if(x==0)
- openPolls = polls[x];
- else
- openPolls = openPolls + ", " + polls[x];
- }
- if(openPolls.equals(""))
- quickReply("No polls currently open. Type \""+Bot.botName+" poll help\" to start your own!");
- else
- quickReply("Open Polls: " + openPolls);
- }
- public void pollInfo()
- {
- String poll = msgArray[2];
- String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll;
- File f = new File(path);
- if(f.exists())
- {
- String pathOptions= "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\options.txt";
- try {
- BufferedReader br = new BufferedReader(new FileReader(pathOptions));
- ArrayList<String> options = new ArrayList<String>();
- String line;
- int counter =1;
- while ((line = br.readLine()) != null)
- {
- options.add(counter+" - "+line);
- counter++;
- }
- br.close();
- String pathQuestion = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\question.txt";
- BufferedReader questionReader = new BufferedReader(new FileReader(pathQuestion));
- String question = questionReader.readLine();
- questionReader.close();
- quickReply(question);
- for(int x=0;x<options.size();x++)
- {
- quickReply(options.get(x));
- }
- quickReply("To vote type: "+Bot.botName+" vote "+poll+ " #");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- quickReply("This poll has no options.");
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- quickReply("Poll does not exist");
- }
- }
- public void vote()
- {
- String poll = msgArray[2];
- String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll;
- File f = new File(path);
- if(f.exists())
- {
- if((!Bot.hostname.contains("kiwiirc.com")))
- {
- if(validateInt(msgArray[3]))
- {
- String pathVote = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\"+hostname+".txt";
- File voteFile = new File(pathVote);
- if(!voteFile.exists())
- {
- try {
- voteFile.createNewFile();
- FileWriter writer = new FileWriter(voteFile.getAbsoluteFile(), true);
- writer.write(msgArray[3]);
- writer.close();
- Reply("Vote recorded.");
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- quickReply("You already voted for that poll. Cheater.");
- }
- } else {
- quickReply("syntax error");
- }
- } else {
- quickReply("Votes cannot be cast from kiwiirc.com");
- }
- } else {
- quickReply("Poll does not exist.");
- }
- }
- public void newPoll()
- {
- String name = msgArray[2];
- String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+name;
- File f = new File(path);
- try {
- if (!f.exists())
- {
- f.mkdir();
- String pathQuestion= "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+name+"\\question.txt";
- File question = new File(pathQuestion);
- question.createNewFile();
- FileWriter fw = new FileWriter(question.getAbsoluteFile(), true);
- String questionString = "";
- for(int x=3;x<msgArray.length;x++)
- {
- questionString = questionString + msgArray[x]+" ";
- }
- fw.write(questionString);
- fw.close();
- String pathAuthor= "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+name+"\\author.txt";
- File authorFile = new File(pathAuthor);
- authorFile.createNewFile();
- FileWriter authorWriter = new FileWriter(authorFile.getAbsoluteFile(), true);
- authorWriter.write(sender);
- authorWriter.close();
- quickReply("Poll " + name + " created. Add choices by typing: "+Bot.botName+" addOption [poll_name] [option]");
- } else {
- quickReply("Poll already exists");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void addOption()
- {
- String poll = msgArray[2];
- if(checkAuthor(poll))
- {
- String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\options.txt";
- File f = new File(path);
- try {
- if (!f.exists())
- f.createNewFile();
- String option = "";
- for(int x=3;x<msgArray.length;x++)
- {
- option = option + msgArray[x]+" ";
- }
- FileWriter fw = new FileWriter(f.getAbsoluteFile(), true);
- fw.write(option+"\r\n");
- fw.close();
- quickReply("Option added.");
- } catch (IOException e) {
- e.printStackTrace();
- quickReply("Something didn't work");
- }
- } else {
- quickReply("Only the creator of a poll can add options.");
- }
- }
- public void pollResults()
- {
- String poll = msgArray[2];
- String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\options.txt";
- ArrayList<String> options = new ArrayList<String>();
- BufferedReader br;
- try {
- br = new BufferedReader(new FileReader(path));
- String line;
- while ((line = br.readLine()) != null)
- {
- options.add(line);
- }
- br.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- int[] results = new int[options.size()];
- int totalVotes=0;
- String pollDir = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\";
- File dir = new File(pollDir);
- String[] votes = dir.list();
- for(int x=0;x<votes.length;x++)
- {
- if(!(votes[x].equalsIgnoreCase("options.txt")) && !(votes[x].equalsIgnoreCase("author.txt")) && !(votes[x].equalsIgnoreCase("question.txt")))
- {
- try {
- BufferedReader reader = new BufferedReader(new FileReader(pollDir+"\\"+votes[x]));
- String vote = reader.readLine();
- System.out.println(vote);
- if(validateInt(vote))
- {
- int voteNum = Integer.parseInt(vote);
- results[voteNum-1]++;
- totalVotes++;
- }
- reader.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- for(int x=0;x<options.size();x++)
- {
- double percentDouble = ((results[x] *100)/ totalVotes);
- String percent = percentDouble + "%";
- String optionResult = percent + " (" + results[x] + " votes) - " + options.get(x);
- quickReply(optionResult);
- }
- }
- public void endPoll()
- {
- String poll = msgArray[2];
- if(checkAuthor(poll))
- {
- String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll;
- File f = new File(path);
- f.delete();
- quickReply(poll + " poll has ended.");
- } else {
- quickReply("Only the poll creator can end a poll");
- }
- }
- public boolean checkAuthor(String poll)
- {
- String pathAuthor = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\author.txt";
- try {
- BufferedReader reader = new BufferedReader(new FileReader(pathAuthor));
- String author = reader.readLine();
- reader.close();
- if(author.equalsIgnoreCase(sender))
- return true;
- else
- return false;
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- return false;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement