]> git.defcon.no Git - YAVote/blob - src/no/defcon/yavote/Votemanager.java
8690db30732d35f923b6af0f8a40948c136c96ba
[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("storm") ||
45 type.equalsIgnoreCase("day") ||
46 type.equalsIgnoreCase("night") ))
47 {
48 plugin.getLogger().info("Tried to start a vote of invalid type. Code error!");
49 return false;
50 }
51
52 voteType = type;
53 voteRunning = true;
54
55 if ( yesVoters == null )
56 yesVoters = new ArrayList<String>();
57 else
58 yesVoters.clear();
59
60 if ( noVoters == null )
61 noVoters = new ArrayList<String>();
62 else
63 noVoters.clear();
64
65 yesVoters.add(p.getName());
66
67 //if ( checkRatio( ) )
68 if ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length ) >= getRequired() )
69 {
70 applyVote( voteType );
71 return true;
72 }
73
74 expireTask = plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable(){
75 @Override
76 public void run()
77 {
78 if ( voteRunning )
79 {
80 plugin.getServer().broadcastMessage("Time expired for voting on " + ((voteType != null) ? voteType : " .. uhm *blush*"));
81 clearState();
82 }
83 else
84 {
85 plugin.getLogger().info("Timer expired but vote was not running :S");
86 clearState();
87 }
88 }
89 }, plugin.getConfig().getLong("vote.timeoutSeconds")*20L );
90
91 plugin.getServer().broadcastMessage("A vote has started for " + voteType.toLowerCase() );
92 return true;
93 }
94
95 public boolean isVoteRunning()
96 {
97 return voteRunning;
98 }
99
100 public String getVoteType ()
101 {
102 return voteType;
103 }
104
105 public void removeVote(Player player )
106 {
107 if ( ! voteRunning || (voteType == null ) )
108 return;
109
110 plugin.getLogger().info("Debugging removeVote() with player: " + player.getName() );
111 if ( yesVoters.contains(player.getName()) )
112 {
113 yesVoters.remove(player.getName());
114 plugin.getLogger().info("Debugging, removeVote() removed YES vote for " + player.getName() );
115 }
116
117 if ( noVoters.contains(player.getName()) )
118 {
119 noVoters.remove(player.getName());
120 plugin.getLogger().info("Debugging, removeVote() removed NO vote for " + player.getName() );
121 }
122
123 if ( yesVoters.size() + noVoters.size() == 0 )
124 {
125 plugin.getLogger().info("Debugging: No voters remaining in lists. Cancel vote.");
126 cancelVote();
127 return;
128 }
129
130 plugin.getLogger().info("Debugging: yesVoters.size() = " + yesVoters.size() );
131 plugin.getLogger().info("Debugging: getRequired() = " + getRequired() );
132 plugin.getLogger().info("Debugging: getOnlinePlayers - 1 = " + (plugin.getServer().getOnlinePlayers().length - 1) );
133 plugin.getLogger().info("Debugging: ratio = " + ( (float) yesVoters.size() / (float) (plugin.getServer().getOnlinePlayers().length - 1) ) );
134
135 //if( checkRatio( ) )
136 if ( ((float) yesVoters.size() / (float) (plugin.getServer().getOnlinePlayers().length - 1) ) >= getRequired() )
137 {
138 plugin.getLogger().info("Debugging: after removing vote, ratio test returns TRUE, applying vote");
139 applyVote( voteType );
140 }
141 plugin.getLogger().info("Returning from removeVote()");
142 }
143
144 public boolean addVote(Player player, boolean yes)
145 {
146 if ( ! voteRunning || (voteType == null ) )
147 return false;
148
149 if( yesVoters.contains(player.getName()) || noVoters.contains(player.getName()) )
150 {
151 player.sendMessage("You have already cast your vote");
152 return true;
153 }
154 if (yes == true) yesVoters.add(player.getName());
155 else noVoters.add(player.getName());
156
157 //if ( (yes == true) && checkRatio( ) )
158 if ( ( yes == true) && ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length ) >= getRequired() ) )
159 {
160 applyVote( voteType );
161 return true;
162 }
163 else
164 {
165 if ( yesVoters.size() + noVoters.size() >= plugin.getServer().getOnlinePlayers().length )
166 {
167 plugin.getServer().broadcastMessage("Vote failed for " + voteType);
168 plugin.getServer().getScheduler().cancelTask(expireTask);
169 clearState();
170 return true;
171 }
172 player.sendMessage("Vote counted. Thank you.");
173 return true;
174 }
175 }
176
177 private void applyVote(String type)
178 {
179 plugin.getServer().getScheduler().cancelTask(expireTask);
180 if ( ! voteRunning || (voteType == null ) )
181 {
182 plugin.getLogger().info("No vote running, but I was told to apply one. ERRR");
183 return;
184 }
185
186 List<World> worlds = plugin.getServer().getWorlds();
187 for ( World w : worlds)
188 {
189 if ( voteType.equalsIgnoreCase("sun"))
190 w.setStorm(false);
191 else if ( voteType.equalsIgnoreCase("rain"))
192 w.setStorm(true);
193 else if ( voteType.equalsIgnoreCase("storm"))
194 w.setThundering(true);
195 else if ( voteType.equalsIgnoreCase("day"))
196 w.setTime( plugin.getConfig().getInt("vote.time.dayStart") );
197 else if ( voteType.equalsIgnoreCase("night"))
198 w.setTime( plugin.getConfig().getInt("vote.time.nightStart") );
199 }
200 plugin.getServer().broadcastMessage("Vote succeeded for " + ((voteType != null) ? voteType : " .. uhm *blush*"));
201 clearState();
202 }
203
204 private float getRequired( )
205 {
206 float req = 0.0f;
207 if ( voteType.equalsIgnoreCase("sun") || voteType.equalsIgnoreCase("rain") || voteType.equalsIgnoreCase("storm") )
208 req = (float)plugin.getConfig().getInt("vote.weather.requiredPercent") / 100.0F;
209 else if ( voteType.equalsIgnoreCase("day") || voteType.equalsIgnoreCase("night") )
210 req = (float)plugin.getConfig().getInt("vote.time.requiredPercent") / 100.0F;
211
212 return req;
213 }
214
215 /*
216 // Original approach...
217 private boolean checkRatio( )
218 {
219 float required = getRequired();
220 float ratio = (float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length;
221 if ( ratio >= required ) return true;
222 return false;
223 }
224
225 // Replacement code
226 private boolean checkRatio( )
227 {
228 if ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length ) >= getRequired() )
229 return true;
230 return false;
231 }
232
233 // But, trying to do it inline instead
234 */
235
236 private void clearState()
237 {
238 voteRunning = false;
239 voteType = null;
240 yesVoters.clear();
241 noVoters.clear();
242 }
243
244 }