OSDN Git Service

任務のページが空になるとNullPointerExceptionになるのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / QuestInfo.cs
1 // Copyright (C) 2013 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 class QuestInfo\r
25     {\r
26         private DateTime _lastCreared;\r
27         private readonly SortedDictionary<int, NameAndProgress> _quests = new SortedDictionary<int, NameAndProgress>();\r
28 \r
29         public void Inspect(dynamic json)\r
30         {\r
31             var resetTime = DateTime.Today.AddHours(5);\r
32             if (DateTime.Now >= resetTime && _lastCreared < resetTime)\r
33             {\r
34                 _quests.Clear(); // 前日に未消化のデイリーを消す。\r
35                 _lastCreared = DateTime.Now;\r
36             }\r
37             if (json.api_list == null)\r
38                 return;\r
39             foreach (var entry in json.api_list)\r
40             {\r
41                 if (entry is double) // -1の場合がある。\r
42                     continue;\r
43 \r
44                 var id = (int)entry.api_no;\r
45                 var state = (int)entry.api_state;\r
46                 var progress = (int)entry.api_progress_flag;\r
47                 var name = (string)entry.api_title;\r
48 \r
49                 switch (progress)\r
50                 {\r
51                     case 0:\r
52                         break;\r
53                     case 1:\r
54                         progress = 50;\r
55                         break;\r
56                     case 2:\r
57                         progress = 80;\r
58                         break;\r
59                 }\r
60                 switch (state)\r
61                 {\r
62                     case 2:\r
63                         _quests[id] = new NameAndProgress {Name = name, Progress = progress};\r
64                         break;\r
65                     case 1:\r
66                     case 3:\r
67                         _quests.Remove(id);\r
68                         continue;\r
69                 }\r
70             }\r
71         }\r
72 \r
73         public NameAndProgress[] Quests\r
74         {\r
75             get { return _quests.Values.ToArray(); }\r
76         }\r
77 \r
78         public struct NameAndProgress\r
79         {\r
80             public string Name { get; set; }\r
81             public int Progress { get; set; }\r
82         }\r
83     }\r
84 }