Skip to content

Commit

Permalink
GachaKit // Fix needsItemIDFix(), etc.
Browse files Browse the repository at this point in the history
- This commit now enforces that all itemID values shall be integer literal.
  • Loading branch information
ShikiSuen committed Nov 18, 2024
1 parent 27bac8f commit 2aa4b7d
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ extension [CDGachaMO4GI] {
}

public mutating func fixItemIDs(with givenLanguage: GachaLanguage? = nil) {
let needsItemIDFix = !filter { $0.itemId.isEmpty }.isEmpty
let needsItemIDFix = !filter(\.itemId.isNotInt).isEmpty
guard !isEmpty, needsItemIDFix else { return }
var languages: [HoYo.APILang] = [.langCHS]
if mightHaveNonCHSLanguageTag, !possibleLanguages.isEmpty {
Expand Down Expand Up @@ -180,7 +180,7 @@ extension [CDGachaMO4GI] {
var newItemContainer = Self()
// 君子协定:这里要求 UIGFGachaItem 的 itemID 必须是有效值,否则会出现灾难性的后果。
try forEach { currentItem in
guard Int(currentItem.itemId) != nil else {
guard currentItem.itemId.isInt else {
throw GachaMeta.GMDBError.itemIDInvalid(name: currentItem.name, game: currentItem.game)
}
let lang = lang.sanitized(by: currentItem.game)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public struct GIGF: Decodable {
public internal(set) var list: [GIGFGachaItem]

public var needsItemIDFix: Bool {
!list.filter { $0.itemID.isEmpty }.isEmpty
!list.filter(\.itemID.isNotInt).isEmpty
}

// MARK: Internal
Expand Down Expand Up @@ -296,19 +296,19 @@ extension GIGF {
list.forEach { v2Item in
var newItemID = v2Item.itemID
var newName = v2Item.name
if newItemID.isEmpty {
if newItemID.isNotInt {
if let newItemIDInt = revDB[v2Item.name] {
newItemID = newItemIDInt.description
}
}
if !newItemID.isEmpty {
if newItemID.isInt {
newName = GachaMeta.sharedDB.mainDB4GI.plainQueryForNames(
itemID: newItemID, langID: self.info.lang.rawValue
) ?? v2Item.name
}

var maybeRankType: String? = v2Item.rankType?.rawValue.description ?? nil
if maybeRankType == nil, !newItemID.isEmpty,
if maybeRankType == nil, newItemID.isInt,
let intMaybeRankType = mainDB.plainQueryForRarity(itemID: newItemID) {
maybeRankType = intMaybeRankType.description
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ extension SRGFv1 {
if Int(id) == nil { error = SRGFv1.makeDecodingError(CodingKeys.id) }

self.itemID = try container.decode(String.self, forKey: .itemID)
if Int(itemID) == nil { error = SRGFv1.makeDecodingError(CodingKeys.itemID) }
if !itemID.isInt { error = SRGFv1.makeDecodingError(CodingKeys.itemID) }

self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType)
if itemType?.isEmpty ?? false { error = SRGFv1.makeDecodingError(CodingKeys.itemType) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension UIGFv4 {
if Int(id) == nil { error = UIGFv4.makeDecodingError(CodingKeys.id) }

self.itemID = try container.decode(String.self, forKey: .itemID)
if Int(itemID) == nil { error = UIGFv4.makeDecodingError(CodingKeys.itemID) }
if !itemID.isInt { error = UIGFv4.makeDecodingError(CodingKeys.itemID) }

self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType)
if itemType?.isEmpty ?? false { error = UIGFv4.makeDecodingError(CodingKeys.itemType) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension UIGFv4 {
if Int(id) == nil { error = UIGFv4.makeDecodingError(CodingKeys.id) }

self.itemID = try container.decode(String.self, forKey: .itemID)
if Int(itemID) == nil { error = UIGFv4.makeDecodingError(CodingKeys.itemID) }
if !itemID.isInt { error = UIGFv4.makeDecodingError(CodingKeys.itemID) }

self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType)
if itemType?.isEmpty ?? false { error = UIGFv4.makeDecodingError(CodingKeys.itemType) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension UIGFv4 {
if Int(id) == nil { error = UIGFv4.makeDecodingError(CodingKeys.id) }

self.itemID = try container.decode(String.self, forKey: .itemID)
if Int(itemID) == nil { error = UIGFv4.makeDecodingError(CodingKeys.itemID) }
if !itemID.isInt { error = UIGFv4.makeDecodingError(CodingKeys.itemID) }

self.itemType = try container.decodeIfPresent(String.self, forKey: .itemType)
if itemType?.isEmpty ?? false { error = UIGFv4.makeDecodingError(CodingKeys.itemType) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ extension Array where Element: UIGFGachaItemProtocol {
var newItemContainer = Self()
// 君子协定:这里要求 UIGFGachaItem 的 itemID 必须是有效值,否则会出现灾难性的后果。
try forEach { currentItem in
guard Int(currentItem.itemID) != nil else {
guard currentItem.itemID.isInt else {
throw GachaMeta.GMDBError.itemIDInvalid(name: currentItem.name ?? "", game: currentItem.game)
}
let lang = lang.sanitized(by: currentItem.game)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension GachaFetchModels.PageFetched.FetchedEntry {
newEntry.time = time
}

if fixItemIDs, game == .genshinImpact, itemID.isEmpty {
if fixItemIDs, game == .genshinImpact, itemID.isNotInt {
var newItemID = GachaMeta.sharedDB.reverseQuery4GI(for: name)
if newItemID == nil {
try await GachaMeta.Sputnik.updateLocalGachaMetaDB(for: .genshinImpact)
Expand Down Expand Up @@ -61,7 +61,7 @@ extension GachaFetchModels.PageFetched {

extension PZGachaEntryProtocol {
public mutating func fixItemID() async throws {
guard Pizza.SupportedGame(rawValue: game) == .genshinImpact, itemID.isEmpty else { return }
guard Pizza.SupportedGame(rawValue: game) == .genshinImpact, itemID.isNotInt else { return }
var newItemID = GachaMeta.sharedDB.reverseQuery4GI(for: name)
if newItemID == nil {
try await GachaMeta.Sputnik.updateLocalGachaMetaDB(for: .genshinImpact)
Expand All @@ -75,7 +75,7 @@ extension PZGachaEntryProtocol {

public func asItemIDFixed() async throws -> Self {
guard Pizza.SupportedGame(rawValue: game) == .genshinImpact else { return self }
guard itemID.isEmpty else { return self }
guard itemID.isNotInt else { return self }
var result = self
var newItemID = GachaMeta.sharedDB.reverseQuery4GI(for: name)
if newItemID == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct CDGachaMODebugView: View {
}
ForEach(delegate.managedObjs, id: \.enumID) { gachaItemMO in
let theEntry = gachaItemMO.asPZGachaEntrySendable.expressible
let isWrecked = theEntry.itemID.isEmpty
let isWrecked = theEntry.itemID.isNotInt
GachaEntryBar(entry: theEntry, showDate: true, debug: true, debugMenu: true)
.foregroundStyle(isWrecked ? Color.red : Color.primary)
}
Expand Down
10 changes: 10 additions & 0 deletions Packages/GachaKit/Sources/GachaKit/GachaKit_OSImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ extension String.LocalizationValue {
String(localized: self, bundle: .module)
}
}

extension StringProtocol {
public var isInt: Bool {
Int(self) != nil
}

public var isNotInt: Bool {
Int(self) == nil
}
}

0 comments on commit 2aa4b7d

Please sign in to comment.