Skip to content
Permalink
Browse files
Add full player details when syncing player-count-change
  • Loading branch information
jkelin committed May 14, 2020
1 parent 453aa14 commit 5a7937954b1dd9068b9d375e2cbcbdfdbe98a352
Showing 5 changed files with 28 additions and 22 deletions.
@@ -14,6 +14,7 @@
"128": "favicon.png"
}
},
"permissions": ["idle"],
"background": {
"scripts": ["lib/lodash.4.17.15.js", "lib/socket.io.2.3.0.dev.js", "ticker.js"]
}
@@ -84,7 +84,7 @@ function initSocketIo() {

socket.on('connect', function () {
console.info('Connected');
socket.send('get-player-count');
socket.emit('get-player-count');
});

socket.on('full-player-count', function (data) {
@@ -94,12 +94,12 @@ function initSocketIo() {

socket.on('reconnect', () => {
console.info('Reconnected');
socket.send('get-player-count');
socket.emit('get-player-count');
});

browser_idle.onStateChanged.addListener(() => {
console.info('Idle state changed');
socket.send('get-player-count');
socket.emit('get-player-count');
});
}

@@ -2,6 +2,8 @@ import { Server } from 'http';
import SocketIO from 'socket.io';
import Events from './events';
import { getServerPlayerCount } from './servers';
import { emptyDir } from 'fs-extra';
import { IPlayerCountChangeMessage } from './types';

export function initSocketIO(server: Server) {
const io = SocketIO(server, { origins: '*:*' });
@@ -23,8 +25,16 @@ export function initSocketIO(server: Server) {

Events.filter((x) => x.type === 'chat-message').subscribe((e) => io.emit(e.type, e.data));
Events.filter((x) => x.type === 'player-count-change').subscribe(async (e) => {
io.emit(e.type, e.data);
io.emit('full-player-count', await getServerPlayerCount());
const data = e.data as IPlayerCountChangeMessage;
io.emit(e.type, { ...data, players: data.players.length });

const dbPlayers = await getServerPlayerCount();
dbPlayers.forEach((x) => {
if (x.id === data.server) {
x.players = data.players;
}
});
io.emit('full-player-count', dbPlayers);
});

Events.subscribe((e) => console.info('EVENT:', e));
@@ -69,7 +69,7 @@ export async function handleTribesServerData(data: ITribesServerQueryResponse) {
type: 'player-count-change',
data: {
server: `${server.ip}:${server.port}`,
players: data.players.length,
players: data.players,
origin: selfEventId,
},
});
@@ -1,7 +1,5 @@
export interface IFullReportPlayer extends Record<string, number | string> {
ip: string;
export interface IPlayerData {
name: string;
team: string;
score: number;
kills: number;
deaths: number;
@@ -10,6 +8,10 @@ export interface IFullReportPlayer extends Record<string, number | string> {
style: number;
}

export type IFullReportPlayer = (IPlayerData & Record<string, number | string>) & {
ip: string;
};

export interface IFullReport {
players: IFullReportPlayer[];
}
@@ -51,7 +53,7 @@ export interface IChatMessage {

export interface IPlayerCountChangeMessage {
server: string;
players: number;
players: IUploadedPlayer[];
origin: string;
}

@@ -61,18 +63,11 @@ export interface INews {
date: Date;
}

export interface IUploadedPlayer extends Record<string, undefined | string | number | boolean> {
ip?: string;
name: string;
score: number;
kills: number;
deaths: number;
offense: number;
defense: number;
style: number;

isUntracked?: boolean;
}
export type IUploadedPlayer = IPlayerData &
Record<string, undefined | string | number | boolean> & {
ip?: string;
isUntracked?: boolean;
};

export interface IUploadedData {
players: IUploadedPlayer[];

0 comments on commit 5a79379

Please sign in to comment.