OSDN Git Service

艦隊晒しページ用のデータを生成できるようにする
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / TextGenerator.cs
1 // Copyright (C) 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.Collections.Generic;\r
16 using System.IO;\r
17 using System.Linq;\r
18 using System.Text;\r
19 \r
20 namespace KancolleSniffer\r
21 {\r
22     public static class TextGenerator\r
23     {\r
24         public static string GenerateShipList(IEnumerable<ShipStatus> shipList)\r
25             => "ID,艦種,艦名,レベル,cond\r\n" +\r
26                string.Join("\r\n",\r
27                    from ship in shipList\r
28                    orderby ship.Spec.ShipType, -ship.Level, ship.ExpToNext\r
29                    select $"{ship.Id},{ship.Spec.ShipTypeName},{ship.Name},{ship.Level},{ship.Cond}");\r
30 \r
31         public static string GenerateKantaiSarashiData(IEnumerable<ShipStatus> shipList)\r
32         {\r
33             return ".2|" +\r
34                    string.Join("|", from ship in shipList\r
35                        where ship.Locked\r
36                        group ship by ship.Spec.Remodel.Base\r
37                        into grp\r
38                        orderby grp.Key\r
39                        select grp.Key + ":" + string.Join(",", from ship in grp\r
40                            orderby -ship.Level\r
41                            select ship.Level +\r
42                                   (ship.Level >= ship.Spec.Remodel.Level && ship.Spec.Remodel.Step != 0\r
43                                       ? "." + ship.Spec.Remodel.Step\r
44                                       : "")));\r
45         }\r
46 \r
47         public static string GenerateItemList(IEnumerable<ItemStatus> itemList)\r
48             => "区分,装備名,熟練度,改修,個数\r\n" +\r
49                string.Join("\r\n",\r
50                    (from item in itemList\r
51                        where item.Spec.Id != -1\r
52                        orderby item.Spec.Type, item.Spec.Id, item.Alv, item.Level\r
53                        group item by\r
54                            $"{item.Spec.TypeName},{item.Spec.Name},{item.Alv},{item.Level}"\r
55                        into grp\r
56                        select grp.Key + $",{grp.Count()}"));\r
57 \r
58         public static string GenerateFleetData(Sniffer sniffer)\r
59         {\r
60             var dict = new ItemName();\r
61             var sb = new StringBuilder();\r
62             var fn = new[] {"第一艦隊", "第二艦隊", "第三艦隊", "第四艦隊"};\r
63             for (var f = 0; f < fn.Length; f++)\r
64             {\r
65                 sb.Append(fn[f] + "\r\n");\r
66                 foreach (var s in sniffer.GetShipStatuses(f))\r
67                 {\r
68                     sb.Append($"{s.Name} Lv{s.Level}");\r
69                     foreach (var item in s.Slot.Where(item => item.Id != -1))\r
70                     {\r
71                         sb.Append(" " + dict[item.Spec.Name] +\r
72                                   (item.Alv == 0 ? "" : "+" + item.Alv) +\r
73                                   (item.Level == 0 ? "" : "★" + item.Level));\r
74                     }\r
75                     if (s.SlotEx.Id > 0)\r
76                     {\r
77                         var item = s.SlotEx;\r
78                         sb.Append(" " + dict[item.Spec.Name]);\r
79                     }\r
80                     sb.Append("\r\n");\r
81                 }\r
82                 var fp = sniffer.GetFighterPower(f);\r
83                 sb.Append($"制空: {(fp[0] == fp[1] ? fp[0].ToString() : fp[0] + "~" + fp[1])} " +\r
84                           $"索敵: {sniffer.GetFleetLineOfSights(f):F1}\r\n");\r
85             }\r
86             return sb.ToString();\r
87         }\r
88 \r
89         private class ItemName\r
90         {\r
91             private readonly Dictionary<string, string> _dict = new Dictionary<string, string>();\r
92 \r
93             public ItemName()\r
94             {\r
95                 try\r
96                 {\r
97                     foreach (var line in File.ReadLines("ItemName.csv"))\r
98                     {\r
99                         var cols = line.Split(',');\r
100                         _dict[cols[0]] = cols[1];\r
101                     }\r
102                 }\r
103                 catch (IOException)\r
104                 {\r
105                 }\r
106             }\r
107 \r
108             public string this[string name]\r
109             {\r
110                 get\r
111                 {\r
112                     string shortName;\r
113                     return _dict.TryGetValue(name, out shortName) ? shortName : name;\r
114                 }\r
115             }\r
116         }\r
117 \r
118         public static string GenerateDeckBuilderData(Sniffer sniffer)\r
119         {\r
120             var sb = new StringBuilder("{\"version\": 4,");\r
121             for (var f = 0; f < ShipInfo.FleetCount; f++)\r
122             {\r
123                 if (f != 0)\r
124                     sb.Append(",");\r
125                 sb.Append($"\"f{f + 1}\":{{");\r
126                 var ships = sniffer.GetShipStatuses(f);\r
127                 for (var s = 0; s < ships.Length; s++)\r
128                 {\r
129                     if (s != 0)\r
130                         sb.Append(",");\r
131                     var st = ships[s];\r
132                     sb.Append(\r
133                         $"\"s{s + 1}\":{{\"id\":\"{st.Spec.Id}\",\"lv\":{st.Level},\"luck\":{st.Lucky},\"items\":{{");\r
134                     var items = st.Slot;\r
135                     for (var i = 0; i < items.Length; i++)\r
136                     {\r
137                         var it = items[i];\r
138                         if (it.Id == -1)\r
139                             continue;\r
140                         if (i != 0)\r
141                             sb.Append(",");\r
142                         sb.Append($"\"i{i + 1}\":{{\"id\":{it.Spec.Id},\"rf\":{it.Level},\"mas\":{it.Alv}}}");\r
143                     }\r
144                     sb.Append("}}");\r
145                 }\r
146                 sb.Append("}");\r
147             }\r
148             sb.Append("}");\r
149             return sb.ToString();\r
150         }\r
151     }\r
152 }