Created
April 8, 2020 09:09
-
-
Save Ragnok123/73634b6d39488120360813d6c0aab7d1 to your computer and use it in GitHub Desktop.
VotingManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.novaplay.bedwars.arena.voting; | |
import net.novaplay.bedwars.arena.Arena; | |
import cn.nukkit.Player; | |
import java.util.*; | |
public class VotingManager { | |
private Arena arena = null; | |
public final List<VotingMap> maps = new ArrayList<VotingMap>(); | |
private final HashMap<String, VotingMap> votes = new HashMap<String, VotingMap>(); | |
private boolean running = false; | |
public VotingManager(Arena arena) { | |
this.arena = arena; | |
run(); | |
} | |
public void addMaps(String[] maps) { | |
for(String map : maps) { | |
VotingMap mapa = new VotingMap(map); | |
this.maps.add(mapa); | |
} | |
} | |
public VotingMap getMapByName(String map) { | |
for(VotingMap mapa : maps) { | |
if(mapa.getName().equals(map)); | |
return mapa; | |
} | |
return null; | |
} | |
public void vote(Player player, VotingMap vote) { | |
if(votes.containsKey(player.getName())) { | |
votes.remove(player.getName()); | |
votes.put(player.getName(),vote); | |
} else { | |
votes.put(player.getName(),vote); | |
} | |
} | |
public VotingMap getWinner() { | |
List<VotingMap> allMaps = new ArrayList<VotingMap>(); | |
allMaps.addAll(maps); | |
Collections.shuffle(allMaps); | |
VotingMap best = maps.get(0); | |
int bestVotes = 0; | |
for(VotingMap map : allMaps) { | |
int votes = getVotes(map); | |
if(votes > bestVotes) { | |
best = map; | |
bestVotes = votes; | |
} | |
} | |
return best; | |
} | |
public int getVotes(VotingMap map) { | |
int votes = 0; | |
for(VotingMap mapa : this.votes.values()) { | |
if(mapa == map) { | |
votes++; | |
} | |
} | |
return votes; | |
} | |
public Map<String,VotingMap> getVotes(){ return votes;} | |
public void end() { | |
running = false; | |
votes.clear(); | |
} | |
public void run() { | |
running = true; | |
} | |
public boolean isRunning() { | |
return running; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.novaplay.bedwars.arena.voting; | |
import java.util.ArrayList; | |
import cn.nukkit.Player; | |
public class VotingMap { | |
public String name = null; | |
public VotingMap(String map) { | |
this.name = map; | |
} | |
public String getName() { | |
return this.name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment