]> git.defcon.no Git - YAVote/blob - src/no/defcon/yavote/Votemanager.java
cf69d7ec9b89db5cb0d9f78c5ab939571c9edf6e
[YAVote] / src / no / defcon / yavote / Votemanager.java
1 package no.defcon.yavote;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.bukkit.World;
7 import org.bukkit.entity.Player;
8 import org.bukkit.plugin.java.JavaPlugin;
9
10 public class Votemanager {
11 private JavaPlugin plugin;
12 private boolean voteRunning;
13 private String voteType;
14 private int expireTask;
15 private List<String> yesVoters;
16 private List<String> noVoters;
17
18
19 public Votemanager(JavaPlugin plugin) {
20 super();
21 this.plugin = plugin;
22 this.voteRunning = false;
23 }
24
25 public boolean cancelVote( )
26 {
27 if ( voteRunning )
28 {
29 plugin.getServer().broadcastMessage("Currently running vote is being canceled.");
30 plugin.getServer().getScheduler().cancelTask(expireTask);
31 clearState();
32 return true;
33 }
34 return false;
35 }
36
37 public boolean startVote( String type, Player p )
38 {
39 if ( this.voteRunning )
40 return false;
41
42 if ( ! (type.equalsIgnoreCase("sun") ||
43 type.equalsIgnoreCase("rain") ||
44 type.equalsIgnoreCase("day") ||
45 type.equalsIgnoreCase("night") ))
46 {
47 plugin.getLogger().info("Tried to start a vote of invalid type. Code error!");
48 return false;
49 }
50
51 voteType = type;
52 voteRunning = true;
53
54 if ( yesVoters == null )
55 yesVoters = new ArrayList<String>();
56 else
57 yesVoters.clear();
58
59 if ( noVoters == null )
60 noVoters = new ArrayList<String>();
61 else
62 noVoters.clear();
63
64 yesVoters.add(p.getName());
65
66 if ( checkRatio( ) )
67 {
68 applyVote( voteType );
69 return true;
70 }
71
72 expireTask = plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable(){
73 @Override
74 public void run()
75 {
76 if ( voteRunning )
77 {
78 plugin.getServer().broadcastMessage("Time expired for voting on " + ((voteType != null) ? voteType : " .. uhm *blush*"));
79 clearState();
80 }
81 else
82 {
83 plugin.getLogger().info("Timer expired but vote was not running :S");
84 clearState();
85 }
86 }
87 }, plugin.getConfig().getLong("vote.timeoutSeconds")*20L );
88
89 plugin.getServer().broadcastMessage("A vote has started for " + voteType.toLowerCase() );
90 return true;
91 }
92
93 public boolean isVoteRunning()
94 {
95 return voteRunning;
96 }
97
98 public String getVoteType ()
99 {
100 return voteType;
101 }
102
103 public boolean addVote(Player player, boolean yes)
104 {
105 if ( ! voteRunning || (voteType == null ) )
106 return false;
107
108 if( yesVoters.contains(player.getName()) || noVoters.contains(player.getName()) )
109 {
110 player.sendMessage("You have already cast your vote");
111 return true;
112 }
113 if (yes == true) yesVoters.add(player.getName());
114 else noVoters.add(player.getName());
115
116 if ( (yes == true) && checkRatio( ) )
117 {
118 applyVote( voteType );
119 return true;
120 }
121 else
122 {
123 if ( yesVoters.size() + noVoters.size() >= plugin.getServer().getOnlinePlayers().length )
124 {
125 plugin.getServer().broadcastMessage("Vote failed for " + voteType);
126 plugin.getServer().getScheduler().cancelTask(expireTask);
127 clearState();
128 return true;
129 }
130 player.sendMessage("Vote counted. Thank you.");
131 return true;
132 }
133 }
134
135 private void applyVote(String type)
136 {
137 plugin.getServer().getScheduler().cancelTask(expireTask);
138 if ( ! voteRunning || (voteType == null ) )
139 {
140 plugin.getLogger().info("No vote running, but I was told to apply one. ERRR");
141 return;
142 }
143
144 List<World> worlds = plugin.getServer().getWorlds();
145 for ( World w : worlds)
146 {
147 if ( voteType.equalsIgnoreCase("sun"))
148 w.setStorm(false);
149 else if ( voteType.equalsIgnoreCase("rain"))
150 w.setStorm(true);
151 else if ( voteType.equalsIgnoreCase("day"))
152 w.setTime( plugin.getConfig().getInt("vote.time.dayStart") );
153 else if ( voteType.equalsIgnoreCase("night"))
154 w.setTime( plugin.getConfig().getInt("vote.time.nightStart") );
155 }
156 plugin.getServer().broadcastMessage("Vote succeeded for " + ((voteType != null) ? voteType : " .. uhm *blush*"));
157 clearState();
158 }
159
160 private boolean checkRatio( )
161 {
162 float required = 0.5F;
163 if ( voteType.equalsIgnoreCase("sun") || voteType.equalsIgnoreCase("rain") )
164 required = (float)plugin.getConfig().getInt("vote.weather.requiredPercent") / 100.0F;
165 else if ( voteType.equalsIgnoreCase("day") || voteType.equalsIgnoreCase("night") )
166 required = (float)plugin.getConfig().getInt("vote.time.requiredPercent") / 100.0F;
167
168 float ratio = (float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length;
169 if ( ratio > required ) return true;
170 return false;
171 }
172
173 private void clearState()
174 {
175 voteRunning = false;
176 voteType = null;
177 yesVoters.clear();
178 noVoters.clear();
179 }
180
181 }