ZU VIEL gemacht

Mein Brain ist kaputt
This commit is contained in:
2022-03-25 19:06:43 +01:00
parent 018ffd987d
commit 556ae98e3b
5 changed files with 205 additions and 129 deletions

View File

@@ -1,3 +1,7 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Random;
public class Query {
DatabaseConnector db;
@@ -12,24 +16,36 @@ public class Query {
*/
public void pullStats(User user) {
String[][] r;
db.executeStatement("SELECT * FROM user WHERE user LIKE '"+ user.getUsername() + "'");
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();
user.setCurrentStreak(Integer.parseInt(r[1][0]));
user.setMaxStreak(Integer.parseInt(r[2][0]));
user.setWins(Integer.parseInt(r[3][0]));
user.setTimesPlayed(Integer.parseInt(r[4][0]));
if (r != null && r.length != 0 && r[0].length == 21) {
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]));
int wonInTurn[] = new int[6];
wonInTurn[0] = Integer.parseInt(r[5][0]);
wonInTurn[1] = Integer.parseInt(r[6][0]);
wonInTurn[2] = Integer.parseInt(r[7][0]);
wonInTurn[3] = Integer.parseInt(r[8][0]);
wonInTurn[4] = Integer.parseInt(r[9][0]);
wonInTurn[5] = Integer.parseInt(r[10][0]);
user.setWonInTurn(wonInTurn);
user.setLastDayWOTDPlayed(Integer.parseInt(r[11][0]));
for (int i = 0; i < 6; i++) {
try {
user.setWonInTurnIndex(Integer.parseInt(r[0][8 + i]), i);
} catch (NumberFormatException e) {
user.setWonInTurnIndex(0, i);
}
}
for (int i = 0; i < 5; i++) {
try {
user.setWonInTurnIndex(Integer.parseInt(r[0][8 + i]), i);
} catch (NumberFormatException e) {
user.getGame().setGuessesIndex(r[0][15 + i], i);
}
}
user.getGame().setWord(r[0][20]);
} else {
System.out.println("Fehler beim Abfragen der Daten für den User " + user.getUsername());
}
}
/**
@@ -38,16 +54,22 @@ public class Query {
* @param user
*/
public void pushStats(User user) {
// TODO: Testen und vllt korrigieren
db.executeStatement("UPDATE INTO Stats (current_streak," + " max_streak, " + "wins, " + "times_played, "
+ "won_on_1, " + "won_on_2, " + "won_on_3, " + "won_on_4, " + "won_on_5, " + "won_on_6, "
+ "last_day_WOTD_finished) " + "VALUES (" + Integer.toString(user.getCurrentStreak())
+ Integer.toString(user.getMaxStreak()) + Integer.toString(user.getWins())
+ Integer.toString(user.getTimesPlayed()) + Integer.toString(user.getWonInTurnIndex(0))
+ Integer.toString(user.getWonInTurnIndex(1)) + Integer.toString(user.getWonInTurnIndex(2))
+ Integer.toString(user.getWonInTurnIndex(3)) + Integer.toString(user.getWonInTurnIndex(4))
+ Integer.toString(user.getWonInTurnIndex(5)) + Integer.toString(user.getLastDayWOTDPlayed()) + ")"
+ "WHERE user_username LIKE \"" + user.getUsername() + "\"");
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()
+ " WHERE Stats_ID IN( SELECT user_username FROM Stats WHERE user_username LIKE '" + user.getUsername()
+ "')");
}
/**
@@ -63,7 +85,6 @@ public class Query {
db.executeStatement("SELECT 1 FROM user WHERE username LIKE \"" + username + "\" " + "AND passwort LIKE \""
+ password + "\"");
QueryResult r = db.getCurrentQueryResult();
System.out.println(db.getErrorMessage());
if (r != null && r.getRowCount() == 1) {
return true;
} else {
@@ -77,19 +98,27 @@ public class Query {
* @return Ein Word als String
*/
public String getWord() {
// TODO: Methode fertigstellen
return "12345";
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.
* pro Tag immer das gleiche Wordle, aber trotzdem für jeden neuen Tag ein anderes Wort
*
* @return Das Wort als String
*/
public String getWOTD() {
// TODO: Methode fertigstellen
return "abcde";
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];
}
/**
@@ -99,7 +128,8 @@ public class Query {
* @return Boolean
*/
public boolean checkWord(String word) {
// TODO: Methode fertigstellen
return true;
db.executeStatement("SELECT 1 FROM words WHERE word LIKE '" + word + "'");
String[][] r = db.getCurrentQueryResult().getData();
return r.length != 0;
}
}