• 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

10.81
/packages/game-engine/src/engine/tasks/processFinishFleetTask.ts
1
import getTaskModel, { ITaskTypeDocument } from '../../models/TaskModel'
4✔
2
import planetRepository from '../../repositories/planetRepository'
4✔
3
import playerRepository from '../../repositories/playerRepository'
4✔
4
import unitRepository from '../../repositories/unitRepository'
4✔
5
import {
4✔
6
  FINISH_FLEET_TASK_TYPE,
7
  FinishFleetTaskType,
8
  ITask,
9
  PENDING_TASK_STATUS
10
} from '../../types/ITask'
11
import GameEngineError from '../errors/GameEngineError'
4✔
12
import getFleetDuration from '../fleets/getFleetDuration'
4✔
13

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

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

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

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

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

NEW
39
  if (task.data.fleetType === 'EXPLORE_FLEET_TYPE') {
×
NEW
40
    const currentFleet = player.fleets.find(
×
NEW
41
      (playerFleet) => playerFleet._id.toString() === task.data.fleetId
×
42
    )
43

NEW
44
    if (!currentFleet) {
×
NEW
45
      throw new GameEngineError('invalid fleet')
×
46
    }
47

NEW
48
    if (task.data.isReturning) {
×
NEW
49
      const playerUnitsInThePlanet = player.fleets.find(
×
NEW
50
        (fleet) => fleet.planet._id.equals(fromPlanet._id) && !fleet.travel
×
51
      )
52

NEW
53
      if (!playerUnitsInThePlanet) {
×
NEW
54
        throw new GameEngineError('invalid units in the planet')
×
55
      }
56

NEW
57
      const units = await unitRepository.findUnits()
×
58

59
      // restore units in the fleet planet
NEW
60
      for (let i = 0; i < task.data.units.length; i++) {
×
NEW
61
        const fleetUnit = task.data.units[i]
×
62

NEW
63
        const planetUnit = playerUnitsInThePlanet.units.find(
×
NEW
64
          (planetFleet) => planetFleet.unit.name === fleetUnit.unit.name
×
65
        )
66

NEW
67
        if (planetUnit) {
×
NEW
68
          planetUnit.amount += fleetUnit.amount
×
69
        } else {
NEW
70
          const unit = units.find((unit) => unit.name === fleetUnit.unit.name)
×
71

NEW
72
          if (!unit) {
×
NEW
73
            throw new GameEngineError('invalid fleet unit')
×
74
          }
75

NEW
76
          playerUnitsInThePlanet.units.push({
×
77
            unit,
78
            amount: fleetUnit.amount
79
          })
80
        }
81
      }
82

NEW
83
      player.fleets = player.fleets.filter((fleet) => !fleet._id.equals(currentFleet._id))
×
84

NEW
85
      return Promise.all([currentFleet.deleteOne(), playerUnitsInThePlanet.save(), player.save()])
×
86
    }
87

NEW
88
    const executeTaskAt = second + getFleetDuration(toPlanet, fromPlanet, task.data.units, player)
×
89

90
    // create returning fleet
NEW
91
    currentFleet.planet = toPlanet
×
NEW
92
    currentFleet.travel!.destination = fromPlanet
×
NEW
93
    currentFleet.travel!.arriveAt = executeTaskAt
×
NEW
94
    currentFleet.travel!.isReturning = true
×
95

NEW
96
    toPlanet.isExplored = true
×
97

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

NEW
131
    if (currentFleet.travel) {
×
NEW
132
      currentFleet.travel.taskId = newTask._id.toString()
×
133
    }
134

135
    // TODO: create exploration report
136

NEW
137
    const isAlreadyExplored = toPlanet.exploredBy.some(
×
NEW
138
      (exploredPlayer) => exploredPlayer === player._id.toString()
×
139
    )
140

NEW
141
    if (!isAlreadyExplored) {
×
NEW
142
      toPlanet.exploredBy.push(player._id.toString())
×
143
    }
144

NEW
145
    return Promise.all([newTask.save(), currentFleet.save(), player.save(), toPlanet.save()])
×
146
  }
147

NEW
148
  throw 'fleet type not implemented'
×
149
}
150

151
export default processFinishFleetTask
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