Skip to content

Commit

Permalink
Support for Properties type 1 (dword)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gliniak committed Apr 24, 2024
1 parent 3f37516 commit 0744fa1
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/application/application.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PersistanceModule } from 'src/infrastructure/persistance/persistance.mo
import { CreatePlayerCommandHandler } from './commandHandlers/CreatePlayerCommandHandler';
import { CreateSessionCommandHandler } from './commandHandlers/CreateSessionCommandHandler';
import { AddSessionContextCommandHandler } from './commandHandlers/AddSessionContextCommandHandler';
import { AddSessionPropertyCommandHandler } from './commandHandlers/AddSessionPropertyCommandHandler';
import {
DeleteSessionCommandHandler,
DeleteSessionsCommandHandler,
Expand Down Expand Up @@ -49,6 +50,7 @@ export const commandHandlers = [
UpdateLeaderboardCommandHandler,
MigrateSessionCommandHandler,
AddSessionContextCommandHandler,
AddSessionPropertyCommandHandler,
AggregateSessionCommandHandler,
ProcessClientAddressCommandHandler,
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Inject } from '@nestjs/common';
import { ICommandHandler, CommandHandler } from '@nestjs/cqrs';
import ISessionRepository, {
ISessionRepositorySymbol,
} from 'src/domain/repositories/ISessionRepository';
import { AddSessionPropertyCommand } from '../commands/AddSessionPropertyCommand';
import { property } from 'tiny-types';

@CommandHandler(AddSessionPropertyCommand)
export class AddSessionPropertyCommandHandler
implements ICommandHandler<AddSessionPropertyCommand>
{
constructor(
@Inject(ISessionRepositorySymbol)
private repository: ISessionRepository,
) {}

async execute(command: AddSessionPropertyCommand) {
const session = await this.repository.findSession(
command.titleId,
command.sessionId,
);

if (!session) {
return undefined;
}

session.addProperty({ property: command.properties });
await this.repository.save(session);

return session;
}
}
10 changes: 10 additions & 0 deletions src/application/commands/AddSessionPropertyCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SessionId from 'src/domain/value-objects/SessionId';
import TitleId from 'src/domain/value-objects/TitleId';

export class AddSessionPropertyCommand {
constructor(
public readonly titleId: TitleId,
public readonly sessionId: SessionId,
public readonly properties: Map<number, { propertyId: number; value: number }>,
) {}
}
16 changes: 15 additions & 1 deletion src/domain/aggregates/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface SessionProps {
players: Xuid[];
deleted: boolean;
context: Map<string, number>;
properties: Map<string, number>;
migration?: SessionId;
}

Expand Down Expand Up @@ -53,7 +54,9 @@ interface CreateMigrationProps {
interface ContextProps {
context: Map<number, { contextId: number; value: number }>;
}

interface PropertyProps {
property: Map<number, { propertyId: number; value: number }>;
}
interface JoinProps {
xuids: Xuid[];
}
Expand All @@ -75,6 +78,7 @@ export default class Session {
players: [],
deleted: false,
context: new Map<string, number>(),
properties: new Map<string, number>(),
});
}

Expand Down Expand Up @@ -112,6 +116,12 @@ export default class Session {
});
}

public addProperty(props: PropertyProps) {
props.property.forEach((entry) => {
this.props.properties.set(entry.propertyId.toString(16), entry.value);
});
}

