From 6f117b6b306926e638f1b405e291054557af2bee Mon Sep 17 00:00:00 2001 From: Asecave Date: Mon, 7 Mar 2022 16:07:07 +0100 Subject: [PATCH] wotd --- src/Game.java | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Game.java b/src/Game.java index c732186..9d4538f 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,3 +1,6 @@ +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + public class Game { private static final int LOGIN_PHASE = 0; @@ -61,15 +64,15 @@ 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("play ")) { - + } 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]; + 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(); } @@ -129,6 +154,12 @@ public class Game { private void sendUniversalError() { 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;