Skip to content

Commit

Permalink
Add codec class and rename parser method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 19, 2024
1 parent 5b0ffcd commit 7f03449
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Spellbook.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
8EEDC3E42AE739A60045D002 /* ConfirmNextAvailableCastController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EEDC3E32AE739A50045D002 /* ConfirmNextAvailableCastController.swift */; };
8EF6EF0B264668F60059BCE8 /* Version.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EF6EF0A264668F60059BCE8 /* Version.swift */; };
8EF6EF0D26485A2E0059BCE8 /* VersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EF6EF0C26485A2E0059BCE8 /* VersionTests.swift */; };
FC2BCF1D2C09934100E923FC /* SpellCodec.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC2BCF1C2C09934100E923FC /* SpellCodec.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -301,6 +302,7 @@
8EEDC3E32AE739A50045D002 /* ConfirmNextAvailableCastController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfirmNextAvailableCastController.swift; sourceTree = "<group>"; };
8EF6EF0A264668F60059BCE8 /* Version.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Version.swift; sourceTree = "<group>"; };
8EF6EF0C26485A2E0059BCE8 /* VersionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionTests.swift; sourceTree = "<group>"; };
FC2BCF1C2C09934100E923FC /* SpellCodec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpellCodec.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -469,6 +471,7 @@
8E74FA5929EBD7D900E13B91 /* SpellSlotsManagerCell.swift */,
8E7EB4EC29F3B87F00408EA4 /* SpellSlotsManagerDelegate.swift */,
8E07AE0E2A10AFA700C0487B /* Operators.swift */,
FC2BCF1C2C09934100E923FC /* SpellCodec.swift */,
);
path = Spellbook;
sourceTree = "<group>";
Expand Down Expand Up @@ -788,6 +791,7 @@
8E74FA5A29EBD7D900E13B91 /* SpellSlotsManagerCell.swift in Sources */,
8E6148F5241991C200842A18 /* SpellTableViewController.swift in Sources */,
8E6148E4241991C200842A18 /* SourcebookFilterController.swift in Sources */,
FC2BCF1D2C09934100E923FC /* SpellCodec.swift in Sources */,
8E6551BA29AC63E800A3BC43 /* Toast.swift in Sources */,
8EF6EF0B264668F60059BCE8 /* Version.swift in Sources */,
8E61491E241991C200842A18 /* Constants.swift in Sources */,
Expand Down
98 changes: 98 additions & 0 deletions Spellbook/SpellCodec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// SpellCodec.swift
// Spellbook
//
// Created by Jonathan Carifio on 5/31/24.
// Copyright © 2024 Jonathan Carifio. All rights reserved.
//

import Foundation

class SpellCodec {
private static let ID_KEY = "id"
private static let NAME_KEY = "name"
private static let PAGE_KEY = "page"
private static let DURATION_KEY = "duration"
private static let RANGE_KEY = "range"
private static let RITUAL_KEY = "ritual"
private static let CONCENTRATION_KEY = "concentration"
private static let LEVEL_KEY = "level"
private static let CASTING_TIME_KEY = "casting_time"
private static let MATERIAL_KEY = "material"
private static let ROYALTY_KEY = "royalty"
private static let COMPONENTS_KEY = "components"
private static let DESCRIPTION_KEY = "desc"
private static let HIGHER_LEVEL_KEY = "higher_level"
private static let SCHOOL_KEY = "school"
private static let CLASSES_KEY = "classes"
private static let SUBCLASSES_KEY = "subclasses"
private static let TCE_EXPANDED_CLASSES_KEY = "tce_expanded_classes"
private static let SOURCEBOOK_KEY = "sourcebook"
private static let LOCATIONS_KEY = "locations"

private static let COMPONENT_STRINGS = ["V", "S", "M", "R"]

private static let CONCENTRATION_PREFIX = "Up to"

func parseSpell(sion: SION, b: SpellBuilder) -> Spell {

// Set the values that need no/trivial parsing
b.setID(intGetter(sion, key: SpellCodec.ID_KEY))
.setName(sion[SpellCodec.NAME_KEY].string!)
.setLevel(intGetter(sion, key: SpellCodec.LEVEL_KEY))
.setSchool(School.fromName(sion[SpellCodec.SCHOOL_KEY].string!))

let locations = sion["locations"]
if !locations.isNil {
if let array = locations.array {
for location in array {
if let sb = Sourcebook.fromCode(location[SpellCodec.SOURCEBOOK_KEY].string) {
b.addLocation(sourcebook: sb, page: intGetter(location, key: SpellCodec.PAGE_KEY))
}
}
}
}

let durationString = sion["duration"].string ?? ""
do {
try b.setDuration(Duration.fromString(durationString))
} catch {
b.setDuration(Duration())
}

do {
try b.setRange(Range.fromString(sion["range"].string ?? ""))
} catch {
b.setRange(Range())
}

let ritual = hasKey(sion, key: "ritual") && (sion["ritual"].bool ?? false)
b.setRitual(ritual)

let concentration = durationString.starts(with: "Up to")
&& (sion["concentration"].bool ?? false)
b.setConcentration(concentration)

let castingTimeString = sion["casting_time"].string ?? ""
do {
try b.setCastingTime(CastingTime.fromString(castingTimeString))

} catch {
b.setCastingTime(CastingTime())
}

if hasKey(sion, key: "material") {
b.setMaterials(sion["material"].string ?? "")
}

if hasKey(obj, key: "royalty") {
b.setRoyalties(sion["royalty"].string ?? "")
}




return b.buildAndReset()

}
}
18 changes: 9 additions & 9 deletions Spellbook/SpellParse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func intGetter(_ sion: SION, key: String) -> Int {
}
}

