-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Frontend: add classes for some of the state variables.
- Loading branch information
Showing
7 changed files
with
133 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,49 @@ | ||
import { computed, reactive, ref, watch } from "vue"; | ||
import { ref, shallowReactive } from "vue"; | ||
|
||
import { hoursToMilliseconds } from "@/utils"; | ||
|
||
export class State { | ||
consts = { | ||
buffs: Array(1 / 0.05 + 1).fill(undefined).map((_, i) => 1 + Math.round(0.05 * i * 100) / 100), | ||
}; | ||
|
||
refs = { | ||
targetDate: ref(Date.now()), | ||
isReverseOn: ref(false), | ||
maxTime: reactive({ | ||
value: hoursToMilliseconds(24), | ||
minimum: hoursToMilliseconds(24), | ||
maximum: hoursToMilliseconds(72), | ||
}), | ||
buffIndex: ref(0), | ||
}; | ||
|
||
computed = { | ||
buff: computed(() => { | ||
return this.consts.buffs[this.refs.buffIndex.value]!; | ||
}), | ||
}; | ||
class Buff { | ||
value = 1.00; | ||
|
||
nextBuff(): [number, number] { | ||
const prevBuff = this.consts.buffs[this.refs.buffIndex.value]!; | ||
this.refs.buffIndex.value = (this.refs.buffIndex.value + 1) % this.consts.buffs.length; | ||
const buff = this.consts.buffs[this.refs.buffIndex.value]!; | ||
return [buff, prevBuff]; | ||
constructor() { | ||
return shallowReactive(this); | ||
} | ||
|
||
* [Symbol.iterator]() { | ||
for (const property in this.refs) { | ||
yield [this.refs[property as keyof typeof this.refs], property] as const; | ||
} | ||
next(): [number, number] { | ||
const prev = this.value; | ||
this.value = Math.round((this.value + 0.05) * 100) / 100; | ||
(this.value > 2.00) && (this.value = 1.00); | ||
return [this.value, prev]; | ||
} | ||
} | ||
|
||
const IDB_NAME = "flowey"; | ||
const OBJECT_STORE_NAME = "state"; | ||
class MaxTime { | ||
value = hoursToMilliseconds(24); | ||
minimum = hoursToMilliseconds(24); | ||
maximum = hoursToMilliseconds(72); | ||
|
||
class Database { | ||
idb: IDBDatabase | null = null; | ||
state: State = new State(); | ||
|
||
async init() { | ||
this.idb = await this.openIndexedBD(); | ||
|
||
for (const [ref, property] of this.state) { | ||
watch(ref, () => { | ||
this.put(ref.value, property); | ||
}); | ||
} | ||
}; | ||
|
||
put(value: unknown, property: string) { | ||
this.idb! | ||
.transaction(OBJECT_STORE_NAME, "readwrite") | ||
.objectStore(OBJECT_STORE_NAME) | ||
.put(value, property); | ||
constructor() { | ||
return shallowReactive(this); | ||
} | ||
|
||
private async openIndexedBD() { | ||
return new Promise<IDBDatabase>((resolve) => { | ||
const request = window.indexedDB.open(IDB_NAME); | ||
|
||
request.onupgradeneeded = (event) => { | ||
const idb = (event.target as IDBOpenDBRequest).result; | ||
const objectStore = idb.createObjectStore(OBJECT_STORE_NAME); | ||
|
||
objectStore.transaction.oncomplete = () => { | ||
const objectStore = idb | ||
.transaction(OBJECT_STORE_NAME, "readwrite") | ||
.objectStore(OBJECT_STORE_NAME); | ||
|
||
for (const [ref, property] of this.state) { | ||
objectStore.put(ref.value, property); | ||
} | ||
}; | ||
}; | ||
|
||
request.onsuccess = (event) => { | ||
const idb = (event.target as IDBOpenDBRequest).result; | ||
const objectStore = idb | ||
.transaction(OBJECT_STORE_NAME, "readonly") | ||
.objectStore(OBJECT_STORE_NAME); | ||
|
||
for (const [ref, property] of this.state) { | ||
objectStore.get(property).onsuccess = (event) => { | ||
const value: unknown = (event.target as IDBRequest).result; | ||
if (value !== undefined && typeof value === typeof ref.value) { | ||
ref.value = value as typeof ref.value; | ||
return; | ||
} | ||
this.put(ref.value, property); | ||
}; | ||
} | ||
increment() { | ||
this.value = Math.min(this.maximum, this.value + hoursToMilliseconds(1)); | ||
} | ||
|
||
resolve(idb); | ||
}; | ||
}); | ||
decrement() { | ||
this.value = Math.max(this.minimum, this.value - hoursToMilliseconds(1)); | ||
} | ||
} | ||
|
||
const database = new Database(); | ||
await database.init(); | ||
export class State { | ||
buff = new Buff(); | ||
isReverseOn = ref(false); | ||
maxTime = new MaxTime(); | ||
targetDate = ref(Date.now()); | ||
|
||
export default database.state; | ||
* [Symbol.iterator]() { | ||
for (const property in this) { | ||
yield [this[property], property] as const; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { watch } from "vue"; | ||
|
||
import { State } from "@/state"; | ||
|
||
const IDB_NAME = "flowey"; | ||
const OBJECT_STORE_NAME = "state"; | ||
|
||
class Store { | ||
idb: IDBDatabase | null = null; | ||
state: State = new State(); | ||
|
||
async init() { | ||
this.idb = await this.openIndexedBD(); | ||
|
||
for (const [ref, property] of this.state) { | ||
watch(ref, () => { | ||
this.put(ref.value, property); | ||
}); | ||
} | ||
}; | ||
|
||
put(value: unknown, property: string) { | ||
this.idb! | ||
.transaction(OBJECT_STORE_NAME, "readwrite") | ||
.objectStore(OBJECT_STORE_NAME) | ||
.put(value, property); | ||
} | ||
|
||
private async openIndexedBD() { | ||
return new Promise<IDBDatabase>((resolve) => { | ||
const request = window.indexedDB.open(IDB_NAME); | ||
|
||
request.onupgradeneeded = (event) => { | ||
const idb = (event.target as IDBOpenDBRequest).result; | ||
const objectStore = idb.createObjectStore(OBJECT_STORE_NAME); | ||
|
||
objectStore.transaction.oncomplete = () => { | ||
const objectStore = idb | ||
.transaction(OBJECT_STORE_NAME, "readwrite") | ||
.objectStore(OBJECT_STORE_NAME); | ||
|
||
for (const [ref, property] of this.state) { | ||
objectStore.put(ref.value, property); | ||
} | ||
}; | ||
}; | ||
|
||
request.onsuccess = (event) => { | ||
const idb = (event.target as IDBOpenDBRequest).result; | ||
const objectStore = idb | ||
.transaction(OBJECT_STORE_NAME, "readonly") | ||
.objectStore(OBJECT_STORE_NAME); | ||
|
||
for (const [ref, property] of this.state) { | ||
objectStore.get(property).onsuccess = (event) => { | ||
const value: unknown = (event.target as IDBRequest).result; | ||
if (value !== undefined && typeof value === typeof ref.value) { | ||
ref.value = value as typeof ref.value; | ||
return; | ||
} | ||
this.put(ref.value, property); | ||
}; | ||
} | ||
|
||
resolve(idb); | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
const store = new Store(); | ||
await store.init(); | ||
|
||
export const state = store.state; |