Compare commits

...

21 Commits

Author SHA1 Message Date
61632a0356 Kleine Bugs gefixt die mich getriggert haben 2022-03-28 20:58:16 +02:00
b27c5bb0a8 Datenbank aktualisiert
Wieder vergessen
2022-03-26 00:36:55 +01:00
6eb5f59b50 Fehler gefixt
Hat mir die Nacht gekostet
2022-03-26 00:34:14 +01:00
1a966b285f Datenbank aktualisiert
Habs vergessen
2022-03-25 19:09:01 +01:00
556ae98e3b ZU VIEL gemacht
Mein Brain ist kaputt
2022-03-25 19:06:43 +01:00
018ffd987d Fehler teilweise gefixt , Sachen hinzugefügt 2022-03-24 23:41:45 +01:00
74968d0f09 Merge remote-tracking branch 'origin/main' into main 2022-03-24 11:36:14 +01:00
d3a55e5278 push & pullStats und checkLogin 2022-03-24 11:36:02 +01:00
Maxim Derksen
9ecd839df1 Befehl Info und Exit
Info und Exit implementiert sowie eine neue Methode checkWord() in Game erstellt
2022-03-24 11:33:46 +01:00
2c14b051df Viel hinzugefügt
-Datenbank-Tabellen hinzugefügt (zum letzten Wordle-game und won_on_turn_n Tabelle)
-Ifception fortgesetzt, das geben eines guesses funktioniert nun
-give guess gefixt
2022-03-23 21:01:11 +01:00
81e54b9d39 Ifception der Wordle-Server Klasse
An der Bearbeitung der Abfragen gearbeitet, versucht die giveGuess-Methode zu fixen
2022-03-20 15:56:05 +01:00
02d6252537 TODOs und Game
Ein paar TODOs für die Organisation eingefügt und an der Klasse Game gearbeitet, hauptsächlich an der Methode giveGuess()
2022-03-18 19:09:19 +01:00
3fc4f6dfca An den if-Blöcken für die Abfragen gearbeitet 2022-03-12 19:51:30 +01:00
1c40e39708 Merge remote-tracking branch 'origin/main' into main 2022-03-03 13:27:46 +01:00
2337ad327d push und pull Stats überarbeitet 2022-03-03 13:27:19 +01:00
Maxim Derksen
fea36b6802 Object diagram extended 2022-03-03 11:33:38 +01:00
Maxim Derksen
a1b47bf828 Extended Protocol implementation 2022-03-03 11:30:40 +01:00
Maxim Derksen
4636051237 Added wins 2022-03-03 11:22:11 +01:00
Maxim Derksen
6c863aa7fc (Properly) added push and pull Stats 2022-03-03 11:20:18 +01:00
Maxim Derksen
e4037082bd Merge branch 'main' of https://git.ngb.schule/maxim.derksen/wordle 2022-03-03 10:46:03 +01:00
Maxim Derksen
4dd9794289 Added push and pull Stats into Query 2022-03-03 10:45:26 +01:00
6 changed files with 690 additions and 197 deletions

205
Game.java
View File

