Skip to content

Commit 0289f92

Browse files
committed
shiritori: add error handling
Change-Id: I1bab0fd196f2aedd5036dfea3b58738b33c39bd8
1 parent 551ca99 commit 0289f92

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

functions/index.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,31 @@ app.intent('actions.intent.MAIN', (conv) => {
4949
conv.ask('どうぞ、始めて下さい');
5050
});
5151

52-
app.intent('actions.intent.TEXT', (conv, input) => new Promise((resolve, reject) => {
52+
app.intent('actions.intent.TEXT', (conv, input) => {
5353
shiritori.loaded.then(() => {
54-
shiritori.interact(dict, input, conv.data.used, {
55-
lose () {
56-
conv.close('ざんねん。あなたの負けです。');
57-
resolve();
58-
},
59-
win (word, kana) {
60-
if (word) {
61-
conv.close(`${word} [${kana}]`);
62-
resolve();
63-
} else {
64-
conv.close('すごい!あなたの勝ちです。');
65-
resolve();
66-
}
67-
},
68-
next (word, kana) {
69-
conv.data.used.unshift(input);
70-
conv.data.used.unshift(word);
71-
conv.ask(new SimpleResponse({
72-
speech: word,
73-
text: `${word} [${kana}]`
74-
}));
75-
resolve();
76-
}
77-
});
54+
shiritori.interact(dict, input, conv.data.used)
55+
.then(result => {
56+
conv.data.used.unshift(input);
57+
conv.data.used.unshift(result.word);
58+
conv.ask(new SimpleResponse({
59+
speech: result.word,
60+
text: `${result.word} [${result.kana}]`
61+
}));
62+
})
63+
.catch(result => {
64+
if (result.win) {
65+
if (result.word) {
66+
conv.close(`${result.word} [${result.kana}]`);
67+
} else {
68+
conv.close('すごい!あなたの勝ちです。');
69+
}
70+
} else if (result.loose) {
71+
conv.close('ざんねん。あなたの負けです。');
72+
} else {
73+
throw result;
74+
}
75+
});
7876
});
79-
}));
77+
});
8078

81-
exports.shiritoriV2 = functions.https.onRequest(app);
79+
exports.shiritoriV3 = functions.https.onRequest(app);

functions/shiritori/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ exports.kanas = (word) => new Set((() => {
9999

100100
// しりとりのゲームループ。
101101
exports.interact = (dict, word, chain) => new Promise((resolve, reject) => {
102-
const next = exports.kanas(word);
102+
const next = exports.kanas(word)
103103
if ((next.size === 0) || !exports.check(word, chain)) {
104104
return reject({loose: true})
105105
}
@@ -118,9 +118,12 @@ exports.interact = (dict, word, chain) => new Promise((resolve, reject) => {
118118
}
119119
const w = unused[Math.floor(Math.random() * unused.length)]
120120
const wk = words[w]
121+
if (wk.length === 0) {
122+
return reject({error: 'empty dictionary entry for key: ' + w})
123+
}
121124
if (wk[wk.length - 1] === 'ん') {
122-
return reject({win: true, 'word': w, 'kana': wk})
125+
return reject({win: true, word: w, kana: wk})
123126
}
124-
return resolve({'word': w, 'kana': wk})
127+
resolve({word: w, kana: wk})
125128
})
126129
})

functions/shiritori/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ shiritori.loaded.then(() => {
3636
.interact(
3737
kana => Promise.resolve(corpus[kana]),
3838
input,
39-
chain,
39+
chain
4040
)
4141
.then(result => {
4242
console.log(`${result.word} [${result.kana}]`)
@@ -48,9 +48,11 @@ shiritori.loaded.then(() => {
4848
if (reason.win) {
4949
console.log('すごい!')
5050
process.exit(0)
51-
} else {
51+
} else if (reason.loose) {
5252
console.log('ざんねん。')
5353
process.exit(-1)
54+
} else {
55+
throw reason
5456
}
5557
})
5658
})

functions/shiritori/test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const dict = k => Promise.resolve({
8080
}[k])
8181

8282
test('internal.next', async t => {
83-
const result = await shiritori.interact(dict, 'べんと', [],)
83+
const result = await shiritori.interact(dict, 'べんと', [])
8484
t.is('とんかつ', result.kana)
8585
t.is('とんかつ', result.word)
8686
})
@@ -109,3 +109,9 @@ test('interact.win kanji', async t => {
109109
t.is('ざぶとん', result.kana)
110110
t.is('座布団', result.word)
111111
})
112+
113+
test('interact.error ', async t => {
114+
const result = await t.throws(shiritori.interact(k => Promise.resolve({'つけまん': ''}),
115+
'とんかつ', ['べんと']))
116+
t.true(result.error.length > 0)
117+
})

0 commit comments

Comments
 (0)