Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into main
  • Loading branch information
ethinot committed Apr 22, 2023
2 parents 18d9c21 + 0db80be commit f02dcd0
Show file tree
Hide file tree
Showing 9 changed files with 5,849 additions and 1,612 deletions.
7,312 changes: 5,727 additions & 1,585 deletions client/package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"buffer": "^6.0.3",
"dotenv": "^16.0.3",
"firebase": "^9.16.0",
"firebase-auth": "^0.1.2",
"phaser": "^3.55.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -27,24 +28,24 @@
"socket.io-client": "^4.5.4"
},
"devDependencies": {
"@types/jest": "^29.4.4",
"@types/node": "^18.11.18",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@types/jest": "^29.4.4",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"@vitejs/plugin-react": "^3.0.0",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-html": "^7.1.0",
"eslint-plugin-react": "^7.32.1",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-react": "^7.32.1",
"jest": "^29.5.0",
"lint-staged": "^13.1.0",
"prettier": "2.8.3",
"supertest": "^6.3.3",
"ts-node": "^10.9.1",
"@types/node": "^18.11.18",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.9.3",
"vite": "^4.0.0"
},
Expand Down
63 changes: 61 additions & 2 deletions client/src/components/Menu/Stats/Statistics.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
/* eslint-disable react/react-in-jsx-scope */
import { FC } from "react";
import { FC, useEffect } from "react";
import "./Statistics.css";
import app from "../../../Firebase";
import { DocumentData, doc, getDoc, getFirestore } from "firebase/firestore";

const Statistics: FC = () => {
useEffect(() => {
const db = getFirestore(app);

const getTodayDateFormatted = () => {
const date = new Date();
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
};

const getPlayerUsername = async (playerId: string) => {
// Get the user's username from users collection
const userRef = doc(db, "users", playerId);
const userDoc = await getDoc(userRef);
if (userDoc.exists() && userDoc.data()?.displayName) {
return userDoc.data()?.displayName;
}
return "Unknown";
};

const formatStats = async (stats: DocumentData) => {
console.log(stats);

const playerList = document.createElement("ul");

for (const playerId in stats) {
const playerStats = stats[playerId];

const listItem = document.createElement("li");
const playerName = await getPlayerUsername(playerId);
listItem.innerHTML = `<strong>${playerName}:</strong>
<ul>
<li>Blocks parcourus: ${playerStats.blockTravelled}</li>
<li>Blocks capturés: ${playerStats.blocksCaptured}</li>
<li>Score le plus élevé: ${playerStats.highestScore || 0}</li>
<li>Nombre de morts: ${playerStats.deaths}</li>
<li>Nombre de kills: ${playerStats.kills}</li>
</ul>`;
playerList.appendChild(listItem);
}
document.getElementById("statsContainer")?.appendChild(playerList);
};

const getStatsToday = async () => {
const todayRef = doc(db, "stats", getTodayDateFormatted());
const todayDoc = await getDoc(todayRef);
if (todayDoc.exists()) {
console.log("Document data:", todayDoc.data());
formatStats(todayDoc.data());
} else {
console.log("No stats for today!");
}
};

getStatsToday();
}, []);

return (
<div className="main-stat-container">
<h2 className="menu-stat-title">🥇 Hall of Fame 🥇</h2>
<p>Ici il y aura pleins de stats ca va être super nespa ?</p>
<div id="statsContainer">
<h2>Les stats du jour</h2>
</div>
</div>
);
};
Expand Down
27 changes: 9 additions & 18 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions server/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,35 @@ const getStats = async () => {
return stats;
};

const checkIfUserExists = async (playerId: string) => {
const doc = await usersRef.doc(playerId).get();
return doc.exists;
};

