@20syldev/api 3.4.2 → 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 +2 -2
- package/app.js +2 -0
- package/modules/v3/tic_tac_toe.js +32 -16
- package/package.json +1 -1
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
|
-
[](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.
|
|
75
|
+
> @20syldev/api@3.4.4 build
|
|
76
76
|
> npm install && node app.js
|
|
77
77
|
|
|
78
78
|
[...]
|
package/app.js
CHANGED
|
@@ -642,11 +642,13 @@ app.get('/:version/website', async (req, res) => {
|
|
|
642
642
|
morpion: process.env.MORPION,
|
|
643
643
|
nitrogen: process.env.NITROGEN,
|
|
644
644
|
old_database: process.env.OLD_DATABASE,
|
|
645
|
+
password: process.env.PASSWORD,
|
|
645
646
|
php: process.env.PHP,
|
|
646
647
|
ping: process.env.PING,
|
|
647
648
|
portfolio: process.env.PORTFOLIO,
|
|
648
649
|
python_api: process.env.PYTHON_API,
|
|
649
650
|
readme: process.env.README,
|
|
651
|
+
timestamp: process.env.TIMESTAMP,
|
|
650
652
|
terminal: process.env.TERMINAL,
|
|
651
653
|
wrkit: process.env.WRKIT,
|
|
652
654
|
zpki: process.env.ZPKI
|
|
@@ -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(
|
|
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 (
|
|
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 (
|
|
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
|
-
|
|
87
|
+
moves.push(play);
|
|
87
88
|
|
|
88
89
|
// Check game result
|
|
89
|
-
const result = checkGame(
|
|
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
|
|
114
|
+
const id = params.game || generateGameId();
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
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