본문 바로가기

잡다한 것

업보 청산 - 1(혐주의)

728x90
반응형
SMALL

이게 뭔가 싶지만...
예. 제가 싼 코드입니다...(묵직) 1200줄 뚫어버리기...

 


변명해보면... 사실 OPEN API 를 경험해보고 싶은 욕심으로
헤헿 좀만 싸야지 했는데...하다보니 끝이 안나서...ㅠㅠㅠㅠㅠ

괜찮읍니다. 유지보수를 위한 좋은 코드를 왜 짜야하는지 뼈쩌리게 느꼈읍니다!!!!!!!!! 진짜 진짜... 잘못했읍니다. 잘못했읍니다.

 

아... 제 코드보고 진짜 감방가야된다고...외치던 친구들이 생각납니다. PTSD..
갖곰님께서 회개하면 된다고...

 

ChatGPT도 선생니뮤도ㅠㅠㅠㅠㅠㅠ 절레절레...ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

 

 

그럼 막막하지만 죄를 달게 받는 마음으로 리펙토링해보겠습니다.

 

import SwiftUI

struct CharacterDetailEquipmentsView: View {
    
    @StateObject private var searchCharacterEqupments = EquipmentsAPI.shared
    @StateObject private var searchCharacterEngraings = EngravingsAPI.shared
    
    let characterName: String
    
    var equipmentsArray: [String] = ["무기", "어깨", "투구", "상의", "하의", "장갑"]
    var bracelet: [String] = ["팔찌"]
    var accessoriesArray: [String] = ["반지", "귀걸이", "목걸이"]
    var abilitystone: [String] = ["어빌리티 스톤"]
    var seal: [String] = ["각인"]
    
    @State var equipmentName: String = ""
    @State var equipmentImage: String = ""
    @State var equipmentQuality: String = ""
    @State var equipmentBasicStat: String = ""
    @State var equipmentBasicText: String = ""
    @State var equipmentOptionStat: String = ""
    @State var equipmentOptionText: String = ""
    @State var equipmentColor: String = ""
    @State var equipmentQuailityColor: Color = .white
    
    @State var engravingName: String = ""
    @State var engravingImage: String = ""
    @State var engravingColor: String = ""
    @State var engravingDescription: String = ""
    @State var engravingLevel: String = ""
    @State var engravingText: String = ""
    
    @State var accessoriesName: String = ""
    @State var accessoriesImage : String = ""
    @State var accessoriesColor : String = ""
    @State var accessoriesOptionalText: String = ""
    @State var accessoriesOptionalStat: String = ""
    @State var accessoriesRandomEffect1: String = ""
    @State var accessoriesRandomEffect2: String = ""
    @State var accessoriesRandomEffect3: String = ""
    @State var accessoriesQuality: String = ""
    @State var accessoriesQualityColor: Color = .white
    @State var accessoriesRandomEffectText: String = ""
    
    @State var braceletName: String = ""
    @State var braceletImage: String = ""
    @State var braceletColor: String = ""
    @State var braceletStatText: String = ""
    @State var braceletOptionalStat: String = ""
    @State var braceletEnchantText: String = ""
    
    @State var abilitystoneName : String = ""
    @State var abilitystoneImage : String = ""
    @State var abilitystoneColor : String = ""
    @State var abilitystoneBasicText: String = ""
    @State var abilitystoneBasicStat: String = ""
    @State var abilitystoneOptionText: String = ""
    @State var abilitystoneOptionStat: String = ""
    @State var abilitystoneOption1Text: String = ""
    @State var abilitystoneOption2Text: String = ""
    @State var abilitystoneOption3Text: String = ""
    @State var abilitystoneQuality: String = ""
    @State var abilitystoneQulityColor: Color = .white
    
    @State var isEquipmentsViewStatus: Int = 0
    
    func findEquipments(equipmentsData: Equipments? ) -> [[String]] {
        guard let equipmentsData = equipmentsData, equipmentsData != nil else {
            return []
        }
        var returnEquipmentsData: [[String]] = []
        for idx in equipmentsData! {
            var componentEquipmentsData: [String] = [idx?.type ?? ""]
            componentEquipmentsData.append(idx?.name ?? "")
            componentEquipmentsData.append(idx?.icon ?? "")
            componentEquipmentsData.append(idx?.grade ?? "")
            let parsedItemData = parseEquipmentsJSONString(jsonString: idx?.tooltip) ?? []
            //            if parsedItemData.isEmpty {
            //                parsedItemData = parseEquipmentsJSONString2(jsonString: idx?.tooltip) ?? []
            //            }
            for item in parsedItemData{
                componentEquipmentsData.append(item)
            }
            returnEquipmentsData.append(componentEquipmentsData)
        }
        return returnEquipmentsData
    }
    
