Skip to content

Commit

Permalink
#patch adding support for two new endpoints, updated persistent messa…
Browse files Browse the repository at this point in the history
…ge to use major order

- added types for Assignment (major order) and News Feed (in-game dispatch messages) APIs
- modified API code to fetch new endpoint data, parse it and store it in db
- added major order embed function using new endpoint
- updated persistent messages to use major order rather than most recent event
- added currency mappings
  • Loading branch information
jgaribsin committed Mar 15, 2024
1 parent 4693b77 commit 572a2a8
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
foo*
newrelic_agent.log
data.json
strippedData.json
differences.json
44 changes: 31 additions & 13 deletions src/api-wrapper/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
MergedPlanetEventData,
ApiData,
WarDifferences,
Assignment,
NewsFeedItem,
} from './types';
import {getFactionName, getPlanetEventType, getPlanetName} from './mapping';
import {existsSync, mkdirSync, writeFileSync} from 'fs';
Expand Down Expand Up @@ -65,6 +67,8 @@ export let data: ApiData = {
Terminids: 0,
},
UTCOffset: 0,
Assignment: [],
NewsFeed: [],
};

export async function getData() {
Expand All @@ -85,12 +89,7 @@ export async function getData() {
})
).data;
const warInfo = warInfoApi as WarInfo;
// const warInfoPath = path.join(
// 'api_responses',
// String(season),
// `${fileTimestamp}_WarInfo.json`
// );
// await writeGzipJson(warInfoPath + '.gz', warInfoApi);

