forked from IF-LK-2020/wordle
Compare commits
2 Commits
4d01389e2e
...
37736dbfad
Author | SHA1 | Date |
---|---|---|
Asecave | 37736dbfad | |
Asecave | 09ea7ea01e |
137
src/Game.java
137
src/Game.java
|
@ -1,5 +1,6 @@
|
|||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Game {
|
||||
|
||||
|
@ -37,6 +38,8 @@ public class Game {
|
|||
this.ip = pClientIP;
|
||||
this.port = pClientPort;
|
||||
|
||||
words = new String[5];
|
||||
|
||||
send("+OK Hello there");
|
||||
}
|
||||
|
||||
|
@ -45,6 +48,9 @@ public class Game {
|
|||
msg = msg.toLowerCase().trim();
|
||||
|
||||
if (msg.equals("quit")) {
|
||||
if (user != null) {
|
||||
saveGame();
|
||||
}
|
||||
send("+OK bye");
|
||||
wordleServer.closeConnection(ip, port);
|
||||
System.exit(0); // TODO remove: Einfacher zum testen
|
||||
|
@ -107,7 +113,11 @@ public class Game {
|
|||
if (result != null && result.getData().length > 0) {
|
||||
wordle = result.getData()[0][0].toLowerCase();
|
||||
phase = GAME_PHASE;
|
||||
send("+OK game ready");
|
||||
if (user != null) {
|
||||
loadGame();
|
||||
} else {
|
||||
send("+OK game ready");
|
||||
}
|
||||
} else {
|
||||
send("-ERR fatal error: WOTD could not be generated.");
|
||||
}
|
||||
|
@ -117,7 +127,16 @@ public class Game {
|
|||
if (user == null) {
|
||||
send("-ERR not logged in.");
|
||||
} else {
|
||||
playRandomWordle();
|
||||
databaseConnector
|
||||
.executeStatement("SELECT lastFinishedWotdDay FROM User WHERE name = '" + user.getName() + "'");
|
||||
QueryResult result = databaseConnector.getCurrentQueryResult();
|
||||
if (result != null && result.getData().length > 0) {
|
||||
if (result.getData()[0][0] != null && Integer.parseInt(result.getData()[0][0]) == currentDay()) {
|
||||
playRandomWordle();
|
||||
} else {
|
||||
send("-ERR WOTD not finished.");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sendUniversalError();
|
||||
|
@ -129,9 +148,9 @@ public class Game {
|
|||
if (msg.startsWith("send ")) {
|
||||
|
||||
String args = msg.substring(5);
|
||||
|
||||
|
||||
System.out.println(wordle);
|
||||
|
||||
|
||||
if (args.length() == 5) {
|
||||
|
||||
databaseConnector.executeStatement("SELECT word FROM words WHERE word LIKE '" + args + "'");
|
||||
|
@ -139,29 +158,14 @@ public class Game {
|
|||
if (result.getData().length > 0) {
|
||||
|
||||
String input = result.getData()[0][0].toLowerCase();
|
||||
String code = "";
|
||||
String code = generateCode(input);
|
||||
|
||||
outer: for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c == wordle.charAt(i)) {
|
||||
code += "2";
|
||||
} else {
|
||||
for (int j = 0; j < wordle.length(); j++) {
|
||||
if (c == wordle.charAt(j)) {
|
||||
code += "1";
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
code += "0";
|
||||
}
|
||||
}
|
||||
|
||||
send(code);
|
||||
send("+OK " + code);
|
||||
if (code.equals("22222")) { // und ein wordle ist 5 Buchstaben lang :D
|
||||
send("game won");
|
||||
|
||||
wordle = null;
|
||||
if (user != null) {
|
||||
setLastPlayedTimestamp();
|
||||
user.increaseCurrentStreak();
|
||||
if (user.getMaxStreak() < user.getCurrentStreak()) {
|
||||
user.setMaxStreak(user.getCurrentStreak());
|
||||
|
@ -173,9 +177,12 @@ public class Game {
|
|||
phase = LOGIN_PHASE;
|
||||
}
|
||||
count = 0;
|
||||
clearWords();
|
||||
} else if (count == 5) {
|
||||
send("game lost");
|
||||
wordle = null;
|
||||
if (user != null) {
|
||||
setLastPlayedTimestamp();
|
||||
user.setCurrentStreak(0);
|
||||
updateStats();
|
||||
phase = END_PHASE;
|
||||
|
@ -183,7 +190,9 @@ public class Game {
|
|||
phase = LOGIN_PHASE;
|
||||
}
|
||||
count = 0;
|
||||
clearWords();
|
||||
} else {
|
||||
words[count] = input;
|
||||
send("game continues");
|
||||
count++;
|
||||
}
|
||||
|
@ -201,8 +210,9 @@ public class Game {
|
|||
} else if (msg.startsWith("exit")) {
|
||||
|
||||
if (user != null) {
|
||||
send("+OK");
|
||||
saveGame();
|
||||
phase = END_PHASE;
|
||||
send("+OK");
|
||||
} else {
|
||||
send("-ERR not logged in.");
|
||||
}
|
||||
|
@ -215,6 +225,7 @@ public class Game {
|
|||
private void endPhase(String msg) {
|
||||
|
||||
if (msg.startsWith("stat")) {
|
||||
send("+OK");
|
||||
|
||||
send("times Played: " + user.getTimesPlayed());
|
||||
|
||||
|
@ -227,20 +238,84 @@ public class Game {
|
|||
for (int i = 1; i <= 6; i++) {
|
||||
send("won in Turn" + i + ": " + user.getWonInTurn(i));
|
||||
}
|
||||
|
||||
send(".");
|
||||
|
||||
} else if (msg.startsWith("play")) {
|
||||
if (wordle == null) {
|
||||
playRandomWordle();
|
||||
} else {
|
||||
loadGame();
|
||||
phase = GAME_PHASE;
|
||||
send("+OK game ready");
|
||||
}
|
||||
} else {
|
||||
sendUniversalError();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void saveGame() {
|
||||
databaseConnector.executeStatement("SELECT * FROM Spielstand WHERE Username LIKE '" + user.getName() + "'");
|
||||
QueryResult result = databaseConnector.getCurrentQueryResult();
|
||||
if (result == null || result.getData().length == 0) {
|
||||
databaseConnector.executeStatement("INSERT INTO Spielstand (Username) VALUES('" + user.getName() + "')");
|
||||
}
|
||||
for (int i = 0; i < words.length; i++) {
|
||||
databaseConnector.executeStatement("UPDATE Spielstand SET word" + (i + 1) + " = '" + words[i]
|
||||
+ "' WHERE Username LIKE '" + user.getName() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
private void loadGame() {
|
||||
|
||||
databaseConnector.executeStatement("SELECT * FROM Spielstand WHERE Username = '" + user.getName() + "'");
|
||||
QueryResult result = databaseConnector.getCurrentQueryResult();
|
||||
if (result != null && result.getData().length > 0) {
|
||||
for (int i = 0; i < result.getData().length; i++) {
|
||||
words[i] = result.getData()[0][i];
|
||||
if (result.getData()[0][i] == null) {
|
||||
count = i - 1;
|
||||
}
|
||||
}
|
||||
send("+OK game running");
|
||||
} else {
|
||||
send("+OK game ready");
|
||||
}
|
||||
}
|
||||
|
||||
private void setLastPlayedTimestamp() {
|
||||
databaseConnector.executeStatement(
|
||||
"UPDATE User SET lastFinishedWotdDay = " + currentDay() + " WHERE name LIKE '" + user.getName() + "'");
|
||||
}
|
||||
|
||||
private void clearWords() {
|
||||
|
||||
for (int i = 0; i < words.length; i++) {
|
||||
words[i] = null;
|
||||
}
|
||||
}
|
||||
|
||||
private String generateCode(String input) {
|
||||
|
||||
String code = "";
|
||||
outer: for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c == wordle.charAt(i)) {
|
||||
code += "2";
|
||||
} else {
|
||||
for (int j = 0; j < wordle.length(); j++) {
|
||||
if (c == wordle.charAt(j)) {
|
||||
code += "1";
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
code += "0";
|
||||
}
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
private void updateStats() {
|
||||
|
||||
user.increaseTimesPlayed();
|
||||
|
||||
int totalWins = 0;
|
||||
|
@ -248,11 +323,12 @@ public class Game {
|
|||
totalWins += user.getWonInTurn(i);
|
||||
}
|
||||
float winP = (float) totalWins / user.getTimesPlayed();
|
||||
|
||||
|
||||
user.setWinPercentage(Math.round(winP * 100));
|
||||
}
|
||||
|
||||
|
||||
private void playRandomWordle() {
|
||||
|
||||
databaseConnector.executeStatement("SELECT word FROM words WHERE id = " + (int) (Math.random() * 950));
|
||||
QueryResult result = databaseConnector.getCurrentQueryResult();
|
||||
if (result != null && result.getData().length > 0) {
|
||||
|
@ -276,11 +352,16 @@ public class Game {
|
|||
|
||||
private int getWOTDIndex() {
|
||||
|
||||
long days = ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
|
||||
int days = currentDay();
|
||||
int hash = ("" + days).hashCode();
|
||||
return hash % 949 + 1;
|
||||
}
|
||||
|
||||
private int currentDay() {
|
||||
|
||||
return (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
|
||||
return ip;
|
||||
|
|
Loading…
Reference in New Issue