first commit
This commit is contained in:
commit
45721a5388
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@ -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
|
||||
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Winternship2024.iml" filepath="$PROJECT_DIR$/Winternship2024.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
20
Winternship2024.iml
Normal file
20
Winternship2024.iml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/24.0.0/annotations-24.0.0.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
13
resource/match_data.txt
Normal file
13
resource/match_data.txt
Normal file
@ -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
|
||||
19
resource/player_data.txt
Normal file
19
resource/player_data.txt
Normal file
@ -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,
|
||||
155
src/Main.java
Normal file
155
src/Main.java
Normal file
@ -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<String, Player> players = new HashMap<String, Player>();
|
||||
|
||||
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<String> legitimatePlayers = new ArrayList<String>();
|
||||
List<String> illegalPlayers = new ArrayList<String>();
|
||||
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<String> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/Player.java
Normal file
31
src/Player.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user