const statusApi = await (
await axios.get(`${API_URL}/WarSeason/${season}/Status`, {
headers: {
Expand All @@ -101,12 +100,26 @@ export async function getData() {
const status = statusApi as Status;
status.timeUtc = Date.now();

// const statusPath = path.join(
// 'api_responses',
// String(season),
// `${fileTimestamp}_Status.json`
// );
// await writeGzipJson(statusPath + '.gz', statusApi);
// https://api.live.prod.thehelldiversgame.com/api/v2/Assignment/War/801

const assignmentApi = await (
await axios.get(`${API_URL}/v2/Assignment/War/${season}`, {
headers: {
'Accept-Language': 'en-us',
},
})
).data;
const assignment = assignmentApi as Assignment[];

//https://api.live.prod.thehelldiversgame.com/api/NewsFeed/801
const newsFeedApi = await (
await axios.get(`${API_URL}/NewsFeed/${season}`, {
headers: {
'Accept-Language': 'en-us',
},
})
).data;
const newsFeed = newsFeedApi as NewsFeedItem[];

const planets: MergedPlanetData[] = [];
const players = {
Expand All @@ -123,7 +136,10 @@ export async function getData() {
const planetStatus = status.planetStatus.find(p => p.index === index);
if (planetStatus) {
const {regenPerSecond} = planetStatus;
const liberation = (1 - planetStatus.health / planet.maxHealth) * 100;
const liberation = +(
(1 - planetStatus.health / planet.maxHealth) *
100
).toFixed(4);
const lossPercPerHour =
((regenPerSecond * 3600) / planet.maxHealth) * 100;
const playerPerc = (planetStatus.players / players['Total']) * 100;
Expand Down Expand Up @@ -165,6 +181,8 @@ export async function getData() {
data = {
WarInfo: warInfo,
Status: status,
Assignment: assignment,
NewsFeed: newsFeed,
Planets: planets,
Campaigns: campaigns,
PlanetEvents: planetEvents,
Expand Down
5 changes: 5 additions & 0 deletions src/api-wrapper/assignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {data} from './api';

export function getLatestAssignment() {
return data.Assignment[0];
}
2 changes: 2 additions & 0 deletions src/api-wrapper/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './mapping';
export * from './api';
export * from './assignment';
export * from './campaign';
export * from './events';
// export * from './info';
Expand Down
3 changes: 3 additions & 0 deletions src/api-wrapper/mapping/currency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"1": "Medals"
}
7 changes: 6 additions & 1 deletion src/api-wrapper/mapping/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Faction, PlanetEventType} from '../types';
import {Currency, Faction, PlanetEventType} from '../types';
import currency from './currency.json';
import factions from './factions.json';
import planetEvents from './planetEvents.json';
import planets from './planets.json';
Expand All @@ -8,6 +9,10 @@ interface JsonFile {
[key: string]: string;
}

export function getCurrencyName(id: number): Currency {
return (currency as JsonFile)[id] as Currency;
}

export function getFactionName(id: number): Faction {
return (factions as JsonFile)[id] as Faction;
}
Expand Down
40 changes: 39 additions & 1 deletion src/api-wrapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type Position = {
y: number;
};

export type Currency = 'Medals';
export type Faction = 'Humans' | 'Total' | 'Automaton' | 'Terminids';
export type PlanetEventType = 'Defend';
export type CampaignType = 'Defend' | 'Liberation';
Expand Down Expand Up @@ -172,6 +173,39 @@ export type Status = {
superEarthWarResults: any[];
};

// /api/v2/Assignment/War/{war_id}
export type Assignment = {
id32: number;
progress: number[];
expiresIn: number;
setting: {
type: number;
overrideTitle: string;
overrideBrief: string;
taskDescription: string;
tasks: {
type: number;
values: number[];
valueTypes: number[];
}[];
reward: {
type: number;
id32: number;
amount: number;
};
flags: number;
};
};

// /api/NewsFeed/{war_id}
export type NewsFeedItem = {
id: number;
published: number;
type: number;
tagIds: number[];
message: string;
};

export type WarOverview = {
warId: number;
startDate: number;
Expand All @@ -185,6 +219,8 @@ export type WarOverview = {
export type ApiData = {
WarInfo: WarInfo;
Status: Status;
Assignment: Assignment[];
NewsFeed: NewsFeedItem[];
Planets: MergedPlanetData[];
Campaigns: MergedCampaignData[];
PlanetEvents: MergedPlanetEventData[];
Expand All @@ -200,6 +236,8 @@ export type ApiData = {
export type StrippedApiData = {
WarInfo: Omit<WarInfo, 'planetInfos'>;
Status: Omit<Status, 'planetStatus'>;
Assignment: Assignment[];
NewsFeed: NewsFeedItem[];
Campaigns: MergedCampaignData[];
PlanetEvents: MergedPlanetEventData[];
ActivePlanets: MergedPlanetData[];
Expand All @@ -214,7 +252,7 @@ export type StrippedApiData = {
export type WarDifferences = {
NewCampaigns: MergedCampaignData[];
NewEvents: GlobalEvent[];
NewMajorOrder?: GlobalEvent;
NewMajorOrder?: Assignment;
WonPlanets: MergedCampaignData[];
LostPlanets: MergedCampaignData[];
Players: {
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/cron/compareData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export async function compareData(): Promise<WarDifferences | void> {
globalEvents: data.Status.globalEvents,
superEarthWarResults: data.Status.superEarthWarResults,
},
Assignment: data.Assignment,
NewsFeed: data.NewsFeed,
Campaigns: data.Campaigns,
PlanetEvents: data.PlanetEvents,
ActivePlanets: data.ActivePlanets,
Expand Down Expand Up @@ -106,6 +108,7 @@ export async function compareData(): Promise<WarDifferences | void> {

// compare old api snapshot to the new one, check for changes
// eg. new campaign, planet owner change, new event, new major order etc.
// TODO: compare major order
// compare old and new campaigns
for (const campaign of newData.Campaigns) {
const {planetName} = campaign;
Expand Down Expand Up @@ -174,7 +177,6 @@ export async function compareData(): Promise<WarDifferences | void> {
for (const event of newData.Events) {
const oldEvent = oldData.Events.find(e => e.eventId === event.eventId);
if (!oldEvent) {
if (event.flag === 0) differences.NewMajorOrder = event;
differences.NewEvents.push(event);
logger.info(`New event: ${event.title}`, {type: 'info'});
newEventUpdate(event, channelIds);
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/cron/dbData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export async function dbData() {
globalEvents: data.Status.globalEvents,
superEarthWarResults: data.Status.superEarthWarResults,
},
Assignment: data.Assignment,
NewsFeed: data.NewsFeed,
Campaigns: data.Campaigns,
PlanetEvents: data.PlanetEvents,
ActivePlanets: data.ActivePlanets,
Expand Down
17 changes: 16 additions & 1 deletion src/handlers/cron/deliverUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
Faction,
MergedCampaignData,
GlobalEvent,
Assignment,
} from '../../api-wrapper';
import {FACTION_COLOUR} from '../../commands/_components';
import {config, helldiversConfig} from '../../config';
import {planetNameTransform} from '../custom';
import {validateChannelArr} from '../discord';
import {majorOrderEmbed} from '../embed';

const {SUBSCRIBE_FOOTER} = config;
const {factionSprites, altSprites} = helldiversConfig;
Expand Down Expand Up @@ -214,9 +216,22 @@ export async function newEventUpdate(event: GlobalEvent, channelIds: string[]) {
}
// TODO: use new endpoint to get this
// export async function newMajorOrdeUpdater(order: ??, channels: (TextChannel | PublicThreadChannel)[]) {}
export async function newMajorOrderUpdater(
assignment: Assignment,
channelIds: string[]
) {
const channels = await validateChannelArr(channelIds);

const embeds = [majorOrderEmbed(assignment)];

// send new updates to subscribed channels
const promises: Promise<any>[] = [];
for (const channel of channels) promises.push(channel.send({embeds: embeds}));
await Promise.all(promises);
return;
}
// TODO: use new endpoint to get this
// export async function newMessageUpdate(message: ??, channels: (TextChannel | PublicThreadChannel)[]) {}

// LostPlanets
// NewCampaigns
// NewEvents
Expand Down
Loading

0 comments on commit 572a2a8

Please sign in to comment.