Skip to content

Commit

Permalink
test(e2e): add GameServerSimulator.connectAllPlayers()
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc committed Aug 1, 2024
1 parent 4be4b12 commit e78c47d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/20-game/01-free-players.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ launchGame(
}),
)

await gameServer.connectAllPlayers()
gameServer.log('World triggered "Round_Start"')
await waitABit(secondsToMilliseconds(10))
gameServer.log('World triggered "Game_Over" reason "Reached Win Limit"')
Expand Down
40 changes: 40 additions & 0 deletions tests/game-server-simulator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { format } from 'date-fns'
import { createSocket, type Socket } from 'dgram'
import { Server } from 'net'
import SteamID from 'steamid'
import { waitABit } from './utils/wait-a-bit'

const RconPacketType = {
SERVERDATA_AUTH: 3,
Expand Down Expand Up @@ -59,6 +61,15 @@ class CVar {
}
}

class AddedPlayer {
constructor(
public readonly steamId64: string,
public readonly name: string,
public readonly team: string,
public readonly gameClass: string,
) {}
}

export class GameServerSimulator {
private readonly socket: Socket
private server: Server
Expand All @@ -70,6 +81,7 @@ export class GameServerSimulator {
new CVar('tv_port', '27020', 'Host SourceTV port'),
new CVar('tv_password', 'tv', 'SourceTV password for all clients'),
]
private addedPlayers: AddedPlayer[] = []

constructor(
readonly apiAddress: string,
Expand Down Expand Up @@ -120,6 +132,20 @@ export class GameServerSimulator {
response.id = packet.id
response.body = `logaddress_add ${address}`
socket.write(response.toBuffer())
} else if (/sm_game_player_add .+/.test(packet.body)) {
const [, steamId64, name, team, gameClass] =
packet.body.match(
/^sm_game_player_add (\d{17}) -name "(.+)" -team (blu|red) -class (.+)$/,
) ?? []
if (steamId64 && name && team && gameClass) {
this.addedPlayers.push(new AddedPlayer(steamId64, name, team, gameClass))
}

const response = new RconPacket()
response.type = RconPacketType.SERVERDATA_RESPONSE_VALUE
response.id = packet.id
response.body = packet.body
socket.write(response.toBuffer())
} else {
const response = new RconPacket()
response.type = RconPacketType.SERVERDATA_RESPONSE_VALUE
Expand Down Expand Up @@ -177,6 +203,20 @@ export class GameServerSimulator {
})
}

async connectAllPlayers() {
const eventDelay = 100
let i = 1
for (const player of this.addedPlayers) {
const team = player.team === 'blu' ? 'Blue' : 'Red'
const steamId3 = new SteamID(player.steamId64).steam3()
this.log(`"${player.name}<${i}><${steamId3}><>" connected, address "127.0.0.1:27005"`)
await waitABit(eventDelay)
this.log(`"${player.name}<${i}><${steamId3}><Unassigned>" joined team "${team}"`)
await waitABit(eventDelay)
i += 1
}
}

async sendHeartbeat() {
const params = new URLSearchParams()
params.set('name', 'Simulated Game Server')
Expand Down

0 comments on commit e78c47d

Please sign in to comment.