    func findAccessories(equipmentsData: Equipments? ) -> [[String]] {
        guard let equipmentsData = equipmentsData, equipmentsData != nil else {
            return []
        }
        var returnEquipmentsData: [[String]] = []
        for idx in equipmentsData! {
            if self.accessoriesArray.contains(idx?.type ?? "") {
                var componentEquipmentsData: [String] = [idx?.type ?? ""]
                componentEquipmentsData.append(idx?.name ?? "")
                componentEquipmentsData.append(idx?.icon ?? "")
                componentEquipmentsData.append(idx?.grade ?? "")
                var parsedItemData = parseAccessoriesJSONString(jsonString: idx?.tooltip) ?? []
                if parsedItemData.isEmpty {
                    parsedItemData = parseAccessoriesJSONString2(jsonString: idx?.tooltip) ?? []
                }
                if parsedItemData.isEmpty {
                    parsedItemData = parseAccessoriesJSONString3(jsonString: idx?.tooltip) ?? []
                }
                for item in parsedItemData{
                    componentEquipmentsData.append(item)
                }
                returnEquipmentsData.append(componentEquipmentsData)
            }
        }
        return returnEquipmentsData
    }
    
    func findAbilityStone(equipmentsData: Equipments? ) -> [[String]] {
        guard let equipmentsData = equipmentsData, equipmentsData != nil else {
            return []
        }
        var returnEquipmentsData: [[String]] = []
        for idx in equipmentsData! {
            if self.abilitystone.contains(idx?.type ?? "") {
                
                var componentEquipmentsData: [String] = [idx?.type ?? ""]
                componentEquipmentsData.append(idx?.name ?? "")
                componentEquipmentsData.append(idx?.icon ?? "")
                componentEquipmentsData.append(idx?.grade ?? "")
                var parsedItemData = parseAbilityStoneJSONString1(jsonString: idx?.tooltip) ?? []
                if parsedItemData.isEmpty {
                    parsedItemData = parseAbilityStoneJSONString2(jsonString: idx?.tooltip) ?? []
                }
                for item in parsedItemData{
                    componentEquipmentsData.append(item)
                }
                returnEquipmentsData.append(componentEquipmentsData)
            }
        }
        return returnEquipmentsData
    }
    func findBracelet(equipmentsData: Equipments? ) -> [[String]] {
        guard let equipmentsData = equipmentsData, equipmentsData != nil else {
            return []
        }
        var returnEquipmentsData: [[String]] = []
        for idx in equipmentsData! {
            if self.bracelet.contains(idx?.type ?? "") {
                
                var componentEquipmentsData: [String] = [idx?.type ?? ""]
                componentEquipmentsData.append(idx?.name ?? "")
                componentEquipmentsData.append(idx?.icon ?? "")
                componentEquipmentsData.append(idx?.grade ?? "")
                let parsedItemData = parseBraceletJSONString(jsonString: idx?.tooltip) ?? []
                
                for item in parsedItemData{
                    componentEquipmentsData.append(item)
                }
                returnEquipmentsData.append(componentEquipmentsData)
            }
        }
        return returnEquipmentsData
    }
    
    func findEngravings(engravingsData: EngravingDatas? ) -> [[String]] {
        guard let engravingsData = engravingsData, engravingsData != nil else {
            return []
        }
        var returnEngravingsData: [[String]] = []
        
        for idx in engravingsData.engravings {
            var engravingData: [String] = []
            engravingData.append(idx?.name ?? "")
            engravingData.append(idx?.icon ?? "")
            let parsedItemData = parseEngravingJSONString(jsonString: idx?.tooltip) ?? []
            for item in parsedItemData{
                engravingData.append(item)
            }
            returnEngravingsData.append(engravingData)
        }
        return returnEngravingsData
    }
    
