139 lines
4.9 KiB
Java
139 lines
4.9 KiB
Java
package de.szut.casino.slots;
|
|
|
|
import de.szut.casino.shared.service.BalanceService;
|
|
import de.szut.casino.user.UserEntity;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
@Service
|
|
public class SlotService {
|
|
private final int REEL_LENGTH = 32;
|
|
|
|
// 98% RTP
|
|
private final int SEVEN_COUNT = 1;
|
|
private final int BAR_COUNT = 4;
|
|
private final int BELL_COUNT = 7;
|
|
private final int CHERRY_COUNT = 10;
|
|
private final int BLANK_COUNT = 10;
|
|
|
|
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;
|
|
|
|
public SlotService(BalanceService balanceService) {
|
|
this.random = new Random();
|
|
this.balanceService = balanceService;
|
|
|
|
List<Symbol> reelStrip = createReelStrip();
|
|
this.firstReel = shuffleReel(reelStrip);
|
|
this.secondReel = shuffleReel(reelStrip);
|
|
this.thirdReel = shuffleReel(reelStrip);
|
|
}
|
|
|
|
public SpinResult spin(BigDecimal betAmount, UserEntity user) {
|
|
int index1 = this.random.nextInt(REEL_LENGTH);
|
|
int index2 = this.random.nextInt(REEL_LENGTH);
|
|
int index3 = this.random.nextInt(REEL_LENGTH);
|
|
|
|
Symbol symbol1 = getSymbolAt(this.firstReel, index1);
|
|
Symbol symbol2 = getSymbolAt(this.secondReel, index2);
|
|
Symbol symbol3 = getSymbolAt(this.thirdReel, index3);
|
|
|
|
boolean isWin = symbol1.equals(symbol2) && symbol1.equals(symbol3);
|
|
|
|
SpinResult spinResult = processResult(betAmount, user, isWin, symbol1);
|
|
buildResultMatrix(spinResult, index1, index2, index3);
|
|
|
|
return spinResult;
|
|
}
|
|
|
|
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);
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
resultMatrix.add(new ArrayList<>(3));
|
|
}
|
|
|
|
resultMatrix.getFirst().add(getSymbolAt(this.firstReel, index1 - 1));
|
|
resultMatrix.getFirst().add(getSymbolAt(this.secondReel, index2 - 1));
|
|
resultMatrix.getFirst().add(getSymbolAt(this.thirdReel, index3 - 1));
|
|
|
|
resultMatrix.get(1).add(getSymbolAt(this.firstReel, index1));
|
|
resultMatrix.get(1).add(getSymbolAt(this.secondReel, index2));
|
|
resultMatrix.get(1).add(getSymbolAt(this.thirdReel, index3));
|
|
|
|
resultMatrix.getLast().add(getSymbolAt(this.firstReel, index1 + 1));
|
|
resultMatrix.getLast().add(getSymbolAt(this.secondReel, index2 + 1));
|
|
resultMatrix.getLast().add(getSymbolAt(this.thirdReel, index3 + 1));
|
|
|
|
spinResult.setResultMatrix(resultMatrix);
|
|
}
|
|
|
|
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);
|
|
addSymbolsToStrip(reelStrip, CHERRY, CHERRY_COUNT);
|
|
addSymbolsToStrip(reelStrip, BELL, BELL_COUNT);
|
|
addSymbolsToStrip(reelStrip, BAR, BAR_COUNT);
|
|
addSymbolsToStrip(reelStrip, SEVEN, SEVEN_COUNT);
|
|
addSymbolsToStrip(reelStrip, BLANK, BLANK_COUNT);
|
|
return reelStrip;
|
|
}
|
|
|
|
private void addSymbolsToStrip(List<Symbol> strip, Symbol symbol, int count) {
|
|
for (int i = 0; i < count; i++) {
|
|
strip.add(symbol);
|
|
}
|
|
}
|
|
|
|
private Symbol getSymbolAt(List<Symbol> reel, int index) {
|
|
int effectiveIndex = index % REEL_LENGTH;
|
|
|
|
if (effectiveIndex < 0) {
|
|
effectiveIndex += REEL_LENGTH;
|
|
}
|
|
|
|
return reel.get(effectiveIndex);
|
|
}
|
|
}
|