forked from IF-LK-2020/wordle
Compare commits
6 Commits
7d66b9c043
...
untested
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f117b6b30 | ||
|
|
e203a8747c | ||
|
|
86d449cba2 | ||
|
|
91ed88d15d | ||
|
|
8e9a9218be | ||
|
|
de0133d1b1 |
104
src/Game.java
104
src/Game.java
@@ -1,8 +1,15 @@
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class Game {
|
||||
// Instanzvariablen - ersetzen Sie das folgende Beispiel mit Ihren Variablen
|
||||
|
||||
private static final int LOGIN_PHASE = 0;
|
||||
private static final int GAME_PHASE = 1;
|
||||
private static final int END_PHASE = 2;
|
||||
|
||||
WordleServer wordleServer;
|
||||
User user;
|
||||
static DatabaseConnector databaseConnector = new DatabaseConnector("", 0, "wordle.db", "", "");;// Datenbankverbindung aufstellen
|
||||
static DatabaseConnector databaseConnector;// Datenbankverbindung aufstellen
|
||||
|
||||
private String ip;
|
||||
private int port;
|
||||
@@ -12,6 +19,18 @@ public class Game {
|
||||
private int count;
|
||||
private int phase;
|
||||
|
||||
private String tempLoginName;
|
||||
|
||||
static {
|
||||
|
||||
databaseConnector = new DatabaseConnector(null, 0, "wordle.db", null, null);
|
||||
|
||||
if (databaseConnector.getErrorMessage() != null) {
|
||||
System.err.println(databaseConnector.getErrorMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Game(WordleServer wordleServer, String pClientIP, int pClientPort) {
|
||||
|
||||
this.wordleServer = wordleServer;
|
||||
@@ -28,53 +47,95 @@ public class Game {
|
||||
if (msg.equals("quit")) {
|
||||
send("+OK bye");
|
||||
wordleServer.closeConnection(ip, port);
|
||||
System.exit(0); // TODO remove: Einfacher zum testen
|
||||
}
|
||||
|
||||
switch (phase) {
|
||||
case 0:
|
||||
case LOGIN_PHASE:
|
||||
loginPhase(msg);
|
||||
break;
|
||||
case 1:
|
||||
case GAME_PHASE:
|
||||
gamePhase(msg);
|
||||
break;
|
||||
case 2:
|
||||
case END_PHASE:
|
||||
endPhase(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void loginPhase(String msg) {
|
||||
|
||||
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) {
|
||||
tempLoginName = result.getData()[0][0];
|
||||
send("+OK");
|
||||
} else {
|
||||
send("-ERR User not found.");
|
||||
}
|
||||
|
||||
} 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();
|
||||
if (result != null && result.getData().length > 0) {
|
||||
user = new User(tempLoginName);
|
||||
send("+OK welcome");
|
||||
} else {
|
||||
send("-ERR login not valid");
|
||||
}
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
|
||||
private void gamePhase(String msg) {
|
||||
|
||||
|
||||
|
||||
if (msg.startsWith("send")) {
|
||||
|
||||
String args = msg.substring(5);
|
||||
//vergleichen von wotd mit
|
||||
//ja = spiel beendet und ausgabe word 11111
|
||||
//nein = ausgabe 01102 oder so und prüfen ob man noch ein wort eingebn kann
|
||||
//nein = ausgabe 01102 oder so und pr<EFBFBD>fen ob man noch ein wort eingebn kann
|
||||
// ja game continues nein = game lost
|
||||
|
||||
} else if (msg.startsWith("info")) {
|
||||
|
||||
|
||||
|
||||
} else if (msg.startsWith("exit")) {
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,17 +147,20 @@ public class Game {
|
||||
}
|
||||
}
|
||||
|
||||
private void spielstandAbrufen(String name)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void send(String msg) {
|
||||
wordleServer.send(ip, port, msg);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
public class User
|
||||
{
|
||||
public class User {
|
||||
|
||||
static DatabaseConnector databaseConnector;// Datenbankverbindung aufstellen
|
||||
private String name = "default";
|
||||
|
||||
/**
|
||||
* Konstruktor für Objekte der Klasse User
|
||||
* Konstruktor für Objekte der Klasse User
|
||||
*/
|
||||
public User(String name)
|
||||
{
|
||||
databaseConnector = new DatabaseConnector("", 0, "wordle.db", "", "");
|
||||
setName(name);
|
||||
}
|
||||
|
||||
@@ -17,14 +14,14 @@ public class User
|
||||
|
||||
public void setPassword(String password)
|
||||
{
|
||||
databaseConnector.executeStatement("UPDATE User SET password = " + password + " Where name = " + this.name);
|
||||
Game.databaseConnector.executeStatement("UPDATE User SET password = " + password + " Where name = " + this.name);
|
||||
}
|
||||
|
||||
|
||||
public String getPassword()
|
||||
{
|
||||
databaseConnector.executeStatement("SELECT password FROM User Where name =" + this.name);
|
||||
String result = databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
Game.databaseConnector.executeStatement("SELECT password FROM User Where name =" + this.name);
|
||||
String result = Game.databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -32,7 +29,7 @@ public class User
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
databaseConnector.executeStatement("UPDATE User SET name = " + name + " Where name = " + this.name);
|
||||
Game.databaseConnector.executeStatement("UPDATE User SET name = " + name + " Where name = " + this.name);
|
||||
}
|
||||
|
||||
|
||||
@@ -45,14 +42,14 @@ public class User
|
||||
|
||||
public void setWinPercentage(float winPercentage)
|
||||
{
|
||||
databaseConnector.executeStatement("UPDATE User SET winPercentage = " + winPercentage + " Where name = " + this.name);
|
||||
Game.databaseConnector.executeStatement("UPDATE User SET winPercentage = " + winPercentage + " Where name = " + this.name);
|
||||
}
|
||||
|
||||
|
||||
public float getWinPercentage()
|
||||
{
|
||||
databaseConnector.executeStatement("SELECT winPercentage FROM User Where name =" + this.name);
|
||||
String result = databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
Game.databaseConnector.executeStatement("SELECT winPercentage FROM User Where name =" + this.name);
|
||||
String result = Game.databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
return Float.parseFloat(result);
|
||||
}
|
||||
|
||||
@@ -60,14 +57,14 @@ public class User
|
||||
|
||||
public void setTimesPlayed(int timesPlayed)
|
||||
{
|
||||
databaseConnector.executeStatement("UPDATE User SET timesPlayed = " + timesPlayed + " Where name = " + this.name);
|
||||
Game.databaseConnector.executeStatement("UPDATE User SET timesPlayed = " + timesPlayed + " Where name = " + this.name);
|
||||
}
|
||||
|
||||
|
||||
public int getTimesPlayed()
|
||||
{
|
||||
databaseConnector.executeStatement("SELECT timesPlayed FROM User Where name =" + this.name);
|
||||
String result = databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
Game.databaseConnector.executeStatement("SELECT timesPlayed FROM User Where name =" + this.name);
|
||||
String result = Game.databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
return Integer.parseInt(result);
|
||||
}
|
||||
|
||||
@@ -83,14 +80,14 @@ public class User
|
||||
|
||||
public void setCurrentStreak(int currentStreak)
|
||||
{
|
||||
databaseConnector.executeStatement("UPDATE User SET currentStreak = " + currentStreak + " Where name = " + this.name);
|
||||
Game.databaseConnector.executeStatement("UPDATE User SET currentStreak = " + currentStreak + " Where name = " + this.name);
|
||||
}
|
||||
|
||||
|
||||
public int getCurrentStreak()
|
||||
{
|
||||
databaseConnector.executeStatement("SELECT currentStreak FROM User Where name =" + this.name);
|
||||
String result = databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
Game.databaseConnector.executeStatement("SELECT currentStreak FROM User Where name =" + this.name);
|
||||
String result = Game.databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
return Integer.parseInt(result);
|
||||
}
|
||||
|
||||
@@ -106,14 +103,14 @@ public class User
|
||||
|
||||
public void setMaxStreak(int maxStreak)
|
||||
{
|
||||
databaseConnector.executeStatement("UPDATE User SET maxStreak = " + maxStreak + " Where name = " + this.name);
|
||||
Game.databaseConnector.executeStatement("UPDATE User SET maxStreak = " + maxStreak + " Where name = " + this.name);
|
||||
}
|
||||
|
||||
|
||||
public int getMaxStreak()
|
||||
{
|
||||
databaseConnector.executeStatement("SELECT maxStreak FROM User Where name =" + this.name);
|
||||
String result = databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
Game.databaseConnector.executeStatement("SELECT maxStreak FROM User Where name =" + this.name);
|
||||
String result = Game.databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
return Integer.parseInt(result);
|
||||
}
|
||||
|
||||
@@ -129,14 +126,14 @@ public class User
|
||||
|
||||
public void setWonInTurn(int row, int value)
|
||||
{
|
||||
databaseConnector.executeStatement("UPDATE User SET wonInTurn" + row + " = " + value + " Where name = " + this.name);
|
||||
Game.databaseConnector.executeStatement("UPDATE User SET wonInTurn" + row + " = " + value + " Where name = " + this.name);
|
||||
}
|
||||
|
||||
|
||||
public int getWonInTurn(int row)
|
||||
{
|
||||
databaseConnector.executeStatement("SELECT wonInTurn" + row + " FROM User Where name =" + this.name);
|
||||
String result = databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
Game.databaseConnector.executeStatement("SELECT wonInTurn" + row + " FROM User Where name =" + this.name);
|
||||
String result = Game.databaseConnector.getCurrentQueryResult().getData()[0][0];
|
||||
return Integer.parseInt(result);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user