generated from jigintern/template-deno-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.deno.js
274 lines (259 loc) · 9.36 KB
/
server.deno.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { serveDir } from 'https://deno.land/std@0.151.0/http/file_server.ts';
import { getCookies } from 'https://deno.land/std@0.74.0/http/mod.ts';
import { UserGame } from './model/UserGame.js';
import {
DIFFICULTY_STRING,
OUTPUT_TOPRANKING_NUMBER,
THEME_SENTENCES,
} from './utils/constantValue.js';
import { MuxAsyncIterator } from 'https://deno.land/std@0.151.0/async/mux_async_iterator.ts';
function makeId() {
return crypto.randomUUID();
}
/**
* ランダムな文章とそのローマ字文章の組み合わせを出力
* @returns [漢字入り文章,読み方]
*/
function getRandomThemeSentence(difficulty) {
return THEME_SENTENCES[difficulty][
Math.floor(Math.random() * THEME_SENTENCES[difficulty].length)
];
}
/**
* エラーレスポンス生成
* @param {String} errorMessage bodyのerrorMessageに載せる文字列
* @param {String} errorCode bodyのerrorCodeに載せる文字列
* @returns 400番のエラーレスポンス
*/
function makeErrorResponse(errorMessage, errorCode) {
return new Response(
JSON.stringify({
'errorMessage': errorMessage,
'errorCode': errorCode,
}),
{
status: 400,
headers: { 'Content-Type': 'application/json; charset=utf-8' },
},
);
}
/**
* 200番レスポンスを生成
* @param {JSON} bodyJson bodyに入れる辞書型
* @returns 200番のレスポンス
*/
function make200Response(bodyJson) {
return new Response(
JSON.stringify(bodyJson),
{
status: 200,
headers: { 'Content-Type': 'application/json; charset=utf-8' },
},
);
}
// idをキーにuserGameのインスタンスを保持する辞書型
const userGames = {};
Deno.serve(async (req) => {
const pathname = new URL(req.url).pathname;
console.log(pathname);
// ユーザidをcookieに設定する
if (req.method === 'GET' && pathname === '/getId') {
if (getCookies(req)['id']) return new Response('id is already set');
return new Response(
'set id',
{
status: 200,
headers: {
'set-cookie': `id=${makeId()}; Max-Age=${2147483647}`,
},
},
);
}
// ゲームスタートと同時に初期化
if (req.method === 'POST' && pathname === '/solo/start') {
if (!getCookies(req)['id']) {
return makeErrorResponse('id in cookies is not set', '10001');
}
const id = getCookies(req)['id'];
// 送られてきた難易度を取得
const reqeustJson = await req.json();
const difficulty = reqeustJson['difficulty'];
// 送られた難易度が規定値以外ならエラー
if (!DIFFICULTY_STRING.includes(difficulty)) {
return makeErrorResponse('difficulty is different!', '10004');
}
const targetSentence = getRandomThemeSentence(difficulty);
userGames[id] = new UserGame(
id,
difficulty,
targetSentence[0],
targetSentence[1],
);
return make200Response({
'endTime': userGames[id].getEndTime(),
'initializedScore': userGames[id].getInitializedScore(),
'sentenceJapanese': userGames[id].getNowJapanese(),
'sentenceAlphabet': userGames[id].getCompletedRoman() +
userGames[id].getRemainingRoman(),
'sentenceKana': userGames[id].getNowYomigana(),
'expectedTime': userGames[id].calcExpectedTime(),
});
}
// cookieのidからユーザごとに文章を取得する
if (req.method === 'GET' && pathname === '/solo/getSentence') {
if (!getCookies(req)['id']) {
return makeErrorResponse('id in cookies is not set', '10001');
}
const id = getCookies(req)['id'];
if (!userGames[id]) {
return makeErrorResponse('UserGame insntance is not made', '10003');
}
return make200Response({
'sentenceJapanese': userGames[id].getNowJapanese(),
'sentenceAlphabet': userGames[id].getCompletedRoman() +
userGames[id].getRemainingRoman(),
'sentenceKana': userGames[id].getNowYomigana(),
'expectedTime': userGames[id].calcExpectedTime(),
});
}
// 1文字ごとに正誤判定を行う
if (req.method === 'POST' && pathname === '/solo/sendCharacter') {
if (!getCookies(req)['id']) {
return makeErrorResponse('id in cookies is not set', '10001');
}
const id = getCookies(req)['id'];
if (!userGames[id]) {
return makeErrorResponse('UserGame insntance is not made', '10003');
}
const reqeustJson = await req.json();
const sentChar = reqeustJson['alphabet'];
const isCorrect = userGames[id].judgeAndCalcScore(sentChar);
const response = make200Response({
'isCorrect': isCorrect,
'isCompleted': userGames[id].isCompleted(),
'score': userGames[id].getTotalScore(),
'meter': userGames[id].getMeter(),
'fireworkSize': userGames[id].calcFireworkSize(),
'enteredChars': userGames[id].getCompletedRoman(),
'notEnteredChars': userGames[id].getRemainingRoman(),
'enteredYomigana': userGames[id].getCompletedYomigana(),
'notEnteredYomigana': userGames[id].getRemainingYomigana(),
});
// 前回と違う文章が出るまで再抽選
if (userGames[id].isCompleted()) {
let targetSentence = getRandomThemeSentence(
userGames[id].getDifficulty(),
);
while (userGames[id].getNowJapanese() === targetSentence[0]) {
targetSentence = getRandomThemeSentence(
userGames[id].getDifficulty(),
);
}
userGames[id].setSentenceNow(targetSentence[0], targetSentence[1]);
}
return response;
}
// 結果取得
if (req.method === 'GET' && pathname === '/solo/getResult') {
if (!getCookies(req)['id']) {
return makeErrorResponse('id in cookies is not set', '10001');
}
const id = getCookies(req)['id'];
if (!userGames[id]) {
return makeErrorResponse('UserGame insntance is not made', '10003');
}
// Deno KVにアクセス
const kv = await Deno.openKv();
// ハイスコアを取得し、KVにないかもしくは今回の得点の方が高かったら更新
const kvHighScore =
(await kv.get(['highScore', userGames[id].getDifficulty(), id]))['value'];
if (!kvHighScore || kvHighScore < userGames[id].getTotalScore()) {
await kv.set(
['highScore', userGames[id].getDifficulty(), id],
userGames[id].getTotalScore(),
);
}
// ハイスコア再取得
const highScore =
(await kv.get(['highScore', userGames[id].getDifficulty(), id]))['value'];
// ランキングスコア取得
const rankingScore =
(await kv.get(['ranking', userGames[id].getDifficulty(), id]))['value']
?.score;
return make200Response({
'score': userGames[id].getTotalScore(),
'fireworkCount': userGames[id].calcTotalFireworks(),
'typesPerSecond': userGames[id].calcTypesPerSecond(),
'typeCount': userGames[id].getTotalCorrectTypeCount(),
'typeMissCount': userGames[id].calcTotalMissType(),
'highScore': highScore,
'rankingScore': rankingScore,
});
}
// ランキング送信
if (req.method === 'POST' && pathname === '/solo/sendRanking') {
if (!getCookies(req)['id']) {
return makeErrorResponse('id in cookies is not set', '10001');
}
const id = getCookies(req)['id'];
if (!userGames[id]) {
return makeErrorResponse('UserGame insntance is not made', '10003');
}
// POST時に送信されてユーザーネームを取得
const reqeustJson = await req.json();
const userName = reqeustJson['userName'];
// Deno KVにアクセス
const kv = await Deno.openKv();
// ハイスコア取得
const highScore =
(await kv.get(['highScore', userGames[id].getDifficulty(), id]))['value'];
// キーと値を設定
const kvKey = ['ranking', userGames[id].getDifficulty(), id];
const kvValue = { 'score': highScore, 'userName': userName };
// ランキングスコアが未登録、またはそれよりハイスコアが上ならスコアを登録する
const rankingScore = (await kv.get(kvKey))['value']?.score;
if (
(highScore || highScore === 0) &&
(!rankingScore || highScore > rankingScore)
) {
const kvResult = await kv.set(kvKey, kvValue);
return make200Response({
'isSuccessful': kvResult['ok'],
});
} // ハイスコアがランキングスコア以下なら登録しない
else {
return make200Response({
'isSuccessful': false,
});
}
}
if (req.method === 'POST' && pathname === '/solo/getRanking') {
// 送られてきた難易度を取得
const reqeustJson = await req.json();
const difficulty = reqeustJson['difficulty'];
// 送られた難易度が規定値以外ならエラー
if (!DIFFICULTY_STRING.includes(difficulty)) {
return makeErrorResponse('difficulty is different!', '10004');
}
// Deno KVにアクセス
const kv = await Deno.openKv();
// ランキングに登録されているkeyを取得してリスト化
const listResult = await kv.list({ prefix: ['ranking', difficulty] });
const listRanking = [];
for await (const item of listResult) {
listRanking.push(item['value']);
}
// 降順ソート
listRanking.sort((a, b) => (a.score > b.score ? -1 : 1));
// 上位10位までスライスして返答
return make200Response({
'top10Results': listRanking.slice(0, OUTPUT_TOPRANKING_NUMBER),
});
}
return serveDir(req, {
fsRoot: 'public',
urlRoot: '',
showDirListing: true,
enableCors: true,
});
});