Bukkit Map Voting

五迷三道 提交于 2019-12-13 23:49:35

问题


I need help. I creating mini game plugin with map voting and I don't know how to do that.

HashMap<World, Integer> votes = new HashMap<World, Integer>();

Let's say the votes are closed and the server is choosing map. Which one? What when 2 maps will have the same biggest num. of votes?

Thanks for help, eNcoo.


回答1:


Tie-Breaker Vote

Have another vote, but restrict choices to top worlds with equal high score.

Random Tie-Breaker

Use a random number generator to break the tie, such as shown in the following:

Map<World, Integer> votes = new HashMap<>();

...

// Get list of entries and sort descending by value
List<Entry<World, Integer>> entries = new ArrayList<>( votes.entrySet() );
Collections.sort( entries, (e1,e2) -> -e1.getValue().compareTo( e2.getValue() ) );

// Collect top scoring worlds
List<World> topWorlds = new ArrayList<>();
int highScore = entries.get( 0 ).getValue();
for ( Entry<World,Integer> e : entries )
    if ( e.getValue() == highScore )
        topWorlds.add( e.getKey() );
    else
        break;

// Pick one at random
World pick = topWorlds.get( new Random().nextInt( topWorlds.size() ) );

Oldest or Newest Vote

Tracking the timestamp of the oldest or newest vote for each world could also be used to break ties. For example, tracking the oldest vote gives preference to the first world vote on (in the set of ties), while the newest vote gives preference to the last world voted on. I am not sure if this is practical and mention it only as an exercise.



来源:https://stackoverflow.com/questions/41889903/bukkit-map-voting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!