OSDN Git Service

Doutakuを導入
[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 final class QuestListCommand: JSONCommand {
12     
13     override class func canExecuteAPI(_ api: API) -> Bool {
14         
15         return api.endpoint == .questList
16     }
17     
18     override func execute() {
19         
20         // 左のタブがAllじゃない時は無視する
21         guard let tab = parameter["api_tab_id"].int, tab == 0 else { return }
22         
23         let questList = data["api_list"]
24         let questNumberList = questList.array?.compactMap { $0["api_no"].int }
25         
26         guard let firstQuestNumber = questNumberList?.first,
27             let pageCount = data["api_page_count"].int,
28             let page = data["api_disp_page"].int else {
29                 
30                 return Logger.shared.log("data is wrong")
31         }
32         
33         let store = ServerDataStore.oneTimeEditor()
34         
35         // 範囲内の任務をいったん遂行中からはずす
36         let lastQuestNumber = questNumberList?.last ?? 9999
37         let minNo = (page == 1) ? 0 : firstQuestNumber
38         let maxNo = (page == pageCount) ? 9999 : lastQuestNumber
39         store.sync { store.quests(in: minNo...maxNo).forEach { $0.state = 1 } }
40         
41         // 新しいデータ投入
42         let quests = store.sync { store.sortedQuestByNo() }
43         questList.forEach { _, quest in
44             
45             guard let no = quest["api_no"].int else { return }
46             
47             let t = store.sync { quests.binarySearch { $0.no ==? no } }
48             
49             guard let new = t ?? store.sync(execute: { store.createQuest() }) else {
50                 
51                 return Logger.shared.log("Can not create Quest")
52             }
53             
54             store.sync {
55                 new.bonus_flag = quest["api_bonus_flag"].int.map { $0 != 0 } ?? false
56                 new.category = quest["api_category"].int ?? 0
57                 new.detail = quest["api_detail"].string ?? ""
58 //                new.get_material_0 = questData["api_get_material_0"] as? Int ?? 0
59 //                new.get_material_1 = questData["api_get_material_1"] as? Int ?? 0
60 //                new.get_material_2 = questData["api_get_material_2"] as? Int ?? 0
61 //                new.get_material_3 = questData["api_get_material_3"] as? Int ?? 0
62                 new.invalid_flag = quest["api_invalid_flag"].int ?? 0
63                 new.no = quest["api_no"].int ?? 0
64                 new.progress_flag = quest["api_progress_flag"].int ?? 0
65                 new.state = quest["api_state"].int ?? 0
66                 new.title = quest["api_title"].string ?? ""
67                 new.type = quest["api_type"].int ?? 0
68             }
69         }
70     }
71 }