-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
49 lines (42 loc) · 1.07 KB
/
store.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
import { reactive, toRefs } from 'vue';
const state = reactive({
credit: 10,
account_credit: 0
});
const useState = () => toRefs(state);
const { credit, account_credit } = useState();
const rewards = {
cherry: 10,
lemon: 20,
orange: 30,
watermelon: 40
};
const chance = percent => {
return Math.random() < percent / 100;
};
const reRollNeeded = () => {
if (credit.value > 40 && credit.value < 60) return chance(30);
if (credit.value > 60) return chance(60);
};
const getRollResult = () => {
const names = Object.keys(rewards);
const choice = () => names[Math.floor(Math.random() * names.length)];
const result = {
first: choice(),
second: choice(),
third: choice()
};
const won = result.first == result.second && result.second == result.third;
if (won) {
if (reRollNeeded()) return getRollResult();
credit.value += rewards[result.first];
} else {
credit.value -= 1;
}
return result;
};
const cashOut = () => {
account_credit.value += credit.value;
credit.value = 0;
};
export { useState, getRollResult, cashOut, chance };