mirror of
https://github.com/Vale54321/schafkopf-bot.git
synced 2025-12-15 19:29:33 +01:00
edited stuff (#50)
This commit is contained in:
committed by
GitHub
parent
b8b89ee696
commit
2e5a42b6d3
@@ -48,13 +48,15 @@ public class BackendServer implements MessageSender {
|
||||
// Configure CORS settings
|
||||
configureCors(context);
|
||||
|
||||
URL webContentUrl = getClass().getClassLoader().getResource("web-content");
|
||||
if (webContentUrl == null) {
|
||||
throw new RuntimeException("Unable to find 'web-content' directory");
|
||||
if (openFrontend) {
|
||||
URL webContentUrl = getClass().getClassLoader().getResource("web-content");
|
||||
if (webContentUrl == null) {
|
||||
throw new RuntimeException("Unable to find 'web-content' directory");
|
||||
}
|
||||
String webContentPath = webContentUrl.toExternalForm();
|
||||
context.setResourceBase(webContentPath);
|
||||
context.addServlet(new ServletHolder("frontend", DefaultServlet.class), "/");
|
||||
}
|
||||
String webContentPath = webContentUrl.toExternalForm();
|
||||
context.setResourceBase(webContentPath);
|
||||
context.addServlet(new ServletHolder("frontend", DefaultServlet.class), "/");
|
||||
|
||||
// Configure specific websocket behavior
|
||||
JettyWebSocketServletContainerInitializer.configure(
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.schafkopf;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
||||
@@ -31,20 +30,11 @@ public class DedicatedServerConnection implements MessageSender {
|
||||
* Class that represents one Frontend Connection.
|
||||
*/
|
||||
public DedicatedServerConnection(String address, MessageListener messageListener) {
|
||||
URI uri = null;
|
||||
try {
|
||||
uri = new URI(address);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
this.messageListener = messageListener;
|
||||
this.closeLatch = new CountDownLatch(1);
|
||||
this.connectionLatch = new CountDownLatch(1);
|
||||
|
||||
String host = uri.getHost();
|
||||
int port = uri.getPort();
|
||||
connect("ws://" + host + ":" + port);
|
||||
connect("ws://" + address);
|
||||
try {
|
||||
connectionLatch.await(); // Wait until the connection is established
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package org.schafkopf;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.schafkopf.GameState.GamePhase;
|
||||
import org.schafkopf.SchafkopfException.NotEnoughPlayersException;
|
||||
import org.schafkopf.SchafkopfMessage.SchafkopfBaseMessage;
|
||||
import org.schafkopf.SchafkopfMessage.SchafkopfMessageType;
|
||||
import org.schafkopf.karte.Karte;
|
||||
import org.schafkopf.karte.KartenFarbe;
|
||||
import org.schafkopf.karte.KartenListe;
|
||||
import org.schafkopf.karte.KartenUtil;
|
||||
@@ -79,23 +76,15 @@ public class Schafkopf {
|
||||
}
|
||||
|
||||
for (Player currentPlayer : player) {
|
||||
if (currentPlayer instanceof OnlinePlayer) {
|
||||
Karte[] karten = new Karte[8];
|
||||
if (currentPlayer instanceof OnlinePlayer onlinePlayer) {
|
||||
KartenListe karten = new KartenListe();
|
||||
for (int i = 7; i >= 0; i--) {
|
||||
karten[i] = austeilen.removeKarten(austeilen.getByIndex(i));
|
||||
karten.addKarten(austeilen.removeKarten(austeilen.getByIndex(i)));
|
||||
}
|
||||
Gson gson = new Gson();
|
||||
JsonObject messageObject = new JsonObject();
|
||||
messageObject.add("cards", gson.toJsonTree(karten));
|
||||
|
||||
messageSender.sendMessage(
|
||||
new SchafkopfBaseMessage(SchafkopfMessageType.ONLINE_PLAYER_HAND, messageObject));
|
||||
onlinePlayer.setAndSendPlayerCards(karten);
|
||||
}
|
||||
}
|
||||
|
||||
// spielThread = new Thread(() -> new Spielablauf(this, spiel));
|
||||
//
|
||||
// spielThread.start();
|
||||
new Spielablauf(this, spiel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.schafkopf.karte;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonElement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -25,6 +25,19 @@ public class KartenListe {
|
||||
return this.kartenListe;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Class that represents a list of Cards.
|
||||
*/
|
||||
public void sort() {
|
||||
KartenListe completeDeck = KartenUtil.initializeSchafKopfCardDeck();
|
||||
completeDeck.removeKarten(this);
|
||||
|
||||
KartenListe completeDeck2 = KartenUtil.initializeSchafKopfCardDeck();
|
||||
completeDeck2.removeKarten(completeDeck);
|
||||
|
||||
this.kartenListe = completeDeck2.getKartenListe();
|
||||
}
|
||||
|
||||
public void shuffle() {
|
||||
Collections.shuffle(this.kartenListe);
|
||||
}
|
||||
@@ -185,12 +198,10 @@ public class KartenListe {
|
||||
/**
|
||||
* A Class that represents a list of Cards.
|
||||
*/
|
||||
public JsonObject getJson() {
|
||||
public JsonElement getJson() {
|
||||
Gson gson = new Gson();
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.add("cards", gson.toJsonTree(this.kartenListe));
|
||||
|
||||
return jsonObject;
|
||||
return gson.toJsonTree(this.kartenListe);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
|
||||
@@ -5,7 +5,9 @@ import org.schafkopf.karte.KartenListe;
|
||||
import org.schafkopf.karte.KartenUtil;
|
||||
import org.schafkopf.spielcontroller.SpielController;
|
||||
|
||||
/** Player that represents the Bot. */
|
||||
/**
|
||||
* Player that represents the Bot.
|
||||
*/
|
||||
public class BotPlayer extends Player {
|
||||
|
||||
private KartenListe eigeneKarten;
|
||||
@@ -32,9 +34,12 @@ public class BotPlayer extends Player {
|
||||
return card;
|
||||
}
|
||||
|
||||
/** Set the Cards of the Player. */
|
||||
/**
|
||||
* Set the Cards of the Player.
|
||||
*/
|
||||
public void setCards(KartenListe cards) {
|
||||
System.out.println("Eigene Karte setzen");
|
||||
cards.sort();
|
||||
this.eigeneKarten = cards;
|
||||
this.unbekannteKarten = KartenUtil.initializeSchafKopfCardDeck();
|
||||
this.unbekannteKarten.removeKarten(eigeneKarten);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.schafkopf.player;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import org.schafkopf.MessageSender;
|
||||
@@ -17,13 +18,26 @@ public class OnlinePlayer extends Player {
|
||||
private final MessageSender messageSender;
|
||||
private final BlockingQueue<Karte> receivedCardQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
private KartenListe karten = new KartenListe();
|
||||
|
||||
public OnlinePlayer(MessageSender messageSender) {
|
||||
this.messageSender = messageSender;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Class that represents a list of Cards.
|
||||
*/
|
||||
public void setAndSendPlayerCards(KartenListe karten) {
|
||||
karten.sort();
|
||||
this.karten = karten;
|
||||
|
||||
sendPlayerCards();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Karte play(SpielController spiel, KartenListe tischKarten, KartenListe gespielteKarten)
|
||||
throws InterruptedException {
|
||||
sendPlayerCards();
|
||||
Karte spielKarte = null;
|
||||
|
||||
// Send the message to request the card from the frontend
|
||||
@@ -32,10 +46,20 @@ public class OnlinePlayer extends Player {
|
||||
|
||||
spielKarte = receivedCardQueue.take();
|
||||
|
||||
this.karten.removeKarten(spielKarte);
|
||||
sendPlayerCards();
|
||||
System.out.println("Karte gespielt: " + spielKarte);
|
||||
return spielKarte;
|
||||
}
|
||||
|
||||
private void sendPlayerCards() {
|
||||
JsonObject messageObject = new JsonObject();
|
||||
messageObject.add("cards", this.karten.getJson());
|
||||
|
||||
messageSender.sendMessage(
|
||||
new SchafkopfBaseMessage(SchafkopfMessageType.ONLINE_PLAYER_HAND, messageObject));
|
||||
}
|
||||
|
||||
/**
|
||||
* Class that represents one Frontend Connection.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user