OSDN Git Service

エラーメッセージのword wrapを止める
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ExMapInfo.cs
1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\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 ExMapInfo : IHaveState\r
25     {\r
26         public class ClearStatus\r
27         {\r
28             public int Map { get; set; }\r
29             public bool Cleared { get; set; }\r
30             public int Rate { get; set; }\r
31         }\r
32 \r
33         private readonly Dictionary<int, ClearStatus> _clearStatus =\r
34             new Dictionary<int, ClearStatus>\r
35             {\r
36                 {15, new ClearStatus {Map = 15, Cleared = false, Rate = 75}},\r
37                 {16, new ClearStatus {Map = 16, Cleared = false, Rate = 75}},\r
38                 {25, new ClearStatus {Map = 25, Cleared = false, Rate = 100}},\r
39                 {35, new ClearStatus {Map = 35, Cleared = false, Rate = 150}},\r
40                 {45, new ClearStatus {Map = 45, Cleared = false, Rate = 180}},\r
41                 {55, new ClearStatus {Map = 55, Cleared = false, Rate = 200}}\r
42             };\r
43 \r
44         private DateTime _lastReset;\r
45 \r
46         private int _currentMap;\r
47 \r
48         public bool NeedSave { get; private set; }\r
49 \r
50         public void InspectMapInfo(dynamic json)\r
51         {\r
52             foreach (var entry in json)\r
53             {\r
54                 var map = (int)entry.api_id;\r
55                 if (map % 10 <= 4)\r
56                     continue;\r
57                 ClearStatus stat;\r
58                 if (!_clearStatus.TryGetValue(map, out stat))\r
59                     _clearStatus.Add(map, stat = new ClearStatus { Map = map});\r
60                 var prev = stat.Cleared;\r
61                 stat.Cleared = (int)entry.api_cleared == 1;\r
62                 if (prev != stat.Cleared)\r
63                     NeedSave = true;\r
64             }\r
65         }\r
66 \r
67         public void InspectMapStart(dynamic json)\r
68         {\r
69             InspectMapNext(json);\r
70         }\r
71 \r
72         public void InspectMapNext(dynamic json)\r
73         {\r
74             _currentMap = (int)json.api_maparea_id * 10 + (int)json.api_mapinfo_no;\r
75             if (!json.api_get_eo_rate() || json.api_get_eo_rate == 0)\r
76                 return;\r
77             ClearStatus stat;\r
78             if (!_clearStatus.TryGetValue(_currentMap, out stat))\r
79                 _clearStatus.Add(_currentMap, stat = new ClearStatus{Map = _currentMap});\r
80             stat.Cleared = true;\r
81             stat.Rate = (int)json.api_get_eo_rate;\r
82             NeedSave = true;\r
83         }\r
84 \r
85         public void InspectBattleResult(dynamic json)\r
86         {\r
87             if (!json.api_get_exmap_rate())\r
88                 return;\r
89             var rate = json.api_get_exmap_rate is string\r
90                 ? int.Parse(json.api_get_exmap_rate)\r
91                 : (int)json.api_get_exmap_rate;\r
92             if (rate == 0)\r
93                 return;\r
94             ClearStatus stat;\r
95             if (!_clearStatus.TryGetValue(_currentMap, out stat))\r
96                 _clearStatus.Add(_currentMap, stat = new ClearStatus{Map = _currentMap});\r
97             stat.Cleared = true;\r
98             stat.Rate = rate;\r
99             NeedSave = true;\r
100         }\r
101 \r
102         public int Achievement => _clearStatus.Values.Where(s => s.Cleared).Sum(s => s.Rate);\r
103 \r
104         public void ResetIfNeeded()\r
105         {\r
106             var now = DateTime.Now;\r
107             if (_lastReset.Month == now.Month)\r
108                 return;\r
109             _lastReset = now;\r
110             foreach (var e in _clearStatus.Values)\r
111                 e.Cleared = false;\r
112         }\r
113 \r
114         // テスト用\r
115         public void ClearClearStatus()\r
116         {\r
117             _clearStatus.Clear();\r
118         }\r
119 \r
120         public void SaveState(Status status)\r
121         {\r
122             NeedSave = false;\r
123             status.ExMapState = new ExMapState\r
124             {\r
125                 ClearStatusList = _clearStatus.Values.ToArray(),\r
126                 LastReset = _lastReset\r
127             };\r
128         }\r
129 \r
130         public void LoadState(Status status)\r
131         {\r
132             if (status.ExMapState == null)\r
133                 return;\r
134             _lastReset = status.ExMapState.LastReset;\r
135             foreach (var s in status.ExMapState.ClearStatusList)\r
136                 _clearStatus[s.Map] = s;\r
137         }\r
138 \r
139         public class ExMapState\r
140         {\r
141             public ClearStatus[] ClearStatusList { get; set; }\r
142             public DateTime LastReset { get; set; }\r
143         }\r
144     }\r
145 }