@@ -1,25 +1,200 @@
public class Game {
String randomWord;
String[] guesses;
//TODO:
Game() {
private String word;
private String[] guesses;
private int guessCount;
private int isWOTD;
// 0 wenn es keinen Wert gibt, 1 wenn das letzte Spiel gewonnen wurde und 2 wenn
// es verloren wurde
private int won;
}
Game() {
isWOTD = 0;
word = "";
won = 0;
guesses = new String[5];
}
public void startGame() {
/**
* Vergleicht den übergebenen Guess mit dem im Spiel gespeicherten Wort und
* überprüft die Korrektheit des Parameter-Strings und gibt eine Zeichenfolge
* aus der Korrektheit der Buchstaben zurück (z.B. 10020). Speichert außerdem
* den Guess in dem Array guesses[].Gibt keine Information darüber ob das Wort
* in der Datenbank existiert oder ob es die korrekte Länge hat.
*
* @param guess ,der String mit dem Wort
* @return Eine Zahlenfolge mit der Korrektheit des Worts
*/
public String giveGuess(String g) {
String[] guess = transformToArray(g);
String[] word = transformToArray(this.word);
String s = "";
if (word.length == 5 && guess.length == 5) {
for (int i = 0; i < this.word.length(); i++) {
if (guess[i].equalsIgnoreCase(word[i])) {
word[i] = null;
s += "2";
} else {
boolean found = false;
for (int r = 0; r < word.length; r++) {
if (guess[i].equalsIgnoreCase(word[r]) && (guess[r] == null || !guess[r].equals(word[r]))) {
s += "1";
found = true;
word[r] = null;
break;
}
}
if (!found) {
s += "0";
}
}
guess[i] = null;
}
guessCount++;
storeInArray(g);
if (s.contains("22222")) {
won = 1;
stopGame();
} else if (guessCount >= 6) {
won = 2;
stopGame();
}
}
return s;
}
}
private String[] transformToArray(String s) {
String[] temp = new String[s.length()];
for (int i = 0; i < s.length(); i++) {
temp[i] = "" + s.charAt(i);
}
return temp;
}
public void startWOTD() {
public String checkGuess(String g) {
String[] guess = transformToArray(g);
String[] word = transformToArray(this.word);
String s = "";
if (word.length == 5 && guess.length == 5) {
for (int i = 0; i < this.word.length(); i++) {
if (guess[i].equalsIgnoreCase(word[i])) {
word[i] = null;
s += "2";
} else {
boolean found = false;
for (int r = 0; r < word.length; r++) {
if (guess[i].equalsIgnoreCase(word[r]) && (guess[r] == null || !guess[r].equals(word[r]))) {
s += "1";
found = true;
word[r] = null;
break;
}
}
if (!found) {
s += "0";
}
}
guess[i] = null;
}
}
return s;
}
}
/**
* Speichert das Wort in der nächsten freien Stelle im Array und gibt danach
* zurück, ob das Array voll ist, d.h. das Spiel beendet werden muss
*
* @param word
* @return Boolean
*/
private void storeInArray(String word) {
for (int i = 0; i < guesses.length; i++) {
if (guesses[i] == null) {
guesses[i] = word;
break;
}
}
}
public boolean isGameRunning() {
return randomWord.equals("")||randomWord.equals(null);
}
public void startGame(String word, int isWOTD) {
this.isWOTD = isWOTD;
System.out.println(word);
guessCount = 0;
this.word = word;
}
private void stopGame() {
public void stopGame() {
word = "";
guesses = new String[guesses.length];
}
}
public String[] getGuesses() {
return guesses;
}
public String getGuessIndex(int index) {
if (guesses[index] != null) {
return "'" + guesses[index] + "'";
}
return null;
}
public int getIsWOTD() {
return isWOTD;
}
public void setGuessesIndex(String guess, int index) {
guesses[index] = guess;
}
public void updateGuessCount() {
int r = 0;
for (int i = 0; i < guesses.length; i++) {
if (guesses[i] != null) {
r++;
} else {
guessCount = r;
}
}
guessCount = r;
}
public void setWord(String word) {
if (word != null) {
this.word = word;
}
}
public void setIsWOTD(int isWOTD) {
this.isWOTD = isWOTD;
}
public String getWord() {
if (word.equals("")) {
return null;
}
return "'" + word + "'";
}
public int getGuessesCount() {
return guessCount;
}
/**
* Gibt 0 zurück wenn das Spiel nicht läuft und noch kein Spiel gespielt wurde
* seitdem das Objekt existiert. Gibt 1 zurück wenn das Spiel läuft. Gibt 2
* zurück wenn das letzte beendete Spiel gewonnen wurde und kein Spiel läuft.
* Gibt 3 zurück wenn das letzte beendete Spiel verloren wurde und kein Spiel
* läuft.
*
* @return Eine Zahl von 0 bis 3
*/
public int getGameStatus() {
if (word.equals("") && won == 0) {
return 0;
}
if (!word.equals("")) {
return 1;
}
return won + 1;
}
}

