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

DaniSomoza / galactic-commander / 11940541664

20 Nov 2024 07:39PM UTC coverage: 65.587% (-31.2%) from 96.824%
11940541664

push

github

web-flow
Frontend initial implementation and player perks feature (#10)

* added vite react-ts template

* Add register and login pages

* Add initial frontend implementation

* Add first iteration of research queue

* first version of the research page

* added assets

* Added pirate research assets

* Added translation support

* updated planet images

* added first version of the bonus

181 of 418 branches covered (43.3%)

Branch coverage included in aggregate %.

532 of 983 new or added lines in 110 files covered. (54.12%)

8 existing lines in 4 files now uncovered.

1176 of 1651 relevant lines covered (71.23%)

4.75 hits per line

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

95.95
/packages/game-engine/src/engine/tasks/processFinishResearchTask.ts
1
import { ITaskTypeDocument, FinishResearchTaskType } from '../../models/TaskModel'
2
import playerRepository from '../../repositories/playerRepository'
4✔
3
import GameEngineError from '../errors/GameEngineError'
4✔
4
import addPoints from '../points/addPoints'
4✔
5
import { IRace } from '../../models/RaceModel'
6
import upgradeResearchBonus from '../bonus/upgradeResearchBonus'
4✔
7
import { IBonus } from '../../types/bonus'
8
import createStartResearchTask from './utils/createStartResearchTask'
4✔
9
import taskRepository from '../../repositories/taskRepository'
4✔
10

11
async function processFinishResearchTask(
12
  task: ITaskTypeDocument<FinishResearchTaskType>,
13
  second: number
14
) {
15
  // get all the required data from DB
16
  const player = await playerRepository.findPlayerById(task.data.player)
11✔
17

18
  if (!player) {
11✔
19
    throw new GameEngineError('invalid player')
1✔
20
  }
21

22
  const research = player.race.researches.find((research) =>
10✔
23
    research._id.equals(task.data.research)
94✔
24
  )
25

26
  if (!research) {
10✔
27
    throw new GameEngineError('invalid research')
1✔
28
  }
29

30
  const playerResearch = player.researches.researched.find(
9✔
31
    (playerResearch) => playerResearch.research.name === research.name
3✔
32
  )
33

34
  const isFirstLevel = !playerResearch
9✔
35

36
  const newLevel = isFirstLevel ? 1 : playerResearch.level + 1
9✔
37

38
  // upgrade player researches
39
  if (isFirstLevel) {
9✔
40
    player.researches.researched.push({
6✔
41
      research,
42
      level: 1
43
    })
44
  } else {
45
    playerResearch.level = newLevel
3✔
46
  }
47

48
  // upgrade player bonus if present in the research
49
  const hasBonusToUpdate = hasBonus(research.bonus)
9✔
50

51
  if (hasBonusToUpdate) {
9✔
52
    const PlayerBonus = player.perks.find((perk) => perk.source.equals(task.data.research))
3✔
53

54
    if (PlayerBonus) {
3✔
55
      PlayerBonus.bonus = upgradeResearchBonus(research.bonus, newLevel)
1✔
56
    } else {
57
      player.perks.push({
2✔
58
        bonus: research.bonus,
59
        source: task.data.research,
60
        sourceName: research.name,
61
        type: 'Research'
62
      })
63
    }
64
  }
65

66
  if (research.isFleetEnergyResearch) {
9✔
67
    player.units.fleets.energy = calculateFleetEnergy(player.race, newLevel)
3✔
68
  }
69

70
  if (research.isTroopsPopulationResearch) {
9✔
71
    player.units.troops.population = calculateTroopsPopulation(player.race, newLevel)
3✔
72
  }
73

74
  // TODO: intergalacticTravel check?
75

76
  const points = task.data.researchResourceCost
9✔
77
  const pointsSource = task.data.research._id
9✔
78
  const pointsSourceName = research.name
9✔
79
  player.points = addPoints(
9✔
80
    player.points,
81
    points,
82
    pointsSource,
83
    pointsSourceName,
84
    'Research',
85
    second
86
  )
87

88
  player.researches.activeResearch = undefined
9✔
89

90
  // check player research queue
91
  const nextResearchName = player.researches.queue.shift()
9✔
92
  const nextResearch = player.race.researches.find((research) => research.name === nextResearchName)
171✔
93

94
  if (nextResearch) {
9!
95
    // TODO: has enough resources???
96

NEW
97
    const startResearchTask = createStartResearchTask(
×
98
      task.universe._id,
99
      player._id,
100
      nextResearch._id
101
    )
102

NEW
103
    return Promise.all([player.save(), taskRepository.createStartResearchTask(startResearchTask)])
×
104
  }
105

106
  return Promise.all([player.save()])
9✔
107
}
108

109
export default processFinishResearchTask
4✔
110

111
function hasBonus(bonus: IBonus): boolean {
112
  const hasBonusDefined = Object.keys(bonus).some((perk) => !!bonus[perk as keyof IBonus])
249✔
113
  return bonus && hasBonusDefined
9✔
114
}
115

116
const TROOP_POPULATION_FACTOR = 2.55
4✔
117

118
// TODO: create file in engine/troops ???
119
function calculateTroopsPopulation(race: IRace, level: number): number {
120
  const isFirstLevel = level === 1
4✔
121

122
  if (isFirstLevel) {
4✔
123
    return race.baseTroopsPopulation
3✔
124
  }
125

126
  const previousLevel = level - 1
1✔
127
  const previousPopulation = calculateTroopsPopulation(race, previousLevel)
1✔
128

129
  return Math.floor(previousPopulation + (previousPopulation * TROOP_POPULATION_FACTOR) / level)
1✔
130
}
131

132
const FLEET_ENERGY_FACTOR = 4
4✔
133

134
// TODO: create file in engine/fleets ???
135
function calculateFleetEnergy(race: IRace, level: number): number {
136
  const isFirstLevel = level === 1
4✔
137

138
  if (isFirstLevel) {
4✔
139
    return race.baseFleetEnergy
3✔
140
  }
141

142
  const previousLevel = level - 1
1✔
143
  const previousEnergy = calculateFleetEnergy(race, previousLevel)
1✔
144

145
  return Math.floor(previousEnergy + (previousEnergy * FLEET_ENERGY_FACTOR) / level)
1✔
146
}
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