在我的迷你游戏中我有多个玩家,他们赌钱。这笔钱然后进入累积奖金,我使用地图来获得累积奖金。在那之后,为了获得一定比例的获胜,我得到下注金额除以累积奖金。如何让程序根据给定的百分比选择一个玩家?
示例:
玩家1:下注25美元
JACKPOT是:25美元
玩家2:投注$ 45
JACKPOT是:70美元
玩家3:投注$ 20
JACKPOT是:$ 90
玩家4:投注$ 21
JACKPOT是:$ 111
这些玩家获得一定比例的胜利,但我如何在玩家2获胜的机会最多的地方进行编程
package dogboy602k.MassBet.Util;
import dogboy602k.MassBet.Main.MassBet;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.text.NumberFormat;
import java.util.*;
/**
* Created by dogboy on 6/18/2016.
*/
public class Manager {
private Economy economy = null;
private Player p;
private MassBet plugin;
private HashMap<UUID, Double> playerBet;
public Manager(MassBet plugin) {
this.plugin = plugin;
this.playerBet = new HashMap<>();
}
public void returninfo(Player player) {
UUID playerUUID = player.getUniqueId();
String Player = player.getName();
if (!playerBet.containsKey(playerUUID)) {
double jackpot = getTotalPotAmount();
int howManyBettors = getamountofPlayers();
Util.sendMsg(player, ChatColor.RED + "[ERROR] Seems to be you havent betted, use " + ChatColor.GREEN + "/mass bet " + player.getName() + " <amount>"+ChatColor.RED+" to bet and to be added to the Jack pot");
Util.sendMsg(player, ChatColor.AQUA + "[INFO] The Jackpot is " + ChatColor.GREEN + "$" + jackpot);
Util.sendMsg(player, ChatColor.AQUA + "[INFO] There are " + ChatColor.GREEN + howManyBettors);
return;
} else if (playerBet.containsKey(playerUUID)) {
NumberFormat defaultFormat = NumberFormat.getPercentInstance();
defaultFormat.setMinimumFractionDigits(2);
double bet = getPlayerBet(playerUUID);
double jackpot = getTotalPotAmount();
int howManyBettors = getamountofPlayers();
double chance = bet/jackpot;
Util.sendMsg(player, ChatColor.AQUA + "[INFO] Your chances are : " +defaultFormat.format(chance));
Util.sendMsg(player, ChatColor.AQUA + "[INFO] You have bet " + ChatColor.GREEN + "$" + bet);
Util.sendMsg(player, ChatColor.AQUA + "[INFO] The Jackpot is " + ChatColor.GREEN + "$" + jackpot);
Util.sendMsg(player, ChatColor.AQUA + "[INFO] There are " + ChatColor.GREEN + howManyBettors);
}
}
public void SendBet(Player player, double betAmount) {
UUID playerUUID = player.getUniqueId();
String Player = player.getName();
// check of the player got enough me money
if (!hasEnoughMoney(player, betAmount)) {
Util.sendMsg(player, ChatColor.RED + "Not Enough Money.");
}
if (hasEnoughMoney(player, betAmount)) {
/** To see the Map printed put
*
* for (Map.Entry<UUID, Double> entry : playerBet.entrySet()) {
* String key = entry.getKey().toString();
* Double value = entry.getValue();
* System.out.println("key, " + key + " value " + value);
* }
**/
if (playerBet.containsKey(playerUUID)) {
double bet = getPlayerBet(playerUUID);
Util.sendMsg(player, ChatColor.RED + "[ERROR] You have betted already");
Util.sendMsg(player, ChatColor.RED + "[ERROR] Use " + ChatColor.GREEN + "/mass retract " + player.getName() + ChatColor.RED + " to remove your bet");
Util.sendMsg(player, ChatColor.RED + "[ERROR] Your previous bet amount was: " + ChatColor.GREEN + "$" + bet);
return;
} else if (!playerBet.containsKey(playerUUID)) {
NumberFormat defaultFormat = NumberFormat.getPercentInstance();
defaultFormat.setMinimumFractionDigits(2);
addPlayerToPot(playerUUID, betAmount);
double bet = getPlayerBet(playerUUID);
double jackpot = getTotalPotAmount();
double chance = bet/jackpot;
Util.sendMsg(player, " Your chances are : " +defaultFormat.format(chance));
Util.sendMsg(player, " You have bet: " + ChatColor.GREEN + "$" + bet);
Util.sendMsg(player, getamountofPlayers() + " have bet, totaling :" + ChatColor.GREEN + "$" + jackpot);
plugin.getEconomy().withdrawPlayer(Player,betAmount);
}
}
}
public double getTotalPotAmount() {
double amount = 0;
for (Map.Entry<UUID, Double> values : playerBet.entrySet()) {
amount = values.getValue() + amount;
}
return amount;
}
public boolean hasEnoughMoney(Player player, double amount) {
if (plugin.getEconomy().getBalance(player.getName()) >= amount) {
return true;
}
return false;
}
public void addPlayerToPot(UUID playerUUID, double amount) {
this.playerBet.put(playerUUID, amount);
}
public void removePlayerFromPot(UUID playerUUID) {
this.playerBet.remove(playerUUID);
}
public Double getPlayerBet(UUID playerUUID) {
return this.playerBet.get(playerUUID);
}
public HashMap<UUID, Double > getPlayerBet() {
return this.playerBet;
}
public int getamountofPlayers() {
return this.playerBet.size();
}
public void returnTheCashInRetract(Player player){
UUID playerUUID = player.getUniqueId();
String Player = player.getName();
if (playerBet.containsKey(playerUUID)) {
double bet = getPlayerBet(playerUUID);
plugin.getEconomy().depositPlayer(player, bet);
Util.sendMsg(player, ChatColor.AQUA + "You have received " + ChatColor.GREEN + "$" + bet + ChatColor.AQUA + " due to your retract");
Util.sendMsg(player, "You have been removed from the Jackpot and game");
return;
}
else {
Util.sendMsg(player, ChatColor.RED + "[ERROR] Seems to be you havent betted, use " + ChatColor.GREEN + "/mass bet " + player.getName() + " <amount>"+ChatColor.RED+" to bet and to be added to the Jack pot");
return;
}
}
}
答案 0 :(得分:0)
您需要将机会映射作为键,将UUID作为值。
private HashMap<Double, UUID> chancesOfPlayer=new HashMap<Double, UUID>();;
在sendBet函数中,创建一个本地地图并用localMap替换原始地图
HashMap<Double, UUID> cop = new HashMap<Double, UUID>();
for (Map.Entry<UUID,Double> e : playerBet.entrySet()) {
UUID uid=e.getKey();
Double userBet=e.getValue();
Double userChance=userBet/jackpot;
cop.put(userChance, uid);
}
chancesOfPlayer=cop;
然后你可以做
chancesOfPlayer.get(chance); to get the player UUID given percentage.
仍然在访问此hashMap期间,多个线程仍然存在复杂性,每次有新的投注时都需要重新计算。