Bukkit 地图投票
Bukkit Map Voting
我需要帮助。
我正在创建带有地图投票功能的小游戏插件,但我不知道该怎么做。
HashMap<World, Integer> votes = new HashMap<World, Integer>();
假设投票结束,服务器正在选择地图。哪一个?
当 2 张地图将具有相同的最大数字时会怎样?票数?
感谢您的帮助,eNcoo。
Tie-Breaker 投票
再投票一次,但将选择限制在得分相同的顶级世界。
随机Tie-Breaker
使用随机数生成器打破平局,如下所示:
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() ) );
最早或最新的投票
跟踪每个世界最早或最新投票的时间戳也可以用来打破平局。例如,跟踪最早的投票会优先考虑第一个世界投票(在关系集中),而最新的投票会优先考虑最后投票的世界。我不确定这是否实用,仅作为练习提及。
我需要帮助。 我正在创建带有地图投票功能的小游戏插件,但我不知道该怎么做。
HashMap<World, Integer> votes = new HashMap<World, Integer>();
假设投票结束,服务器正在选择地图。哪一个? 当 2 张地图将具有相同的最大数字时会怎样?票数?
感谢您的帮助,eNcoo。
Tie-Breaker 投票
再投票一次,但将选择限制在得分相同的顶级世界。
随机Tie-Breaker
使用随机数生成器打破平局,如下所示:
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() ) );
最早或最新的投票
跟踪每个世界最早或最新投票的时间戳也可以用来打破平局。例如,跟踪最早的投票会优先考虑第一个世界投票(在关系集中),而最新的投票会优先考虑最后投票的世界。我不确定这是否实用,仅作为练习提及。