Skip to content
This repository has been archived by the owner on Mar 23, 2022. It is now read-only.

Commit

Permalink
ADD - Manual unit name reference (#149)
Browse files Browse the repository at this point in the history
Signed-off-by: RaenonX <[email protected]>
  • Loading branch information
RaenonX committed Jun 23, 2021
1 parent f2a62b0 commit 2aa9ad1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/api-def
34 changes: 28 additions & 6 deletions src/utils/services/api/requestSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
QuestPostPublishResponse,
RequestPayloadBase,
SupportedLanguages,
UnitNameRefPayload,
UnitNameRefResponse,
UnitType,
} from '../../../api-def/api';
import {getFullApiUrl} from './utils';
Expand Down Expand Up @@ -141,7 +143,7 @@ export class ApiRequestSender {
* Send a character analysis post publish request.
*
* @param {CharaAnalysisPublishPayload} payload payload of a character analysis post
* @return {Promise<QuestPostIdCheckResponse>} promise returned from `fetch`
* @return {Promise<AnalysisPublishResponse>} promise returned from `fetch`
*/
static analysisPublishChara(
payload: CharaAnalysisPublishPayload,
Expand Down Expand Up @@ -174,7 +176,7 @@ export class ApiRequestSender {
*
* @param {string} uid user ID to get the analysis lookup
* @param {SupportedLanguages} lang language to use for getting the analysis info
* @return {Promise<QuestPostIdCheckResponse>} promise returned from `fetch`
* @return {Promise<AnalysisLookupResponse>} promise returned from `fetch`
*/
static analysisLookup(uid: string, lang: SupportedLanguages): Promise<AnalysisLookupResponse> {
return ApiRequestSender.sendRequest<AnalysisLookupResponse, AnalysisLookupPayload>(
Expand All @@ -189,7 +191,7 @@ export class ApiRequestSender {
*
* @param {string} uid user ID to get the analysis lookup
* @param {SupportedLanguages} lang language to use for getting the analysis info
* @return {Promise<QuestPostIdCheckResponse>} promise returned from `fetch`
* @return {Promise<AnalysisLookupLandingResponse>} promise returned from `fetch`
*/
static analysisLookupLanding(uid: string, lang: SupportedLanguages): Promise<AnalysisLookupLandingResponse> {
return ApiRequestSender.sendRequest<AnalysisLookupLandingResponse, AnalysisLookupLandingPayload>(
Expand Down Expand Up @@ -289,7 +291,7 @@ export class ApiRequestSender {
* @param {SupportedLanguages} lang post language
* @param {PostType} postType type of the post
* @param {number} pid post ID
* @return {Promise<AnalysisIdCheckResponse | FailedResponse>} promise returned from `fetch`
* @return {Promise<PostPageMetaResponse | FailedResponse>} promise returned from `fetch`
*/
static getPostMeta(uid: string, lang: SupportedLanguages, postType: PostType, pid: number) {
return ApiRequestSender.sendRequest<PostPageMetaResponse | FailedResponse, PostPageMetaPayload>(
Expand All @@ -307,9 +309,9 @@ export class ApiRequestSender {
/**
* Send a request to get the generic page meta.
*
* @param {string} uid UID
* @param {string} uid User ID
* @param {SupportedLanguages} lang current page language
* @return {Promise<AnalysisIdCheckResponse | FailedResponse>} promise returned from `fetch`
* @return {Promise<PageMetaResponse | FailedResponse>} promise returned from `fetch`
*/
static getPageMeta(uid: string, lang: SupportedLanguages) {
return ApiRequestSender.sendRequest<PageMetaResponse | FailedResponse, PageMetaPayload>(
Expand All @@ -321,6 +323,26 @@ export class ApiRequestSender {

// endregion

// region Data

/**
* Send a request to get all unit name references.
*
* Note that this request is always anonymous (UID sent is an empty string).
*
* @param {SupportedLanguages} lang language of the name
* @return {Promise<UnitNameRefResponse>} promise returned from `fetch`
*/
static getUnitNameReferences(lang: SupportedLanguages) {
return ApiRequestSender.sendRequest<UnitNameRefResponse, UnitNameRefPayload>(
'GET',
ApiEndPoints.DATA_UNIT_NAME_REF,
{uid: '', lang},
);
}

// endregion

/**
* Base method to send an API request.
*
Expand Down
31 changes: 29 additions & 2 deletions src/utils/services/resources/unitInfo/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {SupportedLanguages, UnitType} from '../../../../api-def/api';
import {ApiResponseCode, SupportedLanguages, UnitNameRefResponse, UnitType} from '../../../../api-def/api';
import {CharaInfoData, DragonInfoData, Element, Weapon} from '../../../../api-def/resources';
import {ApiRequestSender} from '../../api/requestSender';
import {ResourceLoader} from '../loader';
import {getUnitNameInfoMap} from './utils';


describe('Unit info utils', () => {
let fnGetCharaInfo: jest.SpyInstance;
let fnGetDragonInfo: jest.SpyInstance;
let fnGetUnitNameRef: jest.SpyInstance;

const charaInfo: CharaInfoData = {
id: 10950101,
Expand Down Expand Up @@ -56,15 +58,25 @@ describe('Unit info utils', () => {
releaseEpoch: 0,
};

const unitNameRefResponse: UnitNameRefResponse = {
code: ApiResponseCode.SUCCESS,
success: true,
data: {
'G!Leonidas': 10950101,
'Non-existent': 30707007,
},
};

beforeEach(() => {
fnGetCharaInfo = jest.spyOn(ResourceLoader, 'getCharacterInfo').mockResolvedValue([charaInfo]);
fnGetDragonInfo = jest.spyOn(ResourceLoader, 'getDragonInfo').mockResolvedValue([dragonInfo]);
fnGetUnitNameRef = jest.spyOn(ApiRequestSender, 'getUnitNameReferences').mockResolvedValue(unitNameRefResponse);
});

it('returns unit info name map', async () => {
const nameIdMap = await getUnitNameInfoMap(SupportedLanguages.EN);

expect(nameIdMap.size).toBe(2);
expect(nameIdMap.size).toBe(3);
expect(nameIdMap.get('CHARA EN')).toStrictEqual({...charaInfo, type: UnitType.CHARACTER});
expect(nameIdMap.get('CHARA JP')).toBeUndefined();
expect(nameIdMap.get('DRAGON EN')).toStrictEqual({...dragonInfo, type: UnitType.DRAGON});
Expand All @@ -76,6 +88,7 @@ describe('Unit info utils', () => {

expect(fnGetCharaInfo).not.toHaveBeenCalledTimes(2);
expect(fnGetDragonInfo).not.toHaveBeenCalledTimes(2);
expect(fnGetUnitNameRef).not.toHaveBeenCalledTimes(2);
});

it('does not overwrite the cached map in different language', async () => {
Expand All @@ -87,11 +100,25 @@ describe('Unit info utils', () => {

expect(fnGetCharaInfo).toHaveBeenCalled();
expect(fnGetDragonInfo).toHaveBeenCalled();
expect(fnGetUnitNameRef).toHaveBeenCalled();

fnGetCharaInfo.mockClear();
fnGetDragonInfo.mockClear();
fnGetUnitNameRef.mockClear();
await getUnitNameInfoMap(SupportedLanguages.EN);
expect(fnGetCharaInfo).not.toHaveBeenCalled();
expect(fnGetDragonInfo).not.toHaveBeenCalled();
expect(fnGetUnitNameRef).not.toHaveBeenCalled();
});

it('merges with unit name references', async () => {
const nameIdMap = await getUnitNameInfoMap(SupportedLanguages.EN);

expect(nameIdMap.size).toBe(3);
expect(nameIdMap.get('CHARA EN')).toStrictEqual({...charaInfo, type: UnitType.CHARACTER});
expect(nameIdMap.get('G!Leonidas')).toStrictEqual({...charaInfo, type: UnitType.CHARACTER});
expect(nameIdMap.get('Non-existent')).toBeUndefined();
expect(nameIdMap.get('CHARA JP')).toBeUndefined();
expect(nameIdMap.get('DRAGON EN')).toStrictEqual({...dragonInfo, type: UnitType.DRAGON});
});
});
22 changes: 19 additions & 3 deletions src/utils/services/resources/unitInfo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import {SupportedLanguages, UnitType} from '../../../../api-def/api';
import {DepotPaths} from '../../../../api-def/resources/paths';
import {UnitInfoData, UnitInfoMap} from '../../../../api-def/resources/types/unitInfo';
import {toUnitInfoMap} from '../../../../api-def/resources/utils/unitInfo';
import {ApiRequestSender} from '../../api/requestSender';
import {ResourceLoader} from '../loader';


const cache: {[lang in SupportedLanguages]?: UnitInfoMap<string>} = {};
const cache: { [lang in SupportedLanguages]?: UnitInfoMap<string> } = {};

export const getUnitNameInfoMap = async (lang: SupportedLanguages): Promise<UnitInfoMap<string>> => {
if (typeof window === 'undefined') {
Expand All @@ -19,9 +20,24 @@ export const getUnitNameInfoMap = async (lang: SupportedLanguages): Promise<Unit
return nameInfoMap;
}

const unitInfo = await Promise.all([ResourceLoader.getCharacterInfo(), ResourceLoader.getDragonInfo()]);
const [charaInfo, dragonInfo, unitNameRef] = await Promise.all([
ResourceLoader.getCharacterInfo(),
ResourceLoader.getDragonInfo(),
ApiRequestSender.getUnitNameReferences(lang),
]);

const unitInfoMap = toUnitInfoMap(charaInfo, dragonInfo, (info) => info.name[lang]);
const unitInfoIdMap = toUnitInfoMap(charaInfo, dragonInfo, (info) => info.id);
Object.entries(unitNameRef.data).forEach(([name, unitId]) => {
const unitInfo = unitInfoIdMap.get(unitId);
if (!unitInfo) {
return;
}
unitInfoMap.set(name, unitInfo);
});

nameInfoMap = cache[lang] = unitInfoMap;

nameInfoMap = cache[lang] = toUnitInfoMap(unitInfo[0], unitInfo[1], (info) => info.name[lang]);
return nameInfoMap;
};

Expand Down

0 comments on commit 2aa9ad1

Please sign in to comment.