zuul extra wip

This commit is contained in:
2025-03-04 14:14:09 +01:00
parent f8f73925e3
commit 3e1b8a629e
9 changed files with 335 additions and 90 deletions

View File

@@ -1,5 +1,6 @@
package de.szut.zuul;
import org.java_websocket.WebSocket;
import java.util.Random;
/**
@@ -23,6 +24,7 @@ public class Game
{
private Parser parser;
private Player player;
private WebSocket conn;
/**
* Create the game and initialise its internal map.
@@ -116,32 +118,26 @@ public class Game
*
* @return
*/
public void play()
public void play(WebSocket conn)
{
printWelcome();
this.conn = conn;
conn.send("Spiel wurde gestartet...");
printWelcome(conn);
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (! finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Good bye.");
}
/**
* Print out the opening message for the player.
*/
private void printWelcome()
public void printWelcome(WebSocket conn)
{
System.out.println();
System.out.println("Welcome to the World of Zuul!");
System.out.println("World of Zuul is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
printRoomInformation();
conn.send("Welcome to the World of Zuul!");
conn.send("World of Zuul is a new, incredibly boring adventure game.");
conn.send("Type 'help' if you need help.");
printRoomInformation(conn);
}
/**
@@ -149,24 +145,25 @@ public class Game
* @param command The command to be processed.
* @return true If the command ends the game, false otherwise.
*/
private boolean processCommand(Command command)
public boolean processCommand(Command command, WebSocket conn)
{
boolean wantToQuit = false;
if(command.isUnknown()) {
System.out.println("I don't know what you mean...");
conn.send("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
switch (commandWord) {
case "help" -> printHelp();
case "go" -> goRoom(command);
case "quit" -> wantToQuit = quit(command);
case "look" -> look(player.getCurrentRoom());
case "status" -> showStatus();
case "take" -> takeItem(command);
case "drop" -> dropItem(command);
case "help" -> printHelp(conn);
case "go" -> goRoom(command, conn);
case "quit" -> wantToQuit = quit(command, conn);
case "look" -> look(player.getCurrentRoom(), conn);
case "status" -> showStatus(conn);
case "take" -> takeItem(command, conn);
case "drop" -> dropItem(command, conn);
case "say" -> {
if (command.hasSecondWord()) {
if (command.getSecondWord().equals("health")) {
@@ -175,10 +172,10 @@ public class Game
Ollama.chat(command.getSecondWord(), player);
}
} else {
System.out.println("Chat what?");
conn.send("Chat what?");
}
}
case "eat" -> eatMuffin(command);
case "eat" -> eatMuffin(command, conn);
}
return wantToQuit;
@@ -191,24 +188,24 @@ public class Game
* Here we print some stupid, cryptic message and a list of the
* command words.
*/
private void printHelp()
public void printHelp(WebSocket conn)
{
System.out.println("You are lost. You are alone. You wander");
System.out.println("through the jungle. At once there is a glade. On it there a buildings...");
System.out.println();
System.out.println("Your command words are:");
System.out.println(parser.showCommands());
conn.send("You are lost. You are alone. You wander");
conn.send("through the jungle. At once there is a glade. On it there a buildings...");
conn.send(" ");
conn.send("Your command words are:");
conn.send(parser.showCommands());
}
/**
* Try to go in one direction. If there is an exit, enter
* the new room, otherwise print an error message.
*/
private void goRoom(Command command)
public void goRoom(Command command, WebSocket conn)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
conn.send("Go where?");
return;
}
@@ -220,11 +217,11 @@ public class Game
nextRoom = player.getCurrentRoom().getExits(direction);
if (nextRoom == null) {
System.out.println("There is no door!");
conn.send("There is no door!");
}
else {
player.goTo(nextRoom);
printRoomInformation();
printRoomInformation(conn);
}
}
@@ -233,34 +230,33 @@ public class Game
* whether we really quit the game.
* @return true, if this command quits the game, false otherwise.
*/
private boolean quit(Command command)
public boolean quit(Command command, WebSocket conn)
{
if(command.hasSecondWord()) {
System.out.println("Quit what?");
conn.send("Quit what?");
return false;
}
else {
return true; // signal that we want to quit
}
}
private void printRoomInformation()
private void printRoomInformation(WebSocket conn)
{
System.out.print(player.getCurrentRoom().getLongDescription());
System.out.println();
conn.send(player.getCurrentRoom().getLongDescription());
}
private void look(Room currentRoom)
private void look(Room currentRoom, WebSocket conn)
{
System.out.println(currentRoom.getLongDescription());
conn.send(currentRoom.getLongDescription());
}
private void showStatus()
private void showStatus(WebSocket conn)
{
System.out.println(player.showStatus());
printRoomInformation();
conn.send(player.showStatus());
printRoomInformation(conn);
}
private void takeItem(Command command)
private void takeItem(Command command, WebSocket conn)
{
if (!command.hasSecondWord()) {
System.out.println("Take what?");
conn.send("Take what?");
return;
}
@@ -268,20 +264,20 @@ public class Game
Item item = player.getCurrentRoom().removeItem(itemName);
if (item != null) {
if (player.takeItem(item)) {
System.out.println("You took the item: " + item.getName());
conn.send("You took the item: " + item.getName());
} else {
System.out.println("You can't take this item. It's too heavy.");
conn.send("You can't take this item. It's too heavy.");
player.getCurrentRoom().putItem(item);
}
} else {
System.out.println("There is no such item in this room.");
conn.send("There is no such item in this room.");
}
showStatus();
showStatus(conn);
}
private void dropItem(Command command)
private void dropItem(Command command, WebSocket conn)
{
if (!command.hasSecondWord()) {
System.out.println("Drop what?");
conn.send("Drop what?");
return;
}
@@ -289,29 +285,33 @@ public class Game
Item item = player.dropItem(itemName);
if (item != null) {
player.getCurrentRoom().putItem(item);
System.out.println("You dropped the item: " + item.getName());
conn.send("You dropped the item: " + item.getName());
} else {
System.out.println("You don't have this item.");
conn.send("You don't have this item.");
}
showStatus();
showStatus(conn);
}
private void eatMuffin(Command command)
private void eatMuffin(Command command, WebSocket conn)
{
if (command.hasSecondWord()) {
if (command.getSecondWord().equals("Muffin")) {
Item item = player.getCurrentRoom().removeItem("Muffin");
if (item != null) {
player.eatMuffin();
System.out.println("You ate a muffin. Your load capacity is now " + player.getLoadCapacity());
showStatus();
conn.send("You ate a muffin. Your load capacity is now " + player.getLoadCapacity());
showStatus(conn);
} else {
System.out.println("There is no muffin in this room.");
conn.send("There is no muffin in this room.");
}
} else {
System.out.println("Eat what?");
conn.send("Eat what?");
}
} else {
System.out.println("Eat what?");
conn.send("Eat what?");
}
}
public Player getPlayer()
{
return player;
}
}

View File

@@ -1,5 +1,6 @@
package de.szut.zuul;
import org.java_websocket.WebSocket;
import java.util.Scanner;
/**
@@ -21,7 +22,6 @@ import java.util.Scanner;
public class Parser
{
private CommandWords commands; // holds all valid command words
private Scanner reader; // source of command input
/**
* Create a parser to read from the terminal window.
@@ -29,39 +29,30 @@ public class Parser
public Parser()
{
commands = new CommandWords();
reader = new Scanner(System.in);
}
/**
* @return The next command from the user.
*/
public Command getCommand()
public Command getCommand(String inputLine, WebSocket conn)
{
String inputLine; // will hold the full input line
String word1 = null;
String word2 = null;
System.out.print("> "); // print prompt
inputLine = reader.nextLine();
// Find up to two words on the line.
Scanner tokenizer = new Scanner(inputLine);
if(tokenizer.hasNext()) {
word1 = tokenizer.next(); // get first word
if(tokenizer.hasNext()) {
word2 = tokenizer.next(); // get second word
// note: we just ignore the rest of the input line.
}
if (inputLine == null || inputLine.trim().isEmpty()) {
// Falls die Eingabe leer oder null ist, eine ungültige Eingabe zurückgeben
return new Command(null, null);
}
// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).
if(commands.isCommand(word1)) {
// Entferne führende und nachfolgende Leerzeichen und teile die Eingabe in Wörter
String[] words = inputLine.trim().split("\\s+");
String word1 = words[0]; // Das erste Wort
String word2 = words.length > 1 ? words[1] : null; // Das zweite Wort, falls vorhanden
// Jetzt prüfen, ob das erste Wort ein gültiger Befehl ist.
if (commands.isCommand(word1)) {
return new Command(word1, word2);
}
else {
return new Command(null, word2);
} else {
// Wenn der Befehl ungültig ist, ein null-Command zurückgeben
return new Command(null, word2);
}
}
public String showCommands()

View File

@@ -7,7 +7,7 @@ public class ZuulUI {
*/
public static void main(String[] args) {
Game game = new Game();
game.play();
//game.play();
}
}

View File

@@ -0,0 +1,67 @@
package de.szut.zuul;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
public class ZuulWebsocket extends WebSocketServer {
private Game game;
private Parser parser;
public ZuulWebsocket(int port) {
super(new InetSocketAddress(port));
this.game = new Game();
this.parser = new Parser();
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
// Begrüßung des Spielers und Aufforderung, das Spiel zu starten
conn.send("Start Game.");
game.play(conn);
}
@Override
public void onMessage(WebSocket conn, String message) {
boolean finished = false;
conn.send(" ");
// Verarbeite den Befehl, nur einmal pro Nachricht
Command command = parser.getCommand(message, conn);
// Beende die Schleife, wenn der Befehl erfolgreich verarbeitet wurde
finished = game.processCommand(command, conn);
// Eine Nachricht zum Beenden des Spiels senden
if (finished) {
conn.send("Thank you for playing. Good bye.");
}
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
// Hier kannst du ggf. noch Aufräumarbeiten machen
}
@Override
public void onError(WebSocket conn, Exception ex) {
System.err.println("Fehler aufgetreten: " + ex.getMessage());
ex.printStackTrace();
}
// Leere Implementierung der onStart-Methode
@Override
public void onStart() {
System.out.println("WebSocket-Server gestartet");
}
public static void main(String[] args) {
int port = 8867; // Port für den WebSocket-Server
ZuulWebsocket server = new ZuulWebsocket(port); // Instanziierung der Klasse
server.start();
System.out.println("WebSocket-Server läuft auf Port " + port);
}
}