const saveUser = async (playerId: string, pseudo: string) => {
const userExists = await checkIfUserExists(playerId);
const user = await admin.auth().getUser(playerId);
if (!userExists) {
await usersRef.doc(playerId).set({});
await usersRef.doc(playerId).update({
createdAt: admin.firestore.FieldValue.serverTimestamp(),
displayName: user.displayName,
uid: playerId,
});
} else {
console.log(
"user exists, dipslayName : " + user.displayName + " pseudo :" + pseudo
);
await usersRef.doc(playerId).update({
displayName: pseudo || user.displayName,
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
}
};

const saveUserStats = async (
playerToken: string,
pseudo: string,
user: PlayerGameStats,
docName: string
) => {
Expand All @@ -40,6 +67,7 @@ const saveUserStats = async (
.auth()
.verifyIdToken(playerToken)
.then((decodedToken) => {
saveUser(decodedToken.uid, pseudo);
statRef.doc(docName).update({
[decodedToken.uid]: {
kills: admin.firestore.FieldValue.increment(user._kills),
Expand All @@ -50,6 +78,7 @@ const saveUserStats = async (
blocksCaptured: admin.firestore.FieldValue.increment(
user._blocksCaptured
),
highestScore: user._highestScore,
},
});
})
Expand Down
1 change: 1 addition & 0 deletions server/src/enums/Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export enum Stats {
KILLED = 2,
BLOCK_CAPTURED = 3,
BLOCK_TRAVELLED = 4,
HIGHEST_SCORE = 5,
}
4 changes: 3 additions & 1 deletion server/src/game/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export default class Game {
disconnected = false
): void {
player.gameStats.Add(Stats.KILLED, 1);
player.gameStats.Add(Stats.HIGHEST_SCORE, player.scoreTotal);
player.socket.emit("gameOver");
player.isAlive = false;
player.outOfHisTerritory = false;
Expand All @@ -259,6 +260,7 @@ export default class Game {
if (killer && killer.id !== player.id) {
const getKilledScoreRatio = 0.1;
killer.score += player.scoreTotal * getKilledScoreRatio;
killer.gameStats.Add(Stats.HIGHEST_SCORE, killer.scoreTotal);
killer.gameStats.Add(Stats.KILL, 1);
killer.socket.emit("kill");
}
Expand Down Expand Up @@ -319,7 +321,7 @@ export default class Game {
// Save stats
player.gameStats.Add(Stats.BLOCK_CAPTURED, 23);
player.gameStats.Add(Stats.BLOCK_TRAVELLED, 43);
saveUserStats(player.token, player.gameStats, docName);
saveUserStats(player.token, player.pseudo, player.gameStats, docName);
});
}

Expand Down
2 changes: 0 additions & 2 deletions server/src/game/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export default class Player {
}

Move(): void {
//Todo: vérifications

switch (this.direction) {
case Direction.Up:
this.position.y -= 1;
Expand Down
14 changes: 14 additions & 0 deletions server/src/game/playerGameStats.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { log } from "console";
import { Stats } from "../enums/Stats";
export default class PlayerGameStats {
private kills: number;
private killed: number;
private blocksCaptured: number;
private blocksTravelled: number;
private highestScore: number;

constructor() {
this.kills = 0;
this.killed = 0;
this.blocksCaptured = 0;
this.blocksTravelled = 0;
this.highestScore = 0;
}

get _kills(): number {
Expand All @@ -28,7 +31,12 @@ export default class PlayerGameStats {
return this.blocksTravelled;
}

get _highestScore(): number {
return this.highestScore;
}

Add(type: Stats, amount: number): void {
console.log("Adding " + amount + " to " + type);
switch (type) {
case Stats.KILL:
this.kills += amount;
Expand All @@ -42,6 +50,12 @@ export default class PlayerGameStats {
case Stats.BLOCK_TRAVELLED:
this.blocksTravelled += amount;
break;
case Stats.HIGHEST_SCORE:
console.log("Highest score: " + amount + " > " + this.highestScore);
if (amount > this.highestScore) {
this.highestScore = amount;
}
break;
}
}
}

0 comments on commit f02dcd0

Please sign in to comment.