Skip to content

Commit

Permalink
Add env variable to skip matchmaking phase and save a lot of developm…
Browse files Browse the repository at this point in the history
…ent time
  • Loading branch information
benjaminbours committed Dec 19, 2023
1 parent e36479e commit 551d44c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 67 deletions.
4 changes: 3 additions & 1 deletion front/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
NEXT_PUBLIC_STAGE=development
# url of the load balancer for backend
NEXT_PUBLIC_BACKEND_URL=http://api.localhost
NEXT_PUBLIC_URL=http://front.localhost
NEXT_PUBLIC_URL=http://front.localhost
NEXT_PUBLIC_STAGE=development
NEXT_PUBLIC_SKIP_MATCHMAKING=1
10 changes: 8 additions & 2 deletions front/app/Game/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ function Game({

useEffect(() => {
if (!tabIsHidden && isSynchronizingTime && socketController) {
socketController.synchronizeTime().then(() => {
const onTimeSynchronized = () => {
appRef.current?.inputsManager.registerEventListeners();
setIsSynchronizingTime(false);
});
};

if (process.env.NEXT_PUBLIC_SKIP_MATCHMAKING) {
onTimeSynchronized();
} else {
socketController.synchronizeTime().then(onTimeSynchronized);
}
}

return () => {
Expand Down
160 changes: 96 additions & 64 deletions front/app/MainApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Levels,
MatchMakingPayload,
PositionLevel,
ProjectionLevel,
Side,
SocketEventType,
TeammateInfoPayload,
Expand Down Expand Up @@ -45,12 +46,19 @@ function MainApp() {
const socketController = useRef<SocketController>();
const [menuScene, setMenuScene] = useState<MenuScene>(MenuScene.HOME);
const [menuMode, setMenuMode] = useState<MenuMode>(MenuMode.DEFAULT);
const [state, setState] = useState<MainState>({
// side: Side.LIGHT,
// selectedLevel: Levels.CRACK_THE_DOOR,
side: undefined,
selectedLevel: undefined,
gameState: undefined,
const [state, setState] = useState<MainState>(() => {
if (process.env.NEXT_PUBLIC_SKIP_MATCHMAKING) {
return {
side: Side.LIGHT,
selectedLevel: Levels.LEARN_TO_FLY,
gameState: undefined,
};
}
return {
side: undefined,
selectedLevel: undefined,
gameState: undefined,
};
});
const [gameIsPlaying, setGameIsPlaying] = useState(false);
const [teamMateDisconnected, setTeamMateDisconnected] = useState(false);
Expand Down Expand Up @@ -167,7 +175,7 @@ function MainApp() {
]);
};

if (shouldEstablishConnection) {
if (process.env.NEXT_PUBLIC_SKIP_MATCHMAKING) {
import('./SocketController')
.then((mod) => mod.SocketController)
.then((SocketController) => {
Expand All @@ -179,66 +187,90 @@ function MainApp() {
);
return;
})
.then(sendMatchMakingInfo);
// establishConnection().then(() => {
// const level = new PositionLevel();
// const initialGameState = new GameState(
// [
// {
// position: {
// x: level.startPosition.shadow.x,
// y: level.startPosition.shadow.y,
// },
// velocity: {
// x: 0,
// y: 0,
// },
// },
// {
// position: {
// x: level.startPosition.light.x,
// y: level.startPosition.light.y,
// },
// velocity: {
// x: 0,
// y: 0,
// },
// },
// ],
// {
// ...level.state,
// doors: {
// ...level.state.doors,
// ground: [0],
// roof: [0],
// },
// },
// Date.now(),
// 0,
// );
// handleGameStart(initialGameState);
// });
} else if (shouldSendMatchMakingInfo) {
console.log('HERE send match making info');
sendMatchMakingInfo();
.then(() => {
const level = new ProjectionLevel();
const initialGameState = new GameState(
[
{
position: {
x: level.startPosition.shadow.x,
y: level.startPosition.shadow.y,
},
velocity: {
x: 0,
y: 0,
},
},
{
position: {
x: level.startPosition.light.x,
y: level.startPosition.light.y,
},
velocity: {
x: 0,
y: 0,
},
},
],
{
...level.state,
},
Date.now(),
0,
);
handleGameStart(initialGameState);
});
} else {
if (shouldEstablishConnection) {
import('./SocketController')
.then((mod) => mod.SocketController)
.then((SocketController) => {
socketController.current = new SocketController(
handleGameStart,
handleGameFinished,
handleTeamMateDisconnect,
handleTeamMateInfo,
);
return;
})
.then(sendMatchMakingInfo);
} else if (shouldSendMatchMakingInfo) {
console.log('HERE send match making info');
sendMatchMakingInfo();
}
}
}, [gameIsPlaying, shouldEstablishConnection, shouldSendMatchMakingInfo]);

// return (
// <>
// {/* {!state.isGameRunning && (
// <Menu mainState={state} setMainState={setState} />
// )} */}
// {state.gameState && (
// <Game
// initialGameState={state.gameState}
// side={state.side!}
// socketController={socketController.current}
// tabIsHidden={tabIsHidden}
// />
// )}
// </>
// );
if (process.env.NEXT_PUBLIC_SKIP_MATCHMAKING) {
return (
<>
{teamMateDisconnected && (
// TODO: Make appear disappear animation
<div className="team-mate-disconnect">
<p>Your team mate disconnected or has quit the room</p>
<button
className="buttonRect white"
onClick={handleClickFindAnotherTeamMate}
>
Find another team mate
</button>
</div>
)}
{/* {!state.isGameRunning && (
<Menu mainState={state} setMainState={setState} />
)} */}
{state.gameState && (
<Game
initialGameState={state.gameState}
side={state.side!}
socketController={socketController.current}
tabIsHidden={tabIsHidden}
stats={statsRef}
/>
)}
</>
);
}

return (
<>
Expand Down

0 comments on commit 551d44c

Please sign in to comment.