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

DaniSomoza / galactic-commander / 10946795972

19 Sep 2024 06:28PM UTC coverage: 96.824% (-0.3%) from 97.116%
10946795972

push

github

web-flow
Improvements (#7)

* fixed mongoose types

* using nodemailer instead of sendGrid

* use source instead of origin

* activeResearch & researched props within player.research

* bump deps

75 of 79 branches covered (94.94%)

Branch coverage included in aggregate %.

66 of 66 new or added lines in 19 files covered. (100.0%)

2 existing lines in 2 files now uncovered.

748 of 771 relevant lines covered (97.02%)

5.57 hits per line

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

91.84
/packages/game-engine/src/engine/tasks/processStartResearchTask.ts
1
import applyBonus from '../../helpers/applyBonus'
4✔
2
import getSecond from '../../helpers/getSecond'
4✔
3
import { IResearch } from '../../models/ResearchModel'
4
import getTaskModel, {
4✔
5
  FINISH_RESEARCH_TASK_TYPE,
6
  FinishResearchTaskType,
7
  ITask,
8
  ITaskTypeDocument,
9
  PENDING_TASK_STATUS,
10
  StartResearchTaskType
11
} from '../../models/TaskModel'
12
import playerRepository from '../../repositories/playerRepository'
4✔
13
import GameEngineError from '../errors/GameEngineError'
4✔
14
import calculateResearchResourceCost from '../resources/calculateResearchResourceCost'
4✔
15

16
// TODO: only taskData required
17
async function processStartResearchTask(
18
  task: ITaskTypeDocument<StartResearchTaskType>,
19
  second: number
20
) {
21
  // get all the required data from DB
22
  const player = await playerRepository.findPlayerById(task.data.player)
6✔
23

24
  if (!player) {
6✔
25
    throw new GameEngineError('invalid player')
1✔
26
  }
27

28
  if (player.researches.activeResearch) {
5✔
29
    throw new GameEngineError('player already researching')
1✔
30
  }
31

32
  const research = player.race.researches.find((research) =>
4✔
33
    research._id.equals(task.data.research)
37✔
34
  )
35

36
  if (!research) {
4✔
37
    throw new GameEngineError('invalid research')
1✔
38
  }
39

40
  const playerResearch = player.researches.researched.find(
3✔
UNCOV
41
    (playerResearch) => playerResearch.research.name === research.name
×
42
  )
43

44
  const level = playerResearch?.level || 0
3✔
45

46
  const researchResourceCost = calculateResearchResourceCost(research, level)
3✔
47

48
  const hasEnoughResources = player.planets.principal.resources >= researchResourceCost
3✔
49

50
  if (!hasEnoughResources) {
3✔
51
    throw new GameEngineError('no resources available')
1✔
52
  }
53

54
  const researchBonus = applyBonus(player.bonus, 'researchBonus', true)
2✔
55
  const baseResearchDuration = calculateResearchDuration(research, level)
2✔
56

57
  const researchDuration = baseResearchDuration * (100 / researchBonus)
2✔
58
  const executeTaskAt = getSecond(second + researchDuration)
2✔
59

60
  const principalPlanet = player.planets.principal
2✔
61

62
  principalPlanet.resources -= researchResourceCost
2✔
63

64
  const activeResearch = {
2✔
65
    research: research._id,
66
    level: level + 1,
67
    executeTaskAt
68
  }
69

70
  player.researches.activeResearch = activeResearch
2✔
71

72
  // TODO: implement createBaseTask helper function
73
  const finishResearchTask: ITask<FinishResearchTaskType> = {
2✔
74
    type: FINISH_RESEARCH_TASK_TYPE,
75
    universe: player.universe._id,
76

77
    data: {
78
      player: player._id,
79
      research: task.data.research,
80
      researchDuration,
81
      researchResourceCost
82
    },
83

84
    status: PENDING_TASK_STATUS,
85
    isCancellable: true,
86

87
    executeTaskAt,
88

89
    processedAt: null,
90
    processingDuration: null,
91

92
    history: [
93
      {
94
        taskStatus: PENDING_TASK_STATUS,
95
        updatedAt: new Date().getTime()
96
      }
97
    ],
98

99
    errorDetails: null
100
  }
101

102
  const taskModel = getTaskModel<FinishResearchTaskType>()
2✔
103
  const newTask = new taskModel(finishResearchTask)
2✔
104

105
  return Promise.all([newTask.save(), principalPlanet.save(), player.save()])
2✔
106
}
107

108
export default processStartResearchTask
4✔
109

110
const RESEARCH_FACTOR = 4.5
4✔
111

112
// TODO: move this to another file ???
113
function calculateResearchDuration(research: IResearch, level: number): number {
114
  const isFirstLevel = level === 0
2✔
115

116
  if (isFirstLevel) {
2✔
117
    return research.initialTime
2✔
118
  }
119

120
  const previousLevel = level - 1
×
121
  const previousTime = calculateResearchDuration(research, previousLevel)
×
122

123
  return previousTime + (previousTime * RESEARCH_FACTOR) / 2
×
124
}
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

© 2025 Coveralls, Inc