-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoringService.js
More file actions
201 lines (175 loc) · 6.47 KB
/
scoringService.js
File metadata and controls
201 lines (175 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// scoringService.js
/*
Scoring system that manages:
- Score calculations
- Score history
- Statistics tracking
- Location discovery tracking
*/
let scoringService = {
calculateScore: function(correctDistance, guessDistance, correctDir, guessDir, playerLat, playerLon, targetLat, targetLon) {
// Direction scoring - Linear from 0° to 90°
let directionDiff = Math.min(
Math.abs(correctDir - guessDir),
360 - Math.abs(correctDir - guessDir)
);
let directionScore = Math.max(0, 100 * (1 - directionDiff / 90));
// Distance scoring - Method 1 (legacy)
let distanceError = Math.abs(correctDistance - guessDistance);
let distanceScore1;
if (correctDistance < 0.1) {
distanceScore1 = Math.max(0, 100 * (1 - distanceError));
} else if (correctDistance < 1) {
const relativeError = distanceError / correctDistance;
distanceScore1 = 100 * (1 - Math.min(1, relativeError));
} else {
const relativeError = distanceError / correctDistance;
distanceScore1 = 100 * Math.exp(-2 * relativeError);
}
// Distance scoring - Method 2 (actual distance between guess and target)
const guessPoint = computeDestinationLatLon(
playerLat,
playerLon,
guessDistance,
guessDir
);
const actualDistance = calculateDistance(
guessPoint.lat, guessPoint.lon,
targetLat, targetLon
);
// If the difference is larger than the distance to target, score is 0
// Otherwise, score is the percentage of how close we got
const distanceScore2 = actualDistance > correctDistance ? 0 : 100 * (1 - actualDistance / correctDistance);
// Return both scoring methods for comparison
return {
score1: Math.round((distanceScore1 + directionScore) / 2),
score2: Math.round(distanceScore2),
details: {
distanceScore1,
distanceScore2,
directionScore,
actualDistance
}
};
},
saveScore: function(score) {
// Round distances according to range
const roundDistance = (dist) => {
const ranges = [
{ max: 0.01, decimals: 3 },
{ max: 0.1, decimals: 2 },
{ max: 1, decimals: 1 },
{ max: 10, decimals: 0 },
{ max: 100, decimals: 0 },
{ max: 1000, decimals: -1 },
{ max: 20000, decimals: -2 }
];
for (let range of ranges) {
if (dist <= range.max) {
if (range.decimals >= 0) {
return Number(dist.toFixed(range.decimals));
} else {
const factor = Math.pow(10, -range.decimals);
return Math.round(dist/factor) * factor;
}
}
}
return Math.round(dist);
};
const scoreData = {
timestamp: Date.now(),
score: score.score2,
scoreOld: score.score1, // (Keep old score for comparison)
locationName: currentQuestion.name,
country: currentQuestion.country,
dataset: localStorage.getItem('selectedDataset') || 'global',
distance: {
guess: roundDistance(score.guessDistance),
actual: roundDistance(score.correctDistance),
error: roundDistance(score.details.actualDistance),
score: Math.round(score.details.distanceScore2)
},
direction: {
guess: score.guessDirection,
actual: score.correctDirection,
score: Math.round(score.details.directionScore)
},
location: {
lat: currentQuestion.lat,
lon: currentQuestion.lon,
type: currentQuestion.type
},
guess: {
lat: score.playerLat,
lon: score.playerLon,
calculatedPoint: computeDestinationLatLon(
score.playerLat,
score.playerLon,
score.guessDistance,
score.guessDirection
)
}
};
let scores = this.getScores();
scores.push(scoreData);
localStorage.setItem('scoreHistory', JSON.stringify(scores));
// Track discovered location
this.addDiscoveredLocation(scoreData);
// Update discovery map markers if we're on that screen
if (currentScreen === 'discoveries' && discoveryMap.map) {
discoveryMap.updateMarkers();
}
return scoreData;
},
addDiscoveredLocation: function(scoreData) {
// For custom locations, always save to custom dataset
const datasetKey = scoreData.location.type === 'custom' ? 'custom' : scoreData.dataset;
const discoveredKey = `discovered_${datasetKey}`;
let discovered = localStorage.getItem(discoveredKey);
discovered = discovered ? JSON.parse(discovered) : {};
// Use location name as key to avoid duplicates
discovered[scoreData.locationName] = {
name: scoreData.locationName,
country: scoreData.country,
lat: scoreData.location.lat,
lon: scoreData.location.lon,
type: scoreData.location.type,
bestScore: discovered[scoreData.locationName]
? Math.max(scoreData.score, discovered[scoreData.locationName].bestScore)
: scoreData.score,
lastScore: scoreData.score,
plays: discovered[scoreData.locationName]
? discovered[scoreData.locationName].plays + 1
: 1
};
localStorage.setItem(discoveredKey, JSON.stringify(discovered));
},
getDiscoveredLocations: function(dataset) {
const discoveredKey = `discovered_${dataset}`;
const discovered = localStorage.getItem(discoveredKey);
return discovered ? Object.values(JSON.parse(discovered)) : [];
},
getScores: function() {
const scoresJson = localStorage.getItem('scoreHistory');
return scoresJson ? JSON.parse(scoresJson) : [];
},
getAverageScore: function(dataset = null) {
const scores = this.getScores();
if (scores.length === 0) return 0;
const filteredScores = dataset
? scores.filter(s => s.dataset === dataset)
: scores;
if (filteredScores.length === 0) return 0;
const sum = filteredScores.reduce((acc, s) => acc + s.score, 0);
return Math.round(sum / filteredScores.length);
},
getBestScore: function(dataset = null) {
const scores = this.getScores();
if (scores.length === 0) return 0;
const filteredScores = dataset
? scores.filter(s => s.dataset === dataset)
: scores;
if (filteredScores.length === 0) return 0;
return Math.max(...filteredScores.map(s => s.score));
}
};