@20syldev/api 3.4.3 → 3.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  <a href="https://api.sylvain.pro"><img src="https://api.sylvain.pro/favicon.ico" alt="Logo" width="25%" height="auto"/></a>
3
3
 
4
4
  # API Personnelle
5
- [![Version](https://custom-icon-badges.demolab.com/badge/Version%20:-v3.4.3-6479ee?logo=api.sylvain.pro&labelColor=23272A)](https://github.com/20syldev/api/releases/latest)
5
+ [![Version](https://custom-icon-badges.demolab.com/badge/Version%20:-v3.4.4-6479ee?logo=api.sylvain.pro&labelColor=23272A)](https://github.com/20syldev/api/releases/latest)
6
6
  </div>
7
7
 
8
8
  ---
@@ -72,7 +72,7 @@ Enfin, **exécutez** le script de build, qui installera les **dépendances** et
72
72
  $ npm run build
73
73
  ```
74
74
  ```console
75
- > @20syldev/api@3.4.3 build
75
+ > @20syldev/api@3.4.4 build
76
76
  > npm install && node app.js
77
77
 
78
78
  [...]
@@ -63,30 +63,31 @@ function playMove(params, games, sessions, u, now) {
63
63
  }
64
64
 
65
65
  // Initialize game if needed
66
- games[game] = games[game] || [];
66
+ games[game] = games[game] || { moves: [], players: [] };
67
+ const moves = games[game].moves;
67
68
 
68
69
  // Check if game is full
69
- const players = [...new Set(games[game].map(play => play.username))];
70
+ const players = [...new Set(moves.map(play => play.username))];
70
71
  if (players.length >= 2 && !players.includes(params.username)) {
71
72
  throw new Error('Game is full, you can only watch.');
72
73
  }
73
74
 
74
75
  // Check if it's player's turn
75
- if (games[game].length > 0 && games[game][games[game].length - 1].username === params.username) {
76
+ if (moves.length > 0 && moves[moves.length - 1].username === params.username) {
76
77
  throw new Error('Please wait for the other player to make a move.');
77
78
  }
78
79
 
79
80
  // Check if move is already made
80
- if (games[game].some(play => play.move === move)) {
81
+ if (moves.some(play => play.move === move)) {
81
82
  throw new Error('Move already made. Please choose a different move.');
82
83
  }
83
84
 
84
85
  // Add move to game
85
86
  const play = { username: params.username, move, session };
86
- games[game].push(play);
87
+ moves.push(play);
87
88
 
88
89
  // Check game result
89
- const result = checkGame(games[game]);
90
+ const result = checkGame(moves);
90
91
  if (result.winner || result.tie) {
91
92
  setTimeout(() => delete games[game], 600000);
92
93
  return {
@@ -110,18 +111,33 @@ function playMove(params, games, sessions, u, now) {
110
111
  * Fetch a game state
111
112
  */
112
113
  function fetchGame(params, games, u) {
113
- const ID = params.game || generateGameId();
114
+ const id = params.game || generateGameId();
114
115
 
115
- // Initialize game if needed
116
- if (!games[ID]) games[ID] = [];
117
-
118
- const data = games[ID];
119
- const last = data.length ? data[data.length - 1].username : null;
120
- const players = [...new Set(data.map(p => p.username))];
121
- const turn = players.find(p => p !== last);
122
- const result = data.length ? checkGame(data) : {};
116
+ if (!games[id]) {
117
+ games[id] = {
118
+ moves: [],
119
+ players: []
120
+ };
121
+ }
122
+ if (!games[id].players.includes(u)) {
123
+ games[id].players.push(u);
124
+ }
123
125
 
124
- return { game: data, turn, ID, ...result };
126
+ const moves = games[id].moves;
127
+ const players = games[id].players;
128
+ const lastPlayer = moves.length ? moves[moves.length - 1].username : null;
129
+ const turn = players.find(p => p !== lastPlayer) || players[0];
130
+ const status = players.length >= 2 ? 'ready' : 'waiting';
131
+ const result = moves.length ? checkGame(moves) : {};
132
+
133
+ return {
134
+ id,
135
+ moves,
136
+ players,
137
+ turn,
138
+ status,
139
+ ...result
140
+ };
125
141
  }
126
142
 
127
143
  /**
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.4.3",
2
+ "version": "3.4.4",
3
3
  "name": "@20syldev/api",
4
4
  "description": "Node.js API with multiple features. Check the documentation at https://docs.sylvain.pro",
5
5
  "main": "app.js",