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

DaniSomoza / galactic-commander / 12457139241

22 Dec 2024 07:38PM UTC coverage: 52.086% (-13.5%) from 65.587%
12457139241

push

github

web-flow
Build units (#11)

* update models in game-engine

* added fleets

* add build unit service and troops page

* added build units dialog

* added build units queue

206 of 790 branches covered (26.08%)

Branch coverage included in aggregate %.

366 of 893 new or added lines in 84 files covered. (40.99%)

10 existing lines in 7 files now uncovered.

1417 of 2326 relevant lines covered (60.92%)

3.83 hits per line

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

18.75
/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 FleetModel from '../../models/FleetModel'
4✔
9
import hasBonus from '../../helpers/hasBonus'
4✔
10
import getTotalAmountOfUnits from '../units/getTotalAmountOfUnits'
4✔
11
import { UnitTypes } from '../../types/IUnit'
12
import { IPlanet } from '../../types/IPlanet'
13
import createStartBuildUnitsTask from './utils/createStartBuildUnitsTask'
4✔
14
import taskRepository from '../../repositories/taskRepository'
4✔
15
import getPlayerUnit from '../units/getPlayerUnit'
4✔
16
import getFirstUnitInTheBuildQueue from '../units/getFirstUnitInTheBuildQueue'
4✔
17

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

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

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

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

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

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

43
  // planet fleet (no travel defined)
NEW
44
  const existingPlanetFleet = player.fleets.find(
×
NEW
45
    (fleet) => fleet.planet._id.equals(planet._id) && !fleet.travel
×
46
  )
47

48
  const planetFleet =
NEW
49
    existingPlanetFleet ||
×
50
    new FleetModel({
51
      planet,
52
      playerId: player._id,
53
      units: [
54
        {
55
          unit,
56
          amount: task.data.build.amount
57
        }
58
      ]
59
    })
60

61
  // update player fleets with the new fleet
NEW
62
  if (!existingPlanetFleet) {
×
NEW
63
    player.fleets.push(planetFleet)
×
64
  }
65

NEW
66
  const fleetUnit = planetFleet.units.find((fleetUnit) => fleetUnit.unit._id.equals(unit._id))
×
67

NEW
68
  if (fleetUnit) {
×
NEW
69
    fleetUnit.amount += task.data.build.amount
×
70
  } else {
NEW
71
    planetFleet.units.push({
×
72
      unit,
73
      amount: task.data.build.amount
74
    })
75
  }
76

77
  // upgrade player bonus if present in the unit
NEW
78
  const hasBonusToUpdate = hasBonus(unit.bonus)
×
79

NEW
80
  if (hasBonusToUpdate) {
×
NEW
81
    const PlayerBonus = player.perks.find((perk) => perk.sourceId === unit._id.toString())
×
82

NEW
83
    if (PlayerBonus) {
×
NEW
84
      const totalAmountOfUnits = getTotalAmountOfUnits(player, unit)
×
NEW
85
      PlayerBonus.bonus = upgradeBonus(unit.bonus, totalAmountOfUnits)
×
86
    } else {
NEW
87
      player.perks.push({
×
88
        bonus: unit.bonus,
89
        sourceId: unit._id.toString(),
90
        sourceName: unit.name,
91
        type: 'Unit'
92
      })
93
    }
94
  }
95

NEW
96
  const point: IPoint = {
×
97
    playerId: player._id.toString(),
98
    taskId: task._id.toString(),
99
    points: task.data.build.resourceCost,
100
    sourceId: task.data.build.unitId,
101
    sourceName: unit.name,
102
    type: 'Unit',
103
    second
104
  }
105

NEW
106
  const points = new PointModel(point)
×
107

NEW
108
  const buildUnitsType: Record<UnitTypes, keyof IPlanet['unitBuild']> = {
×
109
    TROOP: 'troops',
110
    SPACESHIP: 'spaceships',
111
    DEFENSE: 'defenses'
112
  }
113

NEW
114
  planet.unitBuild[buildUnitsType[unit.type]].activeBuild = undefined
×
115

NEW
116
  const nextBuildUnits = planet.unitBuild[buildUnitsType[unit.type]].queue.shift()
×
NEW
117
  const nextUnitInTheQueue = getFirstUnitInTheBuildQueue(player, planet, nextBuildUnits)
×
118

NEW
119
  if (nextUnitInTheQueue && nextBuildUnits) {
×
NEW
120
    const buildUnitsTask = createStartBuildUnitsTask(
×
121
      task.universeId,
122
      player._id.toString(),
123
      planet._id.toString(),
124
      nextUnitInTheQueue._id.toString(),
125
      nextBuildUnits.amount
126
    )
127

NEW
128
    return Promise.all([
×
129
      player.save(),
130
      planetFleet.save(),
131
      points.save(),
132
      planet.save(),
133
      taskRepository.createStartBuildUnitsTask(buildUnitsTask)
134
    ])
135
  }
136

NEW
137
  return Promise.all([player.save(), planetFleet.save(), points.save(), planet.save()])
×
138
}
139

140
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