Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Changes for match-ticket, match-ticket-assignment services hook refactor #8991

Merged
merged 6 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,15 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
Ethereal Engine. All Rights Reserved.
*/

import { NotFound } from '@feathersjs/errors'
import { Id, Params } from '@feathersjs/feathers'
import { KnexAdapter, KnexAdapterOptions } from '@feathersjs/knex/lib'
import { Params } from '@feathersjs/feathers'
import { KnexService } from '@feathersjs/knex/lib'

import { getTicketsAssignment } from '@etherealengine/matchmaking/src/functions'
import {
MatchTicketAssignmentQuery,
MatchTicketAssignmentType
} from '@etherealengine/matchmaking/src/match-ticket-assignment.schema'
import config from '@etherealengine/server-core/src/appconfig'

import { identityProviderPath } from '@etherealengine/engine/src/schemas/user/identity-provider.schema'
import { KnexAdapterParams } from '@feathersjs/knex'
import { Application } from '../../../declarations'
import { emulate_getTicketsAssignment } from '../emulate'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MatchTicketAssignmentParams extends KnexAdapterParams<MatchTicketAssignmentQuery> {}
Expand All @@ -48,27 +42,4 @@ export interface MatchTicketAssignmentParams extends KnexAdapterParams<MatchTick
export class MatchTicketAssignmentService<
T = MatchTicketAssignmentType,
ServiceParams extends Params = MatchTicketAssignmentParams
> extends KnexAdapter<MatchTicketAssignmentType, MatchTicketAssignmentParams> {
app: Application

constructor(options: KnexAdapterOptions, app: Application) {
super(options)
this.app = app
}

async get(id: Id, params: MatchTicketAssignmentParams) {
let assignment: MatchTicketAssignmentType
try {
if (config.server.matchmakerEmulationMode) {
assignment = await emulate_getTicketsAssignment(this.app, id, params[identityProviderPath].userId)
} else {
assignment = await getTicketsAssignment(String(id))
}
} catch (e) {
// todo: handle other errors. like no connection, etc....
throw new NotFound(e.message, e)
}

return assignment
}
}
> extends KnexService<MatchTicketAssignmentType, MatchTicketAssignmentParams> {}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,45 @@ Ethereal Engine. All Rights Reserved.
import { hooks as schemaHooks } from '@feathersjs/schema'
import { disallow } from 'feathers-hooks-common'

import { matchTicketAssignmentQueryValidator } from '@etherealengine/matchmaking/src/match-ticket-assignment.schema'
import {
MatchTicketAssignmentType,
matchTicketAssignmentQueryValidator
} from '@etherealengine/matchmaking/src/match-ticket-assignment.schema'
import linkMatchUserToMatch from '@etherealengine/server-core/src/hooks/matchmaking-link-match-user-to-match'

import { identityProviderPath } from '@etherealengine/engine/src/schemas/user/identity-provider.schema'
import { getTicketsAssignment } from '@etherealengine/matchmaking/src/functions'
import { NotFound } from '@feathersjs/errors'
import { HookContext } from '../../../declarations'
import config from '../../appconfig'
import { emulate_getTicketsAssignment } from '../emulate'
import { MatchTicketAssignmentService } from './match-ticket-assignment.class'
import {
matchTicketAssignmentExternalResolver,
matchTicketAssignmentQueryResolver,
matchTicketAssignmentResolver
} from './match-ticket-assignment.resolvers'

const getTicketAssigment = async (context: HookContext<MatchTicketAssignmentService>) => {
let assignment: MatchTicketAssignmentType
try {
if (config.server.matchmakerEmulationMode) {
assignment = await emulate_getTicketsAssignment(
context.service,
context.id,
context.params[identityProviderPath].userId
)
} else {
assignment = await getTicketsAssignment(String(context.id))
}
} catch (e) {
// todo: handle other errors. like no connection, etc....
throw new NotFound(e.message, e)
}

context.result = assignment
}

