mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
Feat/refactor matchmaker promise format (#293)
This commit is contained in:
28 files changed
+2144
-3159
No files matched your search
@@ -1,8 +1,67 @@
|
||||
import { Like, MatchEvents, Profile } from '../../../typings/match';
|
||||
import {
|
||||
FormattedMatch,
|
||||
FormattedProfile,
|
||||
Like,
|
||||
MatchEvents,
|
||||
Profile,
|
||||
} from '../../../typings/match';
|
||||
import MatchService from './match.service';
|
||||
import { getSource } from '../utils/miscUtils';
|
||||
import { matchLogger } from './match.utils';
|
||||
import { config } from '../server';
|
||||
import { onNetPromise } from '../utils/PromiseNetEvents/onNetPromise';
|
||||
|
||||
onNetPromise<void, FormattedProfile[]>(MatchEvents.GET_PROFILES, (reqObj, resp) => {
|
||||
MatchService.handleGetProfiles(reqObj, resp).catch((e) => {
|
||||
matchLogger.error(
|
||||
`Error occurred in fetch profiles event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNetPromise<void, FormattedProfile>(MatchEvents.GET_MY_PROFILE, (reqObj, resp) => {
|
||||
MatchService.handleGetMyProfile(reqObj, resp).catch((e) => {
|
||||
matchLogger.error(
|
||||
`Error occurred in fetch my profile event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNetPromise<void, FormattedMatch[]>(MatchEvents.GET_MATCHES, (reqObj, resp) => {
|
||||
MatchService.handleGetMatches(reqObj, resp).catch((e) => {
|
||||
matchLogger.error(
|
||||
`Error occurred in fetch matches event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNetPromise<Like[], boolean>(MatchEvents.SAVE_LIKES, (reqObj, resp) => {
|
||||
MatchService.handleSaveLikes(reqObj, resp).catch((e) => {
|
||||
matchLogger.error(`Error occurred in save likes event (${reqObj.source}), Error: ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNetPromise<Profile, FormattedProfile>(MatchEvents.CREATE_MY_PROFILE, (reqObj, resp) => {
|
||||
MatchService.handleCreateMyProfile(reqObj, resp).catch((e) => {
|
||||
matchLogger.error(
|
||||
`Error occurred in create my profile event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNetPromise<Profile, FormattedProfile>(MatchEvents.UPDATE_MY_PROFILE, (reqObj, resp) => {
|
||||
MatchService.handleUpdateMyProfile(reqObj, resp).catch((e) => {
|
||||
matchLogger.error(
|
||||
`Error occurred in update my profile event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(MatchEvents.INITIALIZE, () => {
|
||||
const _source = getSource();
|
||||
@@ -11,34 +70,6 @@ onNet(MatchEvents.INITIALIZE, () => {
|
||||
);
|
||||
});
|
||||
|
||||
onNet(MatchEvents.CREATE_MY_PROFILE, (profile: Profile) => {
|
||||
const _source = getSource();
|
||||
MatchService.handleCreateMyProfile(_source, profile).catch((e) =>
|
||||
matchLogger.error(`Error occurred in createMyProfile event (${_source}), Error: ${e.message}`),
|
||||
);
|
||||
});
|
||||
|
||||
onNet(MatchEvents.UPDATE_MY_PROFILE, (profile: Profile) => {
|
||||
const _source = getSource();
|
||||
MatchService.handleUpdateMyProfile(_source, profile).catch((e) =>
|
||||
matchLogger.error(`Error occurred in updateMyProfile event (${_source}), Error: ${e.message}`),
|
||||
);
|
||||
});
|
||||
|
||||
onNet(MatchEvents.SAVE_LIKES, (likes: Like[]) => {
|
||||
const _source = getSource();
|
||||
MatchService.handleSaveLikes(_source, likes).catch((e) =>
|
||||
matchLogger.error(`Error occurred in saveLikes event (${_source}), Error: ${e.message}`),
|
||||
);
|
||||
});
|
||||
|
||||
onNet(MatchEvents.GET_MATCHES, () => {
|
||||
const _source = getSource();
|
||||
MatchService.handleGetMatches(_source).catch((e) =>
|
||||
matchLogger.error(`Error occurred in getMatches event (${_source}), Error: ${e.message}`),
|
||||
);
|
||||
});
|
||||
|
||||
if (!config.match.allowEditableProfileName && !config.match.generateProfileNameFromUsers) {
|
||||
const warning =
|
||||
`Both allowEdtiableProfileName and generateProfileNameFromUsers ` +
|
||||
|
||||
@@ -1,52 +1,91 @@
|
||||
import { matchLogger } from './match.utils';
|
||||
import { formatMatches, formatProfile, matchLogger } from './match.utils';
|
||||
import MatchDB, { _MatchDB } from './match.db';
|
||||
import { Like, MatchEvents, Profile } from '../../../typings/match';
|
||||
import {
|
||||
FormattedMatch,
|
||||
FormattedProfile,
|
||||
Like,
|
||||
MatchEvents,
|
||||
Profile,
|
||||
} from '../../../typings/match';
|
||||
import PlayerService from '../players/player.service';
|
||||
import { PromiseEventResp, PromiseRequest } from '../utils/PromiseNetEvents/promise.types';
|
||||
|
||||
class _MatchService {
|
||||
private readonly matchDB: _MatchDB;
|
||||
|
||||
constructor() {
|
||||
this.matchDB = MatchDB;
|
||||
matchLogger.debug('Match service started');
|
||||
}
|
||||
|
||||
async dispatchPlayerProfile(identifier: string, source: number): Promise<void> {
|
||||
async handleGetProfiles(
|
||||
reqObj: PromiseRequest<void>,
|
||||
resp: PromiseEventResp<FormattedProfile[]>,
|
||||
): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
const profile = await this.matchDB.getOrCreateProfile(identifier);
|
||||
emitNet(MatchEvents.GET_MY_PROFILE_SUCCESS, source, profile);
|
||||
const profiles = await this.dispatchProfiles(identifier);
|
||||
resp({ status: 'ok', data: profiles });
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to get player profile, ${e.message}`);
|
||||
emitNet(MatchEvents.GET_MY_PROFILE_FAILED, source, {
|
||||
message: 'APPS_MATCH_GET_MY_PROFILE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
matchLogger.error(`Error in handleGetProfiles, ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async dispatchProfiles(identifier: string, source: number): Promise<void> {
|
||||
async handleGetMyProfile(
|
||||
reqObj: PromiseRequest<void>,
|
||||
resp: PromiseEventResp<FormattedProfile>,
|
||||
): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
const profiles = await this.matchDB.getPotentialProfiles(identifier);
|
||||
emitNet(MatchEvents.GET_PROFILES_SUCCESS, source, profiles);
|
||||
const profile = await this.dispatchPlayerProfile(identifier);
|
||||
resp({ status: 'ok', data: profile });
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to retrieve profiles, ${e.message}`);
|
||||
emitNet(MatchEvents.GET_PROFILES_FAILED, source, {
|
||||
message: 'APPS_MATCH_GET_PROFILES_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
matchLogger.error(`Error in handleGetMyProfile, ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleInitialize(src: number) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
matchLogger.debug(`Initializing match for identifier: ${identifier}`);
|
||||
async handleSaveLikes(reqObj: PromiseRequest<Like[]>, resp: PromiseEventResp<boolean>) {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
matchLogger.debug(`Saving likes for identifier ${identifier}`);
|
||||
|
||||
await this.dispatchPlayerProfile(identifier, src);
|
||||
await this.dispatchProfiles(identifier, src);
|
||||
await this.matchDB.updateLastActive(identifier);
|
||||
try {
|
||||
await this.matchDB.saveLikes(identifier, reqObj.data);
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to save likes, ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
|
||||
try {
|
||||
const newMatches = await this.matchDB.findNewMatches(identifier, reqObj.data);
|
||||
if (newMatches.length > 0) {
|
||||
resp({ status: 'ok', data: true });
|
||||
}
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to find new matches, ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleCreateMyProfile(src: number, profile: Profile) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleGetMatches(reqObj: PromiseRequest<void>, resp: PromiseEventResp<FormattedMatch[]>) {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
const matchedProfiles = await this.matchDB.findAllMatches(identifier);
|
||||
const formattedMatches = matchedProfiles.map(formatMatches);
|
||||
resp({ status: 'ok', data: formattedMatches });
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to retrieve matches, ${e.message}`);
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleCreateMyProfile(
|
||||
reqObj: PromiseRequest<Profile>,
|
||||
resp: PromiseEventResp<FormattedProfile>,
|
||||
) {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
const profile = reqObj.data;
|
||||
|
||||
matchLogger.debug(`Creating profile for identifier: ${identifier}`);
|
||||
matchLogger.debug(profile);
|
||||
@@ -57,18 +96,22 @@ class _MatchService {
|
||||
}
|
||||
|
||||
const newProfile = await this.matchDB.createProfile(identifier, profile);
|
||||
emitNet(MatchEvents.CREATE_MY_PROFILE_SUCCESS, src, newProfile);
|
||||
const formattedProfile = formatProfile(newProfile);
|
||||
|
||||
resp({ status: 'ok', data: formattedProfile });
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to update profile for identifier ${identifier}, ${e.message}`);
|
||||
emitNet(MatchEvents.CREATE_MY_PROFILE_FAILED, src, {
|
||||
message: 'APPS_MATCH_CREATE_PROFILE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleUpdateMyProfile(src: number, profile: Profile) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleUpdateMyProfile(
|
||||
reqObj: PromiseRequest<Profile>,
|
||||
resp: PromiseEventResp<FormattedProfile>,
|
||||
) {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
const profile = reqObj.data;
|
||||
|
||||
matchLogger.debug(`Updating profile for identifier: ${identifier}`);
|
||||
matchLogger.debug(profile);
|
||||
|
||||
@@ -78,57 +121,38 @@ class _MatchService {
|
||||
}
|
||||
|
||||
const updatedProfile = await this.matchDB.updateProfile(identifier, profile);
|
||||
emitNet(MatchEvents.UPDATE_MY_PROFILE_SUCCESS, src, updatedProfile);
|
||||
const formattedProfile = formatProfile(updatedProfile);
|
||||
|
||||
resp({ status: 'ok', data: formattedProfile });
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to update profile for identifier ${identifier}, ${e.message}`);
|
||||
emitNet(MatchEvents.UPDATE_MY_PROFILE_FAILED, src, {
|
||||
message: 'APPS_MATCH_UPDATE_PROFILE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleSaveLikes(src: number, likes: Like[]) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
matchLogger.debug(`Saving likes for identifier ${identifier}`);
|
||||
|
||||
async dispatchPlayerProfile(identifier: string): Promise<FormattedProfile> {
|
||||
try {
|
||||
await this.matchDB.saveLikes(identifier, likes);
|
||||
const profile = await this.matchDB.getOrCreateProfile(identifier);
|
||||
return formatProfile(profile);
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to save likes, ${e.message}`);
|
||||
emitNet(MatchEvents.SAVE_LIKES_FAILED, src, {
|
||||
message: 'APPS_MATCH_SAVE_LIKES_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const newMatches = await this.matchDB.findNewMatches(identifier, likes);
|
||||
if (newMatches.length > 0) {
|
||||
emitNet(MatchEvents.NEW_MATCH, src, {
|
||||
message: 'APPS_MATCH_NEW_LIKE_FOUND',
|
||||
type: 'info',
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to find new matches, ${e.message}`);
|
||||
matchLogger.error(`Failed to get player profile, ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async handleGetMatches(src: number) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
|
||||
async dispatchProfiles(identifier: string): Promise<FormattedProfile[]> {
|
||||
try {
|
||||
const matchedProfiles = await this.matchDB.findAllMatches(identifier);
|
||||
emitNet(MatchEvents.GET_MATCHES_SUCCESS, src, matchedProfiles);
|
||||
const profiles = await this.matchDB.getPotentialProfiles(identifier);
|
||||
return profiles.map(formatProfile);
|
||||
} catch (e) {
|
||||
matchLogger.error(`Failed to retrieve matches, ${e.message}`);
|
||||
emitNet(MatchEvents.GET_MATCHES_FAILED, src, {
|
||||
message: 'APPS_MATCH_GET_MATCHES_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
matchLogger.error(`Failed to retrieve profiles, ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async handleInitialize(src: number) {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
matchLogger.debug(`Initializing match for identifier: ${identifier}`);
|
||||
await this.matchDB.updateLastActive(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
const MatchService = new _MatchService();
|
||||
|
||||
@@ -1,3 +1,23 @@
|
||||
import { mainLogger } from '../sv_logger';
|
||||
import { FormattedMatch, FormattedProfile, Match, Profile } from '../../../typings/match';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export const matchLogger = mainLogger.child({ module: 'match' });
|
||||
|
||||
export function formatProfile(profile: Profile): FormattedProfile | null {
|
||||
return {
|
||||
...profile,
|
||||
tagList: profile.tags.split(',').filter((t) => t), // remove any empty tags
|
||||
lastActiveFormatted: dayjs.unix(profile.lastActive).toString(),
|
||||
viewed: false,
|
||||
};
|
||||
}
|
||||
|
||||
export function formatMatches(match: Match): FormattedMatch | null {
|
||||
return {
|
||||
...match,
|
||||
tagList: match.tags.split(',').filter((t) => t), // remove any empty tags
|
||||
lastActiveFormatted: dayjs.unix(match.lastActive).toString(),
|
||||
matchedAtFormatted: dayjs.unix(match.matchedAt).toString(),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user