OSDN Git Service

swiftlint 'line_length'の警告を修正
[kcd/KCD.git] / KCD / QuestListCommand.swift
1 //
2 //  QuestListCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/08.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class QuestListCommand: JSONCommand {
12     override class func canExecuteAPI(_ api: String) -> Bool {
13         if api == "/kcsapi/api_get_member/questlist" { return true }
14         return false
15     }
16     override func execute() {
17         // 左のタブがAllじゃない時は無視する
18         guard let tab = arguments["api_tab_id"].flatMap({ Int($0) }),
19             tab == 0
20             else { return }
21         
22         let j = json as NSDictionary
23         guard let data = j.value(forKeyPath: dataKey) as? [String: Any],
24             let questList = data["api_list"] as? [Any],
25             let ql0 = questList.first as? [String: Any],
26             let ql0No = ql0["api_no"] as? Int,
27             let pageCount = data["api_page_count"] as? Int,
28             let page = data["api_disp_page"] as? Int
29             else { return print("data is wrong") }
30         
31         let store = ServerDataStore.oneTimeEditor()
32         
33         // 範囲内の任務をいったん遂行中からはずす
34         let qlm = questList.last as? [String: Any]
35         let qlmNo = qlm?["api_no"] as? Int ?? 9999
36         let minNo = (page == 1) ? 0 : ql0No
37         let maxNo = (page == pageCount) ? 9999 : qlmNo
38         store.quests(in: minNo...maxNo).forEach { $0.state = 1 }
39         
40         // 新しいデータ投入
41         let quests = store.sortedQuestByNo()
42         questList.forEach {
43             guard let questData = $0 as? [String: Any],
44                 let no = questData["api_no"] as? Int
45             else { return }
46             let t = quests.binarySearch { $0.no ==? no }
47             guard let new = t ?? store.createQuest()
48                 else { return print("Can not create Quest") }
49             new.bonus_flag = questData["api_bonus_flag"] as? Bool ?? false
50             new.category = questData["api_category"] as? Int ?? 0
51             new.detail = questData["api_detail"] as? String ?? ""
52 //            new.get_material_0 = questData["api_get_material_0"] as? Int ?? 0
53 //            new.get_material_1 = questData["api_get_material_1"] as? Int ?? 0
54 //            new.get_material_2 = questData["api_get_material_2"] as? Int ?? 0
55 //            new.get_material_3 = questData["api_get_material_3"] as? Int ?? 0
56             new.invalid_flag = questData["api_invalid_flag"] as? Int ?? 0
57             new.no = questData["api_no"] as? Int ?? 0
58             new.progress_flag = questData["api_progress_flag"] as? Int ?? 0
59             new.state = questData["api_state"] as? Int ?? 0
60             new.title = questData["api_title"] as? String ?? ""
61             new.type = questData["api_type"] as? Int ?? 0
62         }
63     }
64 }