Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds option to play 9p with 4 spies #487

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/scripts/lobby/buttonEventListeners2.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,15 @@ $('#createNewRoomButton').on('click', (data) => {
newRoomPassword: $('#newRoomPassword').val(),
gameMode: $($('.gameModeSelect')[1]).val(),
muteSpectators: $('.muteSpectators')[1].checked,
rebal9p: $('.rebal9p')[1].checked,
ranked: $($('.rankedSelect')[1]).val(),
};

// Update the settings in the in room settings menu.
$($('.maxNumPlayers')[0]).val(sendObj.maxNumPlayers);
$($('.gameModeSelect')[0]).val(sendObj.gameMode);
$('.muteSpectators')[0].checked = sendObj.muteSpectators;
$('.rebal9p')[0].checked = sendObj.rebal9p;
$($('.rankedSelect')[0]).val(sendObj.ranked);

if (inRoom === false) {
Expand Down
7 changes: 7 additions & 0 deletions assets/scripts/lobby/sockets/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,13 @@ $('.muteSpectators').on('change', (e) => {
socket.emit('update-room-muteSpectators', e.target.checked);
});

$('.rebal9p').on('change', (e) => {
$('.rebal9p')[0].checked = e.target.checked;
$('.rebal9p')[1].checked = e.target.checked;

socket.emit('update-room-rebal9p', e.target.checked);
});

// Update the new room menu with the gameModes available.
socket.on('leave-room-requested', () => {
leaveRoom();
Expand Down
29 changes: 27 additions & 2 deletions src/gameplay/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function Game(
gameMode_,
muteSpectators_,
ranked_,
rebal9p_,
callback_,
) {
this.callback = callback_;
Expand Down Expand Up @@ -177,6 +178,7 @@ function Game(
this.chatHistory = []; // Here because chatHistory records after game starts

this.muteSpectators = muteSpectators_;
this.rebal9p = rebal9p_;
}

// Game object inherits all the functions and stuff from Room
Expand Down Expand Up @@ -398,6 +400,12 @@ Game.prototype.startGame = function (options) {
}

// Now we initialise roles
const alliances = this.alliances.concat();
if (this.rebal9p) {
// Swap last two elements of alliances array
[alliances[8], alliances[9]] = [alliances[9], alliances[8]];
}

for (let i = 0; i < this.socketsOfPlayers.length; i++) {
this.playersInGame[i] = {};
// assign them the sockets but with shuffled.
Expand All @@ -409,7 +417,7 @@ Game.prototype.startGame = function (options) {

// set the role to be from the roles array with index of the value
// of the rolesAssignment which has been shuffled
this.playersInGame[i].alliance = this.alliances[rolesAssignment[i]];
this.playersInGame[i].alliance = alliances[rolesAssignment[i]];

this.playerUsernamesInGame.push(
this.socketsOfPlayers[i].request.user.username,
Expand Down Expand Up @@ -1317,6 +1325,12 @@ Game.prototype.finishGame = function (toBeWinner) {
botUsernames = [];
}

let gameMode = this.gameMode;

if (this.playersInGame.length === 9 && this.rebal9p) {
gameMode = 'avalonCustom';
}

const objectToStore = {
timeGameStarted: this.startGameTime,
timeAssassinationStarted: this.startAssassinationTime,
Expand All @@ -1326,8 +1340,9 @@ Game.prototype.finishGame = function (toBeWinner) {
resistanceTeam: this.resistanceUsernames,
numberOfPlayers: this.playersInGame.length,

gameMode: this.gameMode,
gameMode,
botUsernames,
rebal9p: this.rebal9p,

playerUsernamesOrdered: getUsernamesOfPlayersInGame(this),
playerUsernamesOrderedReversed: gameReverseArray(
Expand Down Expand Up @@ -1901,6 +1916,16 @@ Game.prototype.updateMuteSpectators = function (muteSpectators: boolean) {
);
};

Game.prototype.updateRebal9p = function (rebal9p: boolean) {
this.rebal9p = rebal9p;

this.sendText(
this.allSockets,
`Rebalanced 9p option set to ${rebal9p}.`,
'server-text'
);
};

/*
ELO RATING CALCULATION:

Expand Down
2 changes: 2 additions & 0 deletions src/gameplay/gameWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function GameWrapper(
muteSpectators_,
gameMode_,
ranked_,
rebal9p_,
callback_,
) {
// Get all the game properties
Expand All @@ -24,6 +25,7 @@ function GameWrapper(
muteSpectators_,
gameMode_,
ranked_,
rebal9p_,
callback_,
);
}
Expand Down
1 change: 1 addition & 0 deletions src/gameplay/playersReadyNotReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ playersReadyNotReady.prototype.hostTryStartGame = function (options, gameMode) {

rolesInStr += `<br>Ranked: ${this.ranked}`;
rolesInStr += `<br>Mute Spectators: ${this.muteSpectators}`;
rolesInStr += `<br>Rebalanced 9p: ${this.rebal9p}`;

for (let i = 0; i < this.socketsOfPlayers.length; i++) {
this.socketsOfPlayers[i].emit('game-starting', rolesInStr, gameMode);
Expand Down
1 change: 1 addition & 0 deletions src/models/gameRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const gameRecordSchema = new mongoose.Schema({
spyTeam: [String],
resistanceTeam: [String],

rebal9p: Boolean,
gameMode: String,
botUsernames: [String],

Expand Down
9 changes: 9 additions & 0 deletions src/sockets/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ export let modCommands = {
dataObj.gameMode,
dataObj.muteSpectators,
false,
false,
socketCallback,
);
const privateStr = dataObj.newRoomPassword === '' ? '' : 'private ';
Expand Down Expand Up @@ -2126,6 +2127,7 @@ export const server = function (io: SocketServer): void {
socket.on('update-room-game-mode', updateRoomGameMode);
socket.on('update-room-ranked', updateRoomRanked);
socket.on('update-room-muteSpectators', updateRoomMuteSpectators);
socket.on('update-room-rebal9p', updateRoomRebal9p);

//* ***********************
// game data stuff
Expand Down Expand Up @@ -2762,6 +2764,7 @@ function newRoom(dataObj) {
dataObj.gameMode,
dataObj.muteSpectators,
rankedRoom,
dataObj.rebal9p,
socketCallback,
);
const privateStr = !privateRoom ? '' : 'private ';
Expand Down Expand Up @@ -2984,6 +2987,12 @@ function updateRoomMuteSpectators(muteSpectators) {
}
}

function updateRoomRebal9p(rebal9p) {
if (rooms[this.request.user.inRoomId]) {
rooms[this.request.user.inRoomId].updateRebal9p(rebal9p);
}
}

function updateRoomMaxPlayers(number) {
if (rooms[this.request.user.inRoomId]) {
rooms[this.request.user.inRoomId].updateMaxNumPlayers(this, number);
Expand Down
10 changes: 10 additions & 0 deletions src/views/lobby.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,11 @@

<label>Mute spectators for table:</label>
<input type="checkbox" name="muteSpectators" class="muteSpectators" />

<br />

<label>Rebalanced 9P (4 spies):</label>
<input type="checkbox" name="rebal9p" class="rebal9p" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Expand Down Expand Up @@ -1210,6 +1215,11 @@

<label>Mute spectators for table:</label>
<input type="checkbox" name="muteSpectators" class="muteSpectators" />

<br />

<label>Rebalanced 9p (4 spies):</label>
<input type="checkbox" name="rebal9p" class="rebal9p" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">
Expand Down