forked from IF-LK-2020/wordle
Spielstand wird gespeichert (und gelesen)
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
@@ -139,22 +142,7 @@ public class Game {
|
||||
if (result.getData().length > 0) {
|
||||
|
||||
String input = result.getData()[0][0].toLowerCase();
|
||||
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";
|
||||
}
|
||||
}
|
||||
String code = generateCode(input);
|
||||
|
||||
send(code);
|
||||
if (code.equals("22222")) { // und ein wordle ist 5 Buchstaben lang :D
|
||||
@@ -173,8 +161,10 @@ public class Game {
|
||||
phase = LOGIN_PHASE;
|
||||
}
|
||||
count = 0;
|
||||
clearWords();
|
||||
} else if (count == 5) {
|
||||
send("game lost");
|
||||
wordle = null;
|
||||
if (user != null) {
|
||||
user.setCurrentStreak(0);
|
||||
updateStats();
|
||||
@@ -183,7 +173,9 @@ public class Game {
|
||||
phase = LOGIN_PHASE;
|
||||
}
|
||||
count = 0;
|
||||
clearWords();
|
||||
} else {
|
||||
words[count] = input;
|
||||
send("game continues");
|
||||
count++;
|
||||
}
|
||||
@@ -201,8 +193,17 @@ public class Game {
|
||||
} else if (msg.startsWith("exit")) {
|
||||
|
||||
if (user != null) {
|
||||
send("+OK");
|
||||
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() + "'");
|
||||
}
|
||||
phase = END_PHASE;
|
||||
send("+OK");
|
||||
} else {
|
||||
send("-ERR not logged in.");
|
||||
}
|
||||
@@ -232,6 +233,17 @@ public class Game {
|
||||
if (wordle == null) {
|
||||
playRandomWordle();
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
phase = GAME_PHASE;
|
||||
send("+OK game ready");
|
||||
}
|
||||
@@ -240,7 +252,35 @@ public class Game {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -253,6 +293,7 @@ public class Game {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user