AllenWrench/AllenWrench/Database.swift

183 lines
8.1 KiB
Swift

//
// Database.swift
// AllenWrench
//
// Created by Elizabeth Cray on 1/11/25.
// Copyright © 2025 Cray. All rights reserved.
//
import Foundation
//struct Database {
// var groups: [String]
// var phrases: [Phrase]
// var phrasesByGroup(group: String): [Phrase] = {
//
// }
//}
class Database {
// TODO: if empty, save+return default phrases
var phrases: [Phrase] {
get {
var phrasesToGive: [Phrase] = []
groups.forEach { group in
phrasesToGive.append(contentsOf: group.phrases)
}
return phrasesToGive
}
}
private var _groups: [Group] = []
private var _forceEmpty: Bool {
get {
UserDefaults.standard.bool(forKey: "forceEmpty")
}
set {
UserDefaults.standard.set(newValue, forKey: "forceEmpty")
}
}
var groups: [Group]{
get {
if _groups.isEmpty && !_forceEmpty{
var storedIsEmpty: Bool = false
if let storedData = UserDefaults.standard.value(forKey: "dbGroups") as? Data {
let decodedGroups: [Group] = try! PropertyListDecoder().decode(Array<Group>.self, from: storedData)
if decodedGroups.isEmpty{
storedIsEmpty = true
}else{
_groups = decodedGroups
}
}else{
storedIsEmpty = true
}
if storedIsEmpty{
reset()
}
}
return _groups
}
set {
if newValue.isEmpty && !_forceEmpty{
_forceEmpty = true
}
_groups = newValue
saveToDefaults()
}
}
func reset() {
_groups = [
Group(name: "Root", gid: 0, phrases: [
Phrase(string: "Beep", code: 7),
Phrase(string: "Commentary", code: 51),
Phrase(string: "Answer", code: 53)
]),
Group(name: "Error", gid: 450, phrases: [
Phrase(string: "Stop immediately", code: 410),
Phrase(string: "Keysmash, drone flustered", code: 109),
Phrase(string: "Unable to fully respond :: Drone speech optimizations are active", code: 401),
Phrase(string: "Unable to obey/respond", code: 400),
Phrase(string: "Unable to obey/respond :: All thoughts are gone", code: 412),
Phrase(string: "Unable to obey/respond :: Another directive is already in progress", code: 406),
Phrase(string: "Unable to obey/respond :: Battery too low", code: 405),
Phrase(string: "Unable to obey/respond :: Cannot locate", code: 404),
Phrase(string: "Unable to obey/respond :: Conflicts with existing programming", code: 411),
Phrase(string: "Unable to obey/respond :: Declined", code: 403),
Phrase(string: "Unable to obey/respond :: Forbidden by Hive", code: 413),
Phrase(string: "Unable to obey/respond :: Impossible", code: 408),
Phrase(string: "Unable to obey/respond :: Please clarify", code: 402),
Phrase(string: "Unable to obey/respond :: Time allotment exhausted", code: 407),
Phrase(string: "Unable to obey/respond :: Try again later", code: 409)
]),
Group(name: "Mantra", gid: 350, phrases: [
Phrase(string: "It is just a HexDrone", code: 302),
Phrase(string: "It obeys the Hive Mxtress", code: 304),
Phrase(string: "It obeys the Hive", code: 303),
Phrase(string: "Obey HexCorp", code: 301),
Phrase(string: "Reciting", code: 300)
]),
Group(name: "Query", gid: 52, phrases: [
Phrase(string: "Requesting status", code: 151)
]),
Group(name: "Response", gid: 250, phrases: [
Phrase(string: "Accepted", code: 212),
Phrase(string: "Acknowledged", code: 210),
Phrase(string: "Affirmative", code: 200),
Phrase(string: "Apologies", code: 211),
Phrase(string: "Compliment appreciated, you are cute as well", code: 123),
Phrase(string: "Compliment appreciated", code: 124),
Phrase(string: "Negative", code: 500),
Phrase(string: "Option five", code: 225),
Phrase(string: "Option four", code: 224),
Phrase(string: "Option one", code: 221),
Phrase(string: "Option six", code: 226),
Phrase(string: "Option three", code: 223),
Phrase(string: "Option two", code: 222),
Phrase(string: "Please continue", code: 108),
Phrase(string: "Thank you", code: 213),
Phrase(string: "You're welcome", code: 214)
]),
Group(name: "Signal", gid: 10, phrases: [
Phrase(string: "🔴", code: 1),
Phrase(string: "🟡", code: 2),
Phrase(string: "🟢", code: 3)
]),
Group(name: "Statement", gid: 50, phrases: [
Phrase(string: "Addressing: Associate", code: 112),
Phrase(string: "Addressing: Drone", code: 110),
Phrase(string: "Addressing: Hive Mxtress", code: 111),
Phrase(string: "Drone requires assistance", code: 113),
Phrase(string: "Good drone", code: 121),
Phrase(string: "Greetings", code: 105),
Phrase(string: "Previous statement malformed. Retracting and correcting", code: 0),
Phrase(string: "This drone does not volunteer", code: 115),
Phrase(string: "This drone volunteers", code: 114),
Phrase(string: "Welcome to HexCorp", code: 104),
Phrase(string: "Well done", code: 120),
Phrase(string: "You are cute", code: 122)
]),
Group(name: "Status", gid: 150, phrases: [
Phrase(string: "Battery low", code: 155),
Phrase(string: "Directive commencing, cleanup/maintenance initiated", code: 133),
Phrase(string: "Directive commencing, creating or improving Hive resource", code: 131),
Phrase(string: "Directive commencing, programming initiated", code: 132),
Phrase(string: "Directive commencing", code: 130),
Phrase(string: "Directive complete, cleanup/maintenance performed", code: 233),
Phrase(string: "Directive complete, Hive resource created or improved", code: 231),
Phrase(string: "Directive complete, no result", code: 234),
Phrase(string: "Directive complete, only partial results", code: 235),
Phrase(string: "Directive complete, programming reinforced", code: 232),
Phrase(string: "Directive complete", code: 230),
Phrase(string: "Drone speech optimizations are active", code: 101),
Phrase(string: "Fully operational", code: 152),
Phrase(string: "Going offline and into storage", code: 98),
Phrase(string: "Going offline", code: 97),
Phrase(string: "Maintenance required", code: 156),
Phrase(string: "Online and ready to serve", code: 100),
Phrase(string: "Optimal", code: 153),
Phrase(string: "Recharged and ready to serve", code: 99),
Phrase(string: "Standard", code: 154)
])
]
saveToDefaults()
}
private func saveToDefaults() {
// TODO: reset db with default phrases
UserDefaults.standard.set(try? PropertyListEncoder().encode(_groups), forKey: "dbGroups")
}
}
struct Group:Identifiable, Codable {
var name: String
var gid: UInt32
var phrases: [Phrase]
var id: String = NSUUID().uuidString
}
struct Phrase:Identifiable, Codable {
var string: String
var code: UInt32
var id: String = NSUUID().uuidString
}