export default {
around: {
all: [
Expand All @@ -49,7 +79,7 @@ export default {
schemaHooks.resolveQuery(matchTicketAssignmentQueryResolver)
],
find: [],
get: [],
get: [getTicketAssigment],
create: [disallow()],
update: [disallow()],
patch: [disallow()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default (app: Application): void => {
multi: true
}

app.use(matchTicketAssignmentPath, new MatchTicketAssignmentService(options, app), {
app.use(matchTicketAssignmentPath, new MatchTicketAssignmentService(options), {
// A list of all methods this service exposes externally
methods: matchTicketAssignmentMethods,
// You can add additional custom events to be sent to clients here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
Ethereal Engine. All Rights Reserved.
*/

import { BadRequest, NotFound } from '@feathersjs/errors'
import { Id, Params } from '@feathersjs/feathers'
import { KnexAdapter, KnexAdapterOptions } from '@feathersjs/knex/lib'
import { Params } from '@feathersjs/feathers'
import { KnexService } from '@feathersjs/knex/lib'

import { createTicket, deleteTicket, getTicket } from '@etherealengine/matchmaking/src/functions'
import { MatchTicketData, MatchTicketQuery, MatchTicketType } from '@etherealengine/matchmaking/src/match-ticket.schema'
import config from '@etherealengine/server-core/src/appconfig'

import { KnexAdapterParams } from '@feathersjs/knex'
import { Application } from '../../../declarations'
import { emulate_createTicket, emulate_getTicket } from '../emulate'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface MatchTicketParams extends KnexAdapterParams<MatchTicketQuery> {}
Expand All @@ -44,47 +39,4 @@ export interface MatchTicketParams extends KnexAdapterParams<MatchTicketQuery> {
export class MatchTicketService<
T = MatchTicketType,
ServiceParams extends Params = MatchTicketParams
> extends KnexAdapter<MatchTicketType, MatchTicketData, MatchTicketParams> {
app: Application

constructor(options: KnexAdapterOptions, app: Application) {
super(options)
this.app = app
}

async get(id: Id, params: MatchTicketParams): Promise<MatchTicketType> {
if (typeof id !== 'string' || id.length === 0) {
throw new BadRequest('Invalid ticket id, not empty string is expected')
}

let ticket
if (config.server.matchmakerEmulationMode) {
// emulate response from open-match-api
ticket = await emulate_getTicket(this.app, id, params.user!.id)
} else {
ticket = getTicket(String(id))
}

if (!ticket) {
throw new NotFound()
}
return ticket as MatchTicketType
}

async create(data: MatchTicketData) {
if (config.server.matchmakerEmulationMode) {
// emulate response from open-match-api
return emulate_createTicket(data.gameMode)
}

return await createTicket(data.gameMode, data.attributes)
}

async remove(id: Id) {
// skip delete in emulation, user-match will be deleted in hook
if (!config.server.matchmakerEmulationMode) {
await deleteTicket(String(id))
}
return { id }
}
}
> extends KnexService<MatchTicketType, MatchTicketData, MatchTicketParams> {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { hooks as schemaHooks } from '@feathersjs/schema'
import { disallow, iff, isProvider } from 'feathers-hooks-common'

import {
MatchTicketData,
MatchTicketType,
matchTicketDataValidator,
matchTicketQueryValidator
} from '@etherealengine/matchmaking/src/match-ticket.schema'
Expand All @@ -35,13 +37,67 @@ import matchmakingRestrictMultipleQueueing from '@etherealengine/server-core/src
import matchmakingSaveTicket from '@etherealengine/server-core/src/hooks/matchmaking-save-ticket'
import setLoggedInUser from '@etherealengine/server-core/src/hooks/set-loggedin-user-in-body'

import { createTicket, deleteTicket, getTicket } from '@etherealengine/matchmaking/src/functions'
import { BadRequest, NotFound } from '@feathersjs/errors'
import { HookContext } from '../../../declarations'
import config from '../../appconfig'
import { emulate_createTicket, emulate_getTicket } from '../emulate'
import { MatchTicketService } from './match-ticket.class'
import {
matchTicketDataResolver,
matchTicketExternalResolver,
matchTicketQueryResolver,
matchTicketResolver
} from './match-ticket.resolvers'

const ensureId = async (context: HookContext<MatchTicketService>) => {
if (typeof context.id !== 'string' || context.id.length === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use disallow-non-id.ts. Search for disallowNonId in our code

throw new BadRequest('Invalid ticket id, not empty string is expected')
}
}

const getEmulationTicket = async (context: HookContext<MatchTicketService>) => {
let ticket
if (config.server.matchmakerEmulationMode) {
// emulate response from open-match-api
ticket = await emulate_getTicket(context.service, context.id, context.params.user!.id)
} else {
ticket = getTicket(String(context.id!))
}

if (!ticket) {
throw new NotFound()
}
context.result = ticket as MatchTicketType
}

const createInEmulation = async (context: HookContext<MatchTicketService>) => {
if (!context.data || context.method !== 'create') {
throw new BadRequest(`${context.path} service only works for data in ${context.method}`)
}

const data: MatchTicketData[] = Array.isArray(context.data) ? context.data : [context.data]
const result: MatchTicketType[] = []

for (const item of data) {
if (config.server.matchmakerEmulationMode) {
// emulate response from open-match-api
return emulate_createTicket(item.gameMode)
}

result.push(await createTicket(item.gameMode, item.attributes))
}
context.result = result
}

const skipDeleteInEmulation = async (context: HookContext<MatchTicketService>) => {
if (!config.server.matchmakerEmulationMode) {
await deleteTicket(String(context.id))
}

context.result = undefined
}

export default {
around: {
all: [schemaHooks.resolveExternal(matchTicketExternalResolver), schemaHooks.resolveResult(matchTicketResolver)]
Expand All @@ -53,17 +109,18 @@ export default {
schemaHooks.resolveQuery(matchTicketQueryResolver)
],
find: [],
get: [iff(isProvider('external'), setLoggedInUser('userId') as any)],
get: [iff(isProvider('external'), setLoggedInUser('userId') as any), ensureId, getEmulationTicket],
create: [
iff(isProvider('external'), setLoggedInUser('userId') as any),
matchmakingRestrictMultipleQueueing(),
// addUUID(),
() => schemaHooks.validateData(matchTicketDataValidator),
schemaHooks.resolveData(matchTicketDataResolver)
schemaHooks.resolveData(matchTicketDataResolver),
createInEmulation
],
update: [disallow()],
patch: [disallow()],
remove: [iff(isProvider('external'))]
remove: [iff(isProvider('external')), skipDeleteInEmulation]
},

after: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default (app: Application): void => {
multi: true
}

app.use(matchTicketPath, new MatchTicketService(options, app), {
app.use(matchTicketPath, new MatchTicketService(options), {
// A list of all methods this service exposes externally
methods: matchTicketMethods,
// You can add additional custom events to be sent to clients here
Expand Down
Loading