-
Notifications
You must be signed in to change notification settings - Fork 2
/
GiphyGame.js
63 lines (56 loc) · 1.56 KB
/
GiphyGame.js
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
import React from 'react'
import giphy from 'giphy-api'
import { Response, Message } from '../src'
const GIPHY_WORDS = ["happy", "crazy", "hot", "fun", "silly"]
const getRandomWord = () => GIPHY_WORDS[Math.floor(Math.random() * GIPHY_WORDS.length)];
const MAX_TRIES = 3
// fetch giphy data if needed
export const getGiphyData = (state) => {
return new Promise((resolve, reject) => {
if (state) {
return resolve(state)
}
let word = getRandomWord()
return resolve(giphy().search(word).then(res => ({
word,
url: res.data[0].images.fixed_height.url,
tries: -1
})))
})
}
export const GiphyGame = ({ text, state, save }) => {
const incrementTries = () => {
const stateData = Object.assign({}, state, { tries: (state.tries + 1) })
save(stateData)
}
if (state.tries === -1) {
incrementTries()
return (
<Response>
<Message>Welcome to the game ! Guess that image keyword : { state.url }</Message>
</Response>
)
} else if (state.tries >= MAX_TRIES) {
save()
return (
<Response>
<Message>LOST ! { MAX_TRIES } tries</Message>
</Response>
)
} else if (text && text.toLowerCase() === state.word.toLowerCase()) {
save()
return (
<Response>
<Message>Win in { state.tries + 1 } - BRAVO ! づ。◕‿‿◕。)づ</Message>
</Response>
)
} else {
incrementTries();
return (
<Response>
<Message>Try again ({ state.tries + 1 } / { MAX_TRIES }) ! ¯\_(ツ)_/¯ </Message>
</Response>
)
}
}
export default GiphyGame