Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3aceb48057 | ||
| eac315bea1 | |||
|
|
f47b757398 | ||
| 64d528bf5c |
10
CHANGELOG.md
10
CHANGELOG.md
@@ -39,3 +39,13 @@
|
|||||||
### Features
|
### Features
|
||||||
|
|
||||||
* **ui:** Vue Create Game component ([#5](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/issues/5)) ([3efacde](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/commit/3efacde49d295cc615a3ff61939a3a5e8ec7e7af))
|
* **ui:** Vue Create Game component ([#5](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/issues/5)) ([3efacde](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/commit/3efacde49d295cc615a3ff61939a3a5e8ec7e7af))
|
||||||
|
## [0.0.0](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/compare/0.2.0...0.0.0) (2025-12-10)
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* FRO-29 Websocket Communication ([#7](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/issues/7)) ([64d528b](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/commit/64d528bf5c8b5d4f0d31274d600b3f05b8c47740))
|
||||||
|
## [0.0.0](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/compare/0.2.1...0.0.0) (2025-12-10)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* FRO-2 Implement Login Component ([#8](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/issues/8)) ([eac315b](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Frontend/commit/eac315bea1a2075858648bbc49c200b8020e1ff7))
|
||||||
|
|||||||
4
public/env.js
Normal file
4
public/env.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
window.__RUNTIME_CONFIG__ = {
|
||||||
|
API_URL: "http://localhost:9000"
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
globalThis.__RUNTIME_CONFIG__ = {
|
window.__RUNTIME_CONFIG__ = {
|
||||||
API_URL: "${API_URL}"
|
API_URL: "${API_URL}"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
19
src/composables/useUserInfo.ts
Normal file
19
src/composables/useUserInfo.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import {ref, type Ref} from 'vue'
|
||||||
|
|
||||||
|
export const useUserInfo = defineStore('userInfo', () => {
|
||||||
|
const username: Ref<string | null> = ref(null);
|
||||||
|
const userId: Ref<number | null> = ref(null);
|
||||||
|
|
||||||
|
function setUserInfo(name: string, id: number) {
|
||||||
|
username.value = name;
|
||||||
|
userId.value = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearUserInfo() {
|
||||||
|
username.value = null;
|
||||||
|
userId.value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { username, userId, setUserInfo, clearUserInfo };
|
||||||
|
});
|
||||||
55
src/composables/useWebsocket.ts
Normal file
55
src/composables/useWebsocket.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { ref, onMounted, onBeforeUnmount } from "vue";
|
||||||
|
import {
|
||||||
|
connectWebSocket,
|
||||||
|
disconnectWebSocket,
|
||||||
|
sendEvent,
|
||||||
|
sendEventAndWait,
|
||||||
|
onEvent,
|
||||||
|
isWebSocketConnected,
|
||||||
|
} from "@/services/ws";
|
||||||
|
|
||||||
|
export function useWebSocket() {
|
||||||
|
const isConnected = ref(isWebSocketConnected());
|
||||||
|
const lastMessage = ref<any>(null);
|
||||||
|
|
||||||
|
const lastError = ref<string | null>(null);
|
||||||
|
|
||||||
|
async function safeConnect(url?: string) {
|
||||||
|
return connectWebSocket(url)
|
||||||
|
.then(() => {
|
||||||
|
isConnected.value = true;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
lastError.value = err?.message ?? "Unknown WS error";
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function useEvent<T = any>(event: string, handler: (data: T) => void) {
|
||||||
|
const wrapped = (data: T) => {
|
||||||
|
lastMessage.value = { event, data };
|
||||||
|
handler(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
onEvent(event, wrapped);
|
||||||
|
});
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
onEvent(event, () => {});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
isConnected,
|
||||||
|
lastMessage,
|
||||||
|
lastError,
|
||||||
|
|
||||||
|
connect: safeConnect,
|
||||||
|
disconnect: disconnectWebSocket,
|
||||||
|
send: sendEvent,
|
||||||
|
sendAndWait: sendEventAndWait,
|
||||||
|
|
||||||
|
useEvent,
|
||||||
|
};
|
||||||
|
}
|
||||||
15
src/main.ts
15
src/main.ts
@@ -11,17 +11,18 @@ import 'quasar/dist/quasar.css'
|
|||||||
import { createPinia } from 'pinia'
|
import { createPinia } from 'pinia'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import VueAxios from 'vue-axios'
|
import VueAxios from 'vue-axios'
|
||||||
|
import {useUserInfo} from "@/composables/useUserInfo.ts";
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
const pinia = createPinia()
|
const pinia = createPinia()
|
||||||
|
|
||||||
|
app.use(pinia)
|
||||||
app.use(router)
|
app.use(router)
|
||||||
app.use(Quasar, {
|
app.use(Quasar, {
|
||||||
plugins: {
|
plugins: {
|
||||||
Notify
|
Notify
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
app.use(pinia)
|
|
||||||
app.use(VueAxios, axios)
|
app.use(VueAxios, axios)
|
||||||
app.use(Particles, {
|
app.use(Particles, {
|
||||||
init: async engine => {
|
init: async engine => {
|
||||||
@@ -29,4 +30,16 @@ app.use(Particles, {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
axios.interceptors.response.use(
|
||||||
|
res => res,
|
||||||
|
err => {
|
||||||
|
if (err.response?.status === 401) {
|
||||||
|
const info = useUserInfo();
|
||||||
|
info.clearUserInfo();
|
||||||
|
router.replace({name: 'login'});
|
||||||
|
}
|
||||||
|
return Promise.reject(err);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|||||||
@@ -1,49 +1,60 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import HomeView from '../views/HomeView.vue'
|
|
||||||
import LoginView from '../views/LoginView.vue'
|
import LoginView from '../views/LoginView.vue'
|
||||||
import MainMenuView from '../views/MainMenuView.vue'
|
import MainMenuView from '../views/MainMenuView.vue'
|
||||||
import createGameView from '../views/CreateGame.vue'
|
import createGameView from '../views/CreateGame.vue'
|
||||||
import joinGameView from "@/views/JoinGameView.vue";
|
import joinGameView from "@/views/JoinGameView.vue";
|
||||||
|
import axios from "axios";
|
||||||
|
import { useUserInfo } from "@/composables/useUserInfo";
|
||||||
|
|
||||||
|
const api = window?.__RUNTIME_CONFIG__?.API_URL;
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
path: '/',
|
path: '/',
|
||||||
name: 'home',
|
name: 'mainmenu',
|
||||||
component: HomeView,
|
component: MainMenuView,
|
||||||
|
meta: { requiresAuth: true }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/login',
|
path: '/login',
|
||||||
name: 'login',
|
name: 'login',
|
||||||
component: LoginView,
|
component: LoginView,
|
||||||
},
|
meta: { requiresAuth: false }
|
||||||
{
|
|
||||||
path: '/about',
|
|
||||||
name: 'about',
|
|
||||||
// route level code-splitting
|
|
||||||
// this generates a separate chunk (About.[hash].js) for this route
|
|
||||||
// which is lazy-loaded when the route is visited.
|
|
||||||
component: () => import('../views/AboutView.vue'),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: '/mainmenu',
|
|
||||||
name: 'mainmenu',
|
|
||||||
component: MainMenuView
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/create',
|
path: '/create',
|
||||||
name: 'create-Game',
|
name: 'create-Game',
|
||||||
component: createGameView
|
component: createGameView,
|
||||||
|
meta: { requiresAuth: true }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/join',
|
path: '/join',
|
||||||
name: 'join-Game',
|
name: 'join-Game',
|
||||||
component: joinGameView
|
component: joinGameView,
|
||||||
},
|
meta: { requiresAuth: true }
|
||||||
|
}
|
||||||
|
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.beforeEach(async (to, from, next) => {
|
||||||
|
const info = useUserInfo();
|
||||||
|
if (!to.meta.requiresAuth) return next();
|
||||||
|
try {
|
||||||
|
await axios.get(`${api}/userInfo`, { withCredentials: true }).then(
|
||||||
|
res => {
|
||||||
|
info.setUserInfo(res.data.username, res.data.userId);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
next();
|
||||||
|
} catch (err) {
|
||||||
|
info.clearUserInfo();
|
||||||
|
next('/login');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
236
src/services/ws.ts
Normal file
236
src/services/ws.ts
Normal file
@@ -0,0 +1,236 @@
|
|||||||
|
const api = window.__RUNTIME_CONFIG__?.API_URL;
|
||||||
|
|
||||||
|
// ---------- Types ---------------------------------------------------------
|
||||||
|
|
||||||
|
export type ServerMessage<T = any> = {
|
||||||
|
id?: string;
|
||||||
|
event?: string;
|
||||||
|
status?: "success" | "error";
|
||||||
|
data?: T;
|
||||||
|
error?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ClientMessage<T = any> = {
|
||||||
|
id: string;
|
||||||
|
event: string;
|
||||||
|
data: T;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type HandlerFn<T = any> = (data: T) => unknown | Promise<unknown>;
|
||||||
|
|
||||||
|
interface PendingEntry {
|
||||||
|
resolve: (data: any) => void;
|
||||||
|
reject: (err: Error) => void;
|
||||||
|
timer: ReturnType<typeof setTimeout>;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ws: WebSocket | null = null;
|
||||||
|
|
||||||
|
const pending = new Map<string, PendingEntry>();
|
||||||
|
const handlers = new Map<string, HandlerFn>();
|
||||||
|
|
||||||
|
let heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
|
||||||
|
function uuid(): string {
|
||||||
|
return crypto.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
function failAllPending(reason: string) {
|
||||||
|
for (const [, entry] of pending.entries()) {
|
||||||
|
clearTimeout(entry.timer);
|
||||||
|
entry.reject(new Error(reason));
|
||||||
|
}
|
||||||
|
pending.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
function startHeartbeat() {
|
||||||
|
stopHeartbeat();
|
||||||
|
heartbeatTimer = setInterval(() => {
|
||||||
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||||
|
sendEventAndWait("ping", {}, 4000)
|
||||||
|
.then(() => console.debug("[WS] Heartbeat OK"))
|
||||||
|
.catch((err) => console.warn("[WS] Heartbeat failed:", err.message));
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopHeartbeat() {
|
||||||
|
if (heartbeatTimer) {
|
||||||
|
clearInterval(heartbeatTimer);
|
||||||
|
heartbeatTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupSocketHandlers(socket: WebSocket) {
|
||||||
|
socket.onmessage = async (raw) => {
|
||||||
|
console.debug("[WS] MESSAGE:", raw.data);
|
||||||
|
|
||||||
|
let msg: ServerMessage;
|
||||||
|
try {
|
||||||
|
msg = JSON.parse(raw.data);
|
||||||
|
} catch (err) {
|
||||||
|
console.warn("[WS] Bad JSON:", raw.data, err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { id, event, status, data } = msg;
|
||||||
|
|
||||||
|
// RPC response branch
|
||||||
|
if (id && status) {
|
||||||
|
const entry = pending.get(id);
|
||||||
|
if (!entry) return;
|
||||||
|
|
||||||
|
pending.delete(id);
|
||||||
|
clearTimeout(entry.timer);
|
||||||
|
|
||||||
|
if (status === "success") {
|
||||||
|
entry.resolve(data ?? {});
|
||||||
|
} else {
|
||||||
|
entry.reject(new Error(msg.error || "Server returned error"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server event → handler branch
|
||||||
|
if (id && event) {
|
||||||
|
const handler = handlers.get(event);
|
||||||
|
|
||||||
|
const reply = (status: "success" | "error", error?: string) => {
|
||||||
|
if (socket.readyState !== WebSocket.OPEN) return;
|
||||||
|
const resp = { id, event, status, error };
|
||||||
|
socket.send(JSON.stringify(resp));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!handler) {
|
||||||
|
console.warn("[WS] No handler for event:", event);
|
||||||
|
reply("error", `No handler for '${event}'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await handler(data ?? {});
|
||||||
|
reply("success");
|
||||||
|
} catch (err) {
|
||||||
|
reply("error", (err as Error).message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.onerror = (err) => {
|
||||||
|
console.error("[WS] ERROR:", err);
|
||||||
|
stopHeartbeat();
|
||||||
|
failAllPending("WebSocket error");
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.onclose = (ev) => {
|
||||||
|
stopHeartbeat();
|
||||||
|
failAllPending("WebSocket closed");
|
||||||
|
|
||||||
|
if (ev.wasClean) {
|
||||||
|
console.log(`[WS] Closed cleanly: code=${ev.code} reason=${ev.reason}`);
|
||||||
|
} else {
|
||||||
|
console.warn("[WS] Connection died");
|
||||||
|
}
|
||||||
|
|
||||||
|
// You redirect here — if you don’t want auto reconnect, keep as is.
|
||||||
|
location.href = "/mainmenu";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function connectWebSocket(url?: string): Promise<void> {
|
||||||
|
if (!url) {
|
||||||
|
const loc = window.location;
|
||||||
|
const protocol = loc.protocol === "https:" ? "wss:" : "ws:";
|
||||||
|
url = `${protocol}//${api}/websocket`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ws && ws.readyState === WebSocket.CONNECTING) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const prevOnOpen = ws!.onopen;
|
||||||
|
const prevOnError = ws!.onerror;
|
||||||
|
ws!.onopen = (ev) => {
|
||||||
|
if (prevOnOpen) prevOnOpen.call(ws!, ev);
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
ws!.onerror = (err) => {
|
||||||
|
if (prevOnError) prevOnError.call(ws!, err);
|
||||||
|
reject(err);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// New connection
|
||||||
|
ws = new WebSocket(url);
|
||||||
|
setupSocketHandlers(ws);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
ws!.onopen = () => {
|
||||||
|
console.log("[WS] Connected");
|
||||||
|
startHeartbeat();
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
|
||||||
|
ws!.onerror = (err) => reject(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function disconnectWebSocket(code = 1000, reason = "Client disconnect") {
|
||||||
|
stopHeartbeat();
|
||||||
|
failAllPending("Disconnected");
|
||||||
|
|
||||||
|
if (ws) {
|
||||||
|
try {
|
||||||
|
ws.close(code, reason);
|
||||||
|
} catch {}
|
||||||
|
ws = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sendEvent(event: string, data: any) {
|
||||||
|
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||||
|
console.warn("[WS] Send failed: not open");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const message: ClientMessage = { id: uuid(), event, data };
|
||||||
|
console.debug("[WS] SEND:", message);
|
||||||
|
ws.send(JSON.stringify(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sendEventAndWait(
|
||||||
|
event: string,
|
||||||
|
data: any,
|
||||||
|
timeoutMs = 10000
|
||||||
|
): Promise<any> {
|
||||||
|
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||||
|
return Promise.reject(new Error("WebSocket is not open"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const id = uuid();
|
||||||
|
const message: ClientMessage = { id, event, data };
|
||||||
|
|
||||||
|
const promise = new Promise((resolve, reject) => {
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
pending.delete(id);
|
||||||
|
reject(new Error(`Timeout waiting for '${event}'`));
|
||||||
|
}, timeoutMs);
|
||||||
|
|
||||||
|
pending.set(id, { resolve, reject, timer });
|
||||||
|
});
|
||||||
|
|
||||||
|
console.debug("[WS] SEND (await):", message);
|
||||||
|
ws.send(JSON.stringify(message));
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function onEvent(event: string, handler: HandlerFn) {
|
||||||
|
handlers.set(event, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isWebSocketConnected = () =>
|
||||||
|
!!ws && ws.readyState === WebSocket.OPEN;
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
import {type credentials, type token, type user} from '@/types/authTypes'
|
|
||||||
import { defineStore } from 'pinia'
|
|
||||||
import {ref, computed, type Ref} from 'vue'
|
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
|
||||||
const user: Ref<user | null> = ref(null)
|
|
||||||
const token = ref(localStorage.getItem('token') || null)
|
|
||||||
|
|
||||||
const isAuthenticated = computed(() => !!token.value)
|
|
||||||
|
|
||||||
async function login(credentials: credentials) {
|
|
||||||
const response = await fakeLoginApi(credentials)
|
|
||||||
|
|
||||||
token.value = response.token
|
|
||||||
user.value = response.user
|
|
||||||
|
|
||||||
localStorage.setItem('token', token.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
function setToken(newToken: string) {
|
|
||||||
token.value = newToken
|
|
||||||
localStorage.setItem('token', newToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
function logout() {
|
|
||||||
token.value = null
|
|
||||||
user.value = null
|
|
||||||
localStorage.removeItem('token')
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchUser() {
|
|
||||||
if (!token.value) return
|
|
||||||
|
|
||||||
const response = await fakeFetchUserApi(token.value)
|
|
||||||
user.value = response.user
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
user,
|
|
||||||
token,
|
|
||||||
isAuthenticated,
|
|
||||||
login,
|
|
||||||
setToken,
|
|
||||||
logout,
|
|
||||||
fetchUser,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
async function fakeLoginApi(credentials: credentials): Promise<token> {
|
|
||||||
return {
|
|
||||||
token: 'abc123',
|
|
||||||
user: { id: 1, name: 'John Doe' }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fakeFetchUserApi(token: string): Promise<{ user: user }> {
|
|
||||||
return {
|
|
||||||
user: { id: 1, name: 'John Doe' }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
47
src/types/GameSubTypes.ts
Normal file
47
src/types/GameSubTypes.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
type Card = {
|
||||||
|
identifier: string
|
||||||
|
path: string
|
||||||
|
idx: number | null
|
||||||
|
}
|
||||||
|
|
||||||
|
type Hand = {
|
||||||
|
cards: Card[]
|
||||||
|
}
|
||||||
|
|
||||||
|
type Player = {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
dogLife: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type PlayerQueue = {
|
||||||
|
currentPlayer: Player | null
|
||||||
|
players: Player[]
|
||||||
|
}
|
||||||
|
|
||||||
|
type PodiumPlayer = {
|
||||||
|
player: Player
|
||||||
|
position: number
|
||||||
|
roundsWon: number
|
||||||
|
tricksWon: number
|
||||||
|
}
|
||||||
|
|
||||||
|
type Round = {
|
||||||
|
trumpSuit: Card
|
||||||
|
firstRound: boolean
|
||||||
|
trickList: Trick[]
|
||||||
|
}
|
||||||
|
|
||||||
|
type Trick = {
|
||||||
|
cards: { [player: string]: Card }
|
||||||
|
firstCard: Card | null
|
||||||
|
winner: Player | null
|
||||||
|
}
|
||||||
|
|
||||||
|
type User = {
|
||||||
|
id: string
|
||||||
|
username: string
|
||||||
|
host: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { Card, Hand, Player, PlayerQueue, PodiumPlayer, Round, Trick, User }
|
||||||
49
src/types/GameTypes.ts
Normal file
49
src/types/GameTypes.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import type {
|
||||||
|
Hand,
|
||||||
|
Player,
|
||||||
|
PlayerQueue,
|
||||||
|
PodiumPlayer,
|
||||||
|
Round,
|
||||||
|
Trick,
|
||||||
|
User
|
||||||
|
} from "@/types/GameSubTypes.ts";
|
||||||
|
|
||||||
|
|
||||||
|
type GameInfo = {
|
||||||
|
gameId: string
|
||||||
|
self: Player | null
|
||||||
|
hand: Hand | null
|
||||||
|
playerQueue: PlayerQueue
|
||||||
|
currentTrick: Trick | null
|
||||||
|
currentRound: Round | null
|
||||||
|
}
|
||||||
|
|
||||||
|
type LobbyInfo = {
|
||||||
|
gameId: string
|
||||||
|
users: User[]
|
||||||
|
self: User
|
||||||
|
maxPlayers: number
|
||||||
|
}
|
||||||
|
|
||||||
|
type TieInfo = {
|
||||||
|
gameId: string
|
||||||
|
currentPlayer: Player | null
|
||||||
|
self: Player | null
|
||||||
|
tiedPlayers: Player[]
|
||||||
|
highestAmount: number
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrumpInfo = {
|
||||||
|
gameId: string
|
||||||
|
chooser: Player | null
|
||||||
|
self: Player | null
|
||||||
|
selfHand: Hand | null
|
||||||
|
}
|
||||||
|
|
||||||
|
type WonInfo = {
|
||||||
|
gameId: string
|
||||||
|
winner: PodiumPlayer | null
|
||||||
|
allPlayers: PodiumPlayer[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { GameInfo, LobbyInfo, TieInfo, TrumpInfo, WonInfo }
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
|
|
||||||
type token = {
|
|
||||||
token: string
|
|
||||||
user: user
|
|
||||||
}
|
|
||||||
type user = {
|
|
||||||
id: number
|
|
||||||
name: string
|
|
||||||
}
|
|
||||||
type credentials = {
|
|
||||||
username: string
|
|
||||||
password: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type { token, user, credentials }
|
|
||||||
|
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
style="width: 100%"
|
style="width: 100%"
|
||||||
filled
|
filled
|
||||||
dark
|
dark
|
||||||
|
type="password"
|
||||||
label-color="white"
|
label-color="white"
|
||||||
color="white"
|
color="white"
|
||||||
v-model="password"
|
v-model="password"
|
||||||
@@ -60,8 +61,8 @@
|
|||||||
import { useQuasar } from 'quasar'
|
import { useQuasar } from 'quasar'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import {useAuthStore} from "@/stores/auth.ts";
|
|
||||||
import router from "@/router";
|
import router from "@/router";
|
||||||
|
import {useUserInfo} from "@/composables/useUserInfo.ts";
|
||||||
|
|
||||||
const api = window?.__RUNTIME_CONFIG__?.API_URL;
|
const api = window?.__RUNTIME_CONFIG__?.API_URL;
|
||||||
|
|
||||||
@@ -71,14 +72,14 @@ const username = ref(null)
|
|||||||
const password = ref(null)
|
const password = ref(null)
|
||||||
const inProgress = ref(false)
|
const inProgress = ref(false)
|
||||||
const loginError = ref('')
|
const loginError = ref('')
|
||||||
|
const uInfo = useUserInfo()
|
||||||
|
|
||||||
const onSubmit = () => {
|
const onSubmit = () => {
|
||||||
if (inProgress.value) return
|
if (inProgress.value) return
|
||||||
inProgress.value = true
|
inProgress.value = true
|
||||||
loginError.value = ''
|
loginError.value = ''
|
||||||
axios.post(`${api}/login`, {username: username.value, password: password.value}).then(response => {
|
axios.post(`${api}/login`, {username: username.value, password: password.value}, {withCredentials: true}).then((response) => {
|
||||||
const auth = useAuthStore()
|
uInfo.setUserInfo(response.data.user.username, response.data.user.id)
|
||||||
auth.setToken(response.data.token)
|
|
||||||
router.push("/")
|
router.push("/")
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
loginError.value = 'Invalid username or password'
|
loginError.value = 'Invalid username or password'
|
||||||
@@ -107,11 +108,6 @@ const options = {
|
|||||||
},
|
},
|
||||||
"polygon": {
|
"polygon": {
|
||||||
"sides": 5
|
"sides": 5
|
||||||
},
|
|
||||||
"image": {
|
|
||||||
"src": "img/github.svg",
|
|
||||||
"width": 100,
|
|
||||||
"height": 100
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"opacity": {
|
"opacity": {
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
MAJOR=0
|
MAJOR=0
|
||||||
MINOR=2
|
MINOR=3
|
||||||
PATCH=0
|
PATCH=0
|
||||||
|
|||||||
Reference in New Issue
Block a user