import java.util.List;
import java.util.Map;
import java.util.Random;
public class DrawUtil {
//0奖励id 1vip 2最大可抽取数,3概率
private int[][] jackpot;
private Map<Integer,Integer> userAward;
private List<int[]> outputList;
private int vip;
public DrawUtil(int vip,Map<Integer,Integer> userAward)
{
this.jackpot = new int[][]{};
this.userAward = userAward;
this.vip = vip;
}
public List<int[]> getOutputList() {
return outputList;
}
/**
* 抽奖
*/
public void draw(int num)
{
int totalProb = getTotalProb();
for (int i = 0;i<num;i++) {
int[] getAward = singleDraw(totalProb);
outputList.add(getAward);
if (getAward[0]==0){
continue;
}
handleUserAwards(getAward);
awardQualified(getAward);
if (!awardQualified(getAward)){
totalProb -= getAward[3];
}
}
}
/**
* 处理用户已领取的数据
*/
private void handleUserAwards(int[] getAward)
{
if (userAward.containsKey(getAward[0])){
userAward.put(getAward[0],userAward.get(getAward[0])+1);
return;
}
userAward.put(getAward[0],1);
}
/**
* 计算总概率和
*/
private int getTotalProb()
{
int totalProb = 0;
for (int[] award : jackpot){
if (awardQualified(award)){
totalProb += award[3];
}
}
return totalProb;
}
/**
* 奖励是否可领取
*/
private boolean awardQualified(int[] array)
{
if (array[1]>vip || array[2]<=userAward.get(array[0])){
return false;
}
return true;
}
/**
* 单抽
*/
private int[] singleDraw(int totalProb)
{
int randomKey = new Random().nextInt(totalProb);
int prob = 0;
for (int[] getAward : jackpot) {
prob += getAward[4];
if ( randomKey <= prob) {
return getAward;
}
}
return new int[4];
}
}