Skip to content

Commit 34d0a43

Browse files
committed
Refactor
1 parent b99199e commit 34d0a43

File tree

16 files changed

+65
-121
lines changed

16 files changed

+65
-121
lines changed

challenges/es5-1-number-reader/challenge.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

challenges/es5-1-number-reader/solution.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

challenges/es5-2-sort-the-numbers/challenge.json

Lines changed: 0 additions & 36 deletions
This file was deleted.

challenges/es5-2-sort-the-numbers/solution.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

challenges/es6-5-arabic-roman/challenge.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

challenges/es6-5-arabic-roman/solution.js

Lines changed: 0 additions & 15 deletions
This file was deleted.
File renamed without changes.

src/challenges/c1-number-reader/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { Challenge } from '..';
2-
3-
const solution = (n: number) => {
4-
const a = [];
5-
const ns = n + '';
6-
for (let i = 0; i < ns.length; i += 1) {
7-
a.push(['zero', 0, 'two', 'three', 0, 'five', 'six', 'seven', 0, 'nine'][parseInt(ns[i], 10)]);
8-
}
9-
return a.join(' ');
10-
};
2+
import solution from './solution';
113

124
const input = 77209536;
135

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare function play(n: number): string;
2+
3+
export = play;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function play(n) {
2+
const a = []
3+
n += ''
4+
for (let i = 0; i < n.length; i++)
5+
a.push(['zero', 0, 'two', 'three', 0, 'five', 'six', 'seven', 0, 'nine'][n[i]])
6+
return a.join(' ')
7+
}
8+
9+
if (typeof module !== 'undefined') {
10+
module.exports = play;
11+
}

src/challenges/c2-sort-the-numbers/index.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
import { Challenge } from '..';
2-
3-
const solution = (n: number[]) => {
4-
const a = [];
5-
const b = [];
6-
for (let i = 0; i < n.length; i += 1) {
7-
a[n[i]] ? a[n[i] + 1] = n[i] : a[n[i]] = n[i];
8-
}
9-
for (let x = 0; x < a.length; x += 1) {
10-
if (a[x]) b.push(a[x]);
11-
}
12-
return b;
13-
};
2+
import solution from './solution';
143

