]> git.defcon.no Git - YAVote/blob - src/no/defcon/yavote/Votemanager.java
Adding trigger for Logout/Quit handling. Should correct ratio-tests when players...
[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 {
69 applyVote( voteType );
70 return true;
71 }
72
73 expireTask = plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable(){
74 @Override
75 public void run()
76 {
77 if ( voteRunning )
78 {
79 plugin.getServer().broadcastMessage("Time expired for voting on " + ((voteType != null) ? voteType : " .. uhm *blush*"));
80 clearState();
81 }
82 else
83 {
84 plugin.getLogger().info("Timer expired but vote was not running :S");
85 clearState();
86 }
87 }
88 }, plugin.getConfig().getLong("vote.timeoutSeconds")*20L );
89
90 plugin.getServer().broadcastMessage("A vote has started for " + voteType.toLowerCase() );
91 return true;
92 }
93
94 public boolean isVoteRunning()
95 {
96 return voteRunning;
97 }
98
99 public String getVoteType ()
100 {
101 return voteType;
102 }
103
104 public void removeVote(Player player )
105 {
106 if ( ! voteRunning || (voteType == null ) )
107 return;
108
109 plugin.getLogger().info("Debugging removeVote() with player: " + player.getName() );
110 if ( yesVoters.contains(player.getName()) )
111 {
112 yesVoters.remove(player.getName());
113 plugin.getLogger().info("Debugging, removeVote() removed YES vote for " + player.getName() );
114 }
115
116 if ( noVoters.contains(player.getName()) )
117 {
118 noVoters.remove(player.getName());
119 plugin.getLogger().info("Debugging, removeVote() removed NO vote for " + player.getName() );
120 }
121 if( checkRatio( ) )
122 {
123 plugin.getLogger().info("Debugging: after removing vote, checkRatio() returns TRUE, applying vote");
124 applyVote( voteType );
125 }
126 plugin.getLogger().info("Returning from removeVote()");
127 }
128
129 public boolean addVote(Player player, boolean yes)
130 {
131 if ( ! voteRunning || (voteType == null ) )
132 return false;
133
134 if( yesVoters.contains(player.getName()) || noVoters.contains(player.getName()) )
135 {
136 player.sendMessage("You have already cast your vote");
137 return true;
138 }
139 if (yes == true) yesVoters.add(player.getName());
140 else noVoters.add(player.getName());
141
142 if ( (yes == true) && checkRatio( ) )
143 {
144 applyVote( voteType );
145 return true;
146 }
147 else
148 {
149 if ( yesVoters.size() + noVoters.size() >= plugin.getServer().getOnlinePlayers().length )
150 {
151 plugin.getServer().broadcastMessage("Vote failed for " + voteType);
152 plugin.getServer().getScheduler().cancelTask(expireTask);
153 clearState();
154 return true;
155 }
156 player.sendMessage("Vote counted. Thank you.");
157 return true;
158 }
159 }
160
161 private void applyVote(String type)
162 {
163 plugin.getServer().getScheduler().cancelTask(expireTask);
164 if ( ! voteRunning || (voteType == null ) )
165 {
166 plugin.getLogger().info("No vote running, but I was told to apply one. ERRR");
167 return;
168 }
169
170 List<World> worlds = plugin.getServer().getWorlds();
171 for ( World w : worlds)
172 {
173 if ( voteType.equalsIgnoreCase("sun"))
174 w.setStorm(false);
175 else if ( voteType.equalsIgnoreCase("rain"))
176 w.setStorm(true);
177 else if ( voteType.equalsIgnoreCase("storm"))
178 w.setThundering(true);
179 else if ( voteType.equalsIgnoreCase("day"))
180 w.setTime( plugin.getConfig().getInt("vote.time.dayStart") );
181 else if ( voteType.equalsIgnoreCase("night"))
182 w.setTime( plugin.getConfig().getInt("vote.time.nightStart") );
183 }
184 plugin.getServer().broadcastMessage("Vote succeeded for " + ((voteType != null) ? voteType : " .. uhm *blush*"));
185 clearState();
186 }
187
188 private boolean checkRatio( )
189 {
190 float required = 0.5F;
191 if ( voteType.equalsIgnoreCase("sun") || voteType.equalsIgnoreCase("rain") || voteType.equalsIgnoreCase("storm") )
192 required = (float)plugin.getConfig().getInt("vote.weather.requiredPercent") / 100.0F;
193 else if ( voteType.equalsIgnoreCase("day") || voteType.equalsIgnoreCase("night") )
194 required = (float)plugin.getConfig().getInt("vote.time.requiredPercent") / 100.0F;
195
196 float ratio = (float) yesVoters.size() / (float) plugin.getServer().getOnlinePlayers().length;
197 if ( ratio >= required ) return true;
198 return false;
199 }
200
201 private void clearState()
202 {
203 voteRunning = false;
204 voteType = null;
205 yesVoters.clear();
206 noVoters.clear();
207 }
208
209 }