diff --git a/client/src/App.vue b/client/src/App.vue index 6bc4bc9..adaf4ce 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -1,9 +1,15 @@ @@ -22,6 +29,7 @@ import "es6-promise/auto"; // Needed for Promise polyfill import VueSocketIO from "vue-socket.io"; import Vuex, { mapGetters, mapMutations } from "vuex"; import Hand from "./components/Hand"; +import Player from "./components/Player"; import Sockets from "./Sockets"; import { store } from "./vuex-store"; import { BootstrapVue, IconsPlugin } from "bootstrap-vue"; @@ -42,13 +50,14 @@ Vue.use( mutationPrefix: "SOCKET_" }, options: { path: "/socket.io/" } //Optional options - }) + }), store ); export default { name: "App", store: store, components: { + Player, Sockets, Hand }, @@ -58,18 +67,42 @@ export default { }; }, computed: { - ...mapGetters(["isConnected", "name", "socketMessage"]) + ...mapGetters(["isConnected", "name", "text", + "cards", "players", "socketMessage"]) }, methods: { sendMessage: function(message) { console.log("User wants to send", message); this.$socket.emit("message", message); }, + newMessage: function(data) { + console.log("App received new state:", data.data); + this.handleState(data); + }, + handleState: function(data) { + console.log("hS: ", data); + if (data.message === "NewGame") { + console.log("New Game!"); + } + }, + currentCards() { + let currentState = this.socketMessage; + console.log("curCards! state now:", currentState); + if (currentState["data"]) { + if (currentState["data"]["cards"]) { + console.log("Setting cards..."); + return this.cards; + + } else return this.mockCards(); + } else return this.mockCards(); + + // console.log("Current socket.sMs now:", Sockets.sockets.socketMessages); + }, mockCards() { return [ - { value: "ace", color: "hearts" }, - { value: "ace", color: "clubs" }, - { value: "ace", color: "diamonds" }, + // { value: "ace", color: "hearts" }, + // { value: "ace", color: "clubs" }, + // { value: "ace", color: "diamonds" }, { value: "ace", color: "spades" } ]; }, diff --git a/client/src/Sockets.vue b/client/src/Sockets.vue index 3ef58c3..4ffa2d5 100644 --- a/client/src/Sockets.vue +++ b/client/src/Sockets.vue @@ -9,11 +9,14 @@

We're not connected yet...

-

Messages from server"

+

Messages from server:

+ + + {{ m.data.state }} +
Extras: {{ m.data.extras }}
+
+
@@ -32,7 +35,7 @@ export default { connect() { // Fired when the socket connects. this.isConnected = true; - console.log("login"); + console.log(" login"); }, disconnect() { this.isConnected = false; @@ -40,9 +43,12 @@ export default { this.socketMessages = []; }, // Fired when the server sends something on the "messageChannel" channel. - messageChannel(data) { - this.socketMessages.push(data); - console.log("Message received:", data, "!"); + messageChannel(message) { + message = { data: JSON.parse(message), received: new Date().toISOString() }; + console.log("S.mC| Message augmented:", message); + this.socketMessages.push(message); + this.$store.commit("message", message); + this.$emit("message", message); } }, @@ -64,8 +70,13 @@ export default { }, logOff() { this.$socket.ondisconnect(); - this.socketMessage = []; + this.socketMessages = []; } } }; - \ No newline at end of file + + \ No newline at end of file diff --git a/client/src/components/Hand.vue b/client/src/components/Hand.vue index 86c4639..9a453e7 100644 --- a/client/src/components/Hand.vue +++ b/client/src/components/Hand.vue @@ -1,5 +1,5 @@ + \ No newline at end of file diff --git a/client/src/vuex-store.js b/client/src/vuex-store.js new file mode 100644 index 0000000..789410b --- /dev/null +++ b/client/src/vuex-store.js @@ -0,0 +1,81 @@ +import Vue from "vue"; +import Vuex from "vuex"; + +Vue.use(Vuex); + +export const store = new Vuex.Store({ + state: { + isConnected: false, + name: "Menteur inconnu", + text: "none", + socketMessage: "", + gameState: "RESET", + cards: [], + players: [] + }, + getters: { + isConnected: state => state.isConnected, + name: state => state.name, + socketMessage: state => state.socketMessage, + players: state => state.players, + gameState: state => state.gameState + }, + mutations: { + SOCKET_CONNECT(state) { + state.isConnected = true; + }, + + SOCKET_DISCONNECT(state) { + state.isConnected = false; + }, + SOCKET_MESSAGECHANNEL(state, message) { + state.socketMessage = message; + console.log("SMC| Current state now:", state.socketMessage); + }, + name(state, name) { + state.name = name; + }, + text(state, text) { + state.text = text; + }, + gameState(state, gameState) { + state.gameState = gameState; + }, + cards(state, cards) { + state.cards = cards; + }, + message(state, message) { + state.socketMessage = message; + console.log("storeMess| Current message now:" + JSON.stringify(message)); + if (message) { + if (message.data) { + let players = message.data.players; + let gameState = message.data.state; + let text = message.data.text; + let extras = message.data.extras; + if (players) { + console.log("Setting players:", players); + state.players = players; + } + if (gameState) { + console.log("Setting state:", gameState); + state.state = gameState; + } + if (text) { + console.log("Setting text:", text); + state.text = text; + } + if (extras) { + console.log("Extras: ", extras) + let name = extras.name; + if (name) { + console.log("Setting name:", name); + state.name = name; + } + } + + } + } + } + } +}); diff --git a/server/encoding.py b/server/encoding.py index 0992328..2d37a4f 100644 --- a/server/encoding.py +++ b/server/encoding.py @@ -2,28 +2,50 @@ from server.model.color import Color from server.model.value import Value -def encode_value(v: Value): - return { - "Two": "Deux", - "Three": "Trois", - "Four": "Quatre", - "Five": "Cinq", - "Six": "Six", - "Seven": "Sept", - "Eight": "Huit", - "Nine": "Neuf", - "Ten": "Dix", - "Jack": "Valet", - "Queen": "Dame", - "King": "Roi", - "Ace": "As" - }[v.name] +def encode_value(v: Value, lang="en"): + if lang == "fr": + return { + "Two": "Deux", + "Three": "Trois", + "Four": "Quatre", + "Five": "Cinq", + "Six": "Six", + "Seven": "Sept", + "Eight": "Huit", + "Nine": "Neuf", + "Ten": "Dix", + "Jack": "Valet", + "Queen": "Dame", + "King": "Roi", + "Ace": "As" + }[v.name] + else: + return { + "Two": "2", + "Three": "3", + "Four": "4", + "Five": "5", + "Six": "6", + "Seven": "7", + "Eight": "8", + "Nine": "9", + "Ten": "10", + "Jack": "jack", + "Queen": "queen", + "King": "king", + "Ace": "ace" + }[v.name] -def encode_color(c: Color): +def encode_color(c: Color, lang="en"): return { "Hearts": "Coeur", "Spades": "Pique", "Clubs": "Trèfle", "Diamonds": "Carreau" - }[c.name] \ No newline at end of file + }[c.name] if lang == "fr" else { + "Hearts": "hearts", + "Spades": "spades", + "Clubs": "clubs", + "Diamonds": "diamonds" + }[c.name] diff --git a/server/game/lobby.py b/server/game/lobby.py index e43021c..15f93cc 100644 --- a/server/game/lobby.py +++ b/server/game/lobby.py @@ -38,12 +38,12 @@ class LobbyManager(ClientManager): def players_ready(self): return [self.lobby[k] for k, m in self.metadata.items() if m.ready] - def new_player(self, player: Player, sid: str) -> str: + async def new_player(self, player: Player, sid: str) -> None: self.lobby[player.name] = player self.players.append(player) self.metadata[player.name] = Metadata(sid=sid) print(f"Added {player} to a lobby with {len(self.lobby)} players.") - return f"Bienvenu, {player}! Il y a {len(self.lobby)} joueurs en ligne." + await self.send(to=player, message=MessageToPlayer.Welcome, extras={"name": player.name}) def wants_to_play(self, player: Player): """ @@ -98,9 +98,10 @@ class LobbyManager(ClientManager): async def send(self, to: Player, message: MessageToPlayer, extras=None): sid = self.metadata[to.name].sid - data = {"message": str(message.name)} + data = {"state": str(message.name), + "players": [p.name for p in self.players]} if extras: - data["extra"] = extras + data["extras"] = extras print(f"MSGOUT|{sid} ({to.name}), {data}") await self.sio.emit('messageChannel', json.dumps(data), room=sid) @@ -109,6 +110,9 @@ class LobbyManager(ClientManager): message = None extras = {"players": [p.name for p in self.players]} sender = self.which_player(sid) + if not sender: + print(f"ERROR: Unrecognized sender {sid}") + return print(f"MSGIN| {sid} ({sender.name}): {data}.") diff --git a/server/game/message.py b/server/game/message.py index da88060..f5b42ad 100644 --- a/server/game/message.py +++ b/server/game/message.py @@ -2,6 +2,7 @@ from enum import Enum class MessageToPlayer(Enum): + Welcome = "WELCOME" Waiting = "WAITING_ROOM" ReadyToStart = "READY_ROOM" NewGame = "NEW_GAME" diff --git a/server/ws.py b/server/ws.py index ce65b4a..ecd57d3 100644 --- a/server/ws.py +++ b/server/ws.py @@ -39,8 +39,7 @@ class ClientPlayer(Player): async def connect(sid, environ): print("[WS] Connect ", sid, environ) player = ClientPlayer(lobby) - reply: str = lobby.new_player(player, sid) - await sio.emit('messageChannel', reply, room=sid) + await lobby.new_player(player, sid) @sio.event @@ -52,7 +51,7 @@ async def message(sid, data): @sio.on("pingServer") async def ping_server(sid, data): print("[WS] Ping received:", data) - await sio.emit('messageChannel', "PONG", room=sid) + await sio.emit('messageChannel', '{"extras":{"message":"PONG"}}', room=sid) @sio.event