-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path1a2b.play.js
171 lines (125 loc) · 3.45 KB
/
1a2b.play.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const config = require('./config');
const core = require('./1a2b.core');
const games = {};
const meows = {};
const normalInit = (text) => {
const all = text.split(/[\n\r]+/);
const list = [];
for (const i in all) {
const str = all[i].split(/\s+/).join('');
if (str && list.indexOf(str) < 0) {
list.push(str);
}
}
return list;
};
const normalGet = (list) => {
return list[Math.floor(Math.random() * list.length)];
};
const normalHint = (charset) => {
return charset;
};
const meowInit = (meowId, text) => {
meows[meowId] = normalGet(normalInit(text));
};
const meowGet = (meowId) => {
const charset = meows[meowId];
delete meows[meowId];
return charset;
};
const meowHint = (charset) => {
return '喵'.repeat(core.length(charset));
};
const init = (id, text, meowId, onGameInit, onGameExist) => {
// charset selection order: argument -> reply -> meow -> default
if (games[id]) {
return onGameExist();
}
games[id] = {
charset: null,
answer: null,
hint: null,
guess: {},
};
const game = games[id];
const ok = () => {
return game.charset && core.length(game.charset) <= config.abMaxLength;
};
const list = normalInit(text);
if (list.length > 1) {
game.charset = normalGet(list);
game.hint = meowHint(game.charset);
} else if (list.length) {
game.charset = normalGet(list);
game.hint = normalHint(game.charset);
}
if (!ok() && meows[meowId]) {
game.charset = meowGet(meowId);
game.hint = meowHint(game.charset);
}
if (!ok()) {
game.charset = '1234567890';
game.hint = '1234567890';
}
return onGameInit(game);
};
const end = (id, onGameEnd, onGameNotExist) => {
if (!games[id]) {
return onGameNotExist();
}
const game = games[id];
delete game.hint;
delete games[id];
return onGameEnd(game);
};
const verify = (id, str, onValid, onNotValid, onGameNotExist) => {
if (!games[id]) {
return onGameNotExist();
}
const game = games[id];
if (game.answer) {
if (core.length(str) === core.length(game.answer) && !core.extraChar(str, game.charset)) {
return onValid();
}
return onNotValid();
}
if (core.length(str) <= config.abMaxLength && !core.extraChar(str, game.charset)) {
game.answer = core.shuffle(game.charset, core.length(str));
return onValid();
}
return onNotValid();
};
const guess = (id, str, onGuess, onGameEnd, onGuessDuplicated, onGameNotExist) => {
if (!games[id]) {
return onGameNotExist();
}
const game = games[id];
if (game.guess['#' + str]) {
return onGuessDuplicated();
}
game.hint = core.reveal(str, game.hint, game.charset);
game.guess['#' + str] = core.getAB(str, game.answer);
if (Object.keys(game.guess).length > config.abMaxGuess) {
for (const i in game.guess) {
// delete the first one
delete game.guess[i];
break;
}
}
if (game.guess['#' + str][0] === core.length(game.answer)) {
return end(id, onGameEnd, onGameNotExist);
}
return onGuess(game);
};
const count = () => {
return Object.keys(games).length;
};
module.exports = {
meowInit: meowInit,
init: init,
end: end,
verify: verify,
guess: guess,
count: count,
};