View File

@@ -1,24 +1,138 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Random;
public class Query {
DatabaseConnector db;
DatabaseConnector db;
Query(){
db = new DatabaseConnector("", 0, "wordle.db", "", "");
}
public void pullStats(User user){
String[][] r;
db.executeStatement("SELECT * FROM user WHERE user LIKE" + user.username);
r = db.getCurrentQueryResult().getData();
}
Query() {
db = new DatabaseConnector("", 0, "wordle.db", "", "");
}
public void pushStats(User user){
/**
* Speichert die Daten des Users aus der Datenbank auf dem User-Objekt
*
* @param user
*/
public void pullStats(User user) {
String[][] r;
db.executeStatement("SELECT * FROM Stats INNER JOIN won_on_n ON Stats.ID = won_on_n.Stats_ID "
+ "INNER JOIN last_Game ON Stats.ID = last_Game.Stats_ID WHERE user_username = '" + user.getUsername()
+ "'");
r = db.getCurrentQueryResult().getData();
if (r != null && r.length != 0 && r[0].length == 22) {
user.setCurrentStreak(Integer.parseInt(r[0][2]));
user.setMaxStreak(Integer.parseInt(r[0][3]));
user.setWinPercentage(Float.parseFloat(r[0][4]));
user.setTimesPlayed(Integer.parseInt(r[0][5]));
user.setLastDayWOTDPlayed(Integer.parseInt(r[0][6]));
}
for (int i = 0; i < 6; i++) {
try {
user.setWonInTurnIndex(Integer.parseInt(r[0][8 + i]), i);
} catch (NumberFormatException e) {
user.setWonInTurnIndex(0, i);
}
}
public void checkLogin(String username, String password){
for (int i = 0; i < 5; i++) {
user.getGame().setGuessesIndex(r[0][15 + i], i);
}
user.getGame().setWord(r[0][20]);
}
try {
user.getGame().setIsWOTD(Integer.parseInt(r[0][21]));
} catch (NumberFormatException e) {
user.getGame().setIsWOTD(0);
}
} else {
System.out.println("Fehler beim Abfragen der Daten für den User " + user.getUsername());
}
}
public String getWord(){
return "";
}
/**
* Pusht die Daten des Users im Parameter auf die Datenbank
*
* @param user
*/
public void pushStats(User user) {
db.executeStatement("UPDATE Stats SET current_streak = " + user.getCurrentStreak() + ", max_streak = "
+ user.getMaxStreak() + ", win_percentage = " + user.getWinPercentage() + ", times_played = "
+ user.getTimesPlayed() + ", last_day_WOTD_finished = " + user.getLastDayWOTDPlayed()
+ " WHERE user_username LIKE '" + user.getUsername() + "'");
db.executeStatement("UPDATE won_on_n SET won_on_1 = " + user.getWonInTurnIndex(0) + ", won_on_2 = "
+ user.getWonInTurnIndex(1) + ", won_on_3 = " + user.getWonInTurnIndex(2) + ", won_on_4 = "
+ user.getWonInTurnIndex(3) + ", won_on_5 = " + user.getWonInTurnIndex(4) + ", won_on_6 = "
+ user.getWonInTurnIndex(5)
+ " WHERE Stats_ID IN( SELECT user_username FROM Stats WHERE user_username LIKE '" + user.getUsername()
+ "')");
db.executeStatement("UPDATE last_Game SET last_guess_1 = " + user.getGame().getGuessIndex(0)
+ ", last_guess_2 = " + user.getGame().getGuessIndex(1) + ", last_guess_3 = "
+ user.getGame().getGuessIndex(2) + ", last_guess_4 = " + user.getGame().getGuessIndex(3)
+ ", last_guess_5 = " + user.getGame().getGuessIndex(4) + ", word = " + user.getGame().getWord()
+ ", isWOTD = " + user.getGame().getIsWOTD()
+ " WHERE Stats_ID IN( SELECT ID FROM Stats WHERE user_username LIKE '" + user.getUsername() + "')");
}
/**
* �berpr�ft ob es den Username und das Passwort in der Datenbank gibt und
* gibt den Erfolg als Boolean zur�ck
*
* @param username
* @param password
* @return Boolean true = login erfolgreich false = login fehlgeschlagen
*/
public boolean checkLogin(String username, String password) {
// TODO: testen und vllt korrigieren
db.executeStatement("SELECT 1 FROM user WHERE username LIKE \"" + username + "\" " + "AND passwort LIKE \""
+ password + "\"");
QueryResult r = db.getCurrentQueryResult();
if (r != null && r.getRowCount() == 1) {
return true;
} else {
return false;
}
}
/**
* Gibt ein Zuf�lliges Wort aus der Datenbank zur�ck
*
* @return Ein Word als String
*/
public String getWord() {
db.executeStatement("SELECT word FROM words ORDER BY random() LIMIT 1");
String[][] s = db.getCurrentQueryResult().getData();
return s[0][0];
}
/**
* Gibt das Wordle des Tages, welches aus der Datenbank stammt, zur�ck. Ergibt
* pro Tag immer das gleiche Wordle, aber trotzdem für jeden neuen Tag ein
* anderes Wort
*
* @return Das Wort als String
*/
public String getWOTD() {
db.executeStatement("SELECT COUNT(*) FROM words");
String[][] r = db.getCurrentQueryResult().getData();
int words = Integer.parseInt(r[0][0]);
Random random = new Random();
random.setSeed((int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now()));
int randNumber = random.nextInt() % words;
db.executeStatement("SELECT word FROM words WHERE ID LIKE '" + randNumber + "'");
r = db.getCurrentQueryResult().getData();
return r[0][0];
}
/**
* Pr�ft ob das Wort in der Datenbank vorhanden ist
*
* @param word ,ein String mit dem Wort
* @return Boolean
*/
public boolean checkWord(String word) {
db.executeStatement("SELECT 1 FROM words WHERE word LIKE '" + word + "'");
String[][] r = db.getCurrentQueryResult().getData();
return r.length != 0;
}
}

