diff --git a/.gitea/workflows/stale.yml b/.gitea/workflows/stale.yml new file mode 100644 index 0000000..7ba5af9 --- /dev/null +++ b/.gitea/workflows/stale.yml @@ -0,0 +1,15 @@ +name: "Close stale issues and PRs" +on: + workflow_dispatch: + schedule: + - cron: "@hourly" + +jobs: + stale: + runs-on: ubuntu-latest + steps: + - uses: actions/stale@v9 + with: + stale-pr-message: "Will be closed in x days bc yo mom is a bitch. im not telling you when it will be closed fuckface" + days-before-pr-stale: 2 + days-before-pr-close: 3 diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java index c9f57d7..4f22c9d 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java +++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameEntity.java @@ -4,8 +4,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonManagedReference; import de.szut.casino.user.UserEntity; import jakarta.persistence.*; -import jakarta.validation.constraints.NotNull; -import jakarta.validation.constraints.Positive; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; diff --git a/backend/src/main/java/de/szut/casino/dice/DiceDto.java b/backend/src/main/java/de/szut/casino/dice/DiceDto.java index ecbf3d7..f0caf48 100644 --- a/backend/src/main/java/de/szut/casino/dice/DiceDto.java +++ b/backend/src/main/java/de/szut/casino/dice/DiceDto.java @@ -5,12 +5,14 @@ import jakarta.validation.constraints.DecimalMax; import jakarta.validation.constraints.DecimalMin; import jakarta.validation.constraints.NotNull; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; import java.math.BigDecimal; @Getter @Setter +@NoArgsConstructor public class DiceDto extends BetDto { private boolean rollOver; diff --git a/backend/src/main/java/de/szut/casino/dice/DiceService.java b/backend/src/main/java/de/szut/casino/dice/DiceService.java index 71e4584..836620b 100644 --- a/backend/src/main/java/de/szut/casino/dice/DiceService.java +++ b/backend/src/main/java/de/szut/casino/dice/DiceService.java @@ -11,10 +11,11 @@ import java.util.Random; @Service public class DiceService { private static final int MAX_DICE_VALUE = 100; - private final Random random = new Random(); + private final Random random; private final BalanceService balanceService; - public DiceService(BalanceService balanceService) { + public DiceService(Random random, BalanceService balanceService) { + this.random = random; this.balanceService = balanceService; } diff --git a/backend/src/test/java/de/szut/casino/dice/DiceServiceTest.java b/backend/src/test/java/de/szut/casino/dice/DiceServiceTest.java new file mode 100644 index 0000000..6b2e230 --- /dev/null +++ b/backend/src/test/java/de/szut/casino/dice/DiceServiceTest.java @@ -0,0 +1,251 @@ +package de.szut.casino.dice; + +import de.szut.casino.shared.service.BalanceService; +import de.szut.casino.user.UserEntity; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class DiceServiceTest { + + @Mock + private BalanceService balanceService; + + @Mock + private Random random; + + @InjectMocks + private DiceService diceService; + + private UserEntity user; + private DiceDto diceDto; + + @BeforeEach + void setUp() { + user = new UserEntity(); + user.setId(1L); + user.setBalance(BigDecimal.valueOf(1000)); + + diceDto = new DiceDto(); + diceDto.setBetAmount(BigDecimal.valueOf(10)); + diceDto.setTargetValue(BigDecimal.valueOf(50)); + diceDto.setRollOver(true); + } + + @Test + void play_rollOver_win() { + diceDto.setRollOver(true); + diceDto.setTargetValue(BigDecimal.valueOf(50)); + when(random.nextInt(anyInt())).thenReturn(55); + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(56), result.getRolledValue()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, times(1)).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollOver_lose() { + diceDto.setRollOver(true); + diceDto.setTargetValue(BigDecimal.valueOf(50)); + when(random.nextInt(anyInt())).thenReturn(49); + + DiceResult result = diceService.play(user, diceDto); + + assertFalse(result.isWin()); + assertEquals(BigDecimal.valueOf(50), result.getRolledValue()); + assertEquals(BigDecimal.ZERO, result.getPayout()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, never()).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollUnder_win() { + diceDto.setRollOver(false); + diceDto.setTargetValue(BigDecimal.valueOf(50)); + when(random.nextInt(anyInt())).thenReturn(48); + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(49), result.getRolledValue()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, times(1)).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollUnder_lose() { + diceDto.setRollOver(false); + diceDto.setTargetValue(BigDecimal.valueOf(50)); + when(random.nextInt(anyInt())).thenReturn(50); + + DiceResult result = diceService.play(user, diceDto); + + assertFalse(result.isWin()); + assertEquals(BigDecimal.valueOf(51), result.getRolledValue()); + assertEquals(BigDecimal.ZERO, result.getPayout()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, never()).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollOver_targetValueOne_rolledOne_lose() { + diceDto.setRollOver(true); + diceDto.setTargetValue(BigDecimal.valueOf(1)); + when(random.nextInt(anyInt())).thenReturn(0); + + DiceResult result = diceService.play(user, diceDto); + + assertFalse(result.isWin()); + assertEquals(BigDecimal.valueOf(1), result.getRolledValue()); + assertEquals(BigDecimal.ZERO, result.getPayout()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, never()).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollOver_targetValueOne_rolledTwo_win() { + diceDto.setRollOver(true); + diceDto.setTargetValue(BigDecimal.valueOf(1)); + when(random.nextInt(anyInt())).thenReturn(1); + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(2), result.getRolledValue()); + // Win chance for target 1 (roll over) is 99. Multiplier = (100-1)/99 = 1 + assertEquals(diceDto.getBetAmount().stripTrailingZeros(), result.getPayout().stripTrailingZeros()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, times(1)).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollUnder_targetValueOne_alwaysLose_winChanceZero() { + diceDto.setRollOver(false); + diceDto.setTargetValue(BigDecimal.valueOf(1)); + when(random.nextInt(anyInt())).thenReturn(0); + + DiceResult result = diceService.play(user, diceDto); + + assertFalse(result.isWin()); + assertEquals(BigDecimal.valueOf(1), result.getRolledValue()); + assertEquals(BigDecimal.ZERO, result.getPayout()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, never()).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollOver_targetValueNinetyNine_rolledHundred_win() { + diceDto.setRollOver(true); + diceDto.setTargetValue(BigDecimal.valueOf(99)); + when(random.nextInt(anyInt())).thenReturn(99); + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(100), result.getRolledValue()); + // Win chance for target 99 (roll over) is 1. Multiplier = (100-1)/1 = 99 + assertEquals(diceDto.getBetAmount().multiply(BigDecimal.valueOf(99)).stripTrailingZeros(), result.getPayout().stripTrailingZeros()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, times(1)).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollUnder_targetValueNinetyNine_rolledNinetyEight_win() { + diceDto.setRollOver(false); + diceDto.setTargetValue(BigDecimal.valueOf(99)); + when(random.nextInt(anyInt())).thenReturn(97); + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(98), result.getRolledValue()); + // Win chance for target 99 (roll under) is 98. Multiplier = (100-1)/98 = 99/98 + assertEquals(diceDto.getBetAmount().multiply(BigDecimal.valueOf(99).divide(BigDecimal.valueOf(98), 4, RoundingMode.HALF_UP)), result.getPayout()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, times(1)).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollOver_targetValueOneHundred_alwaysLose_winChanceZero() { + diceDto.setRollOver(true); + diceDto.setTargetValue(BigDecimal.valueOf(100)); + when(random.nextInt(anyInt())).thenReturn(99); + + DiceResult result = diceService.play(user, diceDto); + + assertFalse(result.isWin()); + assertEquals(BigDecimal.valueOf(100), result.getRolledValue()); + assertEquals(BigDecimal.ZERO, result.getPayout()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, never()).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_rollUnder_targetValueOneHundred_rolledNinetyNine_win() { + diceDto.setRollOver(false); + diceDto.setTargetValue(BigDecimal.valueOf(100)); + when(random.nextInt(anyInt())).thenReturn(98); + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(99), result.getRolledValue()); + // Win chance for target 100 (roll under) is 99. Multiplier = (100-1)/99 = 1 + assertEquals(diceDto.getBetAmount().stripTrailingZeros(), result.getPayout().stripTrailingZeros()); + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + verify(balanceService, times(1)).addFunds(eq(user), any(BigDecimal.class)); + } + + @Test + void play_payoutCalculationCorrect() { + diceDto.setRollOver(true); + diceDto.setTargetValue(BigDecimal.valueOf(75)); + when(random.nextInt(anyInt())).thenReturn(75); + + // Multiplier for win chance 25: (100-1)/25 = 99/25 = 3.96 + // Payout: 10 * 3.96 = 39.6 + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(39.6).stripTrailingZeros(), result.getPayout().stripTrailingZeros()); + } + + @Test + void play_payoutCalculationCorrect_rollUnder() { + diceDto.setRollOver(false); + diceDto.setTargetValue(BigDecimal.valueOf(25)); + when(random.nextInt(anyInt())).thenReturn(0); + + // Multiplier for win chance 24: (100-1)/24 = 99/24 = 4.125 + // Payout: 10 * 4.125 = 41.25 + + DiceResult result = diceService.play(user, diceDto); + + assertTrue(result.isWin()); + assertEquals(BigDecimal.valueOf(41.25).stripTrailingZeros(), result.getPayout().stripTrailingZeros()); + } + + @Test + void play_betAmountSubtracted() { + when(random.nextInt(anyInt())).thenReturn(50); + + diceService.play(user, diceDto); + + verify(balanceService, times(1)).subtractFunds(user, diceDto.getBetAmount()); + } +} diff --git a/frontend/bun.lock b/frontend/bun.lock index c7ad15f..9f7b345 100644 --- a/frontend/bun.lock +++ b/frontend/bun.lock @@ -1618,7 +1618,7 @@ "sax": ["sax@1.4.1", "", {}, "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg=="], - "schema-utils": ["schema-utils@4.3.2", "", { "dependencies": { "@types/json-schema": "^7.0.9", "ajv": "^8.9.0", "ajv-formats": "^2.1.1", "ajv-keywords": "^5.1.0" } }, "sha512-Gn/JaSk/Mt9gYubxTtSn/QCV4em9mpAPiR1rqy/Ocu19u/G9J5WWdNoUT4SiV6mFC3y6cxyFcFwdzPM3FgxGAQ=="], + "schema-utils": ["schema-utils@4.3.0", "", { "dependencies": { "@types/json-schema": "^7.0.9", "ajv": "^8.9.0", "ajv-formats": "^2.1.1", "ajv-keywords": "^5.1.0" } }, "sha512-Gf9qqc58SpCA/xdziiHz35F4GNIWYWZrEshUc/G/r5BnLph6xpKuLeoJoQuj5WfBIx/eQLf+hmVPYHaxJu7V2g=="], "select-hose": ["select-hose@2.0.0", "", {}, "sha512-mEugaLK+YfkijB4fx0e6kImuJdCIt2LxCRcbEYPqRGCs4F2ogyfZU5IAZRdjCP8JPq2AtdNoC/Dux63d9Kiryg=="], @@ -2120,8 +2120,6 @@ "micromatch/picomatch": ["picomatch@2.3.1", "", {}, "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="], - "mini-css-extract-plugin/schema-utils": ["schema-utils@4.3.0", "", { "dependencies": { "@types/json-schema": "^7.0.9", "ajv": "^8.9.0", "ajv-formats": "^2.1.1", "ajv-keywords": "^5.1.0" } }, "sha512-Gf9qqc58SpCA/xdziiHz35F4GNIWYWZrEshUc/G/r5BnLph6xpKuLeoJoQuj5WfBIx/eQLf+hmVPYHaxJu7V2g=="], - "minipass-flush/minipass": ["minipass@3.3.6", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="], "minipass-pipeline/minipass": ["minipass@3.3.6", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="], @@ -2202,13 +2200,11 @@ "tar/mkdirp": ["mkdirp@1.0.4", "", { "bin": { "mkdirp": "bin/cmd.js" } }, "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw=="], - "terser-webpack-plugin/schema-utils": ["schema-utils@4.3.0", "", { "dependencies": { "@types/json-schema": "^7.0.9", "ajv": "^8.9.0", "ajv-formats": "^2.1.1", "ajv-keywords": "^5.1.0" } }, "sha512-Gf9qqc58SpCA/xdziiHz35F4GNIWYWZrEshUc/G/r5BnLph6xpKuLeoJoQuj5WfBIx/eQLf+hmVPYHaxJu7V2g=="], - "terser-webpack-plugin/terser": ["terser@5.39.0", "", { "dependencies": { "@jridgewell/source-map": "^0.3.3", "acorn": "^8.8.2", "commander": "^2.20.0", "source-map-support": "~0.5.20" }, "bin": { "terser": "bin/terser" } }, "sha512-LBAhFyLho16harJoWMg/nZsQYgTrg5jXOn2nCYjRUcZZEdE3qa2zb8QEDRUGVZBW4rlazf2fxkg8tztybTaqWw=="], "webpack/eslint-scope": ["eslint-scope@5.1.1", "", { "dependencies": { "esrecurse": "^4.3.0", "estraverse": "^4.1.1" } }, "sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw=="], - "webpack-dev-middleware/schema-utils": ["schema-utils@4.3.0", "", { "dependencies": { "@types/json-schema": "^7.0.9", "ajv": "^8.9.0", "ajv-formats": "^2.1.1", "ajv-keywords": "^5.1.0" } }, "sha512-Gf9qqc58SpCA/xdziiHz35F4GNIWYWZrEshUc/G/r5BnLph6xpKuLeoJoQuj5WfBIx/eQLf+hmVPYHaxJu7V2g=="], + "webpack/schema-utils": ["schema-utils@4.3.2", "", { "dependencies": { "@types/json-schema": "^7.0.9", "ajv": "^8.9.0", "ajv-formats": "^2.1.1", "ajv-keywords": "^5.1.0" } }, "sha512-Gn/JaSk/Mt9gYubxTtSn/QCV4em9mpAPiR1rqy/Ocu19u/G9J5WWdNoUT4SiV6mFC3y6cxyFcFwdzPM3FgxGAQ=="], "webpack-dev-server/chokidar": ["chokidar@3.6.0", "", { "dependencies": { "anymatch": "~3.1.2", "braces": "~3.0.2", "glob-parent": "~5.1.2", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", "normalize-path": "~3.0.0", "readdirp": "~3.6.0" }, "optionalDependencies": { "fsevents": "~2.3.2" } }, "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw=="], @@ -2398,8 +2394,6 @@ "log-update/slice-ansi/is-fullwidth-code-point": ["is-fullwidth-code-point@5.0.0", "", { "dependencies": { "get-east-asian-width": "^1.0.0" } }, "sha512-OVa3u9kkBbw7b8Xw5F9P+D/T9X+Z4+JruYVNapTjPYZYUznQ5YfWeFkOj606XYYW8yugTfC8Pj0hYqvi4ryAhA=="], - "mini-css-extract-plugin/schema-utils/ajv-formats": ["ajv-formats@2.1.1", "", { "dependencies": { "ajv": "^8.0.0" } }, "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA=="], - "node-gyp/tar/chownr": ["chownr@3.0.0", "", {}, "sha512-+IxzY9BZOQd/XuYPRmrvEVjF/nqj5kgT4kEq7VofrDoM1MxoRjEWkrCC3EtLi59TVawxTAn+orJwFQcrqEN1+g=="], "node-gyp/tar/mkdirp": ["mkdirp@3.0.1", "", { "bin": { "mkdirp": "dist/cjs/src/bin.js" } }, "sha512-+NsyUUAZDmo6YVHzL/stxSu3t9YS1iljliy3BSDrXJ/dkn1KYdmtZODGGjLcc9XLgVVpH4KshHB8XmZgMhaBXg=="], @@ -2430,16 +2424,14 @@ "tar/minizlib/minipass": ["minipass@3.3.6", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="], - "terser-webpack-plugin/schema-utils/ajv-formats": ["ajv-formats@2.1.1", "", { "dependencies": { "ajv": "^8.0.0" } }, "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA=="], - - "webpack-dev-middleware/schema-utils/ajv-formats": ["ajv-formats@2.1.1", "", { "dependencies": { "ajv": "^8.0.0" } }, "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA=="], - "webpack-dev-server/chokidar/glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="], "webpack-dev-server/chokidar/readdirp": ["readdirp@3.6.0", "", { "dependencies": { "picomatch": "^2.2.1" } }, "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA=="], "webpack/eslint-scope/estraverse": ["estraverse@4.3.0", "", {}, "sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw=="], + "webpack/schema-utils/ajv-formats": ["ajv-formats@2.1.1", "", { "dependencies": { "ajv": "^8.0.0" } }, "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA=="], + "wrap-ansi-cjs/string-width/emoji-regex": ["emoji-regex@8.0.0", "", {}, "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A=="], "wrap-ansi-cjs/strip-ansi/ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="],