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

DaniSomoza / galactic-commander / 12695419504

09 Jan 2025 05:35PM UTC coverage: 47.774% (-4.3%) from 52.086%
12695419504

Pull #12

github

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

214 of 917 branches covered (23.34%)

Branch coverage included in aggregate %.

118 of 418 new or added lines in 39 files covered. (28.23%)

4 existing lines in 3 files now uncovered.

1535 of 2744 relevant lines covered (55.94%)

3.49 hits per line

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

15.28
/packages/game-engine/src/engine/tasks/processStartFleetTask.ts
1
import FleetModel from '../../models/FleetModel'
4✔
2
import getTaskModel, { ITaskTypeDocument } from '../../models/TaskModel'
4✔
3
import planetRepository from '../../repositories/planetRepository'
4✔
4
import playerRepository from '../../repositories/playerRepository'
4✔
5
import {
4✔
6
  FINISH_FLEET_TASK_TYPE,
7
  FinishFleetTaskType,
8
  ITask,
9
  PENDING_TASK_STATUS,
10
  StartFleetTaskType
11
} from '../../types/ITask'
12
import GameEngineError from '../errors/GameEngineError'
4✔
13
import getAmountOfTroops from '../fleets/getAmountOfTroops'
4✔
14
import getFleetDuration from '../fleets/getFleetDuration'
4✔
15
import getFleetResourceCapacity from '../fleets/getFleetResourceCapacity'
4✔
16
import getFleetTroopsCapacity from '../fleets/getFleetTroopsCapacity'
4✔
17

18
async function processStartFleetTask(task: ITaskTypeDocument<StartFleetTaskType>, second: number) {
19
  // get all the required data from DB
NEW
20
  const player = await playerRepository.findPlayerById(task.data.playerId)
×
NEW
21
  const fromPlanet = await planetRepository.findPlanetById(task.data.fromPlanetId)
×
NEW
22
  const toPlanet = await planetRepository.findPlanetById(task.data.toPlanetId)
×
23

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

NEW
28
  if (!fromPlanet) {
×
NEW
29
    throw new GameEngineError('invalid from planet')
×
30
  }
31

NEW
32
  if (!toPlanet) {
×
NEW
33
    throw new GameEngineError('invalid to planet')
×
34
  }
35

NEW
36
  if (toPlanet._id.equals(fromPlanet._id)) {
×
NEW
37
    throw new GameEngineError('invalid planet')
×
38
  }
39

40
  // TODO: check number of active player fleets
41

NEW
42
  const playerUnitsInThePlanet = player.fleets.find(
×
NEW
43
    (fleet) => fleet.planet._id.equals(fromPlanet._id) && !fleet.travel
×
44
  )
45

NEW
46
  if (!playerUnitsInThePlanet) {
×
NEW
47
    throw new GameEngineError('invalid units in the planet')
×
48
  }
49

50
  // units present in the planet and no defenses present
NEW
51
  const isInvalidFleetUnits = task.data.units.some(({ unit, amount }) => {
×
NEW
52
    const planetUnit = playerUnitsInThePlanet.units.find(
×
NEW
53
      (planetUnit) => planetUnit.unit.name === unit.name
×
54
    )
55

NEW
56
    return !planetUnit || amount > planetUnit.amount || unit.type === 'DEFENSE'
×
57
  })
58

NEW
59
  if (isInvalidFleetUnits) {
×
NEW
60
    throw new GameEngineError('invalid units in the planet')
×
61
  }
62

63
  // check troops capacity
NEW
64
  const troopsCapacity = getFleetTroopsCapacity(task.data.units)
×
NEW
65
  const amountOfTroops = getAmountOfTroops(task.data.units)
×
66

NEW
67
  if (amountOfTroops > troopsCapacity) {
×
NEW
68
    throw new GameEngineError('invalid amount of troops in the fleet')
×
69
  }
70

71
  // check resources
NEW
72
  if (task.data.resources > fromPlanet.resources) {
×
NEW
73
    throw new GameEngineError('invalid resources')
×
74
  }
75

NEW
76
  const fleetResourceCapacity = getFleetResourceCapacity(task.data.units)
×
77

NEW
78
  if (task.data.resources > fleetResourceCapacity) {
×
NEW
79
    throw new GameEngineError('invalid fleet resources capacity')
×
80
  }
81

NEW
82
  if (task.data.fleetType === 'EXPLORE_FLEET_TYPE') {
×
NEW
83
    const executeTaskAt = second + getFleetDuration(fromPlanet, toPlanet, task.data.units, player)
×
84

NEW
85
    task.data.units.forEach((fleetUnit) => {
×
NEW
86
      const planetUnit = playerUnitsInThePlanet.units.find(
×
NEW
87
        ({ unit }) => unit.name === fleetUnit.unit.name
×
88
      )
89

NEW
90
      planetUnit!.amount -= fleetUnit.amount
×
91
    })
92

NEW
93
    const newFleet = new FleetModel({
×
94
      planet: fromPlanet,
95
      playerId: player._id,
96
      units: task.data.units,
97
      travel: {
98
        destination: toPlanet,
99
        arriveAt: executeTaskAt,
100
        fleetType: task.data.fleetType,
101
        isReturning: false,
102
        resources: task.data.resources
103
      }
104
    })
105

106
    // TODO: implement createBaseTask helper function
NEW
107
    const finishBuildUnitsTask: ITask<FinishFleetTaskType> = {
×
108
      type: FINISH_FLEET_TASK_TYPE,
109
      universeId: player.universeId,
110
      data: {
111
        playerId: player._id.toString(),
112
        fromPlanetId: fromPlanet._id.toString(),
113
        toPlanetId: toPlanet._id.toString(),
114
        units: task.data.units,
115
        resources: task.data.resources,
116
        fleetType: task.data.fleetType,
117
        allUnitsInThePlanet: task.data.allUnitsInThePlanet,
118
        allResourcesInThePlanet: task.data.allResourcesInThePlanet,
119
        isReturning: false,
120
        arriveAt: executeTaskAt,
121
        fleetId: newFleet._id.toString()
122
      },
123
      status: PENDING_TASK_STATUS,
124
      isCancellable: true,
125
      executeTaskAt,
126
      processedAt: null,
127
      processingDuration: null,
128
      history: [
129
        {
130
          taskStatus: PENDING_TASK_STATUS,
131
          updatedAt: new Date().getTime()
132
        }
133
      ],
134
      errorDetails: null
135
    }
NEW
136
    const taskModel = getTaskModel<FinishFleetTaskType>()
×
NEW
137
    const newTask = new taskModel(finishBuildUnitsTask)
×
138

NEW
139
    if (newFleet.travel) {
×
NEW
140
      newFleet.travel.taskId = newTask._id.toString()
×
141
    }
142

NEW
143
    player.fleets.push(newFleet)
×
144

NEW
145
    return Promise.all([
×
146
      newTask.save(),
147
      player.save(),
148
      playerUnitsInThePlanet.save(),
149
      newFleet.save(),
150
      fromPlanet.save()
151
    ])
152
  }
153

NEW
154
  throw 'fleet type not implemented'
×
155
}
156

157
export default processStartFleetTask
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