parent
db7654cfd0
commit
0020b164f7
@ -0,0 +1,79 @@
|
||||
const express = require('express');
|
||||
const http = require('http');
|
||||
const socketIO = require('socket.io');
|
||||
const cors = require('cors');
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const io = socketIO(server, {
|
||||
cors: {
|
||||
origin: ["http://localhost:3000", "http://localhost:3001"], // Remplacez par l'URL de votre application React
|
||||
methods: ["GET", "POST"],
|
||||
credentials: true
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const map = new Map()
|
||||
// ... le reste de votre configuration du serveur
|
||||
|
||||
server.listen(3002, () => {
|
||||
console.log('Serveur Socket.IO écoutant sur le port 3001');
|
||||
});
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log(socket.id);
|
||||
|
||||
socket.on('network created', (network, person, indices, room) =>{
|
||||
io.to(room).emit("game created", network, person, indices, Math.floor(Math.random() * map.get(room).length))
|
||||
});
|
||||
|
||||
socket.on("lobby joined", (room, name) =>{
|
||||
socket.join(room)
|
||||
if (map.get(room) == undefined){
|
||||
map.set(room, [{id: socket.id, name: name}])
|
||||
}
|
||||
else{
|
||||
const tab = map.get(room)
|
||||
for(let i = 0; i<tab.length; i++){
|
||||
if (tab[i].id === socket.id){
|
||||
tab.splice(i, 1)
|
||||
}
|
||||
}
|
||||
|
||||
map.get(room).push({id: socket.id, name: name})
|
||||
}
|
||||
|
||||
io.to(room).emit("new player", map.get(room))
|
||||
})
|
||||
|
||||
socket.on("lobby created", () =>{
|
||||
io.to(socket.id).emit("lobby created", 134)
|
||||
})
|
||||
|
||||
socket.on("already asked", (nodeId, askingPlayer, askedPlayer) =>{
|
||||
io.to(askingPlayer.id).emit("already asked", nodeId, askedPlayer)
|
||||
})
|
||||
|
||||
socket.on("ask player", (nodeId, playerId, askingPlayer) =>{
|
||||
io.to(playerId).emit("asked", nodeId, askingPlayer)
|
||||
})
|
||||
|
||||
socket.on("disconnect", () =>{
|
||||
for (const k of map.keys()){
|
||||
const tab = map.get(k)
|
||||
for (let i = 0; i<tab.length; i++){
|
||||
if (tab[i].id === socket.id){
|
||||
tab.splice(i, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
socket.on("node checked", (id, works, color, room, playerIndex) =>{
|
||||
console.log(playerIndex)
|
||||
io.to(room).emit("node checked", id, works, color, playerIndex)
|
||||
})
|
||||
|
||||
});
|
@ -0,0 +1,96 @@
|
||||
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
||||
import Indice from '../model/Indices/Indice';
|
||||
import Person from '../model/Person';
|
||||
import PersonNetwork from '../model/PersonsNetwork';
|
||||
import Player from '../model/Player';
|
||||
|
||||
interface GameContextProps {
|
||||
indices: Indice[];
|
||||
indice: Indice | null
|
||||
person: Person | null;
|
||||
personNetwork: PersonNetwork | null;
|
||||
players: Player[]
|
||||
nodeId: number | null
|
||||
askedPersons: Person[];
|
||||
actualPlayerIndex: number;
|
||||
room: string;
|
||||
setIndicesData: (newIndices: Indice[]) => void;
|
||||
setIndiceData: (newIndice: Indice) => void;
|
||||
setPersonData: (newPerson: Person) => void;
|
||||
setPersonNetworkData: (newPersonNetwork: PersonNetwork) => void;
|
||||
setPlayersData: (newPlayer: Player[]) => void;
|
||||
setNodeIdData: (newId: number) => void;
|
||||
setAskedPersonsData: (newAskedPersons: Person[]) => void;
|
||||
setActualPlayerIndexData: (newActualPlayerIndex: number) => void;
|
||||
setRoomData: (newRoom: string) => void;
|
||||
}
|
||||
|
||||
const GameContext = createContext<GameContextProps | undefined>(undefined);
|
||||
|
||||
interface GameProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const GameProvider: React.FC<GameProviderProps> = ({ children }) => {
|
||||
const [indices, setIndices] = useState<Indice[]>([]);
|
||||
const [indice, setIndice] = useState<Indice | null>(null);
|
||||
const [person, setPerson] = useState<Person | null>(null);
|
||||
const [personNetwork, setPersonNetwork] = useState<PersonNetwork | null>(null);
|
||||
const [players, setPlayers] = useState<Player[]>([])
|
||||
const [nodeId, setNodeId] = useState<number | null>(null);
|
||||
const [askedPersons, setAskedPersons] = useState<Person[]>([])
|
||||
const [actualPlayerIndex, setActualPlayerIndex] = useState<number>(-1)
|
||||
const [room, setRoom] = useState<string>("")
|
||||
|
||||
|
||||
const setIndicesData = (newIndices: Indice[]) => {
|
||||
setIndices(newIndices);
|
||||
};
|
||||
|
||||
const setIndiceData = (newIndice: Indice) =>{
|
||||
setIndice(newIndice)
|
||||
};
|
||||
|
||||
|
||||
const setPersonData = (newPerson: Person) => {
|
||||
setPerson(newPerson);
|
||||
};
|
||||
|
||||
const setPersonNetworkData = (newPersonNetwork: PersonNetwork) => {
|
||||
setPersonNetwork(newPersonNetwork);
|
||||
};
|
||||
|
||||
const setPlayersData = (newPlayers: Player[]) => {
|
||||
setPlayers(newPlayers);
|
||||
};
|
||||
|
||||
const setNodeIdData = (newId: number) => {
|
||||
setNodeId(newId);
|
||||
};
|
||||
|
||||
const setAskedPersonsData = (newAskedPerson: Person[]) => {
|
||||
setAskedPersons(newAskedPerson);
|
||||
};
|
||||
|
||||
const setActualPlayerIndexData = (newActualPlayerIndex: number) =>{
|
||||
setActualPlayerIndex(newActualPlayerIndex)
|
||||
}
|
||||
|
||||
const setRoomData = (newRoom: string) =>{
|
||||
setRoom(newRoom)
|
||||
}
|
||||
|
||||
return (
|
||||
<GameContext.Provider value={{ indices, setIndicesData, indice, setIndiceData, person, setPersonData, personNetwork, setPersonNetworkData, players, setPlayersData, nodeId, setNodeIdData, askedPersons, setAskedPersonsData, actualPlayerIndex, setActualPlayerIndexData, room, setRoomData }}>
|
||||
{children}
|
||||
</GameContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useGame = (): GameContextProps => {
|
||||
const context = useContext(GameContext);
|
||||
if (!context) {
|
||||
throw new Error('useGame must be used within an GameProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
@ -0,0 +1,83 @@
|
||||
import AgeIndice from "./model/Indices/AgeIndice";
|
||||
import ColorEdgesIndice from "./model/Indices/ColorEdgesIndice";
|
||||
import ColorIndice from "./model/Indices/ColorIndice";
|
||||
import Indice from "./model/Indices/Indice";
|
||||
import NbEdgesIndice from "./model/Indices/NbEdgesIndice";
|
||||
import NbSportIndice from "./model/Indices/NbSportIndice";
|
||||
import SportIndice from "./model/Indices/SportIndice";
|
||||
import Person from "./model/Person";
|
||||
import PersonNetwork from "./model/PersonsNetwork";
|
||||
|
||||
class JSONParser{
|
||||
|
||||
static JSONToNetwork(jsonString: any): PersonNetwork{
|
||||
const json = JSON.parse(jsonString)
|
||||
const persons: Person[] = []
|
||||
const personFriends = new Map<number, number[]>()
|
||||
json.persons.forEach((personJson: any) => {
|
||||
persons.push(JSONParser.JSONToPerson(personJson))
|
||||
personJson.friends.forEach((f: any) => {
|
||||
if (personFriends.get(personJson.id) == undefined){
|
||||
personFriends.set(personJson.id, [f.id])
|
||||
}
|
||||
else{
|
||||
personFriends.get(personJson.id)?.push(f.id)
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
for(const person of persons){
|
||||
const tab = personFriends.get(person.getId())
|
||||
if (tab != undefined){
|
||||
for(const i of tab){
|
||||
person.addFriend(persons.filter((p) => p.getId() == i)[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new PersonNetwork(persons);
|
||||
}
|
||||
|
||||
static JSONToPerson(json: any): Person {
|
||||
const person = new Person(
|
||||
json.id,
|
||||
json.name,
|
||||
json.age,
|
||||
json.color,
|
||||
json.sports,
|
||||
[]
|
||||
);
|
||||
return person;
|
||||
}
|
||||
|
||||
|
||||
static JSONToIndice(json: any): Indice{
|
||||
switch (json.type){
|
||||
case "AgeIndice":
|
||||
return new AgeIndice(json.id, json.minimum, json.maximum)
|
||||
case "ColorEdgesIndice":
|
||||
return new ColorEdgesIndice(json.id, json.neighborsColors)
|
||||
case "ColorIndice":
|
||||
return new ColorIndice(json.id, json.colors)
|
||||
case "NbEdgesIndice":
|
||||
return new NbEdgesIndice(json.id, json.nbNeighbors)
|
||||
case "NbSportIndice":
|
||||
return new NbSportIndice(json.id, json.nbSport)
|
||||
case "SportIndice":
|
||||
return new SportIndice(json.id, json.sports)
|
||||
default:
|
||||
throw new Error("PARSER unable to parse indice: " + json.type);
|
||||
}
|
||||
}
|
||||
|
||||
static JSONToIndices(jsonString: any): Indice[]{
|
||||
const json = JSON.parse(jsonString)
|
||||
const tabIndice: Indice[] = []
|
||||
json.forEach((i: any) => {
|
||||
tabIndice.push(this.JSONToIndice(i))
|
||||
});
|
||||
return tabIndice
|
||||
}
|
||||
}
|
||||
|
||||
export default JSONParser
|
@ -0,0 +1,6 @@
|
||||
import { io } from "socket.io-client";
|
||||
|
||||
|
||||
const socket = io("http://localhost:3002");
|
||||
|
||||
export {socket}
|
@ -0,0 +1,12 @@
|
||||
class Player{
|
||||
|
||||
public id: string
|
||||
public name: string;
|
||||
|
||||
constructor(id: string, name: string){
|
||||
this.id=id
|
||||
this.name=name
|
||||
}
|
||||
}
|
||||
|
||||
export default Player
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue