forked from IF-LK-2020/wordle
wotd
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class Game {
|
||||
|
||||
private static final int LOGIN_PHASE = 0;
|
||||
@@ -62,14 +65,14 @@ public class Game {
|
||||
|
||||
private void loginPhase(String msg) {
|
||||
|
||||
if (msg.length() <= 5) {
|
||||
sendUniversalError();
|
||||
return;
|
||||
}
|
||||
|
||||
String args = msg.substring(5);
|
||||
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) {
|
||||
@@ -80,6 +83,13 @@ public class Game {
|
||||
}
|
||||
|
||||
} 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();
|
||||
@@ -89,10 +99,25 @@ public class Game {
|
||||
} else {
|
||||
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 {
|
||||
sendUniversalError();
|
||||
}
|
||||
@@ -130,6 +155,12 @@ public class Game {
|
||||
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() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user