• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DaniSomoza / galactic-commander / 15029782185

14 May 2025 07:52PM UTC coverage: 47.664% (-4.4%) from 52.086%
15029782185

Pull #12

github

web-flow
Merge d58e631f3 into a8e301a23
Pull Request #12: [fleets] Explore planets

216 of 930 branches covered (23.23%)

Branch coverage included in aggregate %.

162 of 529 new or added lines in 56 files covered. (30.62%)

10 existing lines in 9 files now uncovered.

1569 of 2815 relevant lines covered (55.74%)

3.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.67
/packages/game-engine/src/engine/tasks/processFinishBuildUnitsTask.ts
1
import { IPoint } from '../../types/IPoint'
2
import { FinishBuildUnitsTaskType } from '../../types/ITask'
3
import GameEngineError from '../errors/GameEngineError'
4✔
4
import PointModel from '../../models/PointModel'
4✔
5
import { ITaskTypeDocument } from '../../models/TaskModel'
6
import playerRepository from '../../repositories/playerRepository'
4✔
7
import upgradeBonus from '../bonus/upgradeBonus'
4✔
8
import hasBonus from '../../helpers/hasBonus'
4✔
9
import getTotalAmountOfUnits from '../units/getTotalAmountOfUnits'
4✔
10
import { UnitTypes } from '../../types/IUnit'
11
import { IPlanet } from '../../types/IPlanet'
12
import createStartBuildUnitsTask from './utils/createStartBuildUnitsTask'
4✔
13
import taskRepository from '../../repositories/taskRepository'
4✔
14
import playerUnitsRepository from '../../repositories/playerUnitsRepository'
4✔
15
import getPlayerUnit from '../units/getPlayerUnit'
4✔
16
import getFirstUnitInTheBuildQueue from '../units/getFirstUnitInTheBuildQueue'
4✔
17
import PlayerUnitsModel from '../../models/PlayerUnitsModel'
4✔
18

19
async function processFinishBuildUnitsTask(
20
  task: ITaskTypeDocument<FinishBuildUnitsTaskType>,
21
  second: number
22
) {
23
  // get all the required data from DB
24
  const player = await playerRepository.findPlayerById(task.data.playerId)
×
25

26
  if (!player) {
×
27
    throw new GameEngineError('invalid player')
×
28
  }
29

30
  const planet = player.planets.colonies.find(
×
31
    (playerColony) => playerColony._id.toString() === task.data.planetId
×
32
  )
33

34
  if (!planet) {
×
35
    throw new GameEngineError('invalid planet owner')
×
36
  }
37

38
  const unit = getPlayerUnit(player, task.data.build.unitId, planet)
×
39

40
  if (!unit) {
×
41
    throw new GameEngineError('invalid unit')
×
42
  }
43

NEW
44
  const playerUnitsInThePlanet = await playerUnitsRepository.findPlayerUnitsInThePlanet(
×
45
    player._id.toString(),
46
    planet._id.toString()
47
  )
48

49
  const unitsInThePlanet =
NEW
50
    playerUnitsInThePlanet ||
×
51
    new PlayerUnitsModel({
52
      planet,
53
      player,
54
      units: []
55
    })
56

NEW
57
  const planetUnit = unitsInThePlanet.units.find(({ unit }) => unit._id.equals(unit._id))
×
58

59
  // add unit or update amount
NEW
60
  if (planetUnit) {
×
NEW
61
    planetUnit.amount += task.data.build.amount
×
62
  } else {
NEW
63
    unitsInThePlanet.units.push({
×
64
      unit,
65
      amount: task.data.build.amount
66
    })
67
  }
68

69
  // upgrade player bonus if present in the unit
70
  const hasBonusToUpdate = hasBonus(unit.bonus)
×
71

72
  if (hasBonusToUpdate) {
×
NEW
73
    const playerUnits = await playerUnitsRepository.findPlayerUnits(player._id.toString())
×
UNCOV
74
    const PlayerBonus = player.perks.find((perk) => perk.sourceId === unit._id.toString())
×
75

76
    if (PlayerBonus) {
×
NEW
77
      const totalAmountOfUnits = getTotalAmountOfUnits(playerUnits, unit)
×
NEW
78
      PlayerBonus.bonus = upgradeBonus(unit.bonus, totalAmountOfUnits + task.data.build.amount)
×
79
    } else {
80
      player.perks.push({
×
81
        bonus: upgradeBonus(unit.bonus, task.data.build.amount),
82
        sourceId: unit._id.toString(),
83
        sourceName: unit.name,
84
        type: 'Unit'
85
      })
86
    }
87
  }
88

89
  const point: IPoint = {
×
90
    playerId: player._id.toString(),
91
    taskId: task._id.toString(),
92
    points: task.data.build.resourceCost,
93
    sourceId: task.data.build.unitId,
94
    sourceName: unit.name,
95
    type: 'Unit',
96
    second
97
  }
98

99
  const points = new PointModel(point)
×
100

101
  const buildUnitsType: Record<UnitTypes, keyof IPlanet['unitBuild']> = {
×
102
    TROOP: 'troops',
103
    SPACESHIP: 'spaceships',
104
    DEFENSE: 'defenses'
105
  }
106

107
  planet.unitBuild[buildUnitsType[unit.type]].activeBuild = undefined
×
108

109
  const nextBuildUnits = planet.unitBuild[buildUnitsType[unit.type]].queue.shift()
×
110
  const nextUnitInTheQueue = getFirstUnitInTheBuildQueue(player, planet, nextBuildUnits)
×
111

112
  if (nextUnitInTheQueue && nextBuildUnits) {
×
113
    const buildUnitsTask = createStartBuildUnitsTask(
×
114
      task.universeId,
115
      player._id.toString(),
116
      planet._id.toString(),
117
      nextUnitInTheQueue._id.toString(),
118
      nextBuildUnits.amount
119
    )
120

121
    return Promise.all([
×
122
      player.save(),
123
      unitsInThePlanet.save(),
124
      points.save(),
125
      planet.save(),
126
      taskRepository.createStartBuildUnitsTask(buildUnitsTask)
127
    ])
128
  }
129

NEW
130
  return Promise.all([player.save(), unitsInThePlanet.save(), points.save(), planet.save()])
×
131
}
132

133
export default processFinishBuildUnitsTask
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc