mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-24 21:43:00 +00:00
minor refactoring
This commit is contained in:
parent
7821aebd68
commit
83098d3dc4
4 changed files with 24 additions and 22 deletions
|
@ -13,13 +13,7 @@ class User(BaseModel):
|
|||
password: str
|
||||
|
||||
|
||||
class Puzzle(BaseModel):
|
||||
background: str
|
||||
font: str
|
||||
solution: str
|
||||
|
||||
|
||||
class Door(BaseModel):
|
||||
class DoorSaved(BaseModel):
|
||||
day: int
|
||||
x1: int
|
||||
y1: int
|
||||
|
@ -27,13 +21,19 @@ class Door(BaseModel):
|
|||
y2: int
|
||||
|
||||
|
||||
Doors: TypeAlias = list[Door] | None
|
||||
DoorsSaved: TypeAlias = list[DoorSaved]
|
||||
|
||||
|
||||
class Puzzle(BaseModel):
|
||||
background: str
|
||||
doors: DoorsSaved = []
|
||||
font: str
|
||||
solution: str
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
admin: User
|
||||
puzzle: Puzzle
|
||||
doors: Doors = []
|
||||
|
||||
|
||||
async def get_config() -> Config:
|
||||
|
|
|
@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends
|
|||
from fastapi.responses import StreamingResponse
|
||||
from PIL import Image
|
||||
|
||||
from ..config import Config, Doors, get_config, set_config
|
||||
from ..config import Config, DoorsSaved, get_config, set_config
|
||||
from ..dav_common import dav_get_file
|
||||
from ._misc import api_return_image
|
||||
|
||||
|
@ -28,22 +28,22 @@ async def get_image_for_day(
|
|||
@router.get("/doors")
|
||||
async def get_doors(
|
||||
cfg: Config = Depends(get_config),
|
||||
) -> Doors:
|
||||
) -> DoorsSaved:
|
||||
"""
|
||||
Türchen lesen
|
||||
"""
|
||||
|
||||
return cfg.doors
|
||||
return cfg.puzzle.doors
|
||||
|
||||
|
||||
@router.put("/doors")
|
||||
async def put_doors(
|
||||
doors: Doors,
|
||||
doors: DoorsSaved,
|
||||
cfg: Config = Depends(get_config),
|
||||
) -> None:
|
||||
"""
|
||||
Türchen setzen
|
||||
"""
|
||||
|
||||
cfg.doors = doors
|
||||
cfg.puzzle.doors = doors
|
||||
await set_config(cfg)
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { Door, DoorSerialized } from "@/lib/door";
|
||||
import { Door, DoorsSaved } from "@/lib/door";
|
||||
import { Options, Vue } from "vue-class-component";
|
||||
|
||||
import BulmaBreadcrumbs, { Step } from "./BulmaBreadcrumbs.vue";
|
||||
|
@ -56,24 +56,24 @@ export default class extends Vue {
|
|||
public doors: Door[] = [];
|
||||
|
||||
public load_doors() {
|
||||
this.$advent22.api_get<DoorSerialized[]>("/general/doors").then((data) => {
|
||||
this.$advent22.api_get<DoorsSaved>("/general/doors").then((data) => {
|
||||
this.doors.length = 0;
|
||||
|
||||
for (const value of data) {
|
||||
this.doors.push(Door.deserialize(value));
|
||||
this.doors.push(Door.load(value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public save_doors() {
|
||||
const data: DoorSerialized[] = [];
|
||||
const data: DoorsSaved = [];
|
||||
|
||||
for (const door of this.doors) {
|
||||
if (door.day === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
data.push(door.serialized);
|
||||
data.push(door.save());
|
||||
}
|
||||
|
||||
this.$advent22.api_put("/general/doors", data);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Rectangle } from "./rectangle";
|
||||
import { Vector2D } from "./vector2d";
|
||||
|
||||
export interface DoorSerialized {
|
||||
export interface DoorSaved {
|
||||
day: number;
|
||||
x1: number;
|
||||
y1: number;
|
||||
|
@ -9,6 +9,8 @@ export interface DoorSerialized {
|
|||
y2: number;
|
||||
}
|
||||
|
||||
export type DoorsSaved = DoorSaved[];
|
||||
|
||||
export class Door {
|
||||
private _day = -1;
|
||||
public position: Rectangle;
|
||||
|
@ -35,7 +37,7 @@ export class Door {
|
|||
}
|
||||
}
|
||||
|
||||
public static deserialize(serialized: DoorSerialized): Door {
|
||||
public static load(serialized: DoorSaved): Door {
|
||||
return new Door(
|
||||
new Rectangle(
|
||||
new Vector2D(serialized.x1, serialized.y1),
|
||||
|
@ -45,7 +47,7 @@ export class Door {
|
|||
);
|
||||
}
|
||||
|
||||
public get serialized(): DoorSerialized {
|
||||
public save(): DoorSaved {
|
||||
return {
|
||||
day: this.day,
|
||||
x1: this.position.origin.x,
|
||||
|
|
Loading…
Reference in a new issue