-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.html
362 lines (344 loc) · 15.7 KB
/
index.html
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=1300, initial-scale=1">
<title>CyberCook</title>
<link rel="stylesheet" href="static/pure.min.css">
<link rel="stylesheet" href="static/main.css">
<script src="static/axios.min.js"></script>
<script src="static/vue.min.js"></script>
<script src="static/dist/cybercook-bundle.js"></script>
</head>
<body>
<div id="app">
<!-- 地图 -->
<div :style="{...psStyle(4, 4, 302, 1212), ...borderStyle()}">
<div :style="{...psStyle(0, 0, 300, 1210), clipPath: 'inset(0)', cursor: 'grab'}" v-on:mousedown="mapMouseDown">
<div :style="{...positionStyle(height/2-centerRow*scale-scale/2, width/2-centerColumn*scale-scale/2)}">
<div v-for="(rowData,row) in state.map" :key="row" :style="{...positionStyle(row*scale, 0)}">
<div v-for="(data,column) in rowData" :key="column" :style="{...positionStyle(0, column*scale)}" v-on:mouseover="hover={row,column,data}" v-on:mouseout="hover=null">
<div v-for="(sprite,index) in getSprites(data)" :key="index" :style="{...spriteStyle(scale, scale, sprite)}"></div>
<div v-if="row===state.posRow && column===state.posColumn" :style="{...spriteStyle(scale, scale, spriteMap.Position)}"></div>
<div v-if="hover && row===hover.row && column===hover.column" :style="{...spriteStyle(scale, scale, spriteMap.Hover)}"></div>
<div :style="{...psStyle(0, 0, scale, scale)}"></div>
</div>
</div>
</div>
</div>
</div>
<!-- 提示 -->
<div :style="{...psStyle(310, 4, 602, 200)}">
<!-- 菜谱 -->
<div :style="{...psStyle(0, 0, 94, 200), ...borderStyle()}">
<pre style="margin: 0; white-space: pre-wrap">{{ `第 ${level} 关 第 ${day} 天\n今日菜谱物品 ID:${state.recipe.join(',')}` }}</pre>
</div>
<!-- 手上物品 -->
<div :style="{...psStyle(98, 0, 250, 200), ...borderStyle()}">
<pre style="margin: 0; white-space: pre-wrap">{{ `当前位置:(${state.posRow},${state.posColumn})`+'\n'+getInventoryText(state.inventory) }}</pre>
</div>
<!-- 悬浮提示 -->
<div :style="{...psStyle(352, 0, 250, 200), ...borderStyle()}">
<pre v-show="hover" style="margin: 0; white-space: pre-wrap">{{ hover && getHover(hover.data) }}</pre>
</div>
</div>
<!-- 执行记录 -->
<div :style="{...psStyle(310, 208, 602, 300), ...borderStyle()}">
<span :style="{...positionStyle(4, 4), fontWeight: 'bold'}">程序执行记录</span>
<div :style="{...psStyle(34, 0, 532, 298), overflowY: 'scroll'}">
<div v-for="(item,index) in history" :key="index" :style="{width: '278px', marginTop: '8px', backgroundColor: historyIndex===index?'#ccc':'#fff', cursor: 'pointer'}" v-on:click="historyIndex=index">
<span>第 {{ item.pc }} 行:{{ item.action }}</span>
</div>
</div>
<span :style="{...psStyle(570, 4, 26, 290), ...borderStyle(), backgroundColor: historyIndex===null?'#ccc':'#fff', cursor: 'pointer'}" v-on:click="historyIndex=null">显示最新状态</span>
</div>
<!-- 程序列表 -->
<div :style="{...psStyle(310, 512, 602, 300), ...borderStyle()}">
<span :style="{...positionStyle(4, 4), fontWeight: 'bold'}">程序列表 {{ programs.length }} / {{ CyberCook.maxProgram }}</span>
<div :style="{...psStyle(34, 0, 566, 298), overflowY: 'scroll'}">
<div v-for="(item,index) in programs" :key="index" :style="{...psStyle(index*34, 0, 34, 278)}">
<span :style="{...psStyle(4, 4, 24, 40), ...borderStyle(), textAlign: 'center', cursor: 'pointer'}" v-on:click="editorName=item.name;editorContent=item.content">打开</span>
<span :style="{...psStyle(4, 54, 24, 40), ...borderStyle(), textAlign: 'center', cursor: 'pointer'}" v-on:click="programs.splice(index,1)">删除</span>
<span v-if="item.learned" :style="{...psStyle(4, 104, 24, 40), ...borderStyle(), textAlign: 'center', cursor: 'pointer'}" v-on:click="runProgram(item)">执行</span>
<span v-if="!item.learned" :style="{...psStyle(4, 94, 24, 60), color: '#888', textAlign: 'center'}">学习中</span>
<span :style="{...positionStyle(4, 154)}">{{ item.name }}</span>
</div>
</div>
</div>
<!-- 编辑器 -->
<div :style="{...psStyle(310, 816, 602, 400), ...borderStyle()}">
<span :style="{...positionStyle(4, 4), fontWeight: 'bold'}">编写程序</span>
<textarea v-model="editorContent" :style="{...psStyle(34, 0, 502, 398), overflowY: 'scroll', whiteSpace: 'pre-warp'}"></textarea>
<span :style="{...positionStyle(540, 4)}">程序名:</span>
<input v-model="editorName" :style="{...psStyle(540, 70, 26, 328)}">
<span :style="{...psStyle(570, 4, 26, 193), ...borderStyle(), cursor: 'pointer', textAlign: 'center'}" v-on:click="saveProgram">保存</span>
<span :style="{...psStyle(570, 201, 26, 193), ...borderStyle(), cursor: 'pointer', textAlign: 'center'}" v-on:click="nextDay">下一天</span>
</div>
</div>
<script>
const spritesUrl = 'static/sprites.png';
const spritesHeight = 96;
const spritesWidth = 160;
const spriteMap = {
ItemStack: [0, 0, 32, 32],
Plate: [0, 32, 32, 32],
Pot: [0, 64, 32, 32],
Position: [0, 96, 32, 32],
Hover: [0, 128, 32, 32],
Item: Array(256).fill(0).map((_, i) => i <= 3 ? [32, i*32, 32, 32] : [32, 4*32, 32, 32]),
ItemFaucet: Array(256).fill(0).map((_, i) => i <= 3 ? [64, i*32, 32, 32] : [64, 4*32, 32, 32]),
};
const initialLevel = Number(decodeURIComponent(location.hash.slice(1)));
let token = new URLSearchParams(window.location.search).get('token');
window.history.replaceState({}, '', '/'+location.hash);
if (token) {
localStorage.setItem('token', token);
} else {
token = localStorage.getItem('token');
}
if (!token) {
token = prompt('Token: ');
localStorage.setItem('token', token);
}
const initialPrograms = [
{name: '示例 1', learned: true, indexInBackup: 0, content: '向右 1 步\n拿起 1 个物品\n向下 2 步\n放下 1 个物品\n向下 1 步\n放下盘子'},
{name: '示例 2', learned: true, indexInBackup: 1, content: '向右 1 步\n拿起 10 个物品\n向下 1 步\n向左 1 步\n放下 1 个物品'},
{name: '示例 3', learned: true, indexInBackup: 2, content: '向右 1 步\n拿起 5 个物品\n向下 1 步\n放下 1 个物品\n如果手上的物品大于等于 3 向上跳转 2 行\n放下盘子'}
];
const initialVm = CyberCook.nextDay(initialLevel, initialPrograms.map(i => i.content));
app = new Vue({
el: "#app",
data: {
level: initialLevel,
day: 0,
vm: initialVm,
historyVm: null,
centerRow: initialVm.info().posRow,
centerColumn: initialVm.info().posColumn,
scale: 32,
width: 1210,
height: 300,
dragX: 0,
dragY: 0,
hover: null,
programs: initialPrograms,
programsBackup: [...initialPrograms],
programExecutions: [],
history: [],
historyIndex: null,
editorName: '',
editorContent: '',
},
computed: {
state() {
if (this.historyIndex === null) {
return this.vm.info();
} else {
return CyberCook.historyStep(this.historyVm, this.history.slice(0, this.historyIndex+1).map(i => i.action).join('\n')).vm.info();
}
},
},
methods: {
positionStyle(top, left) {
return {
position: 'absolute',
top: top+'px',
left: left+'px',
};
},
sizeStyle(height, width) {
return {
height: height+'px',
width: width+'px',
};
},
psStyle(top, left, height, width) {
return {
...this.positionStyle(top, left),
...this.sizeStyle(height, width),
};
},
borderStyle() {
return {
border: '1px solid #000',
};
},
spriteStyle(tHeight, tWidth, [sTop, sLeft, sHeight, sWidth]) {
const hScale = tWidth / sWidth;
const vScale = tHeight / sHeight;
return {
...this.positionStyle(0, 0),
...this.sizeStyle(tHeight, tWidth),
background: `url('${spritesUrl}') ${-sLeft*hScale}px ${-sTop*vScale}px`,
backgroundSize: `${spritesWidth*hScale}px ${spritesHeight*vScale}px`,
};
},
getSprites(x) {
if (x.type === 'ItemStack') {
if (x.withPlate) {
if (x.items.count) {
return [spriteMap.ItemStack, spriteMap.Plate, spriteMap.Item[x.items.itemId]];
} else {
return [spriteMap.ItemStack, spriteMap.Plate];
}
} else {
return [spriteMap.ItemStack];
}
} else if (x.type === 'ItemFaucet') {
return [spriteMap.ItemStack, spriteMap.ItemFaucet[x.items.itemId]];
} else if (x.type === 'Pot') {
return [spriteMap.ItemStack, spriteMap.Pot];
} else {
return [];
}
},
getInventoryText(x) {
if (x.type === 'ItemStack') {
if (x.withPlate) {
if (x.items.count) {
return `手上有盘子\n物品 ID:${x.items.itemId}\n物品数量:${x.items.count}\n\n可以放下物品\n可以拿起物品(同 ID)\n可以放下盘子\n不能拿起盘子`;
} else {
return '手上有空盘子\n\n不能放下物品\n可以拿起物品(任何 ID)\n可以放下盘子\n不能拿起盘子';
}
} else {
return '手上没有盘子\n\n可以放下物品\n可以拿起物品(但食物会被弄脏而浪费掉)\n不能放下盘子\n可以拿起盘子';
}
} else {
return '???';
}
},
getHover(x) {
if (x.type === 'ItemStack') {
if (x.withPlate) {
if (x.items.count) {
return `盘子\n物品 ID:${x.items.itemId}\n物品数量:${x.items.count}\n\n可以放下物品(同 ID)\n可以拿起物品\n不能放下盘子\n可以拿起盘子`;
} else {
return '空盘子\n\n可以放下物品\n不能拿起物品\n不能放下盘子\n可以拿起盘子';
}
} else {
return '空地\n\n可以放下物品(但食物会被弄脏而浪费掉)\n不能拿起物品\n可以放下盘子\n不能拿起盘子';
}
} else if (x.type === 'ItemFaucet') {
return `物品源\n物品 ID:${x.items.itemId}\n物品数量:无限\n\n可以放下物品(任何 ID)\n可以拿起物品\n不能放下盘子\n不能拿起盘子`;
} else if (x.type === 'Pot') {
return `锅\n今日菜谱物品 ID:${x.extra.recipe.join(',')}\n已放入物品 ID:${x.extra.itemsInPot.join(',')}\n\n可以放下物品(一次一个)\n不能拿起物品\n不能放下盘子\n不能拿起盘子`;
} else {
return '???';
}
},
mapMouseDown(e) {
e = e || window.event;
e.preventDefault();
this.dragX = e.clientX;
this.dragY = e.clientY;
document.onmouseup = () => {
document.onmouseup = null;
document.onmousemove = null;
};
document.onmousemove = (e) => {
e = e || window.event;
e.preventDefault();
this.centerRow += (this.dragY - e.clientY) / this.scale;
this.centerColumn += (this.dragX - e.clientX) / this.scale;
this.dragX = e.clientX;
this.dragY = e.clientY;
};
},
saveProgram() {
const r = CyberCook.verifyProgram(this.editorContent);
if (r.result) {
this.programs.push({name: this.editorName, learned: false, indexInBackup: null, content: this.editorContent});
} else {
alert(r.msg);
}
},
runProgram(program) {
this.programExecutions.push(program.indexInBackup);
let {vm, finished, win, msg, log} = CyberCook.step(this.vm, program.content);
this.historyVm = this.vm;
this.vm = vm;
this.centerRow = vm.info().posRow;
this.centerColumn = vm.info().posColumn;
this.history = log;
this.historyIndex = null;
if (!finished) {
alert(msg);
}
if (win) {
console.log('你赢了,正在获取 flag……');
console.log([...this.programsBackup]);
console.log([...this.programExecutions]);
axios.post('/verify', {
token,
level: this.level,
seed: CyberCook.genRandomState(this.level, this.programsBackup.map(i => i.content)).nhash,
programs: this.programsBackup.map(i => i.content),
executions: this.programExecutions,
}).then(r => {
console.log(r);
alert(r.data.message);
}).catch(e => {
console.log(e);
alert('error');
});
}
},
nextDay() {
if (this.programs.length <= CyberCook.maxProgram) {
const vm = CyberCook.nextDay(this.level, this.programs.map(i => i.content));
this.day++;
this.programs = this.programs.map((item, index) => ({...item, learned: true, indexInBackup: index}));
this.programsBackup = [...this.programs];
this.programExecutions = [];
this.historyVm = null;
this.vm = vm;
this.centerRow = vm.info().posRow;
this.centerColumn = vm.info().posColumn;
this.history = [];
this.historyIndex = null;
} else {
alert('程序数量超过上限,无法进入下一天');
}
},
},
});
// 以下接口可以用于在 console 上用程序控制一些操作
// 获取最新状态
function debugGetState() {
return app.vm.info();
}
// 获取程序列表
function debugGetPrograms() {
return app.programs;
}
// 保存程序
function debugSaveProgram(name, content) {
const r = CyberCook.verifyProgram(content);
if (r.result) {
app.programs.push({name, learned: false, indexInBackup: null, content});
} else {
alert(r.msg);
}
}
// 删除程序
function debugDeleteProgram(name) {
const oldPrograms = app.programs;
const newPrograms = [];
for (let i = 0; i < oldPrograms.length; i++) {
if (oldPrograms[i].name !== name) {
newPrograms.push(oldPrograms[i]);
}
}
app.programs = newPrograms;
}
// 执行程序
function debugRunProgram(program) {
app.runProgram(program);
}
// 下一天
function debugNextDay() {
app.nextDay();
}
</script>
</body>
</html>