import { Context , Service as MoleculerService } from 'moleculer' ; import { Action , Method , Service } from 'moleculer-decorators' ; import { getConnection } from 'typeorm' ; import { WeaponRepository } from '@Repositories' ; import { PlanetHelper } from '@ServiceHelpers' ; import connectionInstance from '@Entities/Connection' ; import { IAttack } from '@Interfaces' ; @ Service ( { name : 'attack' , } ) class AttackService extends MoleculerService { public async started ( ) { return await connectionInstance ( ) ; } @ Action ( { params : { weaponName : { type : 'string' , min : 2 } , planetName : { type : 'string' , min : 2 } , } , } ) public async Fire ( ctx ) { const response = await this . FireMethod ( ctx ) ; return response ; } @ Method public async FireMethod ( ctx ) { const { planetName , weaponName } = ctx . params ; const weapon = await WeaponRepository . Get ( weaponName ) ; if ( weapon . ammo <= 0 ) { return { planetMessage : 'Planet took no damage' , weaponMessage : 'This weapon has no ammo' , } ; } const { remainingAmmo } = await WeaponRepository . DecreaseAmmo ( weaponName ) ; const { damage , planetMessage } = await PlanetHelper . Defend ( ctx , { weaponName , planetName , } ) ; const weaponMessage = `${ weapon . name } did ${ damage } damage and left ${ remainingAmmo } ammo.` ; return { planetMessage , weaponMessage , } ; } public async stopped ( ) { return await getConnection ( ) . close ( ) ; } } module . exports = AttackService ;