func has_key(obj: SION, key: String) -> Bool {
func hasKey(_ obj: SION, key: String) -> Bool {
for (k, _) in obj {
if k.string! == key {
return true
Expand All @@ -18,7 +18,7 @@ func has_key(obj: SION, key: String) -> Bool {
return false
}

func load_file(filepath: String) -> String {
func loadFile(filepath: String) -> String {
let text = try! String(contentsOf: URL(fileURLWithPath: filepath))
//print(text)
return text
Expand Down Expand Up @@ -58,14 +58,14 @@ func parseSpell(obj: SION, b: SpellBuilder) -> Spell {
b.setRange(Range())
}

if has_key(obj: obj, key: "ritual") {
if hasKey(obj, key: "ritual") {
b.setRitual(obj["ritual"].bool!)
} else {
b.setRitual(false)
}
if (durationString.starts(with: "Up to")) {
b.setConcentration(true)
} else if has_key(obj: obj, key: "concentration") {
} else if hasKey(obj, key: "concentration") {
b.setConcentration(obj["concentration"].bool!)
} else {
b.setConcentration(false)
Expand All @@ -79,12 +79,12 @@ func parseSpell(obj: SION, b: SpellBuilder) -> Spell {
}

// Material, if necessary
if has_key(obj: obj, key: "material") {
if hasKey(obj, key: "material") {
b.setMaterials(obj["material"].string!)
}

// Royalties, if necessary
if has_key(obj: obj, key: "royalty") {
if hasKey(obj, key: "royalty") {
b.setRoyalties(obj["royalty"].string!)
}

Expand All @@ -102,7 +102,7 @@ func parseSpell(obj: SION, b: SpellBuilder) -> Spell {

// Higher level description
var hlString = ""
if has_key(obj: obj, key: "higher_level") {
if hasKey(obj, key: "higher_level") {
hlString = obj["higher_level"].string!
}
b.setHigherLevelDesc(hlString)
Expand All @@ -114,15 +114,15 @@ func parseSpell(obj: SION, b: SpellBuilder) -> Spell {
}

// Subclasses
if has_key(obj: obj, key: "subclasses") {
if hasKey(obj, key: "subclasses") {
jarr = obj["subclasses"]
for (_, name) in jarr {
b.addSubclass(SubClass.fromName(name.string!))
}
}

// Classes
if has_key(obj: obj, key: "tce_expanded_classes") {
if hasKey(obj, key: "tce_expanded_classes") {
jarr = obj["tce_expanded_classes"]
for (_, name) in jarr {
b.addTashasExpandedClass(CasterClass.fromName(name.string!))
Expand Down

0 comments on commit 7f03449

Please sign in to comment.