feat: refactor and balance winnings
This commit is contained in:
parent
7a0dd0593b
commit
a26aeab86a
2 changed files with 81 additions and 70 deletions
|
@ -40,7 +40,7 @@ public class SlotController {
|
||||||
return ResponseEntity.badRequest().body(Collections.singletonMap("error", "Insufficient funds"));
|
return ResponseEntity.badRequest().body(Collections.singletonMap("error", "Insufficient funds"));
|
||||||
}
|
}
|
||||||
|
|
||||||
SpinResult spinResult = this.slotService.handleSpinResult(
|
SpinResult spinResult = this.slotService.spin(
|
||||||
betDto.getBetAmount(),
|
betDto.getBetAmount(),
|
||||||
user
|
user
|
||||||
);
|
);
|
||||||
|
|
|
@ -12,107 +12,118 @@ import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class SlotService {
|
public class SlotService {
|
||||||
private final int REEL_LENGTH = 60;
|
private final int REEL_LENGTH = 32;
|
||||||
private static final int SEVEN_COUNT = 4;
|
|
||||||
private static final int BAR_COUNT = 20;
|
|
||||||
private static final int CHERRY_COUNT = 36;
|
|
||||||
|
|
||||||
private final List<Symbol> FIRST_REEL = new ArrayList<>(REEL_LENGTH);
|
private final int SEVEN_COUNT = 1;
|
||||||
private final List<Symbol> SECOND_REEL = new ArrayList<>(REEL_LENGTH);
|
private final int BAR_COUNT = 4;
|
||||||
private final List<Symbol> THIRD_REEL = new ArrayList<>(REEL_LENGTH);
|
private final int BELL_COUNT = 7;
|
||||||
|
private final int CHERRY_COUNT = 11;
|
||||||
|
private final int BLANK_COUNT = 9;
|
||||||
|
|
||||||
|
private final Symbol SEVEN = new Symbol("seven", new BigDecimal("1000"));
|
||||||
|
private final Symbol BAR = new Symbol("bar", new BigDecimal("85"));
|
||||||
|
private final Symbol BELL = new Symbol("bell", new BigDecimal("40"));
|
||||||
|
private final Symbol CHERRY = new Symbol("cherry", new BigDecimal("10"));
|
||||||
|
private final Symbol BLANK = new Symbol("blank", new BigDecimal("0"));
|
||||||
|
|
||||||
|
private final List<Symbol> firstReel;
|
||||||
|
private final List<Symbol> secondReel;
|
||||||
|
private final List<Symbol> thirdReel;
|
||||||
|
|
||||||
|
private final Random random;
|
||||||
private final BalanceService balanceService;
|
private final BalanceService balanceService;
|
||||||
|
|
||||||
public SlotService(BalanceService balanceService) {
|
public SlotService(BalanceService balanceService) {
|
||||||
|
this.random = new Random();
|
||||||
this.balanceService = balanceService;
|
this.balanceService = balanceService;
|
||||||
|
|
||||||
initReels();
|
List<Symbol> reelStrip = createReelStrip();
|
||||||
|
this.firstReel = shuffleReel(reelStrip);
|
||||||
|
this.secondReel = shuffleReel(reelStrip);
|
||||||
|
this.thirdReel = shuffleReel(reelStrip);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpinResult handleSpinResult(
|
public SpinResult spin(BigDecimal betAmount, UserEntity user) {
|
||||||
BigDecimal betAmount,
|
int index1 = this.random.nextInt(REEL_LENGTH);
|
||||||
UserEntity user
|
int index2 = this.random.nextInt(REEL_LENGTH);
|
||||||
) {
|
int index3 = this.random.nextInt(REEL_LENGTH);
|
||||||
Random random = new Random();
|
|
||||||
|
|
||||||
int index1 = random.nextInt(REEL_LENGTH);
|
Symbol symbol1 = getSymbolAt(this.firstReel, index1);
|
||||||
int index2 = random.nextInt(REEL_LENGTH);
|
Symbol symbol2 = getSymbolAt(this.secondReel, index2);
|
||||||
int index3 = random.nextInt(REEL_LENGTH);
|
Symbol symbol3 = getSymbolAt(this.thirdReel, index3);
|
||||||
|
|
||||||
Symbol symbol1 = FIRST_REEL.get(index1);
|
|
||||||
Symbol symbol2 = SECOND_REEL.get(index2);
|
|
||||||
Symbol symbol3 = THIRD_REEL.get(index3);
|
|
||||||
|
|
||||||
boolean isWin = symbol1.equals(symbol2) && symbol1.equals(symbol3);
|
boolean isWin = symbol1.equals(symbol2) && symbol1.equals(symbol3);
|
||||||
|
|
||||||
SpinResult spinResult = new SpinResult();
|
SpinResult spinResult = processResult(betAmount, user, isWin, symbol1);
|
||||||
|
|
||||||
if (isWin) {
|
|
||||||
BigDecimal winAmount = betAmount.multiply(symbol1.getPayoutMultiplier());
|
|
||||||
this.balanceService.addFunds(user, winAmount);
|
|
||||||
|
|
||||||
spinResult.setStatus("win");
|
|
||||||
spinResult.setAmount(winAmount);
|
|
||||||
spinResult.setWin(true);
|
|
||||||
} else {
|
|
||||||
this.balanceService.subtractFunds(user, betAmount);
|
|
||||||
|
|
||||||
spinResult.setStatus("lose");
|
|
||||||
spinResult.setAmount(betAmount);
|
|
||||||
spinResult.setWin(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
buildResultMatrix(spinResult, index1, index2, index3);
|
buildResultMatrix(spinResult, index1, index2, index3);
|
||||||
|
|
||||||
return spinResult;
|
return spinResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildResultMatrix(SpinResult spinResult, int index1, int index2, int index3) {
|
private SpinResult processResult(BigDecimal betAmount, UserEntity user, boolean isWin, Symbol winSymbol) {
|
||||||
|
BigDecimal resultAmount;
|
||||||
|
String status;
|
||||||
|
|
||||||
|
if (isWin) {
|
||||||
|
resultAmount = betAmount.multiply(winSymbol.getPayoutMultiplier());
|
||||||
|
status = "win";
|
||||||
|
this.balanceService.addFunds(user, resultAmount);
|
||||||
|
} else {
|
||||||
|
resultAmount = betAmount;
|
||||||
|
status = "lose";
|
||||||
|
this.balanceService.subtractFunds(user, betAmount);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpinResult spinResult = new SpinResult();
|
||||||
|
spinResult.setStatus(status);
|
||||||
|
spinResult.setAmount(resultAmount);
|
||||||
|
spinResult.setWin(isWin);
|
||||||
|
|
||||||
|
return spinResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildResultMatrix(SpinResult spinResult, int index1, int index2, int index3) {
|
||||||
List<List<Symbol>> resultMatrix = new ArrayList<>(3);
|
List<List<Symbol>> resultMatrix = new ArrayList<>(3);
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
resultMatrix.add(new ArrayList<>(3));
|
resultMatrix.add(new ArrayList<>(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
resultMatrix.get(0).add(getSymbolAt(FIRST_REEL, index1 - 1));
|
resultMatrix.getFirst().add(getSymbolAt(this.firstReel, index1 - 1));
|
||||||
resultMatrix.get(1).add(getSymbolAt(FIRST_REEL, index1));
|
resultMatrix.getFirst().add(getSymbolAt(this.secondReel, index2 - 1));
|
||||||
resultMatrix.get(2).add(getSymbolAt(FIRST_REEL, index1 + 1));
|
resultMatrix.getFirst().add(getSymbolAt(this.thirdReel, index3 - 1));
|
||||||
|
|
||||||
resultMatrix.get(0).add(getSymbolAt(SECOND_REEL, index2 - 1));
|
resultMatrix.get(1).add(getSymbolAt(this.firstReel, index1));
|
||||||
resultMatrix.get(1).add(getSymbolAt(SECOND_REEL, index2));
|
resultMatrix.get(1).add(getSymbolAt(this.secondReel, index2));
|
||||||
resultMatrix.get(2).add(getSymbolAt(SECOND_REEL, index2 + 1));
|
resultMatrix.get(1).add(getSymbolAt(this.thirdReel, index3));
|
||||||
|
|
||||||
resultMatrix.get(0).add(getSymbolAt(THIRD_REEL, index3 - 1));
|
resultMatrix.getLast().add(getSymbolAt(this.firstReel, index1 + 1));
|
||||||
resultMatrix.get(1).add(getSymbolAt(THIRD_REEL, index3));
|
resultMatrix.getLast().add(getSymbolAt(this.secondReel, index2 + 1));
|
||||||
resultMatrix.get(2).add(getSymbolAt(THIRD_REEL, index3 + 1));
|
resultMatrix.getLast().add(getSymbolAt(this.thirdReel, index3 + 1));
|
||||||
|
|
||||||
spinResult.setResultMatrix(resultMatrix);
|
spinResult.setResultMatrix(resultMatrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initReels() {
|
private List<Symbol> shuffleReel(List<Symbol> reelStrip) {
|
||||||
|
Collections.shuffle(reelStrip, this.random);
|
||||||
|
|
||||||
|
return reelStrip;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Symbol> createReelStrip() {
|
||||||
List<Symbol> reelStrip = new ArrayList<>(REEL_LENGTH);
|
List<Symbol> reelStrip = new ArrayList<>(REEL_LENGTH);
|
||||||
|
addSymbolsToStrip(reelStrip, CHERRY, CHERRY_COUNT);
|
||||||
for (int i = 0; i < CHERRY_COUNT; i++) {
|
addSymbolsToStrip(reelStrip, BELL, BELL_COUNT);
|
||||||
Symbol CHERRY = new Symbol("cherry", new BigDecimal(5));
|
addSymbolsToStrip(reelStrip, BAR, BAR_COUNT);
|
||||||
reelStrip.add(CHERRY);
|
addSymbolsToStrip(reelStrip, SEVEN, SEVEN_COUNT);
|
||||||
|
addSymbolsToStrip(reelStrip, BLANK, BLANK_COUNT);
|
||||||
|
return reelStrip;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < BAR_COUNT; i++) {
|
private void addSymbolsToStrip(List<Symbol> strip, Symbol symbol, int count) {
|
||||||
Symbol BAR = new Symbol("bar", new BigDecimal(10));
|
for (int i = 0; i < count; i++) {
|
||||||
reelStrip.add(BAR);
|
strip.add(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < SEVEN_COUNT; i++) {
|
|
||||||
Symbol SEVEN = new Symbol("seven", new BigDecimal(85));
|
|
||||||
reelStrip.add(SEVEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
FIRST_REEL.addAll(reelStrip);
|
|
||||||
SECOND_REEL.addAll(reelStrip);
|
|
||||||
THIRD_REEL.addAll(reelStrip);
|
|
||||||
|
|
||||||
Collections.shuffle(FIRST_REEL);
|
|
||||||
Collections.shuffle(SECOND_REEL);
|
|
||||||
Collections.shuffle(THIRD_REEL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Symbol getSymbolAt(List<Symbol> reel, int index) {
|
private Symbol getSymbolAt(List<Symbol> reel, int index) {
|
||||||
|
|
Reference in a new issue