Merge pull request #166 from 11TStudio/getSquad

Store squad list and add squad information as sub-object in player information
This commit is contained in:
Thomas Smyth 2021-03-07 20:51:54 +00:00 committed by GitHub
commit fe248a8880
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 4 deletions

View File

@ -42,6 +42,10 @@ export default class SquadServer extends EventEmitter {
this.updatePlayerListInterval = 30 * 1000; this.updatePlayerListInterval = 30 * 1000;
this.updatePlayerListTimeout = null; this.updatePlayerListTimeout = null;
this.updateSquadList = this.updateSquadList.bind(this);
this.updateSquadListInterval = 30 * 1000;
this.updateSquadListTimeout = null;
this.updateLayerInformation = this.updateLayerInformation.bind(this); this.updateLayerInformation = this.updateLayerInformation.bind(this);
this.updateLayerInformationInterval = 30 * 1000; this.updateLayerInformationInterval = 30 * 1000;
this.updateLayerInformationTimeout = null; this.updateLayerInformationTimeout = null;
@ -69,6 +73,7 @@ export default class SquadServer extends EventEmitter {
await this.rcon.connect(); await this.rcon.connect();
await this.logParser.watch(); await this.logParser.watch();
await this.updateSquadList();
await this.updatePlayerList(); await this.updatePlayerList();
await this.updateLayerInformation(); await this.updateLayerInformation();
await this.updateA2SInformation(); await this.updateA2SInformation();
@ -300,10 +305,15 @@ export default class SquadServer extends EventEmitter {
oldPlayerInfo[player.steamID] = player; oldPlayerInfo[player.steamID] = player;
} }
this.players = (await this.rcon.getListPlayers()).map((player) => ({ const players = [];
...oldPlayerInfo[player.steamID], for (const player of await this.rcon.getListPlayers())
...player players.push({
})); ...oldPlayerInfo[player.steamID],
...player,
squad: await this.getSquadByID(player.teamID, player.squadID)
});
this.players = players;
for (const player of this.players) { for (const player of this.players) {
if (typeof oldPlayerInfo[player.steamID] === 'undefined') continue; if (typeof oldPlayerInfo[player.steamID] === 'undefined') continue;
@ -331,6 +341,22 @@ export default class SquadServer extends EventEmitter {
this.updatePlayerListTimeout = setTimeout(this.updatePlayerList, this.updatePlayerListInterval); this.updatePlayerListTimeout = setTimeout(this.updatePlayerList, this.updatePlayerListInterval);
} }
async updateSquadList() {
if (this.updateSquadListTimeout) clearTimeout(this.updateSquadListTimeout);
Logger.verbose('SquadServer', 1, `Updating squad list...`);
try {
this.squads = await this.rcon.getSquads();
} catch (err) {
Logger.verbose('SquadServer', 1, 'Failed to update squad list.', err);
}
Logger.verbose('SquadServer', 1, `Updated squad list.`);
this.updateSquadListTimeout = setTimeout(this.updateSquadList, this.updateSquadListInterval);
}
async updateLayerInformation() { async updateLayerInformation() {
if (this.updateLayerInformationTimeout) clearTimeout(this.updateLayerInformationTimeout); if (this.updateLayerInformationTimeout) clearTimeout(this.updateLayerInformationTimeout);
@ -422,6 +448,31 @@ export default class SquadServer extends EventEmitter {
return null; return null;
} }
async getSquadByCondition(condition, forceUpdate = false, retry = true) {
let matches;
if (!forceUpdate) {
matches = this.squads.filter(condition);
if (matches.length === 1) return matches[0];
if (!retry) return null;
}
await this.updateSquadList();
matches = this.squads.filter(condition);
if (matches.length === 1) return matches[0];
return null;
}
async getSquadByID(teamID, squadID) {
if (squadID === null) return null;
return this.getSquadByCondition(
(squad) => squad.teamID === teamID && squad.squadID === squadID
);
}
async getPlayerBySteamID(steamID, forceUpdate) { async getPlayerBySteamID(steamID, forceUpdate) {
return this.getPlayerByCondition((player) => player.steamID === steamID, forceUpdate); return this.getPlayerByCondition((player) => player.steamID === steamID, forceUpdate);
} }

View File

@ -88,6 +88,36 @@ export default class SquadRcon extends Rcon {
return players; return players;
} }
async getSquads() {
const responseSquad = await this.execute('ListSquads');
const squads = [];
let teamName;
let teamID;
for (const line of responseSquad.split('\n')) {
const match = line.match(
/ID: ([0-9]+) \| Name: (.+) \| Size: ([0-9]+) \| Locked: (True|False)/
);
const matchSide = line.match(/Team ID: (1|2) \((.+)\)/);
if (matchSide) {
teamID = matchSide[1];
teamName = matchSide[2];
}
if (!match) continue;
await squads.push({
squadID: match[1],
squadName: match[2],
size: match[3],
locked: match[4],
teamID: teamID,
teamName: teamName
});
}
return squads;
}
async broadcast(message) { async broadcast(message) {
await this.execute(`AdminBroadcast ${message}`); await this.execute(`AdminBroadcast ${message}`);
} }