diff --git a/app/sockets/socket.py b/app/sockets/socket.py index 0df3bfa..e69de29 100644 --- a/app/sockets/socket.py +++ b/app/sockets/socket.py @@ -1,5 +0,0 @@ -from app import sIO - -@sIO.on('auth') -def handle_auth(msg): - print(msg) \ No newline at end of file diff --git a/app/static/js/main.js b/app/static/js/main.js new file mode 100644 index 0000000..2e09dfd --- /dev/null +++ b/app/static/js/main.js @@ -0,0 +1,16 @@ +import { createWSClient } from "./ws.js"; +import { + handleP2Connected, + handleGameStarted, + handleCodeGameCreated, +} from "./ws_handlers.js"; + +function main() { + createWSClient({ + onGameStarted: handleGameStarted, + onP2Connected: handleP2Connected, + onGameCreated: handleCodeGameCreated, + }); +} + +main(); diff --git a/app/static/js/ws.js b/app/static/js/ws.js new file mode 100644 index 0000000..c809e9a --- /dev/null +++ b/app/static/js/ws.js @@ -0,0 +1,35 @@ +// using socketio for websocket communication +export function createWSClient(handlers = {}) { + const socket = io(); + + socket.on("connect", () => { + console.log("Connected to server"); + handlers.onConnect?.(); + }); + + socket.on("disconnect", () => { + console.log("Disconnected from server"); + handlers.onDisconnect?.(); + }); + + const serverEvents = { + code_game_created: handlers.onGameCreated, + code_game_joined: handlers.onGameJoined, + game_started: handlers.onGameStarted, + p2_connected: handlers.onP2Connected, + user_move: handlers.onUserMove, + move_accept: handlers.onMoveAccept, + move_reject: handlers.onMoveReject, + game_over: handlers.onGameOver, + //todo: draw, resign, rematch + }; + + let i = 0; + for (const [event, handler] of Object.entries(serverEvents)) { + if (handler) { + socket.on(event, handler); + i++; + } + } + console.log("registered " + i + " server event handlers"); +} diff --git a/app/static/js/ws_handlers.js b/app/static/js/ws_handlers.js new file mode 100644 index 0000000..4db7c48 --- /dev/null +++ b/app/static/js/ws_handlers.js @@ -0,0 +1,11 @@ +export function handleGameStarted(data) { + throw new Error("todo"); +} + +export function handleCodeGameCreated(data) { + throw new Error("todo"); +} + +export function handleP2Connected(data) { + throw new Error("todo"); +} diff --git a/app/static/play.css b/app/static/play.css new file mode 100644 index 0000000..e69de29 diff --git a/app/templates/home.html b/app/templates/home.html index 5159827..cc31329 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -12,4 +12,6 @@
hi, {{ current_user.username }}!
+ Play Chess +