public modify(props: ModifyProps) {
this.props.flags = props.flags;
this.props.privateSlotsCount = props.privateSlotsCount;
Expand Down Expand Up @@ -213,4 +223,8 @@ export default class Session {
get context() {
return this.props.context;
}

get properties() {
return this.props.properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class SessionDomainMapper {
players: session.players.map((xuid) => new Xuid(xuid)),
deleted: session.deleted,
context: session.context,
properties: session.properties,
migration: session.migration
? new SessionId(session.migration)
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class SessionPersistanceMapper {
players: session.players.map((xuid) => xuid.value),
deleted: session.deleted,
context: session.context,
properties: session.properties,
migration: session.migration ? session.migration.value : undefined,
updatedAt,
};
Expand Down
2 changes: 2 additions & 0 deletions src/infrastructure/persistance/models/SessionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export class Session {
deleted: boolean;
@Prop({ required: true })
context: Map<string, number>;
@Prop({ required: true })
properties: Map<string, number>;
@Prop({ required: false })
migration: string;

Expand Down
44 changes: 44 additions & 0 deletions src/infrastructure/presentation/controllers/session.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ import { ModifySessionRequest } from '../requests/ModifySessionRequest';
import { JoinSessionCommand } from 'src/application/commands/JoinSessionCommand';
import { JoinSessionRequest } from '../requests/JoinSessionRequest';
import { GetSessionContextRequest } from '../requests/GetSessionContextRequest';
import { GetSessionPropertyRequest } from '../requests/GetSessionPropertyRequest';
import Xuid from 'src/domain/value-objects/Xuid';
import { SessionSearchRequest } from '../requests/SessionSearchRequest';
import { SessionDetailsResponse } from '../responses/SessionDetailsResponse';
import { LeaveSessionRequest } from '../requests/LeaveSessionRequest';
import { LeaveSessionCommand } from 'src/application/commands/LeaveSessionCommand';
import { DeleteSessionCommand } from 'src/application/commands/DeleteSessionCommand';
import { AddSessionContextCommand } from 'src/application/commands/AddSessionContextCommand';
import { AddSessionPropertyCommand } from 'src/application/commands/AddSessionPropertyCommand';
import { SessionArbitrationResponse } from '../responses/SessionArbitrationResponse';
import { SessionContextResponse } from '../responses/SessionContextResponse';
import { SessionPropertyResponse } from '../responses/SessionPropertyResponse';
import Player from 'src/domain/aggregates/Player';
import { GetPlayerQuery } from 'src/application/queries/GetPlayerQuery';
import { FindPlayerQuery } from 'src/application/queries/FindPlayerQuery';
Expand Down Expand Up @@ -491,6 +494,47 @@ export class SessionController {
};
}

@Post('/:sessionId/properties')
@ApiParam({ name: 'titleId', example: '4D5307E6' })
@ApiParam({ name: 'sessionId', example: 'B36B3FE8467CFAC7' })
async sessionPropertySet(
@Param('titleId') titleId: string,
@Param('sessionId') sessionId: string,
@Body() request: GetSessionPropertyRequest,
) {
const session = await this.commandBus.execute(
new AddSessionPropertyCommand(
new TitleId(titleId),
new SessionId(sessionId),
request.properties,
),
);

if (!session) {
throw new NotFoundException(`Session ${sessionId} was not found.`);
}
}

@Get('/:sessionId/properties')
@ApiParam({ name: 'titleId', example: '4D5307E6' })
@ApiParam({ name: 'sessionId', example: 'B36B3FE8467CFAC7' })
async sessionPropertyGet(
@Param('titleId') titleId: string,
@Param('sessionId') sessionId: string,
): Promise<SessionPropertyResponse> {
const session = await this.queryBus.execute(
new GetSessionQuery(new TitleId(titleId), new SessionId(sessionId)),
);

if (!session) {
throw new NotFoundException(`Session ${sessionId} was not found.`);
}

return {
properties: session.properties,
};
}

@Post('/:sessionId/leaderboards')
@ApiParam({ name: 'titleId', example: '4D5307E6' })
@ApiParam({ name: 'sessionId', example: 'B36B3FE8467CFAC7' })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class GetSessionPropertyRequest {
@ApiProperty()
properties: Map<number, { propertyId: number; value: number }>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface SessionPropertyResponse {
properties: Map<number, number>;
}

0 comments on commit 0744fa1

Please sign in to comment.