mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-02-05 18:11:23 +01:00
Merge 103153ed86 into 8ba29b2561
This commit is contained in:
commit
2678a6e241
6 changed files with 170 additions and 0 deletions
5
public/api/catch-error.json
Normal file
5
public/api/catch-error.json
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"ok": false,
|
||||||
|
"info": "server not enabled",
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
5
public/api/info.json
Normal file
5
public/api/info.json
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"ok": false,
|
||||||
|
"info": "server not enabled",
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
5
public/api/load-map.json
Normal file
5
public/api/load-map.json
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"ok": false,
|
||||||
|
"info": "server not enabled",
|
||||||
|
"data": {}
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,7 @@ async function saveMap(method) {
|
||||||
if (method === "dropbox") saveToDropbox(mapData, filename);
|
if (method === "dropbox") saveToDropbox(mapData, filename);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ERROR && console.error(error);
|
ERROR && console.error(error);
|
||||||
|
window.ServerAPI.postData("api/catch-error.json", error.toString(), {point: "saveMap", timestamp: Date.now()});
|
||||||
alertMessage.innerHTML = /* html */ `An error is occured on map saving. If the issue persists, please copy the message below and report it on ${link(
|
alertMessage.innerHTML = /* html */ `An error is occured on map saving. If the issue persists, please copy the message below and report it on ${link(
|
||||||
"https://github.com/Azgaar/Fantasy-Map-Generator/issues",
|
"https://github.com/Azgaar/Fantasy-Map-Generator/issues",
|
||||||
"GitHub"
|
"GitHub"
|
||||||
|
|
@ -167,6 +168,7 @@ function prepareMapData() {
|
||||||
async function saveToStorage(mapData, showTip = false) {
|
async function saveToStorage(mapData, showTip = false) {
|
||||||
const blob = new Blob([mapData], { type: "text/plain" });
|
const blob = new Blob([mapData], { type: "text/plain" });
|
||||||
await ldb.set("lastMap", blob);
|
await ldb.set("lastMap", blob);
|
||||||
|
window.ServerAPI.postData("api/load-map.json", blob, {showTip: showTip});
|
||||||
showTip && tip("Map is saved to the browser storage", false, "success");
|
showTip && tip("Map is saved to the browser storage", false, "success");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,6 +212,7 @@ async function initiateAutosave() {
|
||||||
lastSavedAt = Date.now();
|
lastSavedAt = Date.now();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ERROR && console.error(error);
|
ERROR && console.error(error);
|
||||||
|
window.ServerAPI.postData("api/catch-error.json", error.toString(), {point: "initiateAutosave", timestamp: Date.now()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
151
public/modules/io/server-api.js
Normal file
151
public/modules/io/server-api.js
Normal file
|
|
@ -0,0 +1,151 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
window.ServerAPI = (function () {
|
||||||
|
const base = "./";
|
||||||
|
let ok = false;
|
||||||
|
|
||||||
|
function checkData(data){
|
||||||
|
if (data === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (typeof data.ok != "boolean" || typeof data.info != "string") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function buildUrl(path, params) {
|
||||||
|
const p = String(path || "");
|
||||||
|
const normalizedPath = p.startsWith("/") ? p : `/${p}`;
|
||||||
|
|
||||||
|
let url = `${base}${normalizedPath}`;
|
||||||
|
|
||||||
|
if (params && typeof params === "object") {
|
||||||
|
const qs = new URLSearchParams();
|
||||||
|
|
||||||
|
Object.keys(params).forEach((key) => {
|
||||||
|
const val = params[key];
|
||||||
|
if (val === undefined || val === null) return;
|
||||||
|
|
||||||
|
if (Array.isArray(val)) val.forEach((item) => qs.append(key, String(item)));
|
||||||
|
else qs.append(key, String(val));
|
||||||
|
});
|
||||||
|
|
||||||
|
const query = qs.toString();
|
||||||
|
if (query) url += (url.includes("?") ? "&" : "?") + query;
|
||||||
|
}
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toTextBlob(mapData) {
|
||||||
|
if (mapData instanceof Blob) return mapData;
|
||||||
|
return new Blob([mapData], { type: "text/plain" });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readJsonSafe(res) {
|
||||||
|
if (!res) return null;
|
||||||
|
if (res.status === 204) return null;
|
||||||
|
|
||||||
|
const headers = res.headers;
|
||||||
|
const ct = headers && headers.get ? (headers.get("content-type") || "") : "";
|
||||||
|
const looksJson = ct.includes("application/json") || ct.includes("+json");
|
||||||
|
|
||||||
|
if (looksJson) {
|
||||||
|
try {
|
||||||
|
const data = await res.json();
|
||||||
|
ok = checkData(data);
|
||||||
|
return data;
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.debug("ServerAPI JSON-parse failed:", e);
|
||||||
|
ok = false;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const text = await res.text();
|
||||||
|
if (!text) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(text);
|
||||||
|
ok = checkData(data);
|
||||||
|
return data;
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
ok = false;
|
||||||
|
return { ok: false, info: text };
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.debug("ServerAPI plain-parse failed:", e);
|
||||||
|
ok = false;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getJson(path, params) {
|
||||||
|
const url = buildUrl(path, params);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(url, {
|
||||||
|
method: "GET",
|
||||||
|
headers: { Accept: "application/json" }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const payload = await readJsonSafe(res);
|
||||||
|
console.debug("ServerAPI GET failed:", { url, status: res.status, payload });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await readJsonSafe(res);
|
||||||
|
} catch (error) {
|
||||||
|
console.debug("ServerAPI GET error:", { url, error });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function postData(path, mapData, params) {
|
||||||
|
if (!ok) {return null;}
|
||||||
|
|
||||||
|
const url = buildUrl(path, params);
|
||||||
|
const body = toTextBlob(mapData);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(url, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { Accept: "application/json" },
|
||||||
|
body
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const payload = await readJsonSafe(res);
|
||||||
|
console.debug("ServerAPI POST failed:", { url, status: res.status, payload });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await readJsonSafe(res);
|
||||||
|
} catch (error) {
|
||||||
|
console.debug("ServerAPI POST error:", { url, error });
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
checkData,
|
||||||
|
getJson,
|
||||||
|
postData
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const data = await window.ServerAPI.getJson("api/info.json", null);
|
||||||
|
if (!window.ServerAPI.checkData(data)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[ServerAPI][enabled]: ", data.info);
|
||||||
|
})();
|
||||||
|
|
@ -8555,6 +8555,7 @@
|
||||||
<script defer src="modules/coa-renderer.js?v=1.99.00"></script>
|
<script defer src="modules/coa-renderer.js?v=1.99.00"></script>
|
||||||
<script defer src="libs/rgbquant.min.js"></script>
|
<script defer src="libs/rgbquant.min.js"></script>
|
||||||
<script defer src="libs/jquery.ui.touch-punch.min.js"></script>
|
<script defer src="libs/jquery.ui.touch-punch.min.js"></script>
|
||||||
|
<script defer src="modules/io/server-api.js"></script>
|
||||||
<script defer src="modules/io/save.js?v=1.111.0"></script>
|
<script defer src="modules/io/save.js?v=1.111.0"></script>
|
||||||
<script defer src="modules/io/load.js?v=1.111.0"></script>
|
<script defer src="modules/io/load.js?v=1.111.0"></script>
|
||||||
<script defer src="modules/io/cloud.js?v=1.106.0"></script>
|
<script defer src="modules/io/cloud.js?v=1.106.0"></script>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue