Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (117 loc) · 5.11 KB
/
Copy pathscript.js
File metadata and controls
132 lines (117 loc) · 5.11 KB
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
/* inject — type the hostile process names, never finish a quarantined one. */
(function () {
"use strict";
var SAFE = ["socket","daemon","kernel","packet","cipher","buffer","router","syntax","binary","module",
"thread","vector","stream","commit","branch","matrix","opcode","memory","script","runtime",
"sandbox","payload","gateway","handler","compile","segment","pointer","decoder"];
var BAD = ["rootkit","keylog","botnet","ransom","trojan","wiper","hijack","spyware","backdoor","exploit"];
var $ = function (i) { return document.getElementById(i); };
var field = $("field"), overlay = $("overlay"), card = $("card"), buf = $("buf"),
echo = $("echo"), line = document.querySelector(".promptline"), traceBox = document.querySelector(".trace");
var vScore = $("v-score"), vCombo = $("v-combo"), vBest = $("v-best"), vHp = $("v-hp"), vWave = $("wave"), bar = $("bar");
function load(k,d){try{var v=localStorage.getItem(k);return v===null?d:v}catch(e){return d}}
function save(k,v){try{localStorage.setItem(k,v)}catch(e){}}
var best = parseInt(load("inj.best","0"),10) || 0;
vBest.textContent = best;
var live = [], score = 0, combo = 1, hp = 100, wave = 1, kills = 0,
running = false, last = 0, nextSpawn = 0;
function hud() {
vScore.textContent = score;
vCombo.textContent = "x" + combo;
vWave.textContent = "wave " + wave;
vHp.textContent = Math.max(0, Math.round(hp)) + "%";
bar.style.width = Math.max(0, hp) + "%";
traceBox.classList.toggle("low", hp <= 35);
}
function spawn() {
var bad = Math.random() < Math.min(0.32, 0.1 + wave * 0.02);
var pool = bad ? BAD : SAFE, taken = live.map(function (e) { return e.word.charAt(0); });
var w = null;
for (var g = 0; g < 24; g++) {
var c = pool[(Math.random() * pool.length) | 0];
if (taken.indexOf(c.charAt(0)) < 0) { w = c; break; }
}
if (!w) return;
var el = document.createElement("span");
el.className = "wd" + (bad ? " bad" : "");
el.textContent = (bad ? "# " : "") + w;
el.style.left = (4 + Math.random() * 62) + "%";
el.style.top = "-6%";
field.appendChild(el);
live.push({ el: el, word: w, bad: bad, y: -6, sp: 8 + Math.random() * 4 + wave * 1.3 });
}
function drop(e, i) { e.el.remove(); live.splice(i, 1); }
function hurt(n) {
hp -= n; combo = 1;
field.classList.add("hit");
setTimeout(function () { field.classList.remove("hit"); }, 220);
if (hp <= 0) { hp = 0; over(); }
hud();
}
function kill(e, i) {
if (e.bad) { drop(e, i); hurt(18); return; }
score += e.word.length * 10 * combo;
combo = Math.min(9, combo + 1);
kills++;
if (kills % 10 === 0) wave++;
drop(e, i);
hud();
}
function mark(v) {
for (var i = 0; i < live.length; i++) {
live[i].el.classList.toggle("lock", v.length > 0 && live[i].word.indexOf(v) === 0);
}
}
function onType() {
var v = buf.value.toLowerCase().replace(/[^a-z]/g, "");
buf.value = v; echo.textContent = v; line.classList.remove("err");
if (!running || !v) { mark(v); return; }
for (var i = 0; i < live.length; i++) {
if (live[i].word === v) { kill(live[i], i); buf.value = ""; echo.textContent = ""; mark(""); return; }
}
var any = live.some(function (e) { return e.word.indexOf(v) === 0; });
if (!any) {
buf.value = ""; echo.textContent = ""; line.classList.add("err");
combo = 1; hud(); mark("");
setTimeout(function () { line.classList.remove("err"); }, 200);
} else mark(v);
}
buf.addEventListener("input", onType);
field.addEventListener("pointerdown", function () { if (running) buf.focus(); });
function loop(ts) {
requestAnimationFrame(loop);
if (!running) { last = ts; return; }
var dt = Math.min(0.08, (ts - last) / 1000); last = ts;
nextSpawn -= dt;
if (nextSpawn <= 0) { spawn(); nextSpawn = Math.max(0.5, 1.7 - wave * 0.09); }
for (var i = live.length - 1; i >= 0; i--) {
var e = live[i];
e.y += e.sp * dt;
e.el.style.top = e.y + "%";
if (e.y > 96) { var bad = e.bad; drop(e, i); if (!bad) hurt(12); }
}
}
requestAnimationFrame(loop);
function over() {
running = false;
if (score > best) { best = score; save("inj.best", String(best)); vBest.textContent = best; }
card.innerHTML = '<h1 class="bad">kernel down</h1><p>They got through at wave ' + wave + '.</p>' +
'<div class="score-rows"><div><span>score</span><b>' + score + '</b></div>' +
'<div><span>best</span><b>' + best + '</b></div></div>' +
'<button class="btn" id="go" type="button">Run again</button>';
overlay.hidden = false;
$("go").addEventListener("click", reset, { once: true });
}
function reset() {
live.forEach(function (e) { e.el.remove(); });
live = []; score = 0; combo = 1; hp = 100; wave = 1; kills = 0; nextSpawn = 0.4;
buf.value = ""; echo.textContent = "";
hud(); start();
}
function start() {
overlay.hidden = true; running = true; last = performance.now();
buf.focus({ preventScroll: true });
}
hud();
$("go").addEventListener("click", function () { nextSpawn = 0.4; start(); }, { once: true });
})();