Skip to content
Snippets Groups Projects
Commit 4b7c6d0a authored by BENCHIKH Ilhem's avatar BENCHIKH Ilhem
Browse files

td3

parent 097c7587
No related branches found
No related tags found
No related merge requests found
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
<html> <html>
<head> <head>
<link rel="icon" href="favicon.svg"/> <link rel="icon" href="favicon.svg"/>
<link href="/bootstrap.min.css" rel="stylesheet" <link href="/bootstrap.min.css"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" rel="stylesheet"
crossorigin="anonymous"> >
<title>La revanche des lettres</title> <title>La revanche des lettres</title>
</head> </head>
......
...@@ -5,8 +5,7 @@ ...@@ -5,8 +5,7 @@
<link rel="icon" href="favicon.svg" /> <link rel="icon" href="favicon.svg" />
<link href="/bootstrap.min.css" <link href="/bootstrap.min.css"
rel="stylesheet" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" >
crossorigin="anonymous">
</head> </head>
<body> <body>
<div class="navbar navbar-dark bg-dark"> <div class="navbar navbar-dark bg-dark">
......
export class Game {
constructor (data){
this.data = data;
}
levels(){
return this.data.levels;
}
level(id){
for (let level of this.data.levels) {
if (level.id === id) {
return level;
}
}
/*return undefined;*/
throw new Error("Niveau non trouvé")
}
word(id){
const word = this.data.words[id];
if(word !== undefined){
return word;
}
throw new Error("Mot non trouvé")
}
stringToArray(string){
let result = [];
for(let i = 0; i < string.length; i++){
result.push(string[i].toUpperCase());
}
return result;
}
stringToArray(string){
return string.split("").map(char => char.toUpperCase());
}
letters(id){
const word = this.word(id);
const letterArray = this.stringToArray(word);
return letterArray.sort();
}
computeLine(id, playedWord){
const word = this.word(id);
const result = [];
for(let i = 0; i < word.length; i++){
const playedLetter = i < playedWord.length ? playedWord[i].toUpperCase() : '_';
const letter = word[i].toUpperCase();
result.push({"letter" : playedLetter, "state" : playedLetter == letter});
}
return result;
}
}
import { data } from "./data.js";
import { Game } from "./game.js";
import { ServerApp } from "./server-app.js";
const game = new Game(data);
const app = new ServerApp(game);
app.start(5000, true);
\ No newline at end of file
{ {
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "set PROGRESSION=\"${npm_config_progression}\" & vitest run all.test.js" "test": "cross-env PROGRESSION=\"${npm_config_progression}\" vitest run all.test.js"
} }
} }
\ No newline at end of file
...@@ -10,4 +10,53 @@ export class ServerApp { ...@@ -10,4 +10,53 @@ export class ServerApp {
constructor(game) { constructor(game) {
this.game = game; this.game = game;
} }
build(logEnabled) {
const fastify = new Fastify({logger: logEnabled});
fastify.register(fastifyView, {engine: {ejs: ejs}});
fastify.register(fastifyStatic, { root: new URL('static', import.meta.url) });
fastify.get("/", (request, reply) => this.getIndex(request, reply));
fastify.get("/level/:id(^\\d+$)", (request, reply) => this.getLevel(request, reply));
fastify.setErrorHandler((error, request, reply) => {this.handleError(error, reply);});
fastify.setNotFoundHandler((request, reply) => {this.handleNotFound(reply);});
return fastify;
}
start(port, logEnabled) {
const fastify = this.build(logEnabled);
fastify.listen({ port: port }, (err, address) => {
if (err) { fastify.log.error(err); process.exit(1) }
});
}
getIndex(request, reply) {
const levels = this.game.levels();
reply.view('templates/index.ejs', { levels: levels });
}
getLevel(request, reply) {
const id = parseInt(request.params.id, 10);
const level = this.game.level(id);
const letters = this.game.letters(id);
reply.view('templates/level.ejs', {
id : id,
level: level,
letters: letters
});
}
handleError(error, reply) {
reply.status(500).view('templates/error.ejs', {
code: 500,
error: error.message
});
}
handleNotFound(reply) {
reply.status(404).view('templates/error.ejs', {
code: 404,
error: 'Impossible de trouver cette page'
});
}
} }
\ No newline at end of file
...@@ -28,9 +28,22 @@ ...@@ -28,9 +28,22 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<!-- TODO : générer la liste des niveaux --> <% for (const level of levels) { %>
<tr>
<td><%= level.length %> lettres</td>
<td><%= level.theme %></td>
<td>
<% for (let i = 0; i < level.difficulty; i++) { %>
&#9733;
<% } %>
</td>
<td><a href="level/<%= level.id %>">Démarrer</a></td>
</tr>
<% } %>
</tbody> </tbody>
</table> </table>
</div> </div>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<img src="/favicon.svg" alt="favicon"> <img src="/favicon.svg" alt="favicon">
<strong>La revanche des lettres</strong> <strong>La revanche des lettres</strong>
</a> </a>
<span class="navbar-text"><!-- TODO 2 --> (<!-- TODO 3--> lettres)</span> <span class="navbar-text"><%= level.theme %> (<%= level.length %> lettres)</span>
</div> </div>
</div> </div>
<div class="container"> <div class="container">
...@@ -22,11 +22,20 @@ ...@@ -22,11 +22,20 @@
<div class="col-4 offset-4"> <div class="col-4 offset-4">
<table class="table table-bordered text-center"> <table class="table table-bordered text-center">
<tbody> <tbody>
<!-- TODO 1 --> <tr>
<% for (let i = 0; i < letters.length; i++) { %>
<td class="text-bg-primary"><%= letters[i] %></td>
<% } %>
</tr>
<!-- TODO 5 --> <!-- TODO 5 -->
</tbody> </tbody>
</table> </table>
<!-- TODO 4 --> <form action="/level/<%= id %>" method="POST" class="mt-3">
<div class="input-group mb-3">
<input type="text" name="word" class="form-control" placeholder="Votre mot" required>
<button type="submit" class="btn btn-outline-secondary">Proposer</button>
</div>
</form>
</div> </div>
</div> </div>
</body> </body>
......
{ {
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "PROGRESSION=\"${npm_config_progression}\" vitest run all.test.js" "test": "cross-env PROGRESSION=\"${npm_config_progression}\" vitest run all.test.js"
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment