Files
rush-character-archive/vault/src/character_service.ts
2025-06-19 23:34:08 -07:00

89 lines
2.3 KiB
TypeScript

import { Server, ServerUnaryCall, sendUnaryData, StatusBuilder } from '@grpc/grpc-js'
import { ReflectionService } from '@grpc/reflection'
import { loadSync } from '@grpc/proto-loader'
import { MongoClient, Collection, Db } from 'mongodb'
import { DatabaseService } from './database'
import {
Character,
GetCharacterRequest,
CreateCharacterRequest,
ListCharacterRequest,
ListCharacterResponse,
} from './proto/character'
import {
characterManagerDefinition,
ICharacterManager,
} from './proto/character.grpc-server'
const testCharacter: Character = {
playerName: 'test player',
characterName: 'test character',
characterAlias: ['test alias'],
version: 'test version',
sourceTable: 'source table',
json: 'test json',
}
export class CharacterService {
reflectionService: ReflectionService
characterManager: ICharacterManager
databaseService: DatabaseService
constructor(databaseService: DatabaseService) {
this.reflectionService = new ReflectionService(
loadSync('./src/proto/character.proto'),
)
this.characterManager = {
createCharacter: this.createCharacterCall.bind(this),
getCharacter: this.getCharacterCall.bind(this),
listCharacters: this.listCharactersCall.bind(this),
}
this.databaseService = databaseService
}
addToServer(server: Server) {
server.addService(characterManagerDefinition, this.characterManager)
this.reflectionService.addToServer(server)
}
async createCharacterCall(
call: ServerUnaryCall<CreateCharacterRequest, Character>,
callback: sendUnaryData<Character>,
) {
let newCharacter = await this.databaseService.insertCharacter(
call.request.characterData,
)
console.log(newCharacter)
callback(null, newCharacter)
}
getCharacterCall(
call: ServerUnaryCall<GetCharacterRequest, Character>,
callback: sendUnaryData<Character>,
) {
callback(null, testCharacter)
}
async listCharactersCall(
call: ServerUnaryCall<ListCharacterRequest, ListCharacterResponse>,
callback: sendUnaryData<ListCharacterResponse>,
) {
// let characters = await this.databaseService.listCharacters(
// call.request.playerName,
// call.request.characterName,
// )
console.log("potato")
callback(new StatusBuilder().withDetails("fek").build(), {
characters: [],
})
}
}