This commit is contained in:
Asecave
2022-03-07 16:07:07 +01:00
parent e203a8747c
commit 6f117b6b30

View File

@@ -1,3 +1,6 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Game { public class Game {
private static final int LOGIN_PHASE = 0; private static final int LOGIN_PHASE = 0;
@@ -62,14 +65,14 @@ public class Game {
private void loginPhase(String msg) { private void loginPhase(String msg) {
if (msg.length() <= 5) {
sendUniversalError();
return;
}
String args = msg.substring(5);
if (msg.startsWith("user ")) { if (msg.startsWith("user ")) {
if (msg.length() <= 5) {
sendUniversalError();
return;
}
String args = msg.substring(5);
databaseConnector.executeStatement("SELECT * FROM User WHERE name = '" + args + "'"); databaseConnector.executeStatement("SELECT * FROM User WHERE name = '" + args + "'");
QueryResult result = databaseConnector.getCurrentQueryResult(); QueryResult result = databaseConnector.getCurrentQueryResult();
if (result != null && result.getData().length > 0) { if (result != null && result.getData().length > 0) {
@@ -80,6 +83,13 @@ public class Game {
} }
} else if (msg.startsWith("pass ")) { } else if (msg.startsWith("pass ")) {
if (msg.length() <= 5) {
sendUniversalError();
return;
}
String args = msg.substring(5);
int pw = args.hashCode(); int pw = args.hashCode();
databaseConnector.executeStatement("SELECT * FROM User WHERE password = '" + pw + "' AND name = '" + tempLoginName + "'"); databaseConnector.executeStatement("SELECT * FROM User WHERE password = '" + pw + "' AND name = '" + tempLoginName + "'");
QueryResult result = databaseConnector.getCurrentQueryResult(); QueryResult result = databaseConnector.getCurrentQueryResult();
@@ -89,10 +99,25 @@ public class Game {
} else { } else {
send("-ERR login not valid"); send("-ERR login not valid");
} }
} else if (msg.startsWith("wotd ")) { } else if (msg.startsWith("wotd")) {
} else if (msg.startsWith("play ")) { 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];
phase = GAME_PHASE;
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 {
}
} else { } else {
sendUniversalError(); sendUniversalError();
} }
@@ -130,6 +155,12 @@ public class Game {
send("-ERR command not valid in this state"); send("-ERR command not valid in this state");
} }
private int getWOTDIndex() {
long days = ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
int hash = ("" + days).hashCode();
return hash % 949 + 1;
}
public String getIp() { public String getIp() {
return ip; return ip;
} }