pendu fini (td3)

This commit is contained in:
2022-09-21 12:09:38 +02:00
parent 7b82663ae0
commit d1d7b2693f
13 changed files with 1689 additions and 0 deletions

BIN
TD/TD3/pendu/.DS_Store vendored Normal file

Binary file not shown.

130
TD/TD3/pendu/.gitignore vendored Normal file
View File

@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

56
TD/TD3/pendu/client.js Normal file
View File

@ -0,0 +1,56 @@
// On se connecte au serveur
var connection = new WebSocket("ws://localhost:8080");
connection.onopen = () => {
console.log('connected');
};
connection.onclose = () => {
console.error('disconnected');
};
connection.onerror = error => {
console.error('failed to connect', error);
};
connection.onmessage = msg => {
let data = JSON.parse(msg.data)
if(data[0] == "Taille") {
for(let i = 0; i < data[1]; ++i) {
let li = document.createElement('li');
li.innerText = "_ ";
document.querySelector('#mot').append(li);
}
}
if(data[0] == "CharError") {
alert("Votre lettre n'est pas correcte.")
}
if(data[0] == "Char") {
const list = document.getElementsByTagName("li");
list[data[2]].innerText = data[1];
}
if(data[0] == "Partie") {
alert("Vous avez gagné ! Félicitations !");
}
};
// Lorsque l'on clique sur le bouton envoyer
document.querySelector('form').addEventListener('submit', event => {
event.preventDefault();
// On récupère la valeur entrée dans le formulaire
let lettre = document.querySelector('#lettre').value;
// On envoie la lettre au serveur
connection.send(JSON.stringify(lettre));
// On vide le formulaire
document.querySelector('#lettre').value = '';
});

20
TD/TD3/pendu/db.json Normal file
View File

@ -0,0 +1,20 @@
{
"mots": [
{
"id": 1,
"value": "voiture"
},
{
"id": 2,
"value": "antilope"
},
{
"id": 3,
"value": "nice"
},
{
"id": 4,
"value": "poney"
}
]
}

18
TD/TD3/pendu/index.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<title>Web Socket Demo</title>
</head>
<body>
<ul id="mot"></ul>
<form>
Lettre : <input id='lettre' type="text"/>
<br />
<button type="submit">Jouer</button>
</form>
<script src="client.js"></script>
</body>
</html>

1393
TD/TD3/pendu/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

11
TD/TD3/pendu/package.json Normal file
View File

@ -0,0 +1,11 @@
{
"scripts": {
"start": "node serveur.js",
"jsonserver": "json-server -p 3001 --watch db.json"
},
"dependencies": {
"json-server": "^0.17.0",
"node-fetch": "^2.6.7",
"ws": "^7.0.1"
}
}

60
TD/TD3/pendu/serveur.js Normal file
View File

@ -0,0 +1,60 @@
const fetch = require('node-fetch');
const WebSocket = require('ws');
var word;
var game;
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function getWordSize() {
return JSON.stringify(["Taille", word.length]);
}
function displayLetter(char) {
return JSON.stringify(["Char", char, word.indexOf(char)]);
}
// On créé notre Web Serveur qui écoute sur le port 8080
const wss = new WebSocket.Server({
port: 8080
});
fetch('http://localhost:3001/mots') // une requete GET
.then((response) => { // lorsque la requête est terminée on traite la réponse
response.json() // que lon passe en JSON
.then((mots) => { // On fois fait on peut travailler sur la liste des mots
word = mots[getRandomInt(Object.keys(mots).length)].value; //on choisi aléatoirement le mot dans la liste mots
console.log("Mot choisi : " + word); //on l'affiche sur notre console serveur pour tester plus facilement
game = word.length;
}
);
}).catch(error => console.error);
// Lorsqu'un client se connecte on reçoit un événement 'connection'
wss.on('connection', ws => {
ws.send(getWordSize());
// Lorsqu'il nous envoie un message on reçoit un événement message ainsi que le message !
ws.on('message', message => {
console.log(`Received message => ${message}`);
let char = message.charAt(1);
if(word.indexOf(char) > -1) {
game--;
ws.send(displayLetter(char));
if(game === 0) {
ws.send(JSON.stringify(["Partie", true]));
}
} else {
ws.send(JSON.stringify(["CharError"]));
}
});
})