Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion games-vue-client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<v-content>
<router-view :key="$route.path" />
</v-content>
<Chat />
<v-footer
fixed
app
Expand Down Expand Up @@ -76,11 +77,12 @@ import store from "./store";
import { mapState } from "vuex";
import CookieLaw from 'vue-cookie-law'
import SettingsView from "@/components/SettingsView";
import Chat from "@/components/chat/Chat";

export default {
name: "App",
store,
components: { CookieLaw, SettingsView },
components: { CookieLaw, SettingsView, Chat },
methods: {
logout() {
Socket.disconnect();
Expand Down
183 changes: 183 additions & 0 deletions games-vue-client/src/components/chat/Chat.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<template>
<div>
<v-card
v-if="display"
:key="componentKey"
width="256"
class="chat"
>
<v-chip-group
active-class="active-chat"
column
>
<v-chip
v-for="chat in chats"
:key="chat.chatId"
close
close-icon="mdi-close"
@click:close="leaveChat(chat.chatId)"
@click="activeChat(chat.chatId)"
>
{{ chat.chatId }}
</v-chip>
</v-chip-group>

<v-divider />

<v-card-title>
<span class="title font-weight-light">Chat {{ currentChat.chatId }}</span>

<v-col class="text-right">
<v-icon @click="toggleDisplay">
mdi-close
</v-icon>
</v-col>
</v-card-title>

<v-card-text>
<div class="messages">
<div
ref="container"
class="messages-body"
>
<div
v-for="message in currentChat.messages"
:key="message.message + message.from + message.date"
>
<span class="from">{{ message.from }}</span>
<span class="message">
{{ message.message }}
</span>
</div>
</div>
</div>

<v-textarea
v-model="newMessage"
label="Message"
rows="2"
counter
maxlength="100"
dense
full-width
autofocus
no-resize
hint="Please be respectful"
append-outer-icon="mdi-send"
@click:append-outer="sendMessage"
@keyup.enter="sendMessage"
/>
</v-card-text>
</v-card>
<v-btn
v-else
elevation="2"
fab
class="open-chat"
@click="toggleDisplay"
>
Chat
</v-btn>
</div>
</template>
<script>
export default {
name: "Chat",
props: [],
data() {
return {
display: true,
currentChat: {},
newMessage: '',
messages: [],
componentKey: 0,
};
},
methods: {
// forceRerender() {
// this.componentKey += 1;
// },
scrollToEnd () {
var content = this.$refs.container;
content.scrollTop = content.scrollHeight;
},
toggleDisplay() {
this.display = !this.display;
},
sendMessage() {
if (this.newMessage.trim().length > 0) {
this.$store.dispatch("chat/message", { chat: { chatId: this.currentChat.chatId, message: this.newMessage.trim() } });
this.scrollToEnd();
}
this.newMessage = '';
},
activeChat(chatId) {
this.currentChat = this.chats[chatId];
},
leaveChat(chatId) {
this.$store.dispatch("chat/leave", { chat: { chatId } });
}
},
mounted() {
this.currentChat = this.chats.server;
this.scrollToEnd();
},
computed: {
chats() {
return this.$store.state.chat.chats;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this.$store.state is not correct. See for example InviteScreen.vue for an example of how to use mapState to make reactivity work correctly

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

},
},
}
</script>
<style>
.open-chat {
position: fixed !important;
bottom: 33px;
right: 20px;
}
.chat {
position: fixed !important;
bottom: 33px;
right: 20px;

border-top-left-radius: 4px;
border-bottom-left-radius: 0;
border-top-right-radius: 4px;
border-bottom-right-radius: 0;
border-top: 1px solid black;
border-left: 1px solid black;
border-right: 1px solid black;
}

.chat .v-card__title {
padding: 2px;
}
.chat .v-card__text {
padding-right: 0;
padding-bottom: 0;
padding-left: 2px;
}
.chat .active-chat {
font-weight: bold;
}
.chat h3 {
font-size: 30px;
text-align: center;
}
.chat .message {
font-size: 14px;
}
.chat .from {
color: teal;
font-weight: bold;
}
.chat .messages {
display: flex;
height: 40vh;
}
.chat .messages-body {
flex-direction: column;
width: 100%;
overflow-y: scroll;
}
</style>
122 changes: 122 additions & 0 deletions games-vue-client/src/components/chat/chatStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import Socket from "@/socket";

const chatStore = {
namespaced: true,
state: {
chats: {
'server': {
chatId: 'server',
users: [
{ "playerId": "431", "name": "RandomCat79" },
{ "playerId": "123", "name": "Zomis" }
],
messages: [
{ message: 'Hello', from: "RandomCat79", date: Date.now() },
{ message: 'Hello there!', from: "Zomis", date: Date.now() },
],
},
'game-UR': {
chatId: 'game-UR',
users: [
{ "playerId": "431", "name": "RandomCat79" },
{ "playerId": "123", "name": "Zomis" },
{ "playerId": "444", "name": "Urmakaren" },
],
messages: [
{ message: 'Hello UR', from: "RandomCat79", date: Date.now() },
{ message: 'Hello you!', from: "Urmakaren", date: Date.now() },
],
},
'game-Dixit': {
chatId: 'game-Dixit',
users: [
{ "playerId": "123456", "name": "JKR" },
{ "playerId": "123", "name": "Zomis" },
],
messages: [
{ message: 'Hello Zomis', from: "JKR", date: Date.now() },
{ message: 'Hello you!', from: "Zomis", date: Date.now() },
{ message: 'Hello you2!', from: "Zomis", date: Date.now() },
{ message: 'Hello you3!', from: "Zomis", date: Date.now() },
{ message: 'Hello you4!', from: "Zomis", date: Date.now() },
{ message: 'Hello you5!', from: "Zomis", date: Date.now() },
{ message: 'Hello you6!', from: "Zomis", date: Date.now() },
{ message: 'Hello you7!', from: "Zomis", date: Date.now() },
{ message: 'Hello you8!', from: "Zomis", date: Date.now() },
{ message: 'Hello you9!', from: "Zomis", date: Date.now() },
{ message: 'Hello you10!', from: "Zomis", date: Date.now() },
{ message: 'Hello you11!', from: "Zomis", date: Date.now() },
{ message: 'Hello you12!', from: "Zomis", date: Date.now() },
],
}
}
},
getters: {
chats: state => { return state.chats }
},
mutations: {
addChat(state, data) {
const { chatId, users } = data;
state.chats[chatId] = { chatId, users }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use Vue.set (see DslGameState.js for an example) in order to make the property reactive.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't see that one yet, nice!

},
removeChat(state, data) {
const { chatId } = data;
delete state.chats[chatId];
},
addUserToChat(state, data) {
const { chatId, user } = data;
const chat = state.chats[chatId];
chat.users.push(user);
},
removeUserFromChat(state, data) {
const { chatId, user } = data;
const chat = state.chats[chatId];
chat.users = chat.users.filter(existingUser => existingUser.playerId !== user.playerId)
},
addMessageToChat(state, data) {
const { chatId, message, from, date } = data;
const chat = state.chats[chatId];
chat.messages.push({ message, from, date });
},
},
actions: {
join(context, data) {
const { chatId } = data.chat;
// context.commit("addChat", { chatId, users: [] });
Socket.route(`chat/${chatId}/join`, {});
},
leave(context, data) {
const { chatId } = data.chat;
// context.commit("removeChat", { chatId });
Socket.route(`chat/${chatId}/leave`, {});
},
message(context, data) {
const { chatId, message } = data.chat;
// context.commit("addMessageToChat", { chatId, from: 'Santa', message, date: Date.now() });
Socket.route(`chat/${chatId}/message`, { message });
},
onSocketMessage(context, data) {
if (data.type === "Chat") {
if (data.chat === "join") {
const { chatId, user, users } = data;
if (user) {
context.commit("addUserToChat", { chatId, user });
} else if (users) {
context.commit("addChat", { chatId, users });
}
}
if (data.chat === "leave") {
context.commit("removeChat", data);
}
if (data.chat === "left") {
context.commit("removeUserFromChat", data);
}
if (data.chat === "message") {
context.commit("addMessageToChat", data);
}
}
}
}
};

export default chatStore;
11 changes: 10 additions & 1 deletion games-vue-client/src/components/lobby/LobbyGameType.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
>
New Game
</v-btn>
<v-btn
rounded
@click="joinChat(gameType)"
>
Chat
</v-btn>
</v-toolbar>
<v-card-title>
Users
Expand Down Expand Up @@ -96,7 +102,10 @@ export default {
},
invite(gameType, playerId) {
Socket.route("invites/invite", { gameType: gameType, invite: [playerId] });
}
},
joinChat(gameType) {
this.$store.dispatch("chat/join", { chat: { chatId: `game-${gameType}` } });
},
},
computed: {
gameTypeCssName() {
Expand Down
5 changes: 5 additions & 0 deletions games-vue-client/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Vuex from "vuex";
import supportedGames from "@/supportedGames"
import Socket from "@/socket";
import lobbyStore from "./components/lobby/lobbyStore";
import chatStore from "./components/chat/chatStore";
import router from "@/router/index";
import settingsStore from "./components/settings/settingsStore";

Expand All @@ -13,6 +14,7 @@ let titlePrefix = "Zomis' Games"
const store = new Vuex.Store({
modules: {
lobby: lobbyStore,
chat: chatStore,
settings: settingsStore,
...supportedGames.storeModules()
},
Expand Down Expand Up @@ -97,6 +99,9 @@ const store = new Vuex.Store({
if (data.type.startsWith("Invite") || data.type.startsWith("Lobby") || data.type === 'GameStarted') {
context.dispatch("lobby/onSocketMessage", data);
}
if (data.type === "Chat") {
context.dispatch("chat/onSocketMessage", data);
}
if (data.gameType) {
let game = supportedGames.games[data.gameType]
if (game.dsl) {
Expand Down