get rid of s3

This commit is contained in:
Soph :3 2024-04-28 15:32:04 +03:00
parent 8cd07e8521
commit ef586df9a6
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
8 changed files with 86 additions and 86 deletions

View file

@ -6,7 +6,7 @@ import {
World,
} from "./classes.ts";
import { config, crypto, log, s3, toHexString } from "../deps.ts";
import { config, crypto, log, toHexString } from "../deps.ts";
type PlayerFunction = (a: Player) => void;
@ -36,30 +36,15 @@ export class Server {
this.server = Deno.listen({ port: port });
try {
await s3.headBucket({
Bucket: "cla66ic",
});
log.info("s3 bucket exists!");
} catch {
log.warning("s3 bucket does not exist.. Creating!");
await s3.createBucket({
Bucket: "cla66ic",
});
}
(await s3.listObjects({
Bucket: "cla66ic",
})).Contents.forEach((e) => {
if (e.Key !== config.main + ".buf") {
log.info(`Autoloaded a world from s3! ${e.Key}`);
const world = new World({ x: 0, y: 0, z: 0 }, e.Key!.split(".buf")[0]);
await Deno.stat("worlds/");
for await (const dirEntry of Deno.readDir("worlds/")) {
const world = new World({ x: 0, y: 0, z: 0 }, dirEntry.name.replace(".buf", ""));
this.worlds.push(world);
}
});
} catch {
await Deno.mkdir("worlds")
}
if (config.onlineMode) {
setInterval(async () => {

View file

@ -1,14 +1,13 @@
import { gzip, ungzip } from "https://cdn.skypack.dev/pako";
import { s3 } from "../deps.ts";
import { Position } from "./classes.ts";
import { cbor } from "../deps.ts";
export class World {
size: Position;
data: Uint8Array;
private dataView: DataView;
name: string;
// deno-lint-ignore no-explicit-any
optionalJson: any = {};
metadata: any = {};
constructor(size: Position, name: string) {
this.size = size;
@ -72,39 +71,33 @@ export class World {
async delete() {
try {
await s3.deleteObject({
Bucket: "cla66ic",
Key: this.name + ".buf",
});
await Deno.remove(`worlds/${this.name}.buf`)
} catch {
// doesn't exist, probably..
// gang
}
}
private async load() {
try {
const head = await s3.headObject({
Bucket: "cla66ic",
Key: this.name + ".buf",
});
const ungziped = ungzip(
(await s3.getObject({
Bucket: "cla66ic",
Key: this.name + ".buf",
})).Body,
await Deno.readFile(`worlds/${this.name}.buf`)
);
if (!(ungziped instanceof Uint8Array)) return;
if (!(ungziped instanceof Uint8Array)) return;
const dv = new DataView(ungziped.buffer);
const cborSize = dv.getUint32(0);
this.metadata = cbor.decode(new Uint8Array(ungziped.buffer.slice(4, cborSize+4)));
this.size = {
x: +head.Metadata.x!,
y: +head.Metadata.y!,
z: +head.Metadata.z!,
x: this.metadata.x!,
y: this.metadata.y!,
z: this.metadata.z!,
};
this.data = ungziped;
this.data = ungziped.slice(cborSize+4);
this.dataView = new DataView(this.data.buffer);
this.optionalJson = JSON.parse(head.Metadata.json || "{}");
} catch {
} catch(e) {
const layers = Math.floor(this.size.y / 2);
for (let i = 0; i < layers; i += 1) {
@ -118,16 +111,18 @@ export class World {
}
async save() {
await s3.putObject({
Bucket: "cla66ic",
Key: this.name + ".buf",
Body: gzip(this.data),
Metadata: {
"x": this.size.x + "",
"y": this.size.y + "",
"z": this.size.z + "",
"json": JSON.stringify(this.optionalJson),
},
});
const metadata = {
x: this.size.x!,
y: this.size.y!,
z: this.size.z!,
...this.metadata
}
const cborData = cbor.encode(metadata);
const buffer = new Uint8Array(4 + cborData.byteLength + this.data.byteLength);
const dv = new DataView(buffer.buffer);
dv.setUint32(0, cborData.byteLength);
buffer.set(cborData, 4);
buffer.set(this.data, 4 + cborData.byteLength);
await Deno.writeFile(`worlds/${this.name}.buf`, gzip(buffer)!);
}
}