mirror of
https://github.com/Vale54321/schafkopf-bot.git
synced 2025-12-15 19:29:33 +01:00
Message types and server2 (#46)
* fixed building issue * fixed building issue
This commit is contained in:
committed by
GitHub
parent
a0a1cfaa4a
commit
2cd8359518
@@ -29,11 +29,11 @@ function joinGame(): void {
|
||||
}
|
||||
|
||||
function sendCard(cardInput: Card): void {
|
||||
const index = botCards.value.findIndex(card => card === cardInput);
|
||||
const index = botCards.value!.findIndex(card => card === cardInput);
|
||||
|
||||
// If card exists in the array, remove it
|
||||
if (index !== -1) {
|
||||
botCards.value.splice(index, 1);
|
||||
botCards.value!.splice(index, 1);
|
||||
}
|
||||
backendConnection.sendMessage(MessageType.PLAYER_CARD, {card: cardInput});
|
||||
}
|
||||
@@ -90,11 +90,11 @@ onMounted(() => {
|
||||
const messageListener = (message: string) => {
|
||||
const message1: BackendMessage = JSON.parse(message);
|
||||
console.log(message1)
|
||||
if (message1.message_type === "GAME_STATE") {
|
||||
if (message1.message_type === "GAME_STATE" && "gamestate" in message1.content) {
|
||||
console.log(message1.content)
|
||||
showGameState(message1.content)
|
||||
showGameState(message1.content.gamestate)
|
||||
}
|
||||
if (message1.message_type === "ONLINE_PLAYER_HAND") {
|
||||
if (message1.message_type === "ONLINE_PLAYER_HAND" && "cards" in message1.content) {
|
||||
botCards.value = message1.content.cards;
|
||||
console.log(message1.content.cards)
|
||||
}
|
||||
|
||||
@@ -1,186 +1,186 @@
|
||||
<script lang="ts" setup>
|
||||
import {onMounted, ref} from 'vue';
|
||||
|
||||
import {scg} from 'ioc-service-container';
|
||||
import CardComp from '../components/CardComponent.vue';
|
||||
import {BackendMessage, Card, GamePhase, GameState} from "../BackendMessage";
|
||||
|
||||
const backendConnection = scg("BackendConnection");
|
||||
|
||||
|
||||
const messageFromServer = ref<string[]>([]);
|
||||
|
||||
const gameStateText = ref<string>("Schafkopf");
|
||||
const gameInfoText = ref<string>("");
|
||||
|
||||
const socket = ref<WebSocket | null>();
|
||||
const tableCards = ref<Card[]>([]);
|
||||
const botCards = ref(0);
|
||||
const trickCard = ref<Card>();
|
||||
|
||||
const showGameSelect = ref(true);
|
||||
|
||||
|
||||
function startSimulation(): void {
|
||||
sendMessageToServer("startsimulation");
|
||||
|
||||
}
|
||||
|
||||
function startDedicated(): void {
|
||||
sendMessageToServer("startdedicated");
|
||||
|
||||
}
|
||||
|
||||
|
||||
function stopSimulation(): void {
|
||||
sendMessageToServer("stopsimulation");
|
||||
}
|
||||
|
||||
function showTrumpf(): void {
|
||||
tableCards.value = [];
|
||||
sendMessageToServer("showtrumpf");
|
||||
}
|
||||
|
||||
function showFarben(): void {
|
||||
tableCards.value = [];
|
||||
sendMessageToServer("showfarben");
|
||||
}
|
||||
|
||||
function setGame(game: string): void {
|
||||
sendMessageToServer(game);
|
||||
}
|
||||
|
||||
|
||||
function sendMessageToServer(message: string) {
|
||||
if (socket.value) {
|
||||
socket.value.send(message);
|
||||
console.log("Sent message to server:", message);
|
||||
}
|
||||
}
|
||||
|
||||
function showGameState(gamestate: GameState) {
|
||||
|
||||
|
||||
switch (gamestate.gamePhase) {
|
||||
case GamePhase.GAME_START:
|
||||
gameStateText.value = "Spiel startet";
|
||||
showGameSelect.value = false;
|
||||
botCards.value = 8;
|
||||
break;
|
||||
case GamePhase.TRICK_START:
|
||||
gameStateText.value = "Runde startet";
|
||||
tableCards.value = [];
|
||||
trickCard.value = undefined
|
||||
gameInfoText.value = "";
|
||||
break;
|
||||
case GamePhase.WAIT_FOR_CARD:
|
||||
gameStateText.value = "Spieler " + gamestate.currentPlayer + " muss eine Karte legen.";
|
||||
break;
|
||||
case GamePhase.PLAYER_CARD:
|
||||
gameStateText.value = "Spieler " + gamestate.currentPlayer + " hat eine Karte gespielt.";
|
||||
if (gamestate.currentPlayer === 0) {
|
||||
botCards.value--
|
||||
}
|
||||
if (gamestate.trumpf) {
|
||||
gameInfoText.value = "TRUMPF";
|
||||
} else {
|
||||
gameInfoText.value = gamestate.color?.toString() ?? "ERROR";
|
||||
}
|
||||
tableCards.value.push(gamestate.card!);
|
||||
|
||||
|
||||
break;
|
||||
case GamePhase.PLAYER_TRICK:
|
||||
gameStateText.value = "Spieler " + gamestate.currentPlayer + " sticht.";
|
||||
trickCard.value = gamestate.card
|
||||
break;
|
||||
case GamePhase.GAME_STOP:
|
||||
showGameSelect.value = true;
|
||||
break;
|
||||
default:
|
||||
gameStateText.value = "Fehler";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
socket.value = backendConnection.getWebSocket();
|
||||
|
||||
const messageListener = (message: string) => {
|
||||
const message1: BackendMessage = JSON.parse(message);
|
||||
console.log(message1)
|
||||
if ('gamestate' in message1) {
|
||||
console.log(message1.gamestate)
|
||||
showGameState(message1.gamestate)
|
||||
}
|
||||
};
|
||||
|
||||
backendConnection.addMessageListener(messageListener);
|
||||
});
|
||||
// import {onMounted, ref} from 'vue';
|
||||
//
|
||||
// import {scg} from 'ioc-service-container';
|
||||
// import CardComp from '../components/CardComponent.vue';
|
||||
// import {BackendMessage, Card, GamePhase, GameState} from "../BackendMessage";
|
||||
//
|
||||
// const backendConnection = scg("BackendConnection");
|
||||
//
|
||||
//
|
||||
// const messageFromServer = ref<string[]>([]);
|
||||
//
|
||||
// const gameStateText = ref<string>("Schafkopf");
|
||||
// const gameInfoText = ref<string>("");
|
||||
//
|
||||
// const socket = ref<WebSocket | null>();
|
||||
// const tableCards = ref<Card[]>([]);
|
||||
// const botCards = ref(0);
|
||||
// const trickCard = ref<Card>();
|
||||
//
|
||||
// const showGameSelect = ref(true);
|
||||
//
|
||||
//
|
||||
// function startSimulation(): void {
|
||||
// sendMessageToServer("startsimulation");
|
||||
//
|
||||
// }
|
||||
//
|
||||
// function startDedicated(): void {
|
||||
// sendMessageToServer("startdedicated");
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// function stopSimulation(): void {
|
||||
// sendMessageToServer("stopsimulation");
|
||||
// }
|
||||
//
|
||||
// function showTrumpf(): void {
|
||||
// tableCards.value = [];
|
||||
// sendMessageToServer("showtrumpf");
|
||||
// }
|
||||
//
|
||||
// function showFarben(): void {
|
||||
// tableCards.value = [];
|
||||
// sendMessageToServer("showfarben");
|
||||
// }
|
||||
//
|
||||
// function setGame(game: string): void {
|
||||
// sendMessageToServer(game);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// function sendMessageToServer(message: string) {
|
||||
// if (socket.value) {
|
||||
// socket.value.send(message);
|
||||
// console.log("Sent message to server:", message);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// function showGameState(gamestate: GameState) {
|
||||
//
|
||||
//
|
||||
// switch (gamestate.gamePhase) {
|
||||
// case GamePhase.GAME_START:
|
||||
// gameStateText.value = "Spiel startet";
|
||||
// showGameSelect.value = false;
|
||||
// botCards.value = 8;
|
||||
// break;
|
||||
// case GamePhase.TRICK_START:
|
||||
// gameStateText.value = "Runde startet";
|
||||
// tableCards.value = [];
|
||||
// trickCard.value = undefined
|
||||
// gameInfoText.value = "";
|
||||
// break;
|
||||
// case GamePhase.WAIT_FOR_CARD:
|
||||
// gameStateText.value = "Spieler " + gamestate.currentPlayer + " muss eine Karte legen.";
|
||||
// break;
|
||||
// case GamePhase.PLAYER_CARD:
|
||||
// gameStateText.value = "Spieler " + gamestate.currentPlayer + " hat eine Karte gespielt.";
|
||||
// if (gamestate.currentPlayer === 0) {
|
||||
// botCards.value--
|
||||
// }
|
||||
// if (gamestate.trumpf) {
|
||||
// gameInfoText.value = "TRUMPF";
|
||||
// } else {
|
||||
// gameInfoText.value = gamestate.color?.toString() ?? "ERROR";
|
||||
// }
|
||||
// tableCards.value.push(gamestate.card!);
|
||||
//
|
||||
//
|
||||
// break;
|
||||
// case GamePhase.PLAYER_TRICK:
|
||||
// gameStateText.value = "Spieler " + gamestate.currentPlayer + " sticht.";
|
||||
// trickCard.value = gamestate.card
|
||||
// break;
|
||||
// case GamePhase.GAME_STOP:
|
||||
// showGameSelect.value = true;
|
||||
// break;
|
||||
// default:
|
||||
// gameStateText.value = "Fehler";
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// onMounted(() => {
|
||||
// socket.value = backendConnection.getWebSocket();
|
||||
//
|
||||
// const messageListener = (message: string) => {
|
||||
// const message1: BackendMessage = JSON.parse(message);
|
||||
// console.log(message1)
|
||||
// if ('gamestate' in message1) {
|
||||
// console.log(message1.gamestate)
|
||||
// showGameState(message1.gamestate)
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// backendConnection.addMessageListener(messageListener);
|
||||
// });
|
||||
</script>
|
||||
<template>
|
||||
<div>
|
||||
<div v-for="message in messageFromServer" :key="message">{{ message }}</div>
|
||||
<!-- <div>-->
|
||||
<!-- <div v-for="message in messageFromServer" :key="message">{{ message }}</div>-->
|
||||
|
||||
<div v-if="showGameSelect">
|
||||
<!-- <div class="flex gap-2 place-content-center">-->
|
||||
<!-- <button @click="setGame('setgame:sauspiel')">Sauspiel</button>-->
|
||||
<!-- <div v-if="showGameSelect">-->
|
||||
<!-- <!– <div class="flex gap-2 place-content-center">–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:sauspiel')">Sauspiel</button>–>-->
|
||||
|
||||
<!-- <button @click="setGame('setgame:herzsolo')">herzsolo</button>-->
|
||||
<!-- <button @click="setGame('setgame:eichelsolo')">eichelsolo</button>-->
|
||||
<!-- <button @click="setGame('setgame:blattsolo')">blattsolo</button>-->
|
||||
<!-- <button @click="setGame('setgame:schellsolo')">schellsolo</button>-->
|
||||
<!-- <!– <button @click="setGame('setgame:herzsolo')">herzsolo</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:eichelsolo')">eichelsolo</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:blattsolo')">blattsolo</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:schellsolo')">schellsolo</button>–>-->
|
||||
|
||||
<!-- <button @click="setGame('setgame:eichelwenz')">eichelwenz</button>-->
|
||||
<!-- <button @click="setGame('setgame:blattwenz')">blattwenz</button>-->
|
||||
<!-- <button @click="setGame('setgame:herzwenz')">herzwenz</button>-->
|
||||
<!-- <button @click="setGame('setgame:schellwenz')">schellwenz</button>-->
|
||||
<!-- <!– <button @click="setGame('setgame:eichelwenz')">eichelwenz</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:blattwenz')">blattwenz</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:herzwenz')">herzwenz</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:schellwenz')">schellwenz</button>–>-->
|
||||
|
||||
<!-- <button @click="setGame('setgame:eichelgeier')">eichelgeier</button>-->
|
||||
<!-- <button @click="setGame('setgame:blattgeier')">blattgeier</button>-->
|
||||
<!-- <button @click="setGame('setgame:herzgeier')">herzgeier</button>-->
|
||||
<!-- <button @click="setGame('setgame:schellgeier')">schellgeier</button>-->
|
||||
<!-- <!– <button @click="setGame('setgame:eichelgeier')">eichelgeier</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:blattgeier')">blattgeier</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:herzgeier')">herzgeier</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:schellgeier')">schellgeier</button>–>-->
|
||||
|
||||
<!-- <button @click="setGame('setgame:geier')">Geier</button>-->
|
||||
<!-- <button @click="setGame('setgame:wenz')">Wenz</button>-->
|
||||
<!-- </div>-->
|
||||
<!-- <div class="flex gap-2 place-content-center">-->
|
||||
<!-- <button @click="showFarben">Zeige alle Farben</button>-->
|
||||
<!-- <button @click="showTrumpf">Zeige alle Trumpfkarten</button>-->
|
||||
<!-- </div>-->
|
||||
<div class="flex gap-2 place-content-center">
|
||||
<button class="v-button" @click="startSimulation">Starten</button>
|
||||
<button class="v-button" @click="stopSimulation">Stoppen</button>
|
||||
</div>
|
||||
</div>
|
||||
<div v-else>
|
||||
<div class="flex gap-2 place-content-center">
|
||||
<button class="v-button" @click="stopSimulation">Stoppen</button>
|
||||
</div>
|
||||
<h1 class=" top-52 text-white font-bold text-6xl absolute text-center w-full">{{ gameInfoText }}</h1>
|
||||
<h1 class=" top-64 text-white font-bold text-6xl absolute text-center w-full">{{ gameStateText }}</h1>
|
||||
<div v-if="tableCards.length > 0">
|
||||
<!-- <div class="grid grid-cols-4 place-content-center">-->
|
||||
<!-- <CardComp v-for="card in tableCards" :card="card" class="md" />-->
|
||||
<!-- </div>-->
|
||||
<CardComp v-if="tableCards.length > 0" :card="tableCards[0]" class="absolute card1 md"/>
|
||||
<CardComp v-if="tableCards.length > 1" :card="tableCards[1]" class="absolute card2 md"/>
|
||||
<CardComp v-if="tableCards.length > 2" :card="tableCards[2]" class="absolute card3 md"/>
|
||||
<CardComp v-if="tableCards.length > 3" :card="tableCards[3]" class="absolute card4 md"/>
|
||||
</div>
|
||||
<div class="absolute left-0 top-1/2 transform -translate-y-1/2">
|
||||
<CardComp v-if="trickCard" :card="trickCard" class="xl"/>
|
||||
</div>
|
||||
<!-- <!– <button @click="setGame('setgame:geier')">Geier</button>–>-->
|
||||
<!-- <!– <button @click="setGame('setgame:wenz')">Wenz</button>–>-->
|
||||
<!-- <!– </div>–>-->
|
||||
<!-- <!– <div class="flex gap-2 place-content-center">–>-->
|
||||
<!-- <!– <button @click="showFarben">Zeige alle Farben</button>–>-->
|
||||
<!-- <!– <button @click="showTrumpf">Zeige alle Trumpfkarten</button>–>-->
|
||||
<!-- <!– </div>–>-->
|
||||
<!-- <div class="flex gap-2 place-content-center">-->
|
||||
<!-- <button class="v-button" @click="startSimulation">Starten</button>-->
|
||||
<!-- <button class="v-button" @click="stopSimulation">Stoppen</button>-->
|
||||
<!-- </div>-->
|
||||
<!-- </div>-->
|
||||
<!-- <div v-else>-->
|
||||
<!-- <div class="flex gap-2 place-content-center">-->
|
||||
<!-- <button class="v-button" @click="stopSimulation">Stoppen</button>-->
|
||||
<!-- </div>-->
|
||||
<!-- <h1 class=" top-52 text-white font-bold text-6xl absolute text-center w-full">{{ gameInfoText }}</h1>-->
|
||||
<!-- <h1 class=" top-64 text-white font-bold text-6xl absolute text-center w-full">{{ gameStateText }}</h1>-->
|
||||
<!-- <div v-if="tableCards.length > 0">-->
|
||||
<!-- <!– <div class="grid grid-cols-4 place-content-center">–>-->
|
||||
<!-- <!– <CardComp v-for="card in tableCards" :card="card" class="md" />–>-->
|
||||
<!-- <!– </div>–>-->
|
||||
<!-- <CardComp v-if="tableCards.length > 0" :card="tableCards[0]" class="absolute card1 md"/>-->
|
||||
<!-- <CardComp v-if="tableCards.length > 1" :card="tableCards[1]" class="absolute card2 md"/>-->
|
||||
<!-- <CardComp v-if="tableCards.length > 2" :card="tableCards[2]" class="absolute card3 md"/>-->
|
||||
<!-- <CardComp v-if="tableCards.length > 3" :card="tableCards[3]" class="absolute card4 md"/>-->
|
||||
<!-- </div>-->
|
||||
<!-- <div class="absolute left-0 top-1/2 transform -translate-y-1/2">-->
|
||||
<!-- <CardComp v-if="trickCard" :card="trickCard" class="xl"/>-->
|
||||
<!-- </div>-->
|
||||
|
||||
<div class="absolute bottom-0 w-full">
|
||||
<div class="flex flex-row gap-3 w-fit mx-auto justify-center">
|
||||
<CardComp v-for="i in botCards" :key="i" :card="Card.BACK" class="sm"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<router-view/>
|
||||
<!-- <div class="absolute bottom-0 w-full">-->
|
||||
<!-- <div class="flex flex-row gap-3 w-fit mx-auto justify-center">-->
|
||||
<!-- <CardComp v-for="i in botCards" :key="i" :card="Card.BACK" class="sm"/>-->
|
||||
<!-- </div>-->
|
||||
<!-- </div>-->
|
||||
<!-- </div>-->
|
||||
<!-- </div>-->
|
||||
<!-- <router-view/>-->
|
||||
</template>
|
||||
<style lang="scss">
|
||||
$card-height: 24rem;
|
||||
|
||||
@@ -1,21 +1,13 @@
|
||||
import {MessageType} from "../BackendMessage.ts";
|
||||
|
||||
interface JsonMessage {
|
||||
origin: string;
|
||||
message: any; // Adjust 'any' type as per your expected message structure
|
||||
}
|
||||
|
||||
export class BackendConnection {
|
||||
|
||||
|
||||
private readonly webSocket: WebSocket;
|
||||
private messageListeners: ((message: string) => void)[] = [];
|
||||
private backendUri: string;
|
||||
|
||||
|
||||
constructor(backendUri: string) {
|
||||
this.backendUri = backendUri;
|
||||
|
||||
this.webSocket = new WebSocket(backendUri);
|
||||
|
||||
// Registering event listener for message reception
|
||||
|
||||
Reference in New Issue
Block a user