import { Context , Service as MoleculerService } from 'moleculer' ; import { Action , Method , Service } from 'moleculer-decorators' ; import { getConnection } from 'typeorm' ; import { PlanetRepository , WeaponRepository } from '@Repositories' ; import { CalculateMeta } from '@Meta' ; import { Planet , Weapon } from '@Entities' ; import connectionInstance from '@Entities/Connection' ; import { IPlanet } from '@Interfaces' ; @ Service ( { name : 'planet' , } ) class PlanetService extends MoleculerService { public async started ( ) { return await connectionInstance ( ) ; } @ Action ( { params : { weaponName : { type : 'string' , min : 2 } , planetName : { type : 'string' , min : 2 } , } , } ) public async Defend ( ctx ) { const response = await this . DefendMethod ( ctx ) ; return response ; } @ Method public async DefendMethod ( ctx ) { const { planetName , weaponName } = ctx . params ; const planet = await PlanetRepository . Get ( planetName ) ; const weapon = await WeaponRepository . Get ( weaponName ) ; const { damage , remainingShield } = await CalculateMeta . Damage ( weapon , planet ) ; await PlanetRepository . DecreaseShield ( planetName , remainingShield ) ; let message ; if ( remainingShield > 0 ) { message = `Planet took ${ damage } damage and has ${ remainingShield } shield left.` ; } else { message = 'Planet shield ruined! war is lost!' ; } return { damage , planetMessage : message } ; } public async stopped ( ) { return await getConnection ( ) . close ( ) ; } } module . exports = PlanetService ;