    func parseEquipmentsJSONString(jsonString : String?) -> [String]? {
        var parsedData = [String]()
        let fontText = "FONT COLOR="
        
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject000 = json["Element_000"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject001 = json["Element_001"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject005 = json["Element_005"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject006 = json["Element_006"] as? [String: AnyObject] else {
                return parsedData
            }
            guard let items000Value = itemsObject000["value"] as? String else {
                return parsedData
            }
            if let rangeS = items000Value.range(of: fontText) {
                let weaponseIndex = items000Value.distance(from: items000Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items000Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            
            guard let items001Value = itemsObject001["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items005Value = itemsObject005["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items006Value = itemsObject006["value"] as? [String:AnyObject] else {
                return parsedData
            }
            // 무기, 장갑, 하의, 상의, 견갑, 투구
            guard let basicText = items005Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let basicStat = items005Value["Element_001"] as? String else {
                return parsedData
            }
            
            guard let optionalText = items006Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let optionalStat = items006Value["Element_001"] as? String else {
                return parsedData
            }
            
            guard let itemquality = items001Value["qualityValue"] as? Int  else {
                return parsedData
            }
            
            let needData = [weaponseColor, optionalText, optionalStat, basicText, basicStat, String(itemquality)]
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        
        return parsedData
    }
    
    func parseAccessoriesJSONString(jsonString : String?) -> [String]? {

        var parsedData = [String]()
        let fontText = "FONT COLOR="
        
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject000 = json["Element_000"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject001 = json["Element_001"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject005 = json["Element_005"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject006 = json["Element_006"] as? [String: AnyObject] else {
                return parsedData
            }
            guard let items000Value = itemsObject000["value"] as? String else {
                return parsedData
            }
            if let rangeS = items000Value.range(of: fontText) {
                let weaponseIndex = items000Value.distance(from: items000Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items000Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            guard let items001Value = itemsObject001["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items005Value = itemsObject005["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items006Value = itemsObject006["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            
            
            guard let randomSealText = items006Value["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffects = randomSealText["contentStr"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffectText = randomSealText["topStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect1 = randomSealEffects["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect1Text = randomSealEffect1["contentStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect2 = randomSealEffects["Element_001"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect2Text = randomSealEffect2["contentStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect3 = randomSealEffects["Element_002"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect3Text = randomSealEffect3["contentStr"] as? String else {
                return parsedData
            }
            
            guard let optionalText = items005Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let optionalStat = items005Value["Element_001"] as? String else {
                return parsedData
            }
            
            guard let itemquality = items001Value["qualityValue"] as? Int  else {
                return parsedData
            }
            
            let needData = [weaponseColor, optionalText, optionalStat, randomSealEffect1Text, randomSealEffect2Text, randomSealEffect3Text,  String(itemquality), randomSealEffectText]
            
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        
        return parsedData
    }
    
    
    func parseAccessoriesJSONString2(jsonString : String?) -> [String]? {
        var parsedData = [String]()
        let fontText = "FONT COLOR="
        
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject000 = json["Element_000"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject001 = json["Element_001"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject005 = json["Element_005"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject006 = json["Element_006"] as? [String: AnyObject] else {
                return parsedData
            }
            guard let items000Value = itemsObject000["value"] as? String else {
                return parsedData
            }
            if let rangeS = items000Value.range(of: fontText) {
                let weaponseIndex = items000Value.distance(from: items000Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items000Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            guard let items001Value = itemsObject001["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items005Value = itemsObject005["value"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let items006Value = itemsObject006["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealText = items006Value["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffects = randomSealText["contentStr"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffectText = randomSealText["topStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect1 = randomSealEffects["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect1Text = randomSealEffect1["contentStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect2 = randomSealEffects["Element_001"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect2Text = randomSealEffect2["contentStr"] as? String else {
                return parsedData
            }
            
            guard let optionalText = items005Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let optionalStat = items005Value["Element_001"] as? String else {
                return parsedData
            }
            
            guard let itemquality = items001Value["qualityValue"] as? Int  else {
                return parsedData
            }
            
            let needData = [weaponseColor, optionalText, optionalStat, randomSealEffect1Text, randomSealEffect2Text, "",  String(itemquality), randomSealEffectText]

            
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        
        return parsedData
    }
   
    func parseAccessoriesJSONString3(jsonString : String?) -> [String]? {
        var parsedData = [String]()
        let fontText = "FONT COLOR="
        
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject000 = json["Element_000"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject001 = json["Element_001"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject005 = json["Element_005"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject006 = json["Element_006"] as? [String: AnyObject] else {
                return parsedData
            }
            guard let items000Value = itemsObject000["value"] as? String else {
                return parsedData
            }
            if let rangeS = items000Value.range(of: fontText) {
                let weaponseIndex = items000Value.distance(from: items000Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items000Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            guard let items001Value = itemsObject001["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items005Value = itemsObject005["value"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let items006Value = itemsObject006["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealText = items006Value["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffects = randomSealText["contentStr"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffectText = randomSealText["topStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect1 = randomSealEffects["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect1Text = randomSealEffect1["contentStr"] as? String else {
                return parsedData
            }
            
            guard let optionalText = items005Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let optionalStat = items005Value["Element_001"] as? String else {
                return parsedData
            }
            
            guard let itemquality = items001Value["qualityValue"] as? Int  else {
                return parsedData
            }
            
            let needData = [weaponseColor, optionalText, optionalStat, randomSealEffect1Text, "", "",  String(itemquality), randomSealEffectText]

            
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        
        return parsedData
    }
   
    
    func parseAbilityStoneJSONString1(jsonString : String?) -> [String]? {
        var parsedData = [String]()
        let fontText = "FONT COLOR="
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject000 = json["Element_000"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject001 = json["Element_001"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject004 = json["Element_004"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject005 = json["Element_005"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject006 = json["Element_006"] as? [String: AnyObject] else {
                return parsedData
            }
            guard let items000Value = itemsObject000["value"] as? String else {
                return parsedData
            }
            if let rangeS = items000Value.range(of: fontText) {
                let weaponseIndex = items000Value.distance(from: items000Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items000Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            guard let items001Value = itemsObject001["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items004Value = itemsObject004["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let basicStatText = items004Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let basicStat = items004Value["Element_001"] as? String else {
                return parsedData
            }
                
            guard let items005Value = itemsObject005["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let optionalStatText = items005Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let optionalStat = items005Value["Element_001"] as? String else {
                return parsedData
            }
                
            guard let items006Value = itemsObject006["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealText = items006Value["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffects = randomSealText["contentStr"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffect1 = randomSealEffects["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect1Text = randomSealEffect1["contentStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect2 = randomSealEffects["Element_001"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect2Text = randomSealEffect2["contentStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect3 = randomSealEffects["Element_002"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect3Text = randomSealEffect3["contentStr"] as? String else {
                return parsedData
            }
            
            guard let itemquality = items001Value["qualityValue"] as? Int  else {
                return parsedData
            }
            
            let needData = [weaponseColor, basicStatText, basicStat, optionalStatText, optionalStat, randomSealEffect1Text, randomSealEffect2Text, randomSealEffect3Text, String(itemquality)]
            
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        return parsedData
    }
    func parseAbilityStoneJSONString2(jsonString : String?) -> [String]? {
        var parsedData = [String]()
        let fontText = "FONT COLOR="
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject000 = json["Element_000"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject001 = json["Element_001"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject004 = json["Element_004"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject005 = json["Element_005"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let items000Value = itemsObject000["value"] as? String else {
                return parsedData
            }
            if let rangeS = items000Value.range(of: fontText) {
                let weaponseIndex = items000Value.distance(from: items000Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items000Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            guard let items001Value = itemsObject001["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let items004Value = itemsObject004["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let basicStatText = items004Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let basicStat = items004Value["Element_001"] as? String else {
                return parsedData
            }
                
            guard let items005Value = itemsObject005["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealText = items005Value["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffects = randomSealText["contentStr"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let randomSealEffect1 = randomSealEffects["Element_000"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect1Text = randomSealEffect1["contentStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect2 = randomSealEffects["Element_001"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect2Text = randomSealEffect2["contentStr"] as? String else {
                return parsedData
            }
            
            guard let randomSealEffect3 = randomSealEffects["Element_002"] as? [String:AnyObject] else {
                return parsedData
            }
            guard let randomSealEffect3Text = randomSealEffect3["contentStr"] as? String else {
                return parsedData
            }
            
            guard let itemquality = items001Value["qualityValue"] as? Int  else {
                return parsedData
            }
            
            let needData = [weaponseColor, basicStatText, basicStat, "", "", randomSealEffect1Text, randomSealEffect2Text, randomSealEffect3Text, String(itemquality)]
            
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        return parsedData
    }
    
    func parseBraceletJSONString(jsonString : String?) -> [String]? {
        var parsedData = [String]()
        let fontText = "FONT COLOR="
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject000 = json["Element_000"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject004 = json["Element_004"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject005 = json["Element_005"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let items000Value = itemsObject000["value"] as? String else {
                return parsedData
            }
            if let rangeS = items000Value.range(of: fontText) {
                let weaponseIndex = items000Value.distance(from: items000Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items000Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            guard let items004Value = itemsObject004["value"] as? [String:AnyObject] else {
                return parsedData
            }
            
            guard let canEnchantText = itemsObject005["value"] as? String else {
                return parsedData
            }
            
            guard let optionalStat = items004Value["Element_001"] as? String else {
                return parsedData
            }
            
            guard let optionalStatText = items004Value["Element_000"] as? String else {
                return parsedData
            }
            
            let needData = [weaponseColor, optionalStatText, optionalStat, canEnchantText]
            
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        return parsedData
    }
    
    func parseEngravingJSONString(jsonString : String?) -> [String]? {
        var parsedData = [String]()
        let fontText = "FONT COLOR="
        let fontEndText = "</FONT>"
        let engravingStartText = "각인 활성 포인트 "
        var engravingLevel = ""
        var weaponseColor = ""
        
        guard let data = (jsonString ?? "").data(using: .utf8) else {
            return nil
        }
        
        do {
            guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let itemsObject001 = json["Element_001"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let items001Value = itemsObject001["value"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let engravingText = items001Value["leftText"] as? String else {
                return parsedData
            }
            if let rangeS = engravingText.range(of: engravingStartText) {
                if let rangeE = engravingText.range(of: fontEndText) {
                    let startIndex = engravingText.distance(from: engravingText.startIndex, to: rangeS.lowerBound) + engravingStartText.count
                    let endIndx = engravingText.distance(from: engravingText.startIndex, to: rangeE.lowerBound)
                    engravingLevel = engravingText.substring(from: startIndex+1, to: endIndx - 1)
                }
            }
            
            guard let itemsObject002 = json["Element_002"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let items002Value = itemsObject002["value"] as? String else {
                return parsedData
            }
            if let rangeS = items002Value.range(of: fontText) {
                let weaponseIndex = items002Value.distance(from: items002Value.startIndex, to: rangeS.lowerBound) + fontText.count
                weaponseColor = items002Value.substring(from: weaponseIndex+1, to: weaponseIndex + 7)
            }
            
            guard let itemsObject003 = json["Element_003"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let items003Value = itemsObject003["value"] as? [String: AnyObject] else {
                return parsedData
            }
            
            guard let optionalText = items003Value["Element_000"] as? String else {
                return parsedData
            }
            
            guard let optionalStat = items003Value["Element_001"] as? String else {
                return parsedData
            }
            let needData = [weaponseColor, optionalText, optionalStat, engravingLevel]
            
            for data in needData {
                parsedData.append(data)
            }
        } catch {
            print("JSON 파싱 에러")
        }
        return parsedData
    }
    
    var body: some View {
        
        let findEquipmentElements: [[String]] = findEquipments(equipmentsData: searchCharacterEqupments.posts![characterName])
        let findAccessoriesElements: [[String]] = findAccessories(equipmentsData: searchCharacterEqupments.posts![characterName])
        let findAbilitystoneElements: [[String]] = findAbilityStone(equipmentsData: searchCharacterEqupments.posts![characterName])
        let findBraceletElements: [[String]] = findBracelet(equipmentsData: searchCharacterEqupments.posts![characterName])
        let findEngravingsElements: [[String]] = findEngravings(engravingsData: searchCharacterEngraings.posts![characterName])
        
        if isEquipmentsViewStatus == 0 {
            HStack {
                VStack(alignment: .leading) {
                    ForEach(findEquipmentElements.indices, id: \.self) {equipmentIdx in
                        if equipmentsArray.contains(findEquipmentElements[equipmentIdx][0]) {
                            let equipmentName = findEquipmentElements[equipmentIdx][1]
                            let equipmentImage =  findEquipmentElements[equipmentIdx][2]
                            let equipmentColor =  findEquipmentElements[equipmentIdx][4]
                            let equipmentOptionText =  findEquipmentElements[equipmentIdx][5]
                            let equipmentOptionStat =  findEquipmentElements[equipmentIdx][6]
                            let equipmentBasicText =  findEquipmentElements[equipmentIdx][7]
                            let equipmentBasicStat =  findEquipmentElements[equipmentIdx][8]
                            let equipmentQuality =  findEquipmentElements[equipmentIdx][9]
                            let qualityColor = findItemQualityColor(quaility: Int(equipmentQuality) ?? 0)
                            
                            HStack {
                                AsyncImage(url: URL(string: equipmentImage)) { phash in
                                    if let image = phash.image {
                                        image.ImageModifier()
                                    } else if phash.error != nil {
                                        Image(systemName: "exclamationmark.icloud.fill").IconModifier().foregroundColor(.red)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    } else {
                                        Image(systemName: "photo.circle.fill").IconModifier().foregroundColor(.clear)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    }
                                }
                                .multilineTextAlignment(.leading)
                                .frame(width: w * 0.05)
                                HStack {
                                    Text("\(equipmentName)")
                                        .foregroundColor(Color(hex: equipmentColor))
                                    Text("[\(equipmentQuality)]")
                                        .foregroundColor(qualityColor)
                                }
                            }.onTapGesture(perform: {
                                self.isEquipmentsViewStatus = 1
                                
                                self.equipmentName = equipmentName
                                self.equipmentImage = equipmentImage
                                self.equipmentQuality = equipmentQuality
                                self.equipmentBasicStat = equipmentBasicStat
                                self.equipmentOptionStat = equipmentOptionStat
                                self.equipmentBasicText = equipmentBasicText
                                self.equipmentOptionText = equipmentOptionText
                                self.equipmentColor = equipmentColor
                                self.equipmentQuailityColor = qualityColor
                            })
                            
                        }
                    }
                    ForEach(findEngravingsElements.indices, id: \.self) { engravingIdx in
                        let engravingName = findEngravingsElements[engravingIdx][0]
                        let engravingImage = findEngravingsElements[engravingIdx][1]
                        let engravingColor = findEngravingsElements[engravingIdx][2]
                        let engravingText = findEngravingsElements[engravingIdx][3]
                        let engravingDescription =  findEngravingsElements[engravingIdx][4]
                        let engravingLevel = findEngravingsElements[engravingIdx][5]
                        
                        HStack {
                            AsyncImage(url: URL(string: engravingImage)) { phash in
                                if let image = phash.image {
                                    image.ImageModifier()
                                } else if phash.error != nil {
                                    Image(systemName: "exclamationmark.icloud.fill").IconModifier().foregroundColor(.red)
                                        .multilineTextAlignment(.leading)
                                        .frame(width: w * 0.05)
                                } else {
                                    Image(systemName: "photo.circle.fill").IconModifier().foregroundColor(.clear)
                                        .multilineTextAlignment(.leading)
                                        .frame(width: w * 0.05)
                                }
                            }
                            .multilineTextAlignment(.leading)
                            .frame(width: w * 0.05)
                            HStack {
                                Text("\(engravingName) + \(engravingLevel)")
                                    .foregroundColor(Color(hex: engravingColor))
                            }
                        }
                        .onTapGesture(perform: {
                            self.isEquipmentsViewStatus = 2
                            self.engravingName = engravingName
                            self.engravingImage = engravingImage
                            self.engravingColor = engravingColor
                            self.engravingText = engravingText
                            self.engravingDescription = engravingDescription
                            self.engravingLevel = engravingLevel
                        })
                    }
                    Spacer()
                }
                
                Spacer()
                
                VStack(alignment: .leading) {
                    ForEach(findAccessoriesElements.indices, id: \.self) {equipmentIdx in
                        if accessoriesArray.contains(findAccessoriesElements[equipmentIdx][0]) {
                            let accessoriesName = findAccessoriesElements[equipmentIdx][1]
                            let accessoriesImage =  findAccessoriesElements[equipmentIdx][2]
                            let accessoriesColor =  findAccessoriesElements[equipmentIdx][4]
                            let accessoriesOptionalText = findAccessoriesElements[equipmentIdx][5]
                            let accessoriesOptionalStat = findAccessoriesElements[equipmentIdx][6]
                            let accessoriesRandomEffect1 = findAccessoriesElements[equipmentIdx][7]
                            let accessoriesRandomEffect2 = findAccessoriesElements[equipmentIdx][8]
                            let accessoriesRandomEffect3 = findAccessoriesElements[equipmentIdx][9]
                            let accessoriesQuality =  findAccessoriesElements[equipmentIdx][10]
                            let accessoriesRandomEffectText = findAccessoriesElements[equipmentIdx][11]
                            
                            let qualityColor = findItemQualityColor(quaility: Int(accessoriesQuality) ?? 0)
                            HStack {
                                AsyncImage(url: URL(string: accessoriesImage)) { phash in
                                    if let image = phash.image {
                                        image.ImageModifier()
                                    } else if phash.error != nil {
                                        Image(systemName: "exclamationmark.icloud.fill").IconModifier().foregroundColor(.red)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    } else {
                                        Image(systemName: "photo.circle.fill").IconModifier().foregroundColor(.clear)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    }
                                }
                                .multilineTextAlignment(.leading)
                                .frame(width: w * 0.05)
                                HStack {
                                    Text("\(accessoriesName)")
                                        .foregroundColor(Color(hex: accessoriesColor))
                                    
                                    Text("[\(accessoriesQuality)]")
                                        .foregroundColor(qualityColor)
                                }
                                
                            }
                            .onTapGesture(perform: {
                                self.isEquipmentsViewStatus = 3
                                
                                self.accessoriesName = accessoriesName
                                self.accessoriesImage = accessoriesImage
                                self.accessoriesColor = accessoriesColor
                                self.accessoriesOptionalText = accessoriesOptionalText
                                self.accessoriesOptionalStat = accessoriesOptionalStat
                                self.accessoriesRandomEffect1 = accessoriesRandomEffect1
                                self.accessoriesRandomEffect2 = accessoriesRandomEffect2
                                self.accessoriesRandomEffect3 = accessoriesRandomEffect3
                                self.accessoriesQuality = accessoriesQuality
                                self.accessoriesQualityColor = qualityColor
                                self.accessoriesRandomEffectText = accessoriesRandomEffectText
                                
                            })
                        }
                    }
                    ForEach(findBraceletElements.indices, id: \.self) {equipmentIdx in
                        if bracelet.contains(findBraceletElements[equipmentIdx][0]){
                            let braceletName = findBraceletElements[equipmentIdx][1]
                            let braceletImage = findBraceletElements[equipmentIdx][2]
                            let braceletColor = findBraceletElements[equipmentIdx][4]
                            let braceletOptionalStatText = findBraceletElements[equipmentIdx][5]
                            let braceletOptionalStat = findBraceletElements[equipmentIdx][6]
                            let braceletCanEnchantText = findBraceletElements[equipmentIdx][7]
                            
                            HStack {
                                AsyncImage(url: URL(string: braceletImage)) { phash in
                                    if let image = phash.image {
                                        image.ImageModifier()
                                    } else if phash.error != nil {
                                        Image(systemName: "exclamationmark.icloud.fill").IconModifier().foregroundColor(.red)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    } else {
                                        Image(systemName: "photo.circle.fill").IconModifier().foregroundColor(.clear)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    }
                                }
                                .multilineTextAlignment(.leading)
                                .frame(width: w * 0.05)
                                HStack {
                                    Text("\(braceletName)")
                                        .foregroundColor(Color(hex: braceletColor))
                                }
                            }
                            .onTapGesture(perform: {
                                self.isEquipmentsViewStatus = 4
            
                                self.braceletName = braceletName
                                self.braceletImage = braceletImage
                                self.braceletColor = braceletColor
                                self.braceletStatText = braceletOptionalStatText
                                self.braceletOptionalStat = braceletOptionalStat
                                self.braceletEnchantText = braceletCanEnchantText
                            })
                        }
                    }
                    ForEach(findAbilitystoneElements.indices, id: \.self) {equipmentIdx in
                        if abilitystone.contains(findAbilitystoneElements[equipmentIdx][0]){
                            let abilitystoneName = findAbilitystoneElements[equipmentIdx][1]
                            let abilitystoneImage =  findAbilitystoneElements[equipmentIdx][2]
                            let abilitystoneColor =  findAbilitystoneElements[equipmentIdx][4]
                            
                            let abilitystoneBasicText =  findAbilitystoneElements[equipmentIdx][5]
                            let abilitystoneBasicStat =  findAbilitystoneElements[equipmentIdx][6]
                            let abilitystoneOptionText =  findAbilitystoneElements[equipmentIdx][7]
                            let abilitystoneOptionStat =  findAbilitystoneElements[equipmentIdx][8]
                            let abilitystoneOption1Text =  findAbilitystoneElements[equipmentIdx][9]
                            let abilitystoneOption2Text =  findAbilitystoneElements[equipmentIdx][10]
                            let abilitystoneOption3Text =  findAbilitystoneElements[equipmentIdx][11]
                            
                            let abilitystoneQuality =  findAbilitystoneElements[equipmentIdx][12]
                            let abilitystoneQulityColor = findItemQualityColor(quaility: Int(abilitystoneQuality) ?? 0)
                            
                            HStack {
                                AsyncImage(url: URL(string: abilitystoneImage)) { phash in
                                    if let image = phash.image {
                                        image.ImageModifier()
                                    } else if phash.error != nil {
                                        Image(systemName: "exclamationmark.icloud.fill").IconModifier().foregroundColor(.red)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    } else {
                                        Image(systemName: "photo.circle.fill").IconModifier().foregroundColor(.clear)
                                            .multilineTextAlignment(.leading)
                                            .frame(width: w * 0.05)
                                    }
                                }
                                .multilineTextAlignment(.leading)
                                .frame(width: w * 0.05)
                                HStack {
                                    Text("\(abilitystoneName)")
                                        .foregroundColor(Color(hex: abilitystoneColor))
                                    
                                    if abilitystoneQuality != "-1" {
                                        Text("[\(abilitystoneQuality)]")
                                            .foregroundColor(abilitystoneQulityColor)
                                    }
                                }
                            }
                            .onTapGesture(perform: {
                                self.isEquipmentsViewStatus = 5
                                
                                self.abilitystoneName = abilitystoneName
                                self.abilitystoneImage = abilitystoneImage
                                self.abilitystoneColor = abilitystoneColor
                                
                                self.abilitystoneName = abilitystoneName
                                self.abilitystoneImage =  abilitystoneImage
                                self.abilitystoneColor =  abilitystoneColor
                                
                                self.abilitystoneBasicText =   abilitystoneBasicText
                                self.abilitystoneBasicStat =  abilitystoneBasicStat
                                self.abilitystoneOptionText =  abilitystoneOptionText
                                self.abilitystoneOptionStat =  abilitystoneOptionStat
                                self.abilitystoneOption1Text =  abilitystoneOption1Text
                                self.abilitystoneOption2Text =  abilitystoneOption2Text
                                self.abilitystoneOption3Text =  abilitystoneOption3Text
                                
                                self.abilitystoneQuality = abilitystoneQuality
                                self.abilitystoneQulityColor = abilitystoneQulityColor
                                
                            })
                        }
                    }
                    Spacer()
                }
            }
        } else if isEquipmentsViewStatus == 1 {
            Button(action: {
                self.isEquipmentsViewStatus = 0
            } ) {
                EquipmentDescriptionView(equipmentName: self.$equipmentName, equipmentImage: self.$equipmentImage, equipmentQuality: self.$equipmentQuality, equipmentBasicStat: self.$equipmentBasicStat, equipmentOptionStat: self.$equipmentOptionStat, equipmentBasicText: self.$equipmentBasicText, equipmentOptionText: self.$equipmentOptionText, equipmentColor: self.$equipmentColor, equipmentQulityColor: $equipmentQuailityColor)
            }
        } else if isEquipmentsViewStatus == 2 {
            Button(action: {
                self.isEquipmentsViewStatus = 0
            } ) {
                EngravingDescriptionView(engravingName: self.$engravingName, engravingImage: self.$engravingImage, engravingColor: self.$engravingColor, engravingDescription: self.$engravingDescription, engravingLevel: self.$engravingLevel,engravingText: self.$engravingText)
            }
        } else if isEquipmentsViewStatus == 3 {
            Button(action: {
                self.isEquipmentsViewStatus = 0
            } ) {
                AccessoriesDescriptionView(accessoriesName: self.$accessoriesName, accessoriesImage: self.$accessoriesImage, accessoriesColor: self.$accessoriesColor, accessoriesOptionalText: self.$accessoriesOptionalText, accessoriesOptionalStat: self.$accessoriesOptionalStat, accessoriesRandomEffect1: self.$accessoriesRandomEffect1, accessoriesRandomEffect2: self.$accessoriesRandomEffect2, accessoriesRandomEffect3: self.$accessoriesRandomEffect3, accessoriesQuality: self.$accessoriesQuality, accessoriesQualityColor: self.$accessoriesQualityColor, accessoriesRandomEffectText: self.$accessoriesRandomEffectText)
            }
        } else if isEquipmentsViewStatus == 4 {
            Button(action: {
                self.isEquipmentsViewStatus = 0
            } ) {
                BraceletDescriptionView(braceletName: self.$braceletName, braceletImage: self.$braceletImage, braceletColor: self.$braceletColor, braceletStatText: self.$braceletStatText, braceletOptionalStat: self.$braceletOptionalStat, braceletEnchantText: self.$braceletEnchantText)
            }
            
        } else if isEquipmentsViewStatus == 5 {
            Button(action: {
                self.isEquipmentsViewStatus = 0
            } ) {
                AbilityStoneDescriptionView(abilitystoneName: self.$abilitystoneName, abilitystoneImage: self.$abilitystoneImage, abilitystoneColor: self.$abilitystoneColor, abilitystoneBasicText: self.$abilitystoneBasicText, abilitystoneBasicStat: self.$abilitystoneBasicStat, abilitystoneOptionText: self.$abilitystoneOptionText, abilitystoneOptionStat: self.$abilitystoneOptionStat, abilitystoneOption1Text: self.$abilitystoneOption1Text, abilitystoneOption2Text: self.$abilitystoneOption2Text, abilitystoneOption3Text: self.$abilitystoneOption3Text, abilitystoneQuality: self.$abilitystoneQuality, abilitystoneQulityColor: self.$abilitystoneQulityColor)
            }
        }
    }
}
728x90
반응형
LIST