]> git.defcon.no Git - YAVote/blob - src/no/defcon/yavote/Votemanager.java
Testing a different approach. Urp.
[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 //if( checkRatio( ) )
130 if ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length -1 ) >= getRequired() )
131 {
132 plugin.getLogger().info("Debugging: after removing vote, ratio test returns TRUE, applying vote");
133 applyVote( voteType );
134 }
135 plugin.getLogger().info("Returning from removeVote()");
136 }
137
138 public boolean addVote(Player player, boolean yes)
139 {
140 if ( ! voteRunning || (voteType == null ) )
141 return false;
142
143 if( yesVoters.contains(player.getName()) || noVoters.contains(player.getName()) )
144 {
145 player.sendMessage("You have already cast your vote");
146 return true;
147 }
148 if (yes == true) yesVoters.add(player.getName());
149 else noVoters.add(player.getName());
150
151 //if ( (yes == true) && checkRatio( ) )
152 if ( ( yes == true) && ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length ) >= getRequired() ) )
153 {
154 applyVote( voteType );
155 return true;
156 }
157 else
158 {
159 if ( yesVoters.size() + noVoters.size() >= plugin.getServer().getOnlinePlayers().length )
160 {
161 plugin.getServer().broadcastMessage("Vote failed for " + voteType);
162 plugin.getServer().getScheduler().cancelTask(expireTask);
163 clearState();
164 return true;
165 }
166 player.sendMessage("Vote counted. Thank you.");
167 return true;
168 }
169 }
170
171 private void applyVote(String type)
172 {
173 plugin.getServer().getScheduler().cancelTask(expireTask);
174 if ( ! voteRunning || (voteType == null ) )
175 {
176 plugin.getLogger().info("No vote running, but I was told to apply one. ERRR");
177 return;
178 }
179
180 List<World> worlds = plugin.getServer().getWorlds();
181 for ( World w : worlds)
182 {
183 if ( voteType.equalsIgnoreCase("sun"))
184 w.setStorm(false);
185 else if ( voteType.equalsIgnoreCase("rain"))
186 w.setStorm(true);
187 else if ( voteType.equalsIgnoreCase("storm"))
188 w.setThundering(true);
189 else if ( voteType.equalsIgnoreCase("day"))
190 w.setTime( plugin.getConfig().getInt("vote.time.dayStart") );
191 else if ( voteType.equalsIgnoreCase("night"))
192 w.setTime( plugin.getConfig().getInt("vote.time.nightStart") );
193 }
194 plugin.getServer().broadcastMessage("Vote succeeded for " + ((voteType != null) ? voteType : " .. uhm *blush*"));
195 clearState();
196 }
197
198 private float getRequired( )
199 {
200 float req = 0.0f;
201 if ( voteType.equalsIgnoreCase("sun") || voteType.equalsIgnoreCase("rain") || voteType.equalsIgnoreCase("storm") )
202 req = (float)plugin.getConfig().getInt("vote.weather.requiredPercent") / 100.0F;
203 else if ( voteType.equalsIgnoreCase("day") || voteType.equalsIgnoreCase("night") )
204 req = (float)plugin.getConfig().getInt("vote.time.requiredPercent") / 100.0F;
205
206 return req;
207 }
208
209 /*
210 // Original approach...
211 private boolean checkRatio( )
212 {
213 float required = getRequired();
214 float ratio = (float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length;
215 if ( ratio >= required ) return true;
216 return false;
217 }
218
219 // Replacement code
220 private boolean checkRatio( )
221 {
222 if ( ((float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length ) >= getRequired() )
223 return true;
224 return false;
225 }
226
227 // But, trying to do it inline instead
228 */
229
230 private void clearState()
231 {
232 voteRunning = false;
233 voteType = null;
234 yesVoters.clear();
235 noVoters.clear();
236 }
237
238 }