Skip to content

Commit 355b17b

Browse files
feat: use station mode by default
1 parent 5980e27 commit 355b17b

File tree

8 files changed

+98
-31
lines changed

8 files changed

+98
-31
lines changed
File renamed without changes.

docs/TODO.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
- [ ] UX - Map - Améliorer le responsive : se baser sur la hauteur de fenêtre aussi
22
- [ ] Trouver un nom
33
- [ ] Mode de Jeu - Ajouter un mode par temps entre stations
4+
- [ ] Unique Stop - Renommer en station

src/Game.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import arrondissements from '../assets/arrondissements.json';
12
import { getSeededRandomStations, pickStations } from './pick-stations.js';
23
import { getAdjacentStops, getRoutes, getStops, getUniqueStops } from './utils.js';
34
import { verifyIfConnected } from './graph.js';
5+
import { ParisMap } from './map.js';
46

57
const stations = getUniqueStops();
68
const adjacentStops = getAdjacentStops();
@@ -22,11 +24,12 @@ export class AlreadyAddedError extends Error {
2224
}
2325

2426
export class Game {
25-
constructor(seed) {
26-
this.pick = pickStations({ stations, adjacentStops, random: getSeededRandomStations(seed) });
27+
constructor({ seed, mode }) {
28+
this.pick = pickStations({ stations, adjacentStops, random: getSeededRandomStations(seed), mode });
2729
this.instruction = `Aujourd'hui, nous allons de <span class="start">${this.pick.start.stop_name}</span> jusqu'à <span class="end">${this.pick.end.stop_name}</span> en passant par le moins de stations possible.`;
2830
this.currentState = new Map();
2931
this.adjacentStops = adjacentStops;
32+
this.mode = mode;
3033
}
3134

3235
init() {

src/components/Game.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { searchStations } from '../utils.front.js';
77
import FinishModal from './FinishModal.vue';
88
99
const props = defineProps(['seed']);
10-
let game = new Game(props.seed);
10+
let game = new Game({ seed: props.seed, mode: 'station' });
1111
1212
const sortedStations = getUniqueStops().map(d => d.stop_name).sort();
1313
@@ -28,7 +28,7 @@ onMounted(() => {
2828
});
2929
3030
watch(() => props.seed, (newSeed) => {
31-
game = new Game(newSeed);
31+
game = new Game({ seed: newSeed, mode: 'station' });
3232
game.init();
3333
instruction.value = game.instruction;
3434
});

src/graph.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
function computeSmallestStationsPath({ start, end, adjacentStops }) {
1+
function computeSmallestStationsPath({ start, end, adjacentStops, mode }) {
22
if (!stopExists(adjacentStops, start))
33
throw new Error('Start stop does not exist');
44

55
if (!stopExists(adjacentStops, end))
66
throw new Error('End stop does not exist');
77

8-
const graph = computeGraph(adjacentStops, start, end);
8+
const graph = computeGraph(adjacentStops, start, end, mode);
99
return graph.dijkstra(start, end);
1010
}
1111

@@ -25,13 +25,13 @@ function verifyIfConnected({ start, end, adjacentStops, stationsToVerify }) {
2525
return graph.isConnectedTo(start, end);
2626
}
2727

28-
function computeGraph(adjacentStops, start, end) {
28+
function computeGraph(adjacentStops, start, end, mode = 'time') {
2929
const graph = new Graph();
3030

3131
adjacentStops.forEach(({ from_stop_id, from_stop_unique_id, to_stop_id, to_stop_unique_id, duration }) => {
3232
graph.addNode(from_stop_id);
3333
graph.addNode(to_stop_id);
34-
graph.addEdge(from_stop_id, to_stop_id, duration);
34+
graph.addEdge(from_stop_id, to_stop_id, mode === 'time' ? duration : 1);
3535

3636
if (from_stop_unique_id === start || from_stop_unique_id === end) {
3737
graph.addNode(from_stop_unique_id);

src/pick-stations.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function makeRandom(seed) {
2929
return next;
3030
}
3131

32-
function pickStations({ stations, adjacentStops, random = getRandomStations } = {}) {
32+
function pickStations({ stations, adjacentStops, random = getRandomStations, mode } = {}) {
3333
const MIN_DISTANCE = 5;
3434

3535
let start = null;
@@ -38,7 +38,7 @@ function pickStations({ stations, adjacentStops, random = getRandomStations } =
3838

3939
do {
4040
[start, end] = random(stations);
41-
path = computeSmallestStationsPath({ start: start.stop_unique_id, end: end.stop_unique_id, adjacentStops });
41+
path = computeSmallestStationsPath({ start: start.stop_unique_id, end: end.stop_unique_id, adjacentStops, mode });
4242
} while (path.distance < MIN_DISTANCE);
4343

4444
return {

tests/Game.spec.js

+10-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Game } from '../src/Game.js';
44
describe('game', () => {
55
describe('#getInformation', () => {
66
it('should return all stats for game', () => {
7-
const game = new Game('123');
7+
const game = new Game({ seed: Number.parseInt('2eeea1', 16), mode: 'station' });
88
game.parisMap = {
99
addStation: () => {
1010
},
@@ -14,27 +14,16 @@ describe('game', () => {
1414
game.addStation({ station: game.pick.end.stop_name, color: '#e52228' });
1515

1616
expect(game.getInformation()).to.deep.equal({
17-
minTry: 17,
17+
minTry: 6,
1818
stops: [
19-
'Mairie de Montreuil - Ligne 9',
20-
'Croix de Chavaux - Ligne 9',
21-
'Robespierre - Ligne 9',
22-
'Porte de Montreuil - Ligne 9',
23-
'Maraîchers - Ligne 9',
24-
'Buzenval - Ligne 9',
25-
'Nation - Ligne 9',
26-
'Nation - Ligne 6',
27-
'Picpus - Ligne 6',
28-
'Bel-Air - Ligne 6',
29-
'Daumesnil - Ligne 6',
30-
'Dugommier - Ligne 6',
31-
'Bercy - Ligne 6',
32-
'Quai de la Gare - Ligne 6',
33-
'Chevaleret - Ligne 6',
34-
'Nationale - Ligne 6',
35-
'Place d\'Italie - Ligne 6',
36-
'Corvisart - Ligne 6',
37-
'Glacière - Ligne 6',
19+
'Pernety - Ligne 13',
20+
'Gaîté - Ligne 13',
21+
'Gare Montparnasse - Ligne 13',
22+
'Gare Montparnasse - Ligne 4',
23+
'Vavin - Ligne 4',
24+
'Raspail - Ligne 4',
25+
'Denfert-Rochereau - Ligne 4',
26+
'Mouton-Duvernet - Ligne 4',
3827
],
3928
try: 0,
4029
});

tests/graph.spec.js

+74
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,80 @@ describe('graph', () => {
3030
],
3131
});
3232
});
33+
34+
describe('when provided mode is time', () => {
35+
it('should return the smallest duration', () => {
36+
const adjacentStops = getAdjacentStops();
37+
38+
const start = {
39+
stop_unique_id: 'IDFM:412687',
40+
stop_name: 'Pernety',
41+
};
42+
const end = {
43+
stop_unique_id: 'IDFM:73653',
44+
stop_name: 'Mouton-Duvernet',
45+
};
46+
47+
const path = computeSmallestStationsPath({
48+
start: start.stop_unique_id,
49+
end: end.stop_unique_id,
50+
adjacentStops,
51+
mode: 'time',
52+
});
53+
54+
expect(path).to.deep.equal({
55+
distance: 437,
56+
path: [
57+
'IDFM:463229',
58+
'IDFM:463184',
59+
'IDFM:462954',
60+
'IDFM:462955',
61+
'IDFM:22172',
62+
'IDFM:22155',
63+
'IDFM:22158',
64+
'IDFM:463103',
65+
'IDFM:463104',
66+
'IDFM:22144',
67+
],
68+
});
69+
});
70+
});
71+
72+
describe('when provided mode is station', () => {
73+
it('should return the smallest path', () => {
74+
const adjacentStops = getAdjacentStops();
75+
76+
const start = {
77+
stop_unique_id: 'IDFM:412687',
78+
stop_name: 'Pernety',
79+
};
80+
const end = {
81+
stop_unique_id: 'IDFM:73653',
82+
stop_name: 'Mouton-Duvernet',
83+
};
84+
85+
const path = computeSmallestStationsPath({
86+
start: start.stop_unique_id,
87+
end: end.stop_unique_id,
88+
adjacentStops,
89+
mode: 'station',
90+
});
91+
92+
expect(path).to.deep.equal({
93+
distance: 7,
94+
path: [
95+
'IDFM:22222',
96+
'IDFM:22230',
97+
'IDFM:22157',
98+
'IDFM:22143',
99+
'IDFM:463016',
100+
'IDFM:22131',
101+
'IDFM:463104',
102+
'IDFM:22144',
103+
],
104+
});
105+
});
106+
});
33107
});
34108

35109
describe('#verifyIsConnected', () => {

0 commit comments

Comments
 (0)