154
const input = [
165
10,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare function play(n: number[]): number[];
2+
3+
export = play;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function play(n) {
2+
const a = [];
3+
const b = [];
4+
for (let i = 0; i < n.length; i += 1) {
5+
a[n[i]] ? a[n[i] + 1] = n[i] : a[n[i]] = n[i];
6+
}
7+
for (let x = 0; x < a.length; x += 1) {
8+
if (a[x]) b.push(a[x]);
9+
}
10+
return b;
11+
};
12+
13+
if (typeof module !== 'undefined') {
14+
module.exports = play;
15+
}

src/game/game.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fs = require('fs');
2+
import path = require('path');
23
import crypto = require('crypto');
34
import lodash = require('lodash');
5+
import { challenges } from '../challenges';
46

57
interface GameStart {
68
key: string;
@@ -29,9 +31,9 @@ interface Entry extends AddEntryRequest {
2931
const jsmin = require('jsmin').jsmin;
3032

3133
let game: Game | undefined;
32-
const savePath = __dirname + '/data/game.json';
34+
const savePath = path.join(__dirname, '..', '..', 'data', 'game.json');
3335

34-
const start = function (data: GameStart) {
36+
const start = (data: GameStart) => {
3537
if (game && game.key === data.key) {
3638
game.running = true;
3739
return save();
@@ -46,35 +48,39 @@ const start = function (data: GameStart) {
4648
return save();
4749
};
4850

49-
const stop = function () {
51+
const stop = () => {
5052
if (game) {
5153
game.running = false;
5254
}
5355

5456
return save();
5557
};
5658

57-
const get = function () {
59+
const get = () => {
5860
return game;
5961
};
6062

61-
const getOrError = function () {
63+
const getCurrentChallenge = () => {
64+
return game ? challenges[game.key] : null;
65+
};
66+
67+
const getOrError = () => {
6268
if (!game) throw new Error('No game');
6369

6470
return game;
6571
};
6672

67-
const addEntry = function (data: AddEntryRequest): { entry?: Partial<Entry>, err?: string } {
68-
const createKey = function () {
73+
const addEntry = (data: AddEntryRequest): { entry?: Partial<Entry>, err?: string } => {
74+
const createKey = () => {
6975
return (Math.round(Math.random() * 100000000000)).toString(36);
7076
};
7177

72-
const getGravatarUrl = function (email: string) {
78+
const getGravatarUrl = (email: string) => {
7379
const hash = crypto.createHash('md5').update(email).digest('hex');
7480
return 'http://www.gravatar.com/avatar/' + hash + '?s=130&d=wavatar';
7581
};
7682

77-
const countStrokes = function (file: string) {
83+
const countStrokes = (file: string) => {
7884
if (fs.existsSync(file)) {
7985
const contents = jsmin(fs.readFileSync(file, 'utf8'), 3).replace(/^\n+/, '');
8086
return contents.length;
@@ -123,7 +129,7 @@ const addEntry = function (data: AddEntryRequest): { entry?: Partial<Entry>, err
123129
return { err: 'Unknown error' };
124130
};
125131

126-
const setValid = function (key: string, valid: boolean) {
132+
const setValid = (key: string, valid: boolean) => {
127133
if (!game) return;
128134

129135
const entry = lodash.find(game.entries, { key });
@@ -134,11 +140,11 @@ const setValid = function (key: string, valid: boolean) {
134140
}
135141
};
136142

137-
const save = function () {
143+
const save = () => {
138144
return fs.writeFileSync(savePath, JSON.stringify(game));
139145
};
140146

141-
const load = function () {
147+
const load = () => {
142148
if (fs.existsSync(savePath)) {
143149
game = JSON.parse(fs.readFileSync(savePath, 'utf8'));
144150
}
@@ -154,5 +160,6 @@ export {
154160
start,
155161
stop,
156162
get,
163+
getCurrentChallenge,
157164
getOrError,
158165
};

src/routes/admin.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import express = require('express');
22
import expressBasicAuth = require('express-basic-auth');
33
import game = require('../game/game');
4-
5-
const challengeLibrary = require('../game/challenge-library');
4+
import challengeLibrary = require('../game/challenge-library');
65

76
const app = express();
87

@@ -13,13 +12,17 @@ const authorizer = (username: string, password: string) => {
1312
app.use(expressBasicAuth({ authorizer, challenge: true }));
1413

1514
app.get('/admin', (req, res) => {
16-
const challenges = challengeLibrary.getChallenges();
15+
const challengeList = challengeLibrary.getChallenges();
16+
17+
const gameState = game.get();
18+
const challenge = game.getCurrentChallenge();
1719

18-
const gameData = JSON.stringify(game.get(), undefined, 2);
20+
const gameData = JSON.stringify(gameState, undefined, 2);
1921

2022
return res.render('admin', {
2123
gameData,
22-
challenges,
24+
challenge,
25+
challengeList,
2326
game: game.get(),
2427
});
2528
});

views/admin.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@ <h4>Game is stopped</h4>
3030
<span class="caret"></span>
3131
</button>
3232
<ul class="dropdown-menu" aria-labelledby="challengeMenu">
33-
{{#challenges}}
33+
{{#challengeList}}
3434
<li>
3535
<a href="javascript:void(0)"
3636
class="challenge-link"
3737
data-key="{{key}}">
3838
{{title}}
3939
</a>
4040
</li>
41-
{{/challenges}}
41+
{{/challengeList}}
4242
</ul>
4343
</div>
4444

4545
<form method="post" action="/start">
4646
<input type="hidden" name="key" id="key" value="{{game.key}}" />
4747

4848
<label for="title">Title <span>(new title starts a new game)</span></label>
49-
<input type="text" name="title" id="title" value="{{game.title}}" readonly="readonly" />
49+
<input type="text" name="title" id="title" value="{{challenge.title}}" readonly="readonly" />
5050

5151
<label for="description">Description</label>
52-
<textarea name="description" id="description" rows="5" readonly="readonly">{{game.description}}</textarea>
52+
<textarea name="description" id="description" rows="5" readonly="readonly">{{challenge.description}}</textarea>
5353

5454
{{#game.entries.length}}
5555
<label>Entries</label>

0 commit comments

Comments
 (0)