mirror of
https://github.com/AsgardEternal/SquadJS.git
synced 2024-09-28 14:04:25 -05:00
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:
commit
fe248a8880
@ -42,6 +42,10 @@ export default class SquadServer extends EventEmitter {
|
||||
this.updatePlayerListInterval = 30 * 1000;
|
||||
this.updatePlayerListTimeout = null;
|
||||
|
||||
this.updateSquadList = this.updateSquadList.bind(this);
|
||||
this.updateSquadListInterval = 30 * 1000;
|
||||
this.updateSquadListTimeout = null;
|
||||
|
||||
this.updateLayerInformation = this.updateLayerInformation.bind(this);
|
||||
this.updateLayerInformationInterval = 30 * 1000;
|
||||
this.updateLayerInformationTimeout = null;
|
||||
@ -69,6 +73,7 @@ export default class SquadServer extends EventEmitter {
|
||||
await this.rcon.connect();
|
||||
await this.logParser.watch();
|
||||
|
||||
await this.updateSquadList();
|
||||
await this.updatePlayerList();
|
||||
await this.updateLayerInformation();
|
||||
await this.updateA2SInformation();
|
||||
@ -300,10 +305,15 @@ export default class SquadServer extends EventEmitter {
|
||||
oldPlayerInfo[player.steamID] = player;
|
||||
}
|
||||
|
||||
this.players = (await this.rcon.getListPlayers()).map((player) => ({
|
||||
...oldPlayerInfo[player.steamID],
|
||||
...player
|
||||
}));
|
||||
const players = [];
|
||||
for (const player of await this.rcon.getListPlayers())
|
||||
players.push({
|
||||
...oldPlayerInfo[player.steamID],
|
||||
...player,
|
||||
squad: await this.getSquadByID(player.teamID, player.squadID)
|
||||
});
|
||||
|
||||
this.players = players;
|
||||
|
||||
for (const player of this.players) {
|
||||
if (typeof oldPlayerInfo[player.steamID] === 'undefined') continue;
|
||||
@ -331,6 +341,22 @@ export default class SquadServer extends EventEmitter {
|
||||
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() {
|
||||
if (this.updateLayerInformationTimeout) clearTimeout(this.updateLayerInformationTimeout);
|
||||
|
||||
@ -422,6 +448,31 @@ export default class SquadServer extends EventEmitter {
|
||||
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) {
|
||||
return this.getPlayerByCondition((player) => player.steamID === steamID, forceUpdate);
|
||||
}
|
||||
|
@ -88,6 +88,36 @@ export default class SquadRcon extends Rcon {
|
||||
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) {
|
||||
await this.execute(`AdminBroadcast ${message}`);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user