From 45721a53883c0b624b6752953ccad6774b72a5a6 Mon Sep 17 00:00:00 2001 From: arturkraak Date: Wed, 15 Nov 2023 20:58:46 +0200 Subject: [PATCH] first commit --- .gitignore | 29 ++++++++ .idea/.gitignore | 3 + .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ Winternship2024.iml | 20 +++++ resource/match_data.txt | 13 ++++ resource/player_data.txt | 19 +++++ src/Main.java | 155 +++++++++++++++++++++++++++++++++++++++ src/Player.java | 31 ++++++++ 10 files changed, 290 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Winternship2024.iml create mode 100644 resource/match_data.txt create mode 100644 resource/player_data.txt create mode 100644 src/Main.java create mode 100644 src/Player.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..eaf91e2 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d3c9b3d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..82c42b9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Winternship2024.iml b/Winternship2024.iml new file mode 100644 index 0000000..25305ff --- /dev/null +++ b/Winternship2024.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resource/match_data.txt b/resource/match_data.txt new file mode 100644 index 0000000..71998d6 --- /dev/null +++ b/resource/match_data.txt @@ -0,0 +1,13 @@ +abae2255-4255-4304-8589-737cdff61640,1.45,0.75,A +a3815c17-9def-4034-a21f-65369f6d4a56,4.34,0.23,B +2b20e5bb-9a32-4d33-b304-a9c7000e6de9,0.54,1.85,DRAW +de0785e2-58cb-413c-8e4b-1d135857733b,0.67,1.52,DRAW +292b3cd6-b463-4c98-94a7-468e03740af0,1.23,0.79,B +f407bca9-cf69-4343-a489-6cee72384297,3.23,0.31,B +d6c8b5a4-31ce-4bf8-8511-206cfd693440,0.88,1.17,B +0037ae59-61ea-46c3-88a8-8ca705acde10,2.43,0.41,B +5de45517-4312-4596-9702-9277b6d649f2,1.21,0.82,DRAW +5936bc82-8d80-46e1-a583-f4477a68896e,0.12,7.21,A +5cbed318-af08-4046-947e-8e08b9f199c6,1.32,0.74,A +45571462-4c54-4dc7-beb2-8d143d1c6f90,4.23,0.21,B +f6ffad45-a63f-448b-9a14-ec8a10c77a45,6.76,0.13,A \ No newline at end of file diff --git a/resource/player_data.txt b/resource/player_data.txt new file mode 100644 index 0000000..8b1581b --- /dev/null +++ b/resource/player_data.txt @@ -0,0 +1,19 @@ +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,DEPOSIT,,4000, +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,BET,abae2255-4255-4304-8589-737cdff61640,500,A +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,BET,a3815c17-9def-4034-a21f-65369f6d4a56,200,A +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,BET,2b20e5bb-9a32-4d33-b304-a9c7000e6de9,100,A +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,BET,de0785e2-58cb-413c-8e4b-1d135857733b,300,B +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,BET,292b3cd6-b463-4c98-94a7-468e03740af0,500,A +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,BET,f407bca9-cf69-4343-a489-6cee72384297,50,A +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,WITHDRAW,,200, +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,WITHDRAW,,1000, +163f23ed-e9a9-4e54-a5b1-4e1fc86f12f4,BET,d6c8b5a4-31ce-4bf8-8511-206cfd693440,50,A +4925ac98-833b-454b-9342-13ed3dfd3ccf,DEPOSIT,,400, +4925ac98-833b-454b-9342-13ed3dfd3ccf,BET,0037ae59-61ea-46c3-88a8-8ca705acde10,20,B +4925ac98-833b-454b-9342-13ed3dfd3ccf,DEPOSIT,,100, +4925ac98-833b-454b-9342-13ed3dfd3ccf,BET,5de45517-4312-4596-9702-9277b6d649f2,30,B +4925ac98-833b-454b-9342-13ed3dfd3ccf,BET,5936bc82-8d80-46e1-a583-f4477a68896e,50,B +4925ac98-833b-454b-9342-13ed3dfd3ccf,BET,5cbed318-af08-4046-947e-8e08b9f199c6,40,A +4925ac98-833b-454b-9342-13ed3dfd3ccf,BET,45571462-4c54-4dc7-beb2-8d143d1c6f90,10,A +4925ac98-833b-454b-9342-13ed3dfd3ccf,BET,f6ffad45-a63f-448b-9a14-ec8a10c77a45,33,A +4925ac98-833b-454b-9342-13ed3dfd3ccf,WITHDRAW,,8093, \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..8717da3 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,155 @@ +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.*; +import java.util.stream.Stream; + +public class Main { + public static Map players = new HashMap(); + + public static void main(String[] args) { + try { + BufferedReader reader = new BufferedReader(new FileReader("./resource/player_data.txt")); + String line; + + while((line = reader.readLine()) != null) { + evaluate(line); + } + + reader.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + // RESULTS + int casinoBalanceTotal = 0; + List legitimatePlayers = new ArrayList(); + List illegalPlayers = new ArrayList(); + for (var player : players.entrySet()) { + if(player.getValue().getInvalid() != null) { + illegalPlayers.add(player.getKey() + " " + player.getValue().getInvalid()); + } else { + String winRate = String.format(Locale.GERMAN,"%,.2f", player.getValue().getWinRate()); + legitimatePlayers.add(player.getKey() + " " + player.getValue().getCoins() +" "+ winRate); + casinoBalanceTotal += player.getValue().getCasinoBalance(); + } + } + + // Debug results to console +// System.out.println("Result: "); +// legitimatePlayers.forEach( (p) -> System.out.println(p)); +// System.out.println(""); +// illegalPlayers.forEach( (p) -> System.out.println(p)); +// System.out.println(""); +// System.out.println(casinoBalanceTotal); + + // Write to results.txt + try { + BufferedWriter writer = new BufferedWriter(new FileWriter("./out/production/Winternship2024/result.txt")); + if(legitimatePlayers.size() > 0) { + for (var player : legitimatePlayers) { + writer.write(player+"\n"); + } + } else { + writer.newLine(); // write empty line for the section if there are no players. + } + writer.newLine(); + if(illegalPlayers.size() > 0) { + for (var player : illegalPlayers) { + writer.write(player+"\n"); + } + } else { + writer.newLine(); // write empty line for the section if there are no players. + } + + writer.newLine(); + writer.write(new Integer(casinoBalanceTotal).toString()); + writer.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public static void evaluate(String s) { + String[] lineArr = s.split(","); + String playerID = lineArr[0]; + String action = lineArr[1]; + int amount = Integer.parseInt(lineArr[3]); + + if(!players.containsKey(playerID)) { // Create player Object if it does not exist. + players.put(playerID, new Player()); + } + Player player = players.get(playerID); + + switch(action) { + case "DEPOSIT": + deposit(player, amount); + break; + case "BET": + String matchID = lineArr[2]; + String betSide = lineArr[4]; + + bet(player, matchID, amount, betSide); + break; + case "WITHDRAW": + withdraw(player, amount); + break; + } + } + + public static void deposit(Player player, int amount){ + player.addCoins(amount); + } + public static void bet(Player player, String matchID, int amount, String betSide) { + String[] matchArr; + try (Stream lines = Files.lines(Paths.get("./resource/match_data.txt"))) { + String match = lines.filter(str->str.startsWith(matchID)).findFirst().get(); + matchArr = match.split(","); + } catch (IOException e) { + throw new RuntimeException(e); + } + + double sideA = Double.parseDouble(matchArr[1]); + double sideB = Double.parseDouble(matchArr[2]); + + if(amount < player.getCoins()) { + player.subCoins(amount); + } else if(player.getInvalid() == null) { + player.setInvalid("BET "+matchID+" "+amount+" "+ betSide); + } + player.incrementBets(); + + String outcome = matchArr[3]; + switch(outcome) { + case "A": + if(betSide.equals("A")) { + player.addCoins(amount); + player.addCoins((int) Math.floor(amount * sideA)); + player.incrementWins(); + player.subCasinoBalance((int) Math.floor(amount * sideA)); + } else { + player.addCasinoBalance(amount); + } + break; + case "B": + if(betSide.equals("B")) { + player.addCoins(amount); + player.addCoins((int) Math.floor(amount * sideB)); + player.incrementWins(); + player.subCasinoBalance((int) Math.floor(amount * sideB)); + } else { + player.addCasinoBalance(amount); + } + break; + case "DRAW": + player.addCoins(amount); + break; + } + } + public static void withdraw(Player player, int amount) { + if(amount < player.getCoins()) { + player.subCoins(amount); + } else if(player.getInvalid() == null) { + player.setInvalid("WITHDRAW null "+amount+ " null"); + } + } +} diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000..e9cfab4 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,31 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class Player { + public long getCasinoBalance() { return casinoBalance; } + private long casinoBalance = 0; // Casino balance relative to the player. This will be totalled if the player is legitimate. + private long coins = 0; + private int bets = 0; + private int wins = 0; + private String invalid; + public long getCoins() { + return coins; + } + public void addCoins(int amount) { this.coins += amount; } + public void subCoins(int amount) { this.coins -= amount; } + public String getInvalid() { + return invalid; + } + public void setInvalid(String invalid) { this.invalid = invalid; } + public void incrementBets() { this.bets++; } + public void incrementWins() { this.wins++; } + public void addCasinoBalance(int amount) { this.casinoBalance += amount; } + public void subCasinoBalance(int amount) { this.casinoBalance -= amount; } + public BigDecimal getWinRate() { + if(this.bets > 0){ + return new BigDecimal((double) this.wins/this.bets).setScale(2, RoundingMode.DOWN); + } else { + return new BigDecimal("0.00"); + } + } +}