From c0e9efcc9fa628bf57aeeb57659af5472d4dbd7b Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Thu, 8 Oct 2020 21:30:39 -0600 Subject: [PATCH] Add server variable to the event handlers. --- src/events/ChancellorNomination.ts | 4 +-- src/events/ChancellorPolicy.ts | 4 +-- src/events/GetPlayerList.ts | 4 +-- src/events/HostGame.ts | 4 +-- src/events/JoinGame.ts | 6 ++--- src/events/KickPlayer.ts | 4 +-- src/events/PresidentPolicies.ts | 4 +-- src/events/StartGame.ts | 4 +-- src/events/VetoConfirm.ts | 4 +-- src/events/VetoRequest.ts | 4 +-- src/events/Vote.ts | 4 +-- src/events/special/Execution/ExecutePlayer.ts | 4 +-- src/events/special/ExecutiveConfirmation.ts | 4 +-- .../InvestigateAffiliation.ts | 4 +-- .../special/SpecialElection/NextPresident.ts | 4 +-- src/main.ts | 26 +++++++++---------- 16 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/events/ChancellorNomination.ts b/src/events/ChancellorNomination.ts index 3f1f30e..14f7631 100644 --- a/src/events/ChancellorNomination.ts +++ b/src/events/ChancellorNomination.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const ChancellorNomination = (socket: Socket, data: ChancellorNomination) => { +import { Server, Socket } from 'socket.io'; +export const ChancellorNomination = (io: Server, socket: Socket, data: ChancellorNomination) => { console.log(`ChancellorNomination event`); }; \ No newline at end of file diff --git a/src/events/ChancellorPolicy.ts b/src/events/ChancellorPolicy.ts index 9cc2b6e..c8cf6e7 100644 --- a/src/events/ChancellorPolicy.ts +++ b/src/events/ChancellorPolicy.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const ChancellorPolicy = (socket: Socket, data: ChancellorPolicy) => { +import { Server, Socket } from 'socket.io'; +export const ChancellorPolicy = (io: Server, socket: Socket, data: ChancellorPolicy) => { console.log(`Chancellor policy`); }; \ No newline at end of file diff --git a/src/events/GetPlayerList.ts b/src/events/GetPlayerList.ts index 6394d25..7c35175 100644 --- a/src/events/GetPlayerList.ts +++ b/src/events/GetPlayerList.ts @@ -4,11 +4,11 @@ Get's the current player list for a game. Emissions: PlayerListResponse->sender - Sends the list of players to the client */ -import { Socket } from 'socket.io'; +import { Server, Socket } from 'socket.io'; import { Game } from '../utils/Game'; import { active_games } from '../main'; -export const GetPlayerList = (socket: Socket, data: GetPlayerList) => { +export const GetPlayerList = (io: Server, socket: Socket, data: GetPlayerList) => { try { if (!Object.keys(active_games).includes(data.game_code)) { socket.emit(`PlayerListResponse`, { diff --git a/src/events/HostGame.ts b/src/events/HostGame.ts index d98afc2..def9763 100644 --- a/src/events/HostGame.ts +++ b/src/events/HostGame.ts @@ -11,12 +11,12 @@ Client Side: After the host receives the `HostInformation` event, the Query String parameters should be updated with the Websocket URI and the game code so that the host is able to just send that to the other players */ -import { Socket } from 'socket.io'; +import { Server, Socket } from 'socket.io'; import { Game } from '../utils/Game'; import { active_games, log } from '../main'; import { generate_game_code } from '../utils/gamecode'; -export const HostGame = (socket: Socket, data: HostGame) => { +export const HostGame = (io: Server, socket: Socket, data: HostGame) => { try { // Get a game code that is not in use to prevent join conflicts diff --git a/src/events/JoinGame.ts b/src/events/JoinGame.ts index 9a93810..a330904 100644 --- a/src/events/JoinGame.ts +++ b/src/events/JoinGame.ts @@ -8,9 +8,9 @@ Emissions: GameJoined->sender - Sent to the client to tell them the current list of players and that they were added to the game either successfully or not */ -import { Socket } from 'socket.io'; +import { Server, Socket } from 'socket.io'; import { active_games, log } from '../main'; -export const JoinGame = (socket: Socket, data: JoinGame) => { +export const JoinGame = (io: Server, socket: Socket, data: JoinGame) => { try { // Check if it's an active game if (!Object.keys(active_games).includes(data.game_code)) { @@ -57,7 +57,7 @@ export const JoinGame = (socket: Socket, data: JoinGame) => { }) // Alert existing players about new player - socket.broadcast.emit(`NewPlayer`, { + socket.to(game.code).emit(`NewPlayer`, { game_code: data.game_code, player: data.username, }); diff --git a/src/events/KickPlayer.ts b/src/events/KickPlayer.ts index a7c35f7..b9490a4 100644 --- a/src/events/KickPlayer.ts +++ b/src/events/KickPlayer.ts @@ -9,9 +9,9 @@ Emissions: name to be itself, it kicks the player back to he main page. Otherwise it just removes the player from the display list. */ -import { Socket } from 'socket.io'; +import { Server, Socket } from 'socket.io'; import { active_games, log } from '../main'; -export const KickPlayer = (socket: Socket, data: KickPlayer) => { +export const KickPlayer = (io: Server, socket: Socket, data: KickPlayer) => { try { // Check if it's an active game if (!Object.keys(active_games).includes(data.game_code)) { diff --git a/src/events/PresidentPolicies.ts b/src/events/PresidentPolicies.ts index de91b20..6b45803 100644 --- a/src/events/PresidentPolicies.ts +++ b/src/events/PresidentPolicies.ts @@ -10,7 +10,7 @@ Emissions: ChancellorChoice - This emits to everyone, but only the active chancellor's client should do anything. */ -import { Socket } from 'socket.io'; -export const PresidentPolicies = (socket: Socket, data: PresidentPolicies) => { +import { Server, Socket } from 'socket.io'; +export const PresidentPolicies = (io: Server, socket: Socket, data: PresidentPolicies) => { console.log(`Received Presidential policies`); }; \ No newline at end of file diff --git a/src/events/StartGame.ts b/src/events/StartGame.ts index bd93fb9..10e45f1 100644 --- a/src/events/StartGame.ts +++ b/src/events/StartGame.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const StartGame = (socket: Socket, data: StartGame) => { +import { Server, Socket } from 'socket.io'; +export const StartGame = (io: Server, socket: Socket, data: StartGame) => { console.log(`Game starting`); }; \ No newline at end of file diff --git a/src/events/VetoConfirm.ts b/src/events/VetoConfirm.ts index 983d20b..5de8644 100644 --- a/src/events/VetoConfirm.ts +++ b/src/events/VetoConfirm.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const VetoConfirm = (socket: Socket, data: VetoConfirm) => { +import { Server, Socket } from 'socket.io'; +export const VetoConfirm = (io: Server, socket: Socket, data: VetoConfirm) => { console.log(`President's veto confirmation`); }; \ No newline at end of file diff --git a/src/events/VetoRequest.ts b/src/events/VetoRequest.ts index 7318a12..b024e82 100644 --- a/src/events/VetoRequest.ts +++ b/src/events/VetoRequest.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const VetoRequest = (socket: Socket, data: VetoRequest) => { +import { Server, Socket } from 'socket.io'; +export const VetoRequest = (io: Server, socket: Socket, data: VetoRequest) => { console.log(`Chancellor requesting veto`); }; \ No newline at end of file diff --git a/src/events/Vote.ts b/src/events/Vote.ts index 0a6c53b..1cdc66c 100644 --- a/src/events/Vote.ts +++ b/src/events/Vote.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const Vote = (socket: Socket, data: Vote) => { +import { Server, Socket } from 'socket.io'; +export const Vote = (io: Server, socket: Socket, data: Vote) => { console.log(`Vote received`); }; \ No newline at end of file diff --git a/src/events/special/Execution/ExecutePlayer.ts b/src/events/special/Execution/ExecutePlayer.ts index a6acfad..6923f9d 100644 --- a/src/events/special/Execution/ExecutePlayer.ts +++ b/src/events/special/Execution/ExecutePlayer.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const ExecutePlayer = (socket: Socket, data: ExecutePlayer) => { +import { Server, Socket } from 'socket.io'; +export const ExecutePlayer = (io: Server, socket: Socket, data: ExecutePlayer) => { console.log(`Killing a player`); }; \ No newline at end of file diff --git a/src/events/special/ExecutiveConfirmation.ts b/src/events/special/ExecutiveConfirmation.ts index a854148..835655c 100644 --- a/src/events/special/ExecutiveConfirmation.ts +++ b/src/events/special/ExecutiveConfirmation.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const ExecutiveConfirmation = (socket: Socket, data: ExecutiveConfirmation) => { +import { Server, Socket } from 'socket.io'; +export const ExecutiveConfirmation = (io: Server, socket: Socket, data: ExecutiveConfirmation) => { console.log(`Just a plain ole confirmation`); }; \ No newline at end of file diff --git a/src/events/special/InvestigateLoyalty/InvestigateAffiliation.ts b/src/events/special/InvestigateLoyalty/InvestigateAffiliation.ts index 5d91ce2..fd1dff3 100644 --- a/src/events/special/InvestigateLoyalty/InvestigateAffiliation.ts +++ b/src/events/special/InvestigateLoyalty/InvestigateAffiliation.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const InvestigateAffiliation = (socket: Socket, data: InvestigateAffiliation) => { +import { Server, Socket } from 'socket.io'; +export const InvestigateAffiliation = (io: Server, socket: Socket, data: InvestigateAffiliation) => { console.log(`Investigating a player`); }; \ No newline at end of file diff --git a/src/events/special/SpecialElection/NextPresident.ts b/src/events/special/SpecialElection/NextPresident.ts index 55c6962..4114668 100644 --- a/src/events/special/SpecialElection/NextPresident.ts +++ b/src/events/special/SpecialElection/NextPresident.ts @@ -1,4 +1,4 @@ -import { Socket } from 'socket.io'; -export const NextPresident = (socket: Socket, data: NextPresident) => { +import { Server, Socket } from 'socket.io'; +export const NextPresident = (io: Server, socket: Socket, data: NextPresident) => { console.log(`Choosing the Next President`); }; \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index e8662be..5348f85 100644 --- a/src/main.ts +++ b/src/main.ts @@ -55,32 +55,32 @@ io.on(`connection`, (socket: sio.Socket) => { log.silly(`Client connected with id ${socket.id}`); // Game Management - socket.on(`HostGame`, (data: HostGame) => HostGame(socket, data)); - socket.on(`JoinGame`, (data: JoinGame) => JoinGame(socket, data)); + socket.on(`HostGame`, (data: HostGame) => HostGame(io, socket, data)); + socket.on(`JoinGame`, (data: JoinGame) => JoinGame(io, socket, data)); // Join a game - socket.on(`StartGame`, (data: any) => StartGame(socket, data)); + socket.on(`StartGame`, (data: any) => StartGame(io, socket, data)); // Chancellor Nominations - socket.on(`ChancellorNomination`, (data: any) => ChancellorNomination(socket, data)); + socket.on(`ChancellorNomination`, (data: any) => ChancellorNomination(io, socket, data)); socket.on(`Vote`, Vote); // Policy Receiving - socket.on(`PresidentPolicies`, (data: any) => PresidentPolicies(socket, data)); - socket.on(`ChancellorPolicy`, (data: any) => ChancellorPolicy(socket, data)); - socket.on(`VetoRequest`, (data: any) => VetoRequest(socket, data)); - socket.on(`VetoConfirm`, (data: any) => VetoConfirm(socket, data)); + socket.on(`PresidentPolicies`, (data: any) => PresidentPolicies(io, socket, data)); + socket.on(`ChancellorPolicy`, (data: any) => ChancellorPolicy(io, socket, data)); + socket.on(`VetoRequest`, (data: any) => VetoRequest(io, socket, data)); + socket.on(`VetoConfirm`, (data: any) => VetoConfirm(io, socket, data)); // Special Actions - socket.on(`ExecutePlayer`, (data: any) => ExecutePlayer(socket, data)); - socket.on(`NextPresident`, (data: any) => NextPresident(socket, data)); - socket.on(`ExecutiveConfirmation`, (data: any) => ExecutiveConfirmation(socket, data)); - socket.on(`InvestigateAffiliation`, (data: any) => InvestigateAffiliation(socket, data)); + socket.on(`ExecutePlayer`, (data: any) => ExecutePlayer(io, socket, data)); + socket.on(`NextPresident`, (data: any) => NextPresident(io, socket, data)); + socket.on(`ExecutiveConfirmation`, (data: any) => ExecutiveConfirmation(io, socket, data)); + socket.on(`InvestigateAffiliation`, (data: any) => InvestigateAffiliation(io, socket, data)); // Utility Events - socket.on(`GetPlayerList`, (data: GetPlayerList) => GetPlayerList(socket, data)); + socket.on(`GetPlayerList`, (data: GetPlayerList) => GetPlayerList(io, socket, data)); }); io.on(`reconnect_attempt`, (socket: sio.Socket) => {