Skip to content

Commit

Permalink
Fix counting room players (including bots), allow users to replace bo…
Browse files Browse the repository at this point in the history
…ts. Fixes #70
  • Loading branch information
Luís Leite committed Mar 17, 2019
1 parent b371b4c commit 457c8fb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/packets/out/room/updatesettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class OutRoomUpdateSettings {
const botEnabled: number = this.settings.areBotsEnabled as unknown as number
outPacket.writeUInt8(botEnabled)

if (botEnabled === 1) {
if (this.settings.areBotsEnabled === true) {
outPacket.writeUInt8(this.settings.botDifficulty)
outPacket.writeUInt8(this.settings.numCtBots)
outPacket.writeUInt8(this.settings.numTrBots)
Expand Down
40 changes: 35 additions & 5 deletions src/room/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class Room {
*/
public getNumOfPlayers(): number {
const realPlayers: number = this.users.length
const botPlayers: number = this.settings.numCtBots + this.settings.numTrBots
const botPlayers: number = this.getNumOfBotCts() + this.getNumOfBotTerrorists()
return realPlayers + botPlayers
}

Expand All @@ -174,6 +174,23 @@ export class Room {
* @returns the free player slots
*/
public getFreeSlots(): number {
if (this.settings.areBotsEnabled === true) {
const hostTeam: RoomTeamNum = this.getUserTeam(this.host)

let botsInHostTeam: number = 0
let humansInHostTeam: number = 0

if (hostTeam === RoomTeamNum.CounterTerrorist) {
botsInHostTeam = this.getNumOfBotCts()
humansInHostTeam = this.getNumOfRealCts()
} else if (hostTeam === RoomTeamNum.Terrorist) {
botsInHostTeam = this.getNumOfBotTerrorists()
humansInHostTeam = this.getNumOfRealTerrorists()
}

return botsInHostTeam - humansInHostTeam
}

const availableSlots: number = this.settings.maxPlayers - this.getNumOfPlayers()
return availableSlots >= 0 ? availableSlots : 0
}
Expand Down Expand Up @@ -324,6 +341,20 @@ export class Room {
return numTerrorists
}

/**
* @returns the ammount of bots in the counter terrorist team
*/
public getNumOfBotCts(): number {
return this.settings.numCtBots - this.getNumOfRealCts()
}

/**
* @returns the ammount of bots in the terrorist team
*/
public getNumOfBotTerrorists(): number {
return this.settings.numTrBots - this.getNumOfRealTerrorists()
}

/**
* gives the number of players (bots included) that are ready
* @returns the num of players ready
Expand Down Expand Up @@ -743,7 +774,7 @@ export class Room {
if (newSettings.botEnabled != null) {
this.updateBotEnabled(newSettings.botEnabled, newSettings.botDifficulty,
newSettings.numCtBots, newSettings.numTrBots)
updatedSet.areBotsEnabled = newSettings.botEnabled as unknown as boolean
updatedSet.areBotsEnabled = !!newSettings.botEnabled
updatedSet.botDifficulty = newSettings.botDifficulty
updatedSet.numCtBots = newSettings.numCtBots
updatedSet.numTrBots = newSettings.numTrBots
Expand Down Expand Up @@ -1061,10 +1092,9 @@ export class Room {
*/
private updateBotEnabled(botEnabled: number, botDifficulty: number,
ctBots: number, terBots: number): void {
const isBotEnabled: boolean = botEnabled as unknown as boolean
this.settings.areBotsEnabled = isBotEnabled
this.settings.areBotsEnabled = !!botEnabled

if (isBotEnabled) {
if (this.settings.areBotsEnabled) {
this.settings.botDifficulty = botDifficulty
this.settings.numCtBots = ctBots
this.settings.numTrBots = terBots
Expand Down

0 comments on commit 457c8fb

Please sign in to comment.