1
- import { ActionType , CellType , Environment , Role } from "@enums" ;
1
+ import { ActionType , Environment , Role } from "@enums" ;
2
2
import { pgQuery } from "data/db" ;
3
3
import { UserRepository } from "data/repository" ;
4
- import { Action , Award , Maze , RankingResult , Score } from "hackthelab" ;
4
+ import { Action , Award , RankingResult , Score } from "hackthelab" ;
5
5
import { MazeService , ScoreService } from "services" ;
6
6
7
- export const getScore = ( userId : number , maze : Maze , actions : Action [ ] ) : number => {
8
- const MOVE_EFFICIENCY_BONUS = 2500 ;
7
+ export const calculateScore = ( actions : Action [ ] ) : number => {
9
8
const EXIT_BONUS = 5000 ;
10
9
const CHEESE_BONUS = 1000 ;
10
+ const HARVEST_BONUS = 2500 ;
11
+ const MOVE_PENALTY = 2 ;
11
12
const ACTION_PENALTY = 1 ;
12
13
13
- // Get the maze open spaces
14
- const wallCount = maze . cells . filter ( cell => cell . type === CellType . Wall ) ;
15
- const openSpaceCount = maze . cells . length - wallCount . length ;
16
-
17
14
// Get stats based on the rat's actions
18
15
const numOfActions = actions . length ;
19
16
20
17
let numOfMoves = 0 ;
21
18
let numOfCheeseEaten = 0 ;
19
+ let numOfCheeseHarvested = 0 ;
22
20
let didExit = false ;
23
21
24
22
actions . forEach ( action => {
@@ -33,18 +31,28 @@ export const getScore = (userId: number, maze: Maze, actions: Action[]): number
33
31
case ActionType . Exit :
34
32
didExit = true ;
35
33
break ;
34
+ case ActionType . Drop :
35
+ numOfCheeseHarvested ++ ;
36
+ break ;
36
37
}
37
38
}
38
39
} ) ;
39
40
40
41
// Calculate the score
41
42
const exitBonus = didExit ? EXIT_BONUS : 0 ;
42
- const moveEfficiencyBonus = didExit ? Math . max ( 0 , ( ( openSpaceCount - numOfMoves ) / openSpaceCount ) * MOVE_EFFICIENCY_BONUS ) : 0 ;
43
43
const cheeseBonus = numOfCheeseEaten * CHEESE_BONUS ;
44
+ const harvestBonus = numOfCheeseHarvested * HARVEST_BONUS ;
44
45
const actionPenalty = numOfActions * ACTION_PENALTY ;
46
+ const movePenalty = numOfMoves * MOVE_PENALTY ;
47
+
48
+ const bonuses = exitBonus + cheeseBonus + harvestBonus ;
49
+ const penalties = actionPenalty + movePenalty ;
50
+
51
+ // Calculate the net score
52
+ const netScore = bonuses - penalties ;
45
53
46
54
// Add up everything, but don't let the score go below 0.
47
- return Math . floor ( Math . max ( 0 , exitBonus + moveEfficiencyBonus + cheeseBonus - actionPenalty ) ) ;
55
+ return Math . max ( 0 , netScore ) ;
48
56
} ;
49
57
50
58
// Returns the rankings for all participants for the given maze ids
@@ -72,7 +80,7 @@ export const getRankings = async (environment: Environment): Promise<RankingResu
72
80
}
73
81
74
82
const actions = await MazeService . getActions ( userId , mazeId ) ;
75
- const score = ScoreService . getScore ( userId , mazes [ mazeId ] , actions ) ;
83
+ const score = ScoreService . calculateScore ( actions ) ;
76
84
77
85
totalScore += score ;
78
86
}
0 commit comments