2 // QuestListCommand.swift
5 // Created by Hori,Masaki on 2017/01/08.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class QuestListCommand: JSONCommand {
13 override class func canExecuteAPI(_ api: API) -> Bool {
15 return api.endpoint == .questList
18 override func execute() {
21 guard let tab = parameter["api_tab_id"].int, tab == 0 else {
26 let questList = data["api_list"]
27 let questNumberList = questList.array?.compactMap { $0["api_no"].int }
29 guard let firstQuestNumber = questNumberList?.first,
30 let pageCount = data["api_page_count"].int,
31 let page = data["api_disp_page"].int else {
33 Logger.shared.log("data is wrong")
38 let store = ServerDataStore.oneTimeEditor()
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 } }
47 let quests = store.sync { store.sortedQuestByNo() }
48 questList.forEach { _, quest in
50 guard let no = quest["api_no"].int else {
55 let t = store.sync { quests.binarySearch { $0.no ==? no } }
57 guard let new = t ?? store.sync(execute: { store.createQuest() }) else {
59 Logger.shared.log("Can not create Quest")
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