Advertisement
jared314

Tom's polls

Apr 11th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.51 KB | None | 0 0
  1.  public void poll()
  2.         {
  3.             if(msgArray[1].equalsIgnoreCase("poll") && msgArray[2].equals("help"))
  4.                 pollHelp();
  5.             if(msgArray[1].equalsIgnoreCase("polls"))
  6.                 listPolls();
  7.             if(msgArray[1].equalsIgnoreCase("poll") && !msgArray[2].equals("help"))
  8.                 pollInfo();
  9.             if(msgArray[1].equalsIgnoreCase("vote"))
  10.                 vote();
  11.             if(msgArray[1].equalsIgnoreCase("newPoll"))
  12.                 newPoll();
  13.             if(msgArray[1].equalsIgnoreCase("addOption"))
  14.                 addOption();
  15.             if(msgArray[1].equalsIgnoreCase("results"))
  16.                 pollResults();
  17.             if(msgArray[1].equalsIgnoreCase("endPoll"))
  18.                 endPoll();
  19.         }
  20.        
  21.         public void pollHelp()
  22.         {
  23.             Reply("I'll pm you info about polls");
  24.            
  25.             sendMessage(sender, Bot.botName+" polls                                 --list open polls");
  26.             sendMessage(sender, Bot.botName+" poll [poll_name]                      --see a poll");
  27.             sendMessage(sender, Bot.botName+" vote [poll_name] [option #]           --vote in a poll");
  28.             sendMessage(sender, Bot.botName+" newPoll [poll_name] [question]        --create a poll");
  29.             sendMessage(sender, Bot.botName+" addOption [poll_name] [option]        --add options to your poll");
  30.             sendMessage(sender, Bot.botName+" results [poll_name]                   --see the results of a poll");
  31.             sendMessage(sender, Bot.botName+" endPoll [poll_name]                   --end a poll (deletes results!)");
  32.         }
  33.        
  34.         public void listPolls()
  35.         {
  36.             String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\";
  37.            
  38.             File f = new File(path);
  39.             String[] polls = f.list();
  40.            
  41.             String openPolls = "";
  42.            
  43.             for(int x=0;x<polls.length;x++)
  44.             {
  45.                 if(x==0)
  46.                     openPolls = polls[x];
  47.                 else
  48.                     openPolls = openPolls + ", " + polls[x];
  49.             }
  50.            
  51.             if(openPolls.equals(""))
  52.                 quickReply("No polls currently open. Type \""+Bot.botName+" poll help\" to start your own!");
  53.             else
  54.                 quickReply("Open Polls: " + openPolls);
  55.         }
  56.        
  57.         public void pollInfo()
  58.         {
  59.             String poll = msgArray[2];
  60.             String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll;
  61.             File f = new File(path);
  62.            
  63.             if(f.exists())
  64.             {
  65.                 String pathOptions= "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\options.txt";
  66.                  try {
  67.                      
  68.                      BufferedReader br = new BufferedReader(new FileReader(pathOptions));
  69.                      
  70.                      ArrayList<String> options = new ArrayList<String>();
  71.                      String line;
  72.                      int counter =1;
  73.                      while ((line = br.readLine()) != null)
  74.                      {
  75.                        options.add(counter+" - "+line);
  76.                        counter++;
  77.                      }
  78.                      br.close();
  79.                      
  80.                     String pathQuestion = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\question.txt";
  81.                     BufferedReader questionReader = new BufferedReader(new FileReader(pathQuestion));
  82.                     String question = questionReader.readLine();
  83.                     questionReader.close();
  84.                    
  85.                    
  86.                     quickReply(question);
  87.                     for(int x=0;x<options.size();x++)
  88.                     {
  89.                         quickReply(options.get(x));
  90.                     }
  91.                     quickReply("To vote type: "+Bot.botName+" vote "+poll+ " #");
  92.                  } catch (FileNotFoundException e) {
  93.                      e.printStackTrace();
  94.                      quickReply("This poll has no options.");
  95.                  } catch (IOException e) {
  96.                      e.printStackTrace();
  97.                  }
  98.                
  99.             } else {
  100.                 quickReply("Poll does not exist");
  101.             }
  102.            
  103.         }
  104.        
  105.         public void vote()
  106.         {
  107.             String poll = msgArray[2];
  108.            
  109.             String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll;
  110.             File f = new File(path);
  111.             if(f.exists())
  112.             {
  113.                 if((!Bot.hostname.contains("kiwiirc.com")))
  114.                 {
  115.                     if(validateInt(msgArray[3]))
  116.                     {
  117.                         String pathVote = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\"+hostname+".txt";
  118.                         File voteFile = new File(pathVote);
  119.                        
  120.                         if(!voteFile.exists())
  121.                         {
  122.                             try {
  123.                                 voteFile.createNewFile();
  124.                                 FileWriter writer = new FileWriter(voteFile.getAbsoluteFile(), true);
  125.                                 writer.write(msgArray[3]);
  126.                                 writer.close();
  127.                                 Reply("Vote recorded.");
  128.                             } catch (IOException e) {
  129.                                 e.printStackTrace();
  130.                             }
  131.                         } else {
  132.                             quickReply("You already voted for that poll. Cheater.");
  133.                         }
  134.                     } else {
  135.                         quickReply("syntax error");
  136.                     }
  137.                 } else {
  138.                     quickReply("Votes cannot be cast from kiwiirc.com");
  139.                 }
  140.             } else {
  141.                 quickReply("Poll does not exist.");
  142.             }
  143.         }
  144.        
  145.         public void newPoll()
  146.         {
  147.                 String name = msgArray[2];
  148.                
  149.                 String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+name;
  150.                 File f = new File(path);
  151.                 try {
  152.                     if (!f.exists())
  153.                     {
  154.                         f.mkdir();
  155.                        
  156.                         String pathQuestion= "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+name+"\\question.txt";
  157.                         File question = new File(pathQuestion);
  158.                         question.createNewFile();
  159.                        
  160.                         FileWriter fw = new FileWriter(question.getAbsoluteFile(), true);
  161.                        
  162.                         String questionString = "";
  163.                         for(int x=3;x<msgArray.length;x++)
  164.                         {
  165.                             questionString = questionString + msgArray[x]+" ";
  166.                         }
  167.                        
  168.                         fw.write(questionString);
  169.                         fw.close();
  170.                        
  171.                         String pathAuthor= "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+name+"\\author.txt";
  172.                         File authorFile = new File(pathAuthor);
  173.                         authorFile.createNewFile();
  174.                         FileWriter authorWriter = new FileWriter(authorFile.getAbsoluteFile(), true);
  175.                         authorWriter.write(sender);
  176.                         authorWriter.close();
  177.                        
  178.                        
  179.                         quickReply("Poll " + name + " created. Add choices by typing: "+Bot.botName+" addOption [poll_name] [option]");
  180.                     } else {
  181.                         quickReply("Poll already exists");
  182.                     }
  183.                 } catch (IOException e) {
  184.                     e.printStackTrace();
  185.                 }        
  186.         }
  187.        
  188.         public void addOption()
  189.         {
  190.             String poll = msgArray[2];
  191.            
  192.             if(checkAuthor(poll))
  193.             {
  194.                 String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\options.txt";
  195.                 File f = new File(path);
  196.                 try {
  197.                     if (!f.exists())
  198.                         f.createNewFile();
  199.                     String option = "";
  200.                     for(int x=3;x<msgArray.length;x++)
  201.                     {
  202.                         option = option + msgArray[x]+" ";
  203.                     }
  204.                     FileWriter fw = new FileWriter(f.getAbsoluteFile(), true);
  205.  
  206.                     fw.write(option+"\r\n");
  207.                     fw.close();
  208.                        
  209.                     quickReply("Option added.");
  210.                    
  211.                 } catch (IOException e) {
  212.                     e.printStackTrace();
  213.                     quickReply("Something didn't work");
  214.                 }  
  215.            
  216.             } else {
  217.                 quickReply("Only the creator of a poll can add options.");
  218.             }
  219.            
  220.  
  221.         }
  222.        
  223.         public void pollResults()
  224.         {
  225.             String poll = msgArray[2];
  226.             String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\options.txt";
  227.            
  228.             ArrayList<String> options = new ArrayList<String>();
  229.             BufferedReader br;
  230.            
  231.             try {
  232.                 br = new BufferedReader(new FileReader(path));
  233.                 String line;
  234.                 while ((line = br.readLine()) != null)
  235.                 {
  236.                   options.add(line);
  237.                 }
  238.                 br.close();
  239.             } catch (FileNotFoundException e) {
  240.                 e.printStackTrace();
  241.             } catch (IOException e) {
  242.                 e.printStackTrace();
  243.             }
  244.            
  245.             int[] results = new int[options.size()];
  246.             int totalVotes=0;
  247.            
  248.             String pollDir = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\";
  249.             File dir = new File(pollDir);
  250.             String[] votes = dir.list();
  251.            
  252.             for(int x=0;x<votes.length;x++)
  253.             {
  254.                 if(!(votes[x].equalsIgnoreCase("options.txt")) && !(votes[x].equalsIgnoreCase("author.txt")) && !(votes[x].equalsIgnoreCase("question.txt")))
  255.                 {
  256.                     try {
  257.                         BufferedReader reader = new BufferedReader(new FileReader(pollDir+"\\"+votes[x]));
  258.                        
  259.                            
  260.                             String vote = reader.readLine();
  261.                             System.out.println(vote);
  262.                             if(validateInt(vote))
  263.                             {
  264.                                 int voteNum = Integer.parseInt(vote);
  265.                                 results[voteNum-1]++;
  266.                                 totalVotes++;
  267.                             }
  268.                        
  269.                         reader.close();
  270.                     } catch (FileNotFoundException e) {
  271.                         e.printStackTrace();
  272.                     } catch (IOException e) {
  273.                         e.printStackTrace();
  274.                     }
  275.                 }
  276.             }
  277.            
  278.            
  279.             for(int x=0;x<options.size();x++)
  280.             {
  281.                 double percentDouble = ((results[x] *100)/ totalVotes);
  282.                 String percent = percentDouble + "%";
  283.                 String optionResult = percent + " (" + results[x] + " votes) - " + options.get(x);
  284.                 quickReply(optionResult);
  285.             }
  286.            
  287.            
  288.         }
  289.        
  290.         public void endPoll()
  291.         {
  292.             String poll = msgArray[2];
  293.             if(checkAuthor(poll))
  294.             {
  295.                
  296.                 String path = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll;
  297.                 File f = new File(path);
  298.                 f.delete();
  299.                 quickReply(poll + " poll has ended.");
  300.             } else {
  301.                 quickReply("Only the poll creator can end a poll");
  302.             }
  303.         }
  304.        
  305.         public boolean checkAuthor(String poll)
  306.         {
  307.             String pathAuthor = "C:\\Users\\Jared\\Documents\\BreakfastBot\\polls\\"+poll+"\\author.txt";
  308.             try {
  309.                 BufferedReader reader = new BufferedReader(new FileReader(pathAuthor));
  310.                 String author = reader.readLine();
  311.                 reader.close();
  312.                
  313.                 if(author.equalsIgnoreCase(sender))
  314.                     return true;
  315.                 else
  316.                     return false;
  317.             } catch (FileNotFoundException e1) {
  318.                 e1.printStackTrace();
  319.                 return false;
  320.             } catch (IOException e) {
  321.                 e.printStackTrace();
  322.                 return false;
  323.             }
  324.            
  325.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement