OSDN Git Service

改修工廠メニューを更新
[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 {
22             
23             return
24         }
25         
26         let questList = data["api_list"]
27         let questNumberList = questList.array?.compactMap { $0["api_no"].int }
28         
29         guard let firstQuestNumber = questNumberList?.first,
30             let pageCount = data["api_page_count"].int,
31             let page = data["api_disp_page"].int else {
32                 
33                 Logger.shared.log("data is wrong")
34                 
35                 return
36         }
37         
38         let store = ServerDataStore.oneTimeEditor()
39         
40         // 範囲内の任務をいったん遂行中からはずす
41         let lastQuestNumber = questNumberList?.last ?? 9999
42         let minNo = (page == 1) ? 0 : firstQuestNumber
43         let maxNo = (page == pageCount) ? 9999 : lastQuestNumber
44         store.sync { store.quests(in: minNo...maxNo).forEach { $0.state = 1 } }
45         
46         // 新しいデータ投入
47         let quests = store.sync { store.sortedQuestByNo() }
48         questList.forEach { _, quest in
49             
50             guard let no = quest["api_no"].int else {
51                 
52                 return
53             }
54             
55             let t = store.sync { quests.binarySearch { $0.no ==? no } }
56             
57             guard let new = t ?? store.sync(execute: { store.createQuest() }) else {
58                 
59                 Logger.shared.log("Can not create Quest")
60                 
61                 return
62             }
63             
64             store.sync {
65                 
66                 new.bonus_flag = quest["api_bonus_flag"].int.map { $0 != 0 } ?? false
67                 new.category = quest["api_category"].int ?? 0
68                 new.detail = quest["api_detail"].string ?? ""
69 //                new.get_material_0 = questData["api_get_material_0"] as? Int ?? 0
70 //                new.get_material_1 = questData["api_get_material_1"] as? Int ?? 0
71 //                new.get_material_2 = questData["api_get_material_2"] as? Int ?? 0
72 //                new.get_material_3 = questData["api_get_material_3"] as? Int ?? 0
73                 new.invalid_flag = quest["api_invalid_flag"].int ?? 0
74                 new.no = quest["api_no"].int ?? 0
75                 new.progress_flag = quest["api_progress_flag"].int ?? 0
76                 new.state = quest["api_state"].int ?? 0
77                 new.title = quest["api_title"].string ?? ""
78                 new.type = quest["api_type"].int ?? 0
79             }
80         }
81     }
82 }