Skip to content

Commit

Permalink
Add full player details when syncing player-count-change
Browse files Browse the repository at this point in the history
  • Loading branch information
jkelin committed May 14, 2020
1 parent 453aa14 commit 5a79379
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions browser-extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
Expand Down
6 changes: 3 additions & 3 deletions browser-extension/ticker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
});
}

Expand Down
14 changes: 12 additions & 2 deletions src/socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '*:*' });
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Expand Down
27 changes: 11 additions & 16 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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[];
}
Expand Down Expand Up @@ -51,7 +53,7 @@ export interface IChatMessage {

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

Expand All @@ -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[];
Expand Down

0 comments on commit 5a79379

Please sign in to comment.