-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackendSetup.yaml
53 lines (42 loc) · 1.5 KB
/
backendSetup.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# SERVER JS CODE
- Import: express, http, socket.io, chess.js
- Create Express app instance
- Initialize HTTP server with Express
- Instantiate Socket.io on HTTP server
- Create Chess object instance (chess.js)
- Initialize:
- Players object: track socket IDs, roles (white/black)
- CurrentPlayer: track current turn
- Configure Express app:
- Use EJS templating engine
- Serve static files from 'public' directory
- Define route for root URL
- Render EJS template "index"
- Title: "Custom Chess Game"
- Socket.io handles connection event
- Callback executed on client connect
- Server assigns role based on availability:
- If slots empty:
- Assign role (white/black)
- Inform player
- If slots full:
- Designate as spectator
- Client connection:
- Assign role based on game state:
- If no white player, assign white role
- If no black player, assign black role
- Emit "playerRole" event with assigned role
- If both slots filled, designate as spectator
- Emit "spectatorRole" event
- Send initial board state using FEN notation
- Client disconnection:
- Remove assigned role from players object
- Listen for "move" events:
- Validate correct player's turn
- If valid:
- Update game state
- Broadcast move via "move" event
- Send updated board state via "boardState" event
- If invalid:
- Log error message
- Ensure smooth gameplay and real-time updates for all connected clients.