Commit 6294a28c authored by Grant's avatar Grant
Browse files

persistent canvas size (fixes #12)

parent 1d00b53a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -2,6 +2,11 @@
//// THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
//// ------------------------------------------------------

Table Setting {
  key String [pk]
  value String [not null]
}

Table User {
  sub String [pk]
  lastPixelTime DateTime [default: `now()`, not null]
+7 −0
Original line number Diff line number Diff line
-- CreateTable
CREATE TABLE "Setting" (
    "key" TEXT NOT NULL,
    "value" TEXT NOT NULL,

    CONSTRAINT "Setting_pkey" PRIMARY KEY ("key")
);
+5 −0
Original line number Diff line number Diff line
@@ -14,6 +14,11 @@ datasource db {
  url      = env("DATABASE_URL")
}

model Setting {
  key   String @id
  value String // this value will be parsed with JSON.parse
}

model User {
  sub           String    @id
  lastPixelTime DateTime  @default(now()) // the time the last pixel was placed at
+13 −6
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import { Logger } from "./lib/Logger";
import { ExpressServer } from "./lib/Express";
import { SocketServer } from "./lib/SocketServer";
import { OpenID } from "./lib/oidc";
import { loadSettings } from "./lib/Settings";

// Validate environment variables

@@ -57,10 +58,16 @@ if (!process.env.OIDC_CALLBACK_HOST) {
  process.exit(1);
}

Redis.connect();
// run startup tasks, all of these need to be completed to serve
Promise.all([
  Redis.connect(),
  OpenID.setup().then(() => {
    Logger.info("Setup OpenID");
});
  }),
  loadSettings(),
]).then(() => {
  Logger.info("Startup tasks have completed, starting server");

  const express = new ExpressServer();
  new SocketServer(express.httpServer);
});
+19 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import { CanvasConfig } from "@sc07-canvas/lib/src/net";
import { prisma } from "./prisma";
import { Redis } from "./redis";
import { SocketServer } from "./SocketServer";
import { Logger } from "./Logger";

class Canvas {
  /**
@@ -37,7 +38,23 @@ class Canvas {
   * @param height
   */
  async setSize(width: number, height: number) {
    Logger.info("Canvas#setSize has started", {
      old: this.canvasSize,
      new: [width, height],
    });

    this.canvasSize = [width, height];
    await prisma.setting.upsert({
      where: { key: "canvas.size" },
      create: {
        key: "canvas.size",
        value: JSON.stringify({ width, height }),
      },
      update: {
        key: "canvas.size",
        value: JSON.stringify({ width, height }),
      },
    });

    // we're about to use the redis keys, make sure they are all updated
    await this.pixelsToRedis();
@@ -51,6 +68,8 @@ class Canvas {
    await this.getPixelsArray().then((pixels) => {
      SocketServer.instance.io.emit("canvas", pixels);
    });

    Logger.info("Canvas#setSize has finished");
  }

  /**
Loading