203
User.java
View File

@@ -1,102 +1,145 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class User {
String connectionID;
String username;
String password;
int timesPlayed;
int winPercentage;
int currentStreak;
int maxStreak;
int lastDayWOTDFinished;
int state;
int [] wonInTurn;
Game game;
private String connectionID;
private String username;
private String password;
private int timesPlayed;
private float winPercentage;
private int currentStreak;
private int maxStreak;
private int lastDayWOTDPlayed;
private int state;
private int[] wonInTurn;
private Game game;
public User (String pConnectionID){
connectionID =pConnectionID;
game = new Game();
}
public Game getGame(){
return game;
}
public String getConnectionID() {
return connectionID;
}
public User(String pConnectionID) {
connectionID = pConnectionID;
game = new Game();
wonInTurn = new int[6];
}
public int getCurrentStreak() {
return currentStreak;
}
public Game getGame() {
return game;
}
public int getLastDayWOTDFinished() {
return lastDayWOTDFinished;
}
public String getConnectionID() {
return connectionID;
}
public int getMaxStreak() {
return maxStreak;
}
public int getCurrentStreak() {
return currentStreak;
}
public int getTimesPlayed() {
return timesPlayed;
}
public int getLastDayWOTDPlayed() {
return lastDayWOTDPlayed;
}
public int getState() {
return state;
}
public int getMaxStreak() {
return maxStreak;
}
public int getWinPercentage() {
return winPercentage;
}
public int getTimesPlayed() {
return timesPlayed;
}
public int[] getWonInTurn() {
return wonInTurn;
}
public int getState() {
return state;
}
public String getPassword() {
return password;
}
public float getWinPercentage() {
return winPercentage;
}
public String getUsername() {
return username;
}
public int getWonInTurnIndex(int index) {
return wonInTurn[index];
}
public void setConnectionID(String connectionID) {
this.connectionID = connectionID;
}
public int[] getWonInTurn() {
return wonInTurn;
}
public void setCurrentStreak(int currentStreak) {
this.currentStreak = currentStreak;
}
public String getPassword() {
return password;
}
public void setLastDayWOTDFinished(int lastDayWOTDFinished) {
this.lastDayWOTDFinished = lastDayWOTDFinished;
}
public String getUsername() {
return username;
}
public void setMaxStreak(int maxStreak) {
this.maxStreak = maxStreak;
}
public void setConnectionID(String connectionID) {
this.connectionID = connectionID;
}
public void setPassword(String password) {
this.password = password;
}
public void setCurrentStreak(int currentStreak) {
this.currentStreak = currentStreak;
if (maxStreak < currentStreak) {
maxStreak = currentStreak;
}
}
public void setState(int state) {
this.state = state;
}
public void setMaxStreak(int maxStreak) {
this.maxStreak = maxStreak;
}
public void setTimesPlayed(int timesPlayed) {
this.timesPlayed = timesPlayed;
}
private int getTotalWins() {
int w = 0;
for (int i = 0; i < wonInTurn.length; i++) {
w += wonInTurn[i];
}
return w;
}
public void setUsername(String username) {
this.username = username;
}
public void updateWinPercentage() {
winPercentage = (int) (getTotalWins() / timesPlayed) * 100;
}
public void setWinPercentage(int winPercentage) {
this.winPercentage = winPercentage;
}
public void updateLastDayWOTDPlayed() {
lastDayWOTDPlayed = (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
}
public void setWonInTurn(int[] wonInTurn) {
this.wonInTurn = wonInTurn;
}
public void setPassword(String password) {
this.password = password;
}
public void setState(int state) {
this.state = state;
}
public void setTimesPlayed(int timesPlayed) {
this.timesPlayed = timesPlayed;
}
public void setUsername(String username) {
this.username = username;
}
public void setWinPercentage(float winPercentage) {
this.winPercentage = winPercentage;
}
public void setWonInTurnIndex(int content, int index) {
wonInTurn[index] = content;
}
public void increaseWonInTurnIndex(int index) {
wonInTurn[index]++;
}
public boolean loggedIn() {
if (password != null && username != null) {
return (!password.equals("") && !username.equals(""));
} else {
return false;
}
}
public String[] getLastGuesses() {
return game.getGuesses();
}
public void setLastDayWOTDPlayed(int time) {
lastDayWOTDPlayed = time;
}
}

View File

@@ -4,7 +4,7 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>192</x>
<x>114</x>
<y>438</y>
<w>270</w>
<h>96</h>
@@ -27,7 +27,7 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>258</x>
<x>180</x>
<y>348</y>
<w>126</w>
<h>42</h>
@@ -41,7 +41,7 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>78</x>
<x>0</x>
<y>642</y>
<w>126</w>
<h>42</h>
@@ -54,7 +54,7 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>264</x>
<x>186</x>
<y>642</y>
<w>126</w>
<h>42</h>
@@ -67,7 +67,7 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>258</x>
<x>180</x>
<y>246</y>
<w>126</w>
<h>42</h>
@@ -80,7 +80,7 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>438</x>
<x>360</x>
<y>642</y>
<w>126</w>
<h>42</h>
@@ -93,7 +93,7 @@
<element>
<id>Relation</id>
<coordinates>
<x>318</x>
<x>240</x>
<y>384</y>
<w>18</w>
<h>66</h>
@@ -104,7 +104,7 @@
<element>
<id>Relation</id>
<coordinates>
<x>318</x>
<x>240</x>
<y>282</y>
<w>18</w>
<h>78</h>
@@ -115,18 +115,18 @@
<element>
<id>Relation</id>
<coordinates>
<x>318</x>
<y>606</y>
<x>240</x>
<y>612</y>
<w>18</w>
<h>48</h>
<h>42</h>
</coordinates>
<panel_attributes>lt=&lt;..</panel_attributes>
<additional_attributes>10.0;60.0;10.0;10.0</additional_attributes>
<additional_attributes>10.0;50.0;10.0;10.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>198</x>
<x>120</x>
<y>654</y>
<w>78</w>
<h>18</h>
@@ -137,7 +137,7 @@
<element>
<id>Relation</id>
<coordinates>
<x>384</x>
<x>306</x>
<y>654</y>
<w>66</w>
<h>18</h>
@@ -148,8 +148,8 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>264</x>
<y>552</y>
<x>186</x>
<y>558</y>
<w>126</w>
<h>60</h>
</coordinates>
@@ -157,6 +157,7 @@
--
- pullStats(user)
+ pushStats(user)
+ createNewUser(user)
+ checkForLogin(String username, String passwort):boolean
+ getWord(int)</panel_attributes>
<additional_attributes/>
@@ -164,7 +165,7 @@
<element>
<id>UMLClass</id>
<coordinates>
<x>540</x>
<x>462</x>
<y>426</y>
<w>126</w>
<h>156</h>
@@ -191,7 +192,7 @@
<element>
<id>Relation</id>
<coordinates>
<x>456</x>
<x>378</x>
<y>480</y>
<w>96</w>
<h>24</h>
@@ -203,21 +204,21 @@ m1=n</panel_attributes>
<element>
<id>Relation</id>
<coordinates>
<x>318</x>
<x>240</x>
<y>528</y>
<w>24</w>
<h>36</h>
<h>42</h>
</coordinates>
<panel_attributes>lt=&lt;..
1
</panel_attributes>
<additional_attributes>10.0;40.0;10.0;10.0</additional_attributes>
<additional_attributes>10.0;50.0;10.0;10.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
<x>660</x>
<x>582</x>
<y>474</y>
<w>114</w>
<h>30</h>
@@ -230,7 +231,7 @@ m1=1
<element>
<id>UMLClass</id>
<coordinates>
<x>762</x>
<x>684</x>
<y>450</y>
<w>126</w>
<h>102</h>

View File

@@ -1,3 +1,5 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
/**
@@ -5,79 +7,237 @@ import java.util.HashMap;
*/
public class WordleServer extends Server {
public static final int DEFAULT_PORT = 1000;
public static final int DEFAULT_PORT = 1000;
public static void main( String[] args ) {
new WordleServer();
}
public static void main(String[] args) {
new WordleServer();
}
private HashMap<String, User> users;
private Query query;
private DatabaseConnector db;
public WordleServer() {
this(DEFAULT_PORT);
}
private HashMap<String,User>users;
private Query query;
public WordleServer(int pPort) {
super(pPort);
query = new Query();
users = new HashMap<>();
}
@Override
public void processNewConnection(String pClientIP, int pClientPort) {
String connectionID = pClientIP + ":" + Integer.toString(pClientPort);
User u = new User(connectionID);
users.put(connectionID, u);
send(pClientIP, pClientPort, "+OK Connection to Wordle-Server successful");
u.setState(1);
public WordleServer() {
this(DEFAULT_PORT);
}
}
public WordleServer( int pPort ) {
super(pPort);
query = new Query();
db = new DatabaseConnector("", 0, "wordle.db", "", "");
users = new HashMap<>();
}
@Override
public void processClosingConnection(String pClientIP, int pClientPort) {
String connectionID = pClientIP + ":" + Integer.toString(pClientPort);
User u = users.get(connectionID);
if (u.loggedIn()) {
query.pushStats(u);
}
}
@Override
public void processNewConnection( String pClientIP, int pClientPort ) {
String connectionID= pClientIP +":"+ Integer.toString(pClientPort);
User u = new User(connectionID);
users.put(connectionID,u);
send(pClientIP,pClientPort,"+OK Verbindung zum Wordle Server erfolgreich aufgebaut");
u.setState(1);
@Override
public void processMessage(String pClientIP, int pClientPort, String pMessage) {
String connectionID = pClientIP + ":" + Integer.toString(pClientPort);
User u = users.get(connectionID);
u.setConnectionID(connectionID);
u.getGame().updateGuessCount();
if (pMessage.length() < 4) {
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
} else {
String m = pMessage.substring(0, 4);
System.out.println("Message: " + m + " State: " + u.getState());
if (pMessage.equalsIgnoreCase("QUIT")) {
send(pClientIP, pClientPort, "+OK Bye");
closeConnection(pClientIP, pClientPort);
}
switch (u.getState()) {
case 1:
// Anmeldephase
if (m.equalsIgnoreCase("USER") && !u.loggedIn()) {
m = pMessage.substring(5, pMessage.length());
u.setUsername(m);
send(pClientIP, pClientPort, "+OK");
} else if (m.equalsIgnoreCase("PASS") && !u.loggedIn()) {
m = pMessage.substring(5, pMessage.length());
u.setPassword(m);
if (u.loggedIn()) {
if (query.checkLogin(u.getUsername(), u.getPassword())) {
send(pClientIP, pClientPort, "+OK welcome");
query.pullStats(u);
} else {
u.setPassword("");
u.setUsername("");
send(pClientIP, pClientPort, "-ERR login not valid");
}
} else {
u.setPassword("");
u.setUsername("");
send(pClientIP, pClientPort, "-ERR login not valid");
}
} else if (m.equalsIgnoreCase("WOTD")) {
if (!isToday(u.getLastDayWOTDPlayed())) {
send(pClientIP, pClientPort, "+OK game ready");
u.getGame().startGame(query.getWOTD(), 1);
u.setState(2);
u.updateLastDayWOTDPlayed();
} else if (u.getGame().getGameStatus() == 1 && u.getGame().getIsWOTD() == 1) {
send(pClientIP, pClientPort, "+OK Game continues");
u.setState(2);
u.updateLastDayWOTDPlayed();
} else {
send(pClientIP, pClientPort, "-ERR game finished");
}
} else if (m.equalsIgnoreCase("PLAY")) {
if (u.loggedIn()) {
if (isToday(u.getLastDayWOTDPlayed())) {
if (u.getGame().getGameStatus() != 1) {
u.getGame().startGame(query.getWord(), 0);
u.setState(2);
send(pClientIP, pClientPort, "+OK Game ready");
} else if (u.getGame().getIsWOTD() == 0) {
u.setState(2);
send(pClientIP, pClientPort, "+OK Game continues");
}
} else {
send(pClientIP, pClientPort, "-ERR wotd not finished");
}
} else {
send(pClientIP, pClientPort, "-ERR Not logged in");
}
} else {
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
}
break;
case 2:
// Spielphase
if (m.equalsIgnoreCase("SEND")) {
m = pMessage.substring(5, pMessage.length());
if (m.length() == 5) {
if (query.checkWord(m)) {
String a = "+OK " + u.getGame().giveGuess(m);
switch (u.getGame().getGameStatus()) {
case (1):
a += " game continues";
break;
case (2):
a += " game won";
u.increaseWonInTurnIndex(u.getGame().getGuessesCount() - 1);
u.setCurrentStreak(u.getCurrentStreak() + 1);
u.setTimesPlayed(u.getTimesPlayed() + 1);
u.updateWinPercentage();
u.setState(3);
break;
case (3):
a += " game lost";
u.setTimesPlayed(u.getTimesPlayed() + 1);
u.setCurrentStreak(0);
u.updateWinPercentage();
u.setState(3);
break;
}
send(pClientIP, pClientPort, a);
} else {
send(pClientIP, pClientPort, "-ERR word not in dict");
}
} else {
send(pClientIP, pClientPort, "-ERR wrong format");
}
} else if (m.equalsIgnoreCase("INFO")) {
bruh();
String substring = "0";
if (pMessage.length() > 4) {
substring += pMessage.substring(4, pMessage.length()).replaceAll(" ", "");
}
int n = Integer.parseInt("0" + substring);
String[] guesses = u.getGame().getGuesses();
if (6 >= n && n >= 1) {
if (guesses[n - 1] != null) {
String guess = guesses[n - 1];
send(pClientIP, pClientPort, "+OK " + guess + " " + u.getGame().checkGuess(guess));
} else {
send(pClientIP, pClientPort, "-ERR word not typed yet");
}
} else if (n == 0) {
String a = "+OK";
for (int i = 0; i < 5; i++) {
if (guesses[i] != null) {
send(pClientIP, pClientPort, guesses[i] + " " + u.getGame().checkGuess(guesses[i]));
}
}
send(pClientIP, pClientPort, ".");
}
} else if (m.equalsIgnoreCase("EXIT")) {
u.updateLastDayWOTDPlayed();
u.getGame().stopGame();
u.setState(3);
send(pClientIP, pClientPort, "+OK");
} else {
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
}
break;
case 3:
// Zwischenphase(Angemeldet)
if (m.equalsIgnoreCase("STAT")) {
send(pClientIP, pClientPort, "+OK");
send(pClientIP, pClientPort, "times played: " + u.getTimesPlayed());
send(pClientIP, pClientPort, "times played: " + u.getTimesPlayed());
send(pClientIP, pClientPort, "win percentage: " + u.getWinPercentage());
send(pClientIP, pClientPort, "current streak: " + u.getCurrentStreak());
send(pClientIP, pClientPort, "max streak: " + u.getMaxStreak());
send(pClientIP, pClientPort, "won in 1 turn: " + u.getWonInTurnIndex(0));
send(pClientIP, pClientPort, "won in 2 turn: " + u.getWonInTurnIndex(1));
send(pClientIP, pClientPort, "won in 3 turn: " + u.getWonInTurnIndex(2));
send(pClientIP, pClientPort, "won in 4 turn: " + u.getWonInTurnIndex(3));
send(pClientIP, pClientPort, "won in 5 turn: " + u.getWonInTurnIndex(4));
send(pClientIP, pClientPort, "won in 6 turn: " + u.getWonInTurnIndex(5));
} else if (m.equalsIgnoreCase("PLAY")) {
if (u.loggedIn()) {
if (isToday(u.getLastDayWOTDPlayed())) {
if (u.getGame().getGameStatus() != 1) {
u.getGame().startGame(query.getWord(), 0);
u.setState(2);
send(pClientIP, pClientPort, "+OK Game ready");
} else if (u.getGame().getIsWOTD() == 0) {
u.setState(2);
send(pClientIP, pClientPort, "+OK Game continues");
}
} else {
send(pClientIP, pClientPort, "-ERR wotd not finished");
}
} else {
send(pClientIP, pClientPort, "-ERR not logged in");
}
} else {
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
}
break;
}
}
}
}
@Override
public void processClosingConnection( String pClientIP, int pClientPort ) {
}
@Override
public void processMessage( String pClientIP, int pClientPort, String pMessage ) {
String conectionID = pClientIP + ":" + Integer.toString(pClientPort);
User u = users.get(conectionID);
if(pMessage.equals("QUIT")) {
send(pClientIP, pClientPort, "+OK Bye");
close();
} else {
send(pClientIP,pClientPort,"-ERR Command not valid in this state");
}
switch(u.getState()){
case 1:
//Anmeldephase
String m = pMessage.substring(0,3);
if(m.equalsIgnoreCase("USER")){
m = pMessage.substring(6,pMessage.length()-1);
u.setUsername(m);
} else if(m.equalsIgnoreCase("PASS")){
m = pMessage.substring(6,pMessage.length()-1);
u.setPassword(m);
}
break;
case 2:
//Spielphase
break;
case 3:
//Zwischenphase(Angemeldet)
break;
}
}
/**
* Gibt zurück ob es sich bei dem im Parameter angegebenen Tag um den heutigen
* handelt
*
* @param pDay ,die Anzahl der Tage seit dem Epoch-Day
* @return true oder false
*/
public boolean isToday(int pDay) {
return pDay == (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
}
public void bruh() {
}
}

BIN
wordle.db

Binary file not shown.