forked from IF-LK-2020/wordle
Compare commits
7 Commits
4d01389e2e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc8d803d32 | ||
|
|
68549bbc8b | ||
|
|
92d4f15e75 | ||
|
|
a78fac1baf | ||
|
|
84a81bf2b4 | ||
|
|
37736dbfad | ||
|
|
09ea7ea01e |
160
src/Game.java
160
src/Game.java
@@ -37,6 +37,8 @@ public class Game {
|
|||||||
this.ip = pClientIP;
|
this.ip = pClientIP;
|
||||||
this.port = pClientPort;
|
this.port = pClientPort;
|
||||||
|
|
||||||
|
words = new String[5];
|
||||||
|
|
||||||
send("+OK Hello there");
|
send("+OK Hello there");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,9 +47,11 @@ public class Game {
|
|||||||
msg = msg.toLowerCase().trim();
|
msg = msg.toLowerCase().trim();
|
||||||
|
|
||||||
if (msg.equals("quit")) {
|
if (msg.equals("quit")) {
|
||||||
|
if (user != null) {
|
||||||
|
saveGame();
|
||||||
|
}
|
||||||
send("+OK bye");
|
send("+OK bye");
|
||||||
wordleServer.closeConnection(ip, port);
|
wordleServer.closeConnection(ip, port);
|
||||||
System.exit(0); // TODO remove: Einfacher zum testen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (phase) {
|
switch (phase) {
|
||||||
@@ -107,7 +111,11 @@ public class Game {
|
|||||||
if (result != null && result.getData().length > 0) {
|
if (result != null && result.getData().length > 0) {
|
||||||
wordle = result.getData()[0][0].toLowerCase();
|
wordle = result.getData()[0][0].toLowerCase();
|
||||||
phase = GAME_PHASE;
|
phase = GAME_PHASE;
|
||||||
|
if (user != null) {
|
||||||
|
loadGame();
|
||||||
|
} else {
|
||||||
send("+OK game ready");
|
send("+OK game ready");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
send("-ERR fatal error: WOTD could not be generated.");
|
send("-ERR fatal error: WOTD could not be generated.");
|
||||||
}
|
}
|
||||||
@@ -117,7 +125,16 @@ public class Game {
|
|||||||
if (user == null) {
|
if (user == null) {
|
||||||
send("-ERR not logged in.");
|
send("-ERR not logged in.");
|
||||||
} else {
|
} else {
|
||||||
|
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();
|
playRandomWordle();
|
||||||
|
} else {
|
||||||
|
send("-ERR WOTD not finished.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sendUniversalError();
|
sendUniversalError();
|
||||||
@@ -130,8 +147,6 @@ public class Game {
|
|||||||
|
|
||||||
String args = msg.substring(5);
|
String args = msg.substring(5);
|
||||||
|
|
||||||
System.out.println(wordle);
|
|
||||||
|
|
||||||
if (args.length() == 5) {
|
if (args.length() == 5) {
|
||||||
|
|
||||||
databaseConnector.executeStatement("SELECT word FROM words WHERE word LIKE '" + args + "'");
|
databaseConnector.executeStatement("SELECT word FROM words WHERE word LIKE '" + args + "'");
|
||||||
@@ -139,29 +154,14 @@ public class Game {
|
|||||||
if (result.getData().length > 0) {
|
if (result.getData().length > 0) {
|
||||||
|
|
||||||
String input = result.getData()[0][0].toLowerCase();
|
String input = result.getData()[0][0].toLowerCase();
|
||||||
String code = "";
|
String code = generateCode(input);
|
||||||
|
|
||||||
outer: for (int i = 0; i < input.length(); i++) {
|
send("+OK " + code);
|
||||||
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);
|
|
||||||
if (code.equals("22222")) { // und ein wordle ist 5 Buchstaben lang :D
|
if (code.equals("22222")) { // und ein wordle ist 5 Buchstaben lang :D
|
||||||
send("game won");
|
send("game won");
|
||||||
|
|
||||||
wordle = null;
|
wordle = null;
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
setLastPlayedTimestamp();
|
||||||
user.increaseCurrentStreak();
|
user.increaseCurrentStreak();
|
||||||
if (user.getMaxStreak() < user.getCurrentStreak()) {
|
if (user.getMaxStreak() < user.getCurrentStreak()) {
|
||||||
user.setMaxStreak(user.getCurrentStreak());
|
user.setMaxStreak(user.getCurrentStreak());
|
||||||
@@ -173,9 +173,12 @@ public class Game {
|
|||||||
phase = LOGIN_PHASE;
|
phase = LOGIN_PHASE;
|
||||||
}
|
}
|
||||||
count = 0;
|
count = 0;
|
||||||
|
clearWords();
|
||||||
} else if (count == 5) {
|
} else if (count == 5) {
|
||||||
send("game lost");
|
send("game lost");
|
||||||
|
wordle = null;
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
setLastPlayedTimestamp();
|
||||||
user.setCurrentStreak(0);
|
user.setCurrentStreak(0);
|
||||||
updateStats();
|
updateStats();
|
||||||
phase = END_PHASE;
|
phase = END_PHASE;
|
||||||
@@ -183,7 +186,9 @@ public class Game {
|
|||||||
phase = LOGIN_PHASE;
|
phase = LOGIN_PHASE;
|
||||||
}
|
}
|
||||||
count = 0;
|
count = 0;
|
||||||
|
clearWords();
|
||||||
} else {
|
} else {
|
||||||
|
words[count] = input;
|
||||||
send("game continues");
|
send("game continues");
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
@@ -198,11 +203,44 @@ public class Game {
|
|||||||
|
|
||||||
} else if (msg.startsWith("info")) {
|
} else if (msg.startsWith("info")) {
|
||||||
|
|
||||||
|
if (msg.length() == 4) {
|
||||||
|
send("+OK");
|
||||||
|
for (int i = 0; i < words.length; i++) {
|
||||||
|
if (words[i] == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
send(words[i] + " " + generateCode(words[i]));
|
||||||
|
}
|
||||||
|
send(".");
|
||||||
|
} else if (msg.length() == 6) {
|
||||||
|
|
||||||
|
String rawLine = msg.substring(5);
|
||||||
|
int line = -1;
|
||||||
|
try {
|
||||||
|
line = Integer.parseInt(rawLine) - 1;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
send("-ERR wrong argument.");
|
||||||
|
}
|
||||||
|
if (line >= 0 && line < 5) {
|
||||||
|
if (words[line] != null) {
|
||||||
|
send("+OK " + words[line] + " " + generateCode(words[line]));
|
||||||
|
} else {
|
||||||
|
send("-ERR game hasn't progressed to this line yet.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
send("-ERR couldn't parse line.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
sendUniversalError();
|
||||||
|
}
|
||||||
|
|
||||||
} else if (msg.startsWith("exit")) {
|
} else if (msg.startsWith("exit")) {
|
||||||
|
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
send("+OK");
|
saveGame();
|
||||||
phase = END_PHASE;
|
phase = END_PHASE;
|
||||||
|
send("+OK");
|
||||||
} else {
|
} else {
|
||||||
send("-ERR not logged in.");
|
send("-ERR not logged in.");
|
||||||
}
|
}
|
||||||
@@ -215,6 +253,7 @@ public class Game {
|
|||||||
private void endPhase(String msg) {
|
private void endPhase(String msg) {
|
||||||
|
|
||||||
if (msg.startsWith("stat")) {
|
if (msg.startsWith("stat")) {
|
||||||
|
send("+OK");
|
||||||
|
|
||||||
send("times Played: " + user.getTimesPlayed());
|
send("times Played: " + user.getTimesPlayed());
|
||||||
|
|
||||||
@@ -228,19 +267,86 @@ public class Game {
|
|||||||
send("won in Turn" + i + ": " + user.getWonInTurn(i));
|
send("won in Turn" + i + ": " + user.getWonInTurn(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
send(".");
|
||||||
|
|
||||||
} else if (msg.startsWith("play")) {
|
} else if (msg.startsWith("play")) {
|
||||||
if (wordle == null) {
|
if (wordle == null) {
|
||||||
playRandomWordle();
|
playRandomWordle();
|
||||||
} else {
|
} else {
|
||||||
|
loadGame();
|
||||||
phase = GAME_PHASE;
|
phase = GAME_PHASE;
|
||||||
send("+OK game ready");
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sendUniversalError();
|
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 && !result.getData()[0][0].equals("null")) {
|
||||||
|
boolean foundEnd = false;
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
if (!result.getData()[0][i].equals("null")) {
|
||||||
|
words[i] = result.getData()[0][i];
|
||||||
|
} else if (!foundEnd) {
|
||||||
|
count = i;
|
||||||
|
foundEnd = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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() {
|
private void updateStats() {
|
||||||
|
|
||||||
user.increaseTimesPlayed();
|
user.increaseTimesPlayed();
|
||||||
|
|
||||||
int totalWins = 0;
|
int totalWins = 0;
|
||||||
@@ -253,6 +359,7 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void playRandomWordle() {
|
private void playRandomWordle() {
|
||||||
|
|
||||||
databaseConnector.executeStatement("SELECT word FROM words WHERE id = " + (int) (Math.random() * 950));
|
databaseConnector.executeStatement("SELECT word FROM words WHERE id = " + (int) (Math.random() * 950));
|
||||||
QueryResult result = databaseConnector.getCurrentQueryResult();
|
QueryResult result = databaseConnector.getCurrentQueryResult();
|
||||||
if (result != null && result.getData().length > 0) {
|
if (result != null && result.getData().length > 0) {
|
||||||
@@ -276,11 +383,16 @@ public class Game {
|
|||||||
|
|
||||||
private int getWOTDIndex() {
|
private int getWOTDIndex() {
|
||||||
|
|
||||||
long days = ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
|
int days = currentDay();
|
||||||
int hash = ("" + days).hashCode();
|
int hash = ("" + days).hashCode();
|
||||||
return hash % 949 + 1;
|
return hash % 949 + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int currentDay() {
|
||||||
|
|
||||||
|
return (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
|
||||||
|
}
|
||||||
|
|
||||||
public String getIp() {
|
public String getIp() {
|
||||||
|
|
||||||
return ip;
|
return ip;
|
||||||
|
|||||||
Reference in New Issue
Block a user