summaryrefslogtreecommitdiff
path: root/kartik/online
diff options
context:
space:
mode:
Diffstat (limited to 'kartik/online')
-rwxr-xr-xkartik/online/global.js374
-rwxr-xr-xkartik/online/levels.txt200
-rwxr-xr-xkartik/online/server.json4
3 files changed, 578 insertions, 0 deletions
diff --git a/kartik/online/global.js b/kartik/online/global.js
new file mode 100755
index 0000000..0cdb6f4
--- /dev/null
+++ b/kartik/online/global.js
@@ -0,0 +1,374 @@
+global.pingStart = null;
+global.connecting = false;
+global.role = null;
+global.online = true;
+global.guestInfo = {
+ hostCar: null,
+ guestCar: null,
+ circuit: null,
+ music: null
+};
+global.onlineInitDone = false;
+
+function max(input) {
+ if (toString.call(input) !== "[object Array]")
+ return false;
+ return Math.max.apply(null, input);
+}
+
+pingHistory = [];
+function updatePing(ping) {
+ if (ping < 100000) {
+ document.getElementById('ping').innerText = ping + " ms";
+
+ pingHistory.push(ping);
+ while (pingHistory.length > 70) {
+ pingHistory.shift();
+ }
+
+ dom = "";
+ highest = max(pingHistory);
+ pingHistory.forEach((p) => {
+ high = 1
+ perc = (p/highest)*100
+ if (ping > 70 && ping < 150) {
+ dom = dom + `<span style="display: inline-block;background: orange;width: ${high}px;height: ${perc}%;"></span>`;
+ } else if (ping > 150) {
+ dom = dom + `<span style="display: inline-block;background: red;width: ${high}px;height: ${perc}%;"></span>`;
+ } else {
+ dom = dom + `<span style="display: inline-block;background: limegreen;width: ${high}px;height: ${perc}%;"></span>`;
+ }
+ })
+
+ document.getElementById('ping-chart').innerHTML = dom;
+ }
+}
+
+window.addEventListener('load', () => {
+ document.getElementById('credits').style.backgroundColor = "#000000";
+ document.getElementById('cars-n1').innerText = lang.online.car0;
+ document.getElementById('cars-n2').innerText = lang.online.car1;
+
+ class MessageBuffer {
+ constructor(delimiter) {
+ this.delimiter = delimiter
+ this.buffer = ""
+ }
+
+ isFinished() {
+ if (
+ this.buffer.length === 0 ||
+ this.buffer.indexOf(this.delimiter) === -1
+ ) {
+ return true
+ }
+ return false
+ }
+
+ push(data) {
+ this.buffer += data
+ }
+
+ getMessage() {
+ const delimiterIndex = this.buffer.indexOf(this.delimiter)
+ if (delimiterIndex !== -1) {
+ const message = this.buffer.slice(0, delimiterIndex)
+ this.buffer = this.buffer.replace(message + this.delimiter, "")
+ return message
+ }
+ return null
+ }
+
+ handleData() {
+ /**
+ * Try to accumulate the buffer with messages
+ *
+ * If the server isnt sending delimiters for some reason
+ * then nothing will ever come back for these requests
+ */
+ const message = this.getMessage()
+ return message
+ }
+ }
+
+ const sampleData = {
+ _type: "init",
+ name: "Kartik Core",
+ version: require('../package.json').version,
+ id: null,
+ modded: false
+ }
+
+ global.clientWriter = (data) => {
+ client.write(data + "\n");
+ }
+
+ function crash(e) {
+ console.error(e);
+ if (!quitting) {
+ location.href = "online.html#" + btoa(lang.online.error.connection);
+ }
+ }
+
+ var net = require('net');
+
+ var host = require('../online/server.json').hostname;
+ var port = require('../online/server.json').port;
+
+ global.client = new net.Socket();
+ client.initialized = false;
+
+ client.connect(port, host, () => {
+ console.log("Connected to " + host + ":" + port);
+ clientWriter(JSON.stringify(sampleData) + "|");
+ setInterval(() => {
+ if (role === null) {
+ clientWriter(JSON.stringify({
+ _type: "ping"
+ }) + "|")
+ } else {
+ clientWriter(JSON.stringify({
+ _type: "ipc",
+ action: "Ping",
+ message: null
+ }) + "|")
+ }
+ global.pingCrash = setTimeout(() => {
+ location.href = "online.html#" + btoa(lang.online.timeout);
+ }, 10000);
+ global.pingStart = new Date();
+ }, 1000)
+ })
+
+ let received = new MessageBuffer("\n")
+ client.on("data", chunk => {
+ received.push(chunk)
+ while (!received.isFinished()) {
+ const data = received.handleData()
+
+ raw = data.toString().replaceAll("}{", "}|{");
+ datas = raw.split("|").filter(i => i.trim() !== "");
+ datas.forEach((data) => {
+ try {
+ inf = JSON.parse(data);
+ } catch (e) {
+ console.dir(data);
+ throw e;
+ }
+
+ if (typeof inf['_type'] != "string") {
+ crash(new Error("Invalid JSON data"));
+ }
+ if (!client.initialized) {
+ switch (inf['_type']) {
+ case "init":
+ if (inf['name'] !== "Kartik Server") {
+ crash(new Error("Invalid server"));
+ }
+ console.log("Connection initialized. Server running " + inf.name + " version " + inf.version + ", client ID " + inf.id);
+ document.getElementById("serveraddr").innerText = host + ":" + port;
+ if (inf.version.endsWith("-iridium")) {
+ document.getElementById("servername").innerText = "Iridium " + inf.version.substr(0, inf.version.length - "-iridium".length);
+ document.getElementById("servericon").src = "../logo/iridium.png";
+ } else {
+ document.getElementById("servername").innerText = "Kartik Legacy Server " + inf.version;
+ document.getElementById("servericon").src = "../logo/server.png";
+ }
+ document.getElementById('yourid').innerText = inf.id.toUpperCase();
+ document.getElementById('intro').style.display = "";
+ document.getElementById('connecting').style.display = "none";
+ document.getElementById('loading').style.display = "none";
+ client.initialized = true;
+ break;
+ case "error":
+ console.log(inf['type'] + ": " + inf['message']);
+ break;
+ default:
+ crash(new Error("Trying to receive data but client not initialized"));
+ break;
+ }
+ } else {
+ switch (inf['_type']) {
+ case "init":
+ crash(new Error("Trying to initialize client but client is already initialized"));
+ break;
+ case "error":
+ console.log(inf['type'] + ": " + inf['message']);
+ location.href = "online.html#" + btoa(inf['type'] + ": " + inf['message']);
+ break;
+ case "linked":
+ console.log("Now hooked into link: (H) " + inf['ids']['host'] + " <-> " + inf['ids']['guest'] + " (G)");
+ document.getElementById('intro').style.display = "none";
+ document.getElementById('connecting').style.display = "none";
+ document.getElementById('loading').style.display = "";
+ document.getElementById('loading').innerText = lang.online.generate;
+ global.role = inf['role'];
+ if (role === "host") {
+ startHooks.forEach((hook) => {
+ hook(this);
+ })
+ $("#online-login").fadeOut(200);
+ }
+ break;
+ default:
+ if (inf['_type'] === "ipc" && inf['action'] === "Ping") {
+ clientWriter(JSON.stringify({
+ _type: "ipc",
+ action: "Pong",
+ message: null
+ }) + "|")
+ return;
+ }
+ if ((inf['_type'] === "ipc" && inf['action'] === "Pong") || inf['_type'] === "pong") {
+ pingEnd = new Date();
+ ping = Math.round(pingEnd - pingStart);
+ global.pingStart = null;
+ clearTimeout(global.pingCrash);
+ updatePing(ping);
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'] === "posTop") {
+ try {
+ document.getElementById('car1').style.top = inf['message'];
+ } catch (e) {}
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'] === "progressLaps") {
+ try {
+ document.getElementById("laps-car1").innerText = inf['message'];
+ } catch (e) {}
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'] === "progressLose") {
+ global.quitting = true;
+ location.href = "win.html?sp#car1";
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'] === "posLeft") {
+ try {
+ document.getElementById('car1').style.left = inf['message'];
+ } catch (e) {}
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'] === "gameIsReady") {
+ startgame();
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'] === "abort") {
+ location.href = "online.html#" + btoa(lang.online.aborted);
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'] === "posRot") {
+ try {
+ document.getElementById('car1').style.transform = inf['message'];
+ } catch (e) {}
+ return;
+ }
+ if (inf['_type'] === "ipc" && inf['action'].startsWith("RaceData:")) {
+ rd = inf['action'].substr(9);
+ switch (rd) {
+ case "hostCar":
+ guestInfo.hostCar = inf['message'];
+ if (guestInfo.music !== null && guestInfo.circuit !== null && guestInfo.guestCar !== null && guestInfo.hostCar !== null && !onlineInitDone) {
+ onlineInitDone = true;
+ startHooks.forEach((hook) => {
+ hook(this);
+ })
+ $("#online-login").fadeOut(200);
+ }
+ break;
+ case "guestCar":
+ guestInfo.guestCar = inf['message'];
+ if (guestInfo.music !== null && guestInfo.circuit !== null && guestInfo.guestCar !== null && guestInfo.hostCar !== null && !onlineInitDone) {
+ onlineInitDone = true;
+ startHooks.forEach((hook) => {
+ hook(this);
+ })
+ $("#online-login").fadeOut(200);
+ }
+ break;
+ case "circuit":
+ guestInfo.circuit = inf['message'];
+ if (guestInfo.music !== null && guestInfo.circuit !== null && guestInfo.guestCar !== null && guestInfo.hostCar !== null && !onlineInitDone) {
+ onlineInitDone = true;
+ startHooks.forEach((hook) => {
+ hook(this);
+ })
+ $("#online-login").fadeOut(200);
+ }
+ break;
+ case "music":
+ guestInfo.music = inf['message'];
+ if (guestInfo.music !== null && guestInfo.circuit !== null && guestInfo.guestCar !== null && guestInfo.hostCar !== null && !onlineInitDone) {
+ onlineInitDone = true;
+ startHooks.forEach((hook) => {
+ hook(this);
+ })
+ $("#online-login").fadeOut(200);
+ }
+ break;
+ }
+ return;
+ }
+ break;
+ }
+ }
+ })
+ }
+ })
+
+ client.on('close', () => {
+ console.log("Kicked from server");
+ })
+
+ client.on('error', (e) => {
+ switch (e.code) {
+ case "ECONNREFUSED":
+ location.href = "online.html#" + btoa(lang.online.unable);
+ break;
+ default:
+ location.href = "online.html#" + btoa(lang.online.internal);
+ break;
+ }
+ crash(e);
+ })
+
+ setInterval(() => {
+ if (pingStart !== null && new Date() - pingStart >= 10000) {
+ location.href = "online.html#" + btoa(lang.online.timeout);
+ }
+ }, 50)
+})
+
+function checkOnlineLogin() {
+ document.getElementById('theirid').value = document.getElementById('theirid').value.toUpperCase();
+ if (/[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]/gm.test(document.getElementById('theirid').value)) {
+ document.getElementById('theirid').value = "";
+ }
+ if (document.getElementById('theirid').value.length === 8) {
+ if (document.getElementById('theirid').value === document.getElementById('yourid').innerText) {
+ document.getElementById('theirid').value = "";
+ } else {
+ if (!connecting) {
+ connecting = true;
+ document.getElementById('intro').style.display = "none";
+ document.getElementById('connecting').style.display = "none";
+ document.getElementById('loading').style.display = "";
+ console.log("Linking to client " + document.getElementById('theirid').value + "...")
+ clientWriter(JSON.stringify({
+ _type: "link",
+ client: document.getElementById('theirid').value.toLowerCase()
+ }));
+ }
+ }
+ }
+}
+
+$("#theirid").keydown(function(e) {
+ if (e.keyCode === 27) { // esc
+ $("body").fadeOut(200);
+ setTimeout(() => {
+ location.href = "menu.html?back";
+ }, 250)
+ }
+}) \ No newline at end of file
diff --git a/kartik/online/levels.txt b/kartik/online/levels.txt
new file mode 100755
index 0000000..6566fea
--- /dev/null
+++ b/kartik/online/levels.txt
@@ -0,0 +1,200 @@
+1:1
+2:2
+3:3
+4:4
+5:5
+6:7
+7:9
+8:11
+9:13
+10:16
+11:19
+12:22
+13:25
+14:29
+15:33
+16:37
+17:41
+18:46
+19:51
+20:56
+21:61
+22:67
+23:73
+24:79
+25:85
+26:92
+27:99
+28:106
+29:113
+30:121
+31:129
+32:137
+33:145
+34:154
+35:163
+36:172
+37:181
+38:191
+39:201
+40:211
+41:221
+42:232
+43:243
+44:254
+45:265
+46:277
+47:289
+48:301
+49:313
+50:326
+51:339
+52:352
+53:365
+54:379
+55:393
+56:407
+57:421
+58:436
+59:451
+60:466
+61:481
+62:497
+63:513
+64:529
+65:545
+66:562
+67:579
+68:596
+69:613
+70:631
+71:649
+72:667
+73:685
+74:704
+75:723
+76:742
+77:761
+78:781
+79:801
+80:821
+81:841
+82:862
+83:883
+84:904
+85:925
+86:947
+87:969
+88:991
+89:1013
+90:1036
+91:1059
+92:1082
+93:1105
+94:1129
+95:1153
+96:1177
+97:1201
+98:1226
+99:1251
+100:1276
+101:1301
+102:1327
+103:1353
+104:1379
+105:1405
+106:1432
+107:1459
+108:1486
+109:1513
+110:1541
+111:1569
+112:1597
+113:1625
+114:1654
+115:1683
+116:1712
+117:1741
+118:1771
+119:1801
+120:1831
+121:1861
+122:1892
+123:1923
+124:1954
+125:1985
+126:2017
+127:2049
+128:2081
+129:2113
+130:2146
+131:2179
+132:2212
+133:2245
+134:2279
+135:2313
+136:2347
+137:2381
+138:2416
+139:2451
+140:2486
+141:2521
+142:2557
+143:2593
+144:2629
+145:2665
+146:2702
+147:2739
+148:2776
+149:2813
+150:2851
+151:2889
+152:2927
+153:2965
+154:3004
+155:3043
+156:3082
+157:3121
+158:3161
+159:3201
+160:3241
+161:3281
+162:3322
+163:3363
+164:3404
+165:3445
+166:3487
+167:3529
+168:3571
+169:3613
+170:3656
+171:3699
+172:3742
+173:3785
+174:3829
+175:3873
+176:3917
+177:3961
+178:4006
+179:4051
+180:4096
+181:4141
+182:4187
+183:4233
+184:4279
+185:4325
+186:4372
+187:4419
+188:4466
+189:4513
+190:4561
+191:4609
+192:4657
+193:4705
+194:4754
+195:4803
+196:4852
+197:4901
+198:4951
+199:5001
+200:5051 \ No newline at end of file
diff --git a/kartik/online/server.json b/kartik/online/server.json
new file mode 100755
index 0000000..4ba17eb
--- /dev/null
+++ b/kartik/online/server.json
@@ -0,0 +1,4 @@
+{
+ "hostname": "kartik.hopto.org",
+ "port": 8408
+}