Skip to content

Commit

Permalink
- remove initiator from game
Browse files Browse the repository at this point in the history
- add silentnight and silent option
  • Loading branch information
DrSkunk committed May 31, 2021
1 parent 2e246fd commit 458c7aa
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/ConversationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export async function getPlayerList(
} catch (errorCollected) {
collected = errorCollected;
}
Log.info(collected);
Log.log('Collected a reaction');
if (collected.array().length === 0) {
return [];
}
Expand Down
19 changes: 9 additions & 10 deletions src/Game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TextChannel, VoiceChannel, GuildMember, User } from 'discord.js';
import { TextChannel, VoiceChannel, GuildMember } from 'discord.js';
import {
MAXIMUM_PLAYERS,
MINIMUM_PLAYERS,
Expand Down Expand Up @@ -38,7 +38,6 @@ export class Game {
private _startTime: Date | null;
public newDoppelgangerRole: RoleName | null;
private _hasVoice: boolean;
private _initiator: User;
private _soundManager: SoundManager;
private _phase: Phase;

Expand All @@ -47,7 +46,8 @@ export class Game {
textChannel: TextChannel,
voiceChannel: VoiceChannel,
chosenRoles: RoleName[],
initiator: User
silentNight: boolean,
silent: boolean
) {
if (players.length < MINIMUM_PLAYERS || players.length > MAXIMUM_PLAYERS) {
throw new Error('Invalid amount of players');
Expand All @@ -60,8 +60,11 @@ export class Game {
this._started = false;
this._startTime = null;
this.newDoppelgangerRole = null;
this._initiator = initiator;
this._soundManager = new SoundManager(voiceChannel.guild.id);
this._soundManager = new SoundManager(
voiceChannel.guild.id,
silentNight,
silent
);
this._phase = Phase.night;

try {
Expand All @@ -70,14 +73,10 @@ export class Game {
Log.info('Joining voice channel.');
} catch (error) {
this._hasVoice = false;
Log.info('Not joining voice, already in a voice channel.');
Log.info(error.message);
}
}

public get initiator(): User {
return this._initiator;
}

public get startGameState(): GameState {
return this._startGameState;
}
Expand Down
22 changes: 10 additions & 12 deletions src/GamesManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Collection,
TextChannel,
VoiceChannel,
GuildMember,
User,
} from 'discord.js';
import { Collection, TextChannel, VoiceChannel, GuildMember } from 'discord.js';
import { CARDS_ON_TABLE } from './Constants';
import { RoleName } from './enums/RoleName';
import { Game } from './Game';
Expand All @@ -20,10 +14,11 @@ class GamesManager {
}

public quickStartGame(
initiator: User,
players: GuildMember[],
textChannel: TextChannel,
voiceChannel: VoiceChannel
voiceChannel: VoiceChannel,
silentNight: boolean,
silent: boolean
): void {
const quickStartRoles = this._quickStart.get(textChannel.id);
if (quickStartRoles === undefined) {
Expand All @@ -36,19 +31,21 @@ class GamesManager {
);
}
this.startNewGame(
initiator,
players,
textChannel,
voiceChannel,
silentNight,
silent,
quickStartRoles
);
}

public startNewGame(
initiator: User,
players: GuildMember[],
textChannel: TextChannel,
voiceChannel: VoiceChannel,
silentNight: boolean,
silent: boolean,
roleNames: RoleName[]
): void {
if (this._games.has(textChannel.id)) {
Expand All @@ -62,7 +59,8 @@ class GamesManager {
textChannel,
voiceChannel,
roleNames,
initiator
silentNight,
silent
);

this._games.set(textChannel.id, game);
Expand Down
27 changes: 23 additions & 4 deletions src/SoundManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ export class SoundManager {
private _connection: VoiceConnection | null;
private _dispatcher: StreamDispatcher | null;
private _guildId: string;
private _silentNight: boolean;
private _silent: boolean;

constructor(guildId: string) {
constructor(guildId: string, silentNight: boolean, silent: boolean) {
this._voiceChannel = null;
this._connection = null;
this._dispatcher = null;
this._guildId = guildId;
this._silentNight = silentNight;
this._silent = silent;
this.disconnectWhenEmpty();
}

Expand All @@ -23,10 +27,17 @@ export class SoundManager {
if (client.isUsingVoice(this._guildId, voiceChannel)) {
throw new Error('Already connected to a voice channel of this guild.');
}
if (this._silent) {
throw new Error('Running in silent mode.');
}
this._voiceChannel = voiceChannel;
}

public async startNightLoop(): Promise<void> {
if (this._silentNight || this._silent) {
Log.info('Silent night mode, not playing night loop');
return;
}
Log.info('Starting night loop');

if (!this._voiceChannel) {
Expand All @@ -42,11 +53,19 @@ export class SoundManager {
if (!this._voiceChannel) {
return;
}
await this.fadeOut(1);
this._dispatcher = this.play(Sound.rooster);
if (!this._silentNight && !this._silent) {
await this.fadeOut(1);
}
if (!this._silent) {
this._dispatcher = this.play(Sound.rooster);
}
}

public playGong(): void {
if (this._silent) {
Log.info('Silent night mode, not playing "time almost up" sample');
return;
}
Log.info('Playing "time almost up" sample');
this.play(Sound.gong);
}
Expand All @@ -71,7 +90,7 @@ export class SoundManager {
}
this._dispatcher.setVolume(volume);
await new Promise((resolve) => setTimeout(resolve, 100));
await this.fadeOut(volume - 0.025);
await this.fadeOut(volume - 0.05);
}

public stop(): void {
Expand Down
37 changes: 27 additions & 10 deletions src/commands/Start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@ import { getGamesManagerInstance } from '../GamesManager';
import { Log } from '../Log';
import { Command } from '../types/Command';

enum Optional {
quick = 'quick',
silentnight = 'silentnight',
silent = 'silent',
}

const command: Command = {
names: ['start'],
description:
"Start a new game. Supply the _'quick'_ option to reuse previous settings.",
description: `Start a new game. Supply the _'quick'_ option to reuse previous settings.
Supply the _'silentnight'_ option to mute the ambient night noises.
Supply the _'silent'_ option to mute all sound effects.`,
params: [
{
optional: true,
name: 'quick',
name: Optional.quick,
},
{
optional: true,
name: Optional.silentnight,
},
{
optional: true,
name: Optional.silent,
},
],
execute,
Expand All @@ -29,10 +44,10 @@ async function execute(msg: Message, args: string[]): Promise<void> {
const textChannel = msg.channel as TextChannel;
const gamesManager = getGamesManagerInstance();

let quickStart = false;
if (args[0] === 'quick') {
quickStart = true;
}
const lowerArgs = args.map((arg) => arg.toLowerCase());
const quickStart = lowerArgs.includes(Optional.quick);
const silentNight = lowerArgs.includes(Optional.silentnight);
const silent = lowerArgs.includes(Optional.silent);

const voiceChannel = msg.member?.voice.channel;
if (!voiceChannel) {
Expand Down Expand Up @@ -81,21 +96,23 @@ async function execute(msg: Message, args: string[]): Promise<void> {
}
if (quickStart) {
await gamesManager.quickStartGame(
msg.author,
players,
msg.channel as TextChannel,
voiceChannel
voiceChannel,
silentNight,
silent
);
} else {
const roles = [
...werewolves,
...(await ChooseRoles(author, textChannel, amountToPick)),
];
await gamesManager.startNewGame(
msg.author,
players,
msg.channel as TextChannel,
voiceChannel,
silentNight,
silent,
roles
);
}
Expand Down
3 changes: 2 additions & 1 deletion test/Game.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function newGame(size: number, players: Player[] = []): Game {
textChannel,
voiceChannel,
roles,
users[0].user
false,
false
);
}

Expand Down

0 comments on commit 458c7aa

Please sign in to comment.