OSDN Git Service

各種報告書の表を列単位で検索可能にする
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Model / Achievement.cs
1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 // \r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.Xml.Serialization;\r
18 \r
19 namespace KancolleSniffer.Model\r
20 {\r
21     public class Achievement : IHaveState\r
22     {\r
23         private int _current;\r
24 \r
25         public int Start { get; set; }\r
26         public int StartOfMonth { get; set; }\r
27         public DateTime LastReset { get; set; }\r
28         public DateTime LastResetOfMonth { get; set; }\r
29 \r
30         private const double ExpPerAch = 10000 / 7.0;\r
31         public double Value => (_current - Start) / ExpPerAch;\r
32         public double ValueOfMonth => (_current - StartOfMonth) / ExpPerAch;\r
33 \r
34         [XmlIgnore]\r
35         public List<int> ResetHours { private get; set; }\r
36 \r
37         [XmlIgnore]\r
38         public bool NeedSave { get; private set; }\r
39 \r
40         public Achievement()\r
41         {\r
42             ResetHours = new List<int>();\r
43         }\r
44 \r
45         public void InspectBasic(dynamic json)\r
46         {\r
47             var now = DateTime.Now;\r
48             var today = DateTime.Today;\r
49             _current = (int)json.api_experience;\r
50             if (Start == 0)\r
51                 Reset(_current);\r
52             if (StartOfMonth == 0)\r
53                 ResetMonth(_current);\r
54             foreach (var hour in ResetHours)\r
55             {\r
56                 var time = today.AddHours(hour);\r
57                 if (now >= time && LastReset < time)\r
58                     Reset(_current);\r
59             }\r
60             var limitTime = now.AddDays(1).Month != now.Month // 今日が今月末\r
61                 ? today.AddHours(22) // 今日22時\r
62                 : today.AddDays(-today.Day).AddHours(22); // 先月末22時\r
63             if (now >= limitTime && LastResetOfMonth < limitTime)\r
64                 ResetMonth(_current);\r
65         }\r
66 \r
67         public void Reset()\r
68         {\r
69             Reset(_current);\r
70         }\r
71 \r
72         private void Reset(int current)\r
73         {\r
74             Start = current;\r
75             LastReset = DateTime.Now;\r
76             NeedSave = true;\r
77         }\r
78 \r
79         private void ResetMonth(int current)\r
80         {\r
81             StartOfMonth = current;\r
82             LastResetOfMonth = DateTime.Now;\r
83             NeedSave = true;\r
84         }\r
85 \r
86         public void SaveState(Status status)\r
87         {\r
88             NeedSave = false;\r
89             status.Achievement = this;\r
90         }\r
91 \r
92         public void LoadState(Status status)\r
93         {\r
94             var ac = status.Achievement;\r
95             if (ac == null)\r
96                 return;\r
97             Start = ac.Start;\r
98             StartOfMonth = ac.StartOfMonth;\r
99             LastReset = ac.LastReset;\r
100             LastResetOfMonth = ac.LastResetOfMonth;\r
101         }\r
102     }\r
103 }