forked from IF-LK-2020/wordle
416 lines
9.6 KiB
Java
416 lines
9.6 KiB
Java
import java.time.LocalDate;
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
public class Game {
|
|
|
|
private static final int LOGIN_PHASE = 0;
|
|
private static final int GAME_PHASE = 1;
|
|
private static final int END_PHASE = 2;
|
|
|
|
WordleServer wordleServer;
|
|
User user;
|
|
static DatabaseConnector databaseConnector;// Datenbankverbindung aufstellen
|
|
|
|
private String ip;
|
|
private int port;
|
|
|
|
private String wordle;
|
|
private String[] words;
|
|
private int count;
|
|
private int phase;
|
|
|
|
private String tempLoginName;
|
|
|
|
static {
|
|
|
|
databaseConnector = new DatabaseConnector(null, 0, "wordle.db", null, null);
|
|
|
|
if (databaseConnector.getErrorMessage() != null) {
|
|
System.err.println(databaseConnector.getErrorMessage());
|
|
}
|
|
|
|
}
|
|
|
|
public Game(WordleServer wordleServer, String pClientIP, int pClientPort) {
|
|
|
|
this.wordleServer = wordleServer;
|
|
this.ip = pClientIP;
|
|
this.port = pClientPort;
|
|
|
|
words = new String[5];
|
|
|
|
send("+OK Hello there");
|
|
}
|
|
|
|
public void processMessage(String msg) {
|
|
|
|
msg = msg.toLowerCase().trim();
|
|
|
|
if (msg.equals("quit")) {
|
|
if (user != null) {
|
|
saveGame();
|
|
}
|
|
send("+OK bye");
|
|
wordleServer.closeConnection(ip, port);
|
|
}
|
|
|
|
switch (phase) {
|
|
case LOGIN_PHASE:
|
|
loginPhase(msg);
|
|
break;
|
|
case GAME_PHASE:
|
|
gamePhase(msg);
|
|
break;
|
|
case END_PHASE:
|
|
endPhase(msg);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void loginPhase(String msg) {
|
|
|
|
if (msg.startsWith("user ")) {
|
|
|
|
if (msg.length() <= 5) {
|
|
sendUniversalError();
|
|
return;
|
|
}
|
|
String args = msg.substring(5);
|
|
|
|
databaseConnector.executeStatement("SELECT * FROM User WHERE name = '" + args + "'");
|
|
QueryResult result = databaseConnector.getCurrentQueryResult();
|
|
if (result != null && result.getData().length > 0) {
|
|
tempLoginName = result.getData()[0][0];
|
|
send("+OK");
|
|
} else {
|
|
send("-ERR User not found.");
|
|
}
|
|
|
|
} else if (msg.startsWith("pass ")) {
|
|
|
|
if (msg.length() <= 5) {
|
|
sendUniversalError();
|
|
return;
|
|
}
|
|
String args = msg.substring(5);
|
|
|
|
int pw = args.hashCode();
|
|
databaseConnector.executeStatement(
|
|
"SELECT * FROM User WHERE password = '" + pw + "' AND name = '" + tempLoginName + "'");
|
|
QueryResult result = databaseConnector.getCurrentQueryResult();
|
|
if (result != null && result.getData().length > 0) {
|
|
user = new User(tempLoginName);
|
|
send("+OK welcome");
|
|
} else {
|
|
send("-ERR login not valid");
|
|
}
|
|
} else if (msg.startsWith("wotd")) {
|
|
|
|
databaseConnector.executeStatement("SELECT word FROM words WHERE id = " + getWOTDIndex());
|
|
QueryResult result = databaseConnector.getCurrentQueryResult();
|
|
if (result != null && result.getData().length > 0) {
|
|
wordle = result.getData()[0][0].toLowerCase();
|
|
phase = GAME_PHASE;
|
|
if (user != null) {
|
|
loadGame();
|
|
} else {
|
|
send("+OK game ready");
|
|
}
|
|
} else {
|
|
send("-ERR fatal error: WOTD could not be generated.");
|
|
}
|
|
|
|
} else if (msg.startsWith("play")) {
|
|
|
|
if (user == null) {
|
|
send("-ERR not logged in.");
|
|
} 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();
|
|
} else {
|
|
send("-ERR WOTD not finished.");
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
sendUniversalError();
|
|
}
|
|
}
|
|
|
|
private void gamePhase(String msg) {
|
|
|
|
if (msg.startsWith("send ")) {
|
|
|
|
String args = msg.substring(5);
|
|
|
|
if (args.length() == 5) {
|
|
|
|
databaseConnector.executeStatement("SELECT word FROM words WHERE word LIKE '" + args + "'");
|
|
QueryResult result = databaseConnector.getCurrentQueryResult();
|
|
if (result.getData().length > 0) {
|
|
|
|
String input = result.getData()[0][0].toLowerCase();
|
|
String code = generateCode(input);
|
|
|
|
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());
|
|
}
|
|
user.increaseWonInTurn(count + 1);
|
|
updateStats();
|
|
phase = END_PHASE;
|
|
} else {
|
|
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;
|
|
} else {
|
|
phase = LOGIN_PHASE;
|
|
}
|
|
count = 0;
|
|
clearWords();
|
|
} else {
|
|
words[count] = input;
|
|
send("game continues");
|
|
count++;
|
|
}
|
|
|
|
} else {
|
|
send("-ERR word not in dictionary.");
|
|
}
|
|
|
|
} else {
|
|
send("-ERR wrong format.");
|
|
}
|
|
|
|
} 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")) {
|
|
|
|
if (user != null) {
|
|
saveGame();
|
|
phase = END_PHASE;
|
|
send("+OK");
|
|
} else {
|
|
send("-ERR not logged in.");
|
|
}
|
|
|
|
} else {
|
|
sendUniversalError();
|
|
}
|
|
}
|
|
|
|
private void endPhase(String msg) {
|
|
|
|
if (msg.startsWith("stat")) {
|
|
send("+OK");
|
|
|
|
send("times Played: " + user.getTimesPlayed());
|
|
|
|
send("win Percentage: " + (int) user.getWinPercentage() + "%");
|
|
|
|
send("current Streak: " + user.getCurrentStreak());
|
|
|
|
send("max Streak: " + user.getMaxStreak());
|
|
|
|
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;
|
|
}
|
|
} 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 && !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() {
|
|
|
|
user.increaseTimesPlayed();
|
|
|
|
int totalWins = 0;
|
|
for (int i = 1; i <= 6; i++) {
|
|
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) {
|
|
wordle = result.getData()[0][0].toLowerCase();
|
|
phase = GAME_PHASE;
|
|
send("+OK game ready");
|
|
} else {
|
|
send("-ERR fatal error: Wordle could not be generated.");
|
|
}
|
|
}
|
|
|
|
private void send(String msg) {
|
|
|
|
wordleServer.send(ip, port, msg);
|
|
}
|
|
|
|
private void sendUniversalError() {
|
|
|
|
send("-ERR command not valid in this state");
|
|
}
|
|
|
|
private int getWOTDIndex() {
|
|
|
|
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;
|
|
}
|
|
|
|
public void setIp(String ip) {
|
|
|
|
this.ip = ip;
|
|
}
|
|
|
|
public int getPort() {
|
|
|
|
return port;
|
|
}
|
|
|
|
public void setPort(int port) {
|
|
|
|
this.port = port;
|
|
}
|
|
}
|