Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/improved event types #91

Merged
merged 2 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/client/census.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RestClient, RestClientOptions } from '../rest/rest.client';
import { CharacterManager, CharacterManagerOptions } from './managers';
import {
AchievementEarned,
BattleRankUpEvent,
BattleRankUp,
ContinentLock,
Death,
FacilityControl,
Expand Down Expand Up @@ -35,12 +35,12 @@ export type ClientEvents = {
error: (err: Error) => void;
warn: (err: Error) => void;
debug: (info: string) => void;
duplicate: (event: PS2Event) => void;
duplicate: (event: PS2Event<any>) => void;
subscribed: (subscription: EventSubscribed) => void;
serviceState: (worldId: string, online: boolean, detail: string) => void;
ps2Event: (event: PS2Event) => void;
ps2Event: (event: PS2Event<any>) => void;
achievementEarned: (event: AchievementEarned) => void;
battleRankUp: (event: BattleRankUpEvent) => void;
battleRankUp: (event: BattleRankUp) => void;
death: (event: Death) => void;
gainExperience: (event: GainExperience) => void;
itemAdded: (event: ItemAdded) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/client/concerns/stream-filter.contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PS2Event } from '../events/base/ps2.event';

export interface StreamFilterContract {
filter(event: PS2Event): boolean;
filter(event: PS2Event<any>): boolean;
}
9 changes: 2 additions & 7 deletions src/client/constants/ps2.constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export enum Faction {
NONE = '0',
VS = '1',
NC = '2',
TR = '3',
VS = '1',
NSO = '4',
}

Expand All @@ -14,12 +15,6 @@ export enum Loadout {
MAX,
}

export const factionMap = new Map<string, Faction>([
['1', Faction.VS],
['2', Faction.NC],
['3', Faction.TR],
]);

export const loadoutFactionMap = new Map<string, Faction>([
['1', Faction.NC],
['3', Faction.NC],
Expand Down
3 changes: 2 additions & 1 deletion src/client/events/achievement-earned.event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CharacterEvent } from './base/character.event';
import { PS2Events } from '../../stream';

export class AchievementEarned extends CharacterEvent {
export class AchievementEarned extends CharacterEvent<PS2Events.AchievementEarned> {
readonly emit = 'achievementEarned';

readonly achievement_id: string;
Expand Down
68 changes: 21 additions & 47 deletions src/client/events/base/attacker.event.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import { CharacterEvent } from './character.event';
import { CharacterEvent, CharacterEventPayload } from './character.event';
import {
Faction,
factionMap,
Loadout,
loadoutFactionMap,
loadoutTypeMap,
} from '../../constants';

export abstract class AttackerEvent extends CharacterEvent {
export type AttackerEventPayload = Extract<
CharacterEventPayload,
{
attacker_character_id: string;
attacker_team_id: string;
attacker_loadout_id: string;
attacker_vehicle_id: string;
attacker_weapon_id: string;
team_id: string;
}
>;

export abstract class AttackerEvent<
T extends AttackerEventPayload,
> extends CharacterEvent<T> {
/**
* Can be overwritten if necessary
*/
static loadoutFactionMap = loadoutFactionMap;
static loadoutTypeMap = loadoutTypeMap;
static factionMap = factionMap;

readonly attacker_character_id: string;
readonly attacker_team_id: string;
readonly attacker_team_id: Faction;
readonly attacker_loadout_id: string;
readonly attacker_vehicle_id: string;
readonly attacker_weapon_id: string;
readonly team_id: string;
readonly team_id: Faction;

/**
* Fetch the character data from the attacker
Expand All @@ -33,38 +45,16 @@ export abstract class AttackerEvent extends CharacterEvent {
return this.client.characterManager.fetch(this.attacker_character_id);
}

/**
* Team the attacker belongs to
*
* @return {Faction}
*/
get attacker_team(): Faction {
const team = AttackerEvent.factionMap.get(this.attacker_team_id);

if (team === undefined)
throw new TypeError(
`Unknown attacker_team_id when determining team: ${this.attacker_team_id}`,
);

return team;
}

/**
* Faction of the attacker
*
* @return {Faction}
*/
get attacker_faction(): Faction {
const faction = AttackerEvent.loadoutFactionMap.get(
this.attacker_loadout_id,
return (
AttackerEvent.loadoutFactionMap.get(this.attacker_loadout_id) ??
Faction.NONE
);

if (faction === undefined)
throw new TypeError(
`Unknown attacker_loadout_id when determining faction: ${this.attacker_loadout_id}`,
);

return faction;
}

/**
Expand All @@ -82,20 +72,4 @@ export abstract class AttackerEvent extends CharacterEvent {

return loadout;
}

/**
* Team the victim belongs to
*
* @return {Faction}
*/
get team(): Faction {
const team = AttackerEvent.factionMap.get(this.team_id);

if (team === undefined)
throw new TypeError(
`Unknown team_id when determining team: ${this.team_id}`,
);

return team;
}
}
10 changes: 9 additions & 1 deletion src/client/events/base/character-world.event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { PS2Event } from './ps2.event';
import { PS2Event as PS2EventPayload } from '../../../stream';

export abstract class CharacterWorldEvent extends PS2Event {
export type CharacterWorldEventPayload = Extract<
PS2EventPayload,
{ character_id: string }
>;

export abstract class CharacterWorldEvent<
T extends CharacterWorldEventPayload,
> extends PS2Event<T> {
readonly character_id: string;

/**
Expand Down
11 changes: 9 additions & 2 deletions src/client/events/base/character.event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ZoneEvent } from './zone.event';
import { ZoneEvent, ZoneEventPayload } from './zone.event';

export abstract class CharacterEvent extends ZoneEvent {
export type CharacterEventPayload = Extract<
ZoneEventPayload,
{ character_id: string }
>;

export abstract class CharacterEvent<
T extends CharacterEventPayload,
> extends ZoneEvent<T> {
readonly character_id: string;

/**
Expand Down
28 changes: 5 additions & 23 deletions src/client/events/base/ps2.event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { CensusClient, ClientEvents } from '../../census.client';
import { PS2Event as PS2EventPayload } from '../../../stream/types/ps2.events';
import { unixToDate } from '../../../utils/formatters';

export abstract class PS2Event<T extends PS2EventPayload = PS2EventPayload> {
export abstract class PS2Event<T extends PS2EventPayload> {
protected static readonly DATA = 1;

readonly emit: keyof ClientEvents;

readonly event_name: string;
readonly event_name: T['event_name'];
readonly timestamp: Date;
readonly world_id: string;

Expand All @@ -14,27 +16,7 @@ export abstract class PS2Event<T extends PS2EventPayload = PS2EventPayload> {
* @param {PS2Event} raw
*/
constructor(protected readonly client: CensusClient, readonly raw: T) {
this.hydrateObject(raw);
}

/**
* @param {PS2Event} data
* @private
*/
private hydrateObject(data: T) {
Object.assign(this, data, { timestamp: unixToDate(data.timestamp) });

this.cast(data);
}

/**
* Cast additional to formats
*
* @param data
* @protected
*/
protected cast(data: T): void {
// void
Object.assign(this, raw, { timestamp: unixToDate(raw.timestamp) });
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/client/events/base/world.event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ZoneEvent } from './zone.event';
import { ZoneEvent, ZoneEventPayload } from './zone.event';

export abstract class WorldEvent extends ZoneEvent {}
export abstract class WorldEvent<
T extends ZoneEventPayload,
> extends ZoneEvent<T> {}
7 changes: 6 additions & 1 deletion src/client/events/base/zone.event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { PS2Event } from './ps2.event';
import { PS2Event as PS2EventPayload } from '../../../stream';

export abstract class ZoneEvent extends PS2Event {
export type ZoneEventPayload = Extract<PS2EventPayload, { zone_id: string }>;

export abstract class ZoneEvent<
T extends ZoneEventPayload,
> extends PS2Event<T> {
readonly zone_id: string;

get zoneDefinitionId(): number {
Expand Down
17 changes: 15 additions & 2 deletions src/client/events/battle-rank-up.event.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { CharacterEvent } from './base/character.event';
import { PS2Events } from '../../stream';
import { PS2Event } from './base/ps2.event';

export class BattleRankUpEvent extends CharacterEvent {
export class BattleRankUp extends CharacterEvent<PS2Events.BattleRankUp> {
readonly emit = 'battleRankUp';

readonly battle_rank: string;
readonly battle_rank: number;
readonly event_name: 'BattleRankUp';

constructor(
...params: ConstructorParameters<typeof PS2Event<PS2Events.BattleRankUp>>
) {
super(...params);

this.battle_rank = Number.parseInt(
params[BattleRankUp.DATA].battle_rank,
10,
);
}

toHash(): string {
return `BattleRankUp:${this.character_id}:${this.timestamp}:${this.battle_rank}`;
}
Expand Down
37 changes: 28 additions & 9 deletions src/client/events/continent-lock.event.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { WorldEvent } from './base/world.event';
import { PS2Events } from '../../stream';
import { Faction } from '../constants';
import { PS2Event } from './base/ps2.event';

export class ContinentLock extends WorldEvent {
export class ContinentLock extends WorldEvent<PS2Events.ContinentLock> {
readonly emit = 'continentLock';

event_name: 'ContinentLock';
event_type: string;
metagame_event_id: string;
nc_population: string;
previous_faction: string;
tr_population: string;
triggering_faction: string;
vs_population: string;
readonly event_name: 'ContinentLock';
readonly event_type: string;
readonly metagame_event_id: string;
readonly nc_population: number;
readonly tr_population: number;
readonly vs_population: number;
readonly previous_faction: Faction;
readonly triggering_faction: Faction;

constructor(
...params: ConstructorParameters<typeof PS2Event<PS2Events.ContinentLock>>
) {
super(...params);

this.nc_population = Number.parseFloat(
params[ContinentLock.DATA].nc_population,
);
this.tr_population = Number.parseFloat(
params[ContinentLock.DATA].tr_population,
);
this.vs_population = Number.parseFloat(
params[ContinentLock.DATA].vs_population,
);
}

toHash(): string {
return `ContinentLock:${this.world_id}:${this.zone_id}:${this.timestamp}`;
Expand Down
31 changes: 25 additions & 6 deletions src/client/events/continent-unlock.event.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { WorldEvent } from './base/world.event';
import { PS2Events } from '../../stream';
import { Faction } from '../constants';
import { PS2Event } from './base/ps2.event';

export class ContinentUnlock extends WorldEvent {
export class ContinentUnlock extends WorldEvent<PS2Events.ContinentUnlock> {
readonly emit = 'continentUnlock';

event_name: 'ContinentUnlock';
event_type: string;
metagame_event_id: string;
nc_population: string;
previous_faction: string;
tr_population: string;
triggering_faction: string;
vs_population: string;
nc_population: number;
tr_population: number;
vs_population: number;
previous_faction: Faction;
triggering_faction: Faction;

constructor(
...params: ConstructorParameters<typeof PS2Event<PS2Events.ContinentUnlock>>
) {
super(...params);

this.nc_population = Number.parseFloat(
params[ContinentUnlock.DATA].nc_population,
);
this.tr_population = Number.parseFloat(
params[ContinentUnlock.DATA].tr_population,
);
this.vs_population = Number.parseFloat(
params[ContinentUnlock.DATA].vs_population,
);
}

toHash(): string {
return `ContinentUnlock:${this.world_id}:${this.zone_id}:${this.timestamp}`;
Expand Down
Loading