OSDN Git Service

任務の種類を色で区別できるようにする
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / QuestInfo.cs
1 // Copyright (C) 2013, 2015 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>\r
2 // \r
3 // This program is part of KancolleSniffer.\r
4 //\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify\r
6 // it under the terms of the GNU General Public License as published by\r
7 // the Free Software Foundation, either version 3 of the License, or\r
8 // (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 using System;\r
19 using System.Collections.Generic;\r
20 using System.Linq;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public struct QuestStatus\r
25     {\r
26         public int Category { get; set; }\r
27         public string Name { get; set; }\r
28         public int Progress { get; set; }\r
29     }\r
30 \r
31     public class QuestInfo\r
32     {\r
33         private DateTime _lastCreared;\r
34         private readonly SortedDictionary<int, QuestStatus> _quests = new SortedDictionary<int, QuestStatus>();\r
35 \r
36         public int QuestCount { get; set; }\r
37 \r
38         public void Inspect(dynamic json)\r
39         {\r
40             var resetTime = DateTime.Today.AddHours(5);\r
41             if (DateTime.Now >= resetTime && _lastCreared < resetTime)\r
42             {\r
43                 _quests.Clear(); // 前日に未消化のデイリーを消す。\r
44                 _lastCreared = DateTime.Now;\r
45             }\r
46             if (json.api_list == null)\r
47                 return;\r
48             for (var i = 0; i < 2; i++)\r
49             {\r
50                 foreach (var entry in json.api_list)\r
51                 {\r
52                     if (entry is double) // -1の場合がある。\r
53                         continue;\r
54 \r
55                     var id = (int)entry.api_no;\r
56                     var state = (int)entry.api_state;\r
57                     var progress = (int)entry.api_progress_flag;\r
58                     var cat = (int)entry.api_category;\r
59                     var name = (string)entry.api_title;\r
60 \r
61                     switch (progress)\r
62                     {\r
63                         case 0:\r
64                             break;\r
65                         case 1:\r
66                             progress = 50;\r
67                             break;\r
68                         case 2:\r
69                             progress = 80;\r
70                             break;\r
71                     }\r
72                     switch (state)\r
73                     {\r
74                         case 2:\r
75                             _quests[id] = new QuestStatus {Category = cat, Name = name, Progress = progress};\r
76                             break;\r
77                         case 1:\r
78                         case 3:\r
79                             _quests.Remove(id);\r
80                             continue;\r
81                     }\r
82                 }\r
83                 if (_quests.Count <= QuestCount)\r
84                     break;\r
85                 /*\r
86                  * ほかのPCで任務を達成した場合、任務が消えずに受領した任務の数が_questCountを超えることがある。\r
87                  * その場合はいったん任務をクリアして、現在のページの任務だけを登録し直す。\r
88                  */\r
89                 _quests.Clear();\r
90             }\r
91         }\r
92 \r
93         public QuestStatus[] Quests\r
94         {\r
95             get { return _quests.Values.ToArray(); }\r
96         }\r
97     }\r
98 }