OSDN Git Service

艦これ統計データベースにデータを送信できるようにする
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Sun, 20 Dec 2015 08:58:23 +0000 (17:58 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Sun, 27 Dec 2015 12:06:27 +0000 (21:06 +0900)
KancolleSniffer/Config.cs
KancolleSniffer/ConfigDialog.Designer.cs
KancolleSniffer/ConfigDialog.cs
KancolleSniffer/KancolleDb.cs [new file with mode: 0644]
KancolleSniffer/KancolleSniffer.csproj
KancolleSniffer/MainForm.cs

index d65ed52..b1cbe6f 100644 (file)
@@ -76,6 +76,12 @@ namespace KancolleSniffer
         }\r
     }\r
 \r
+    public class KancolleDbConfig\r
+    {\r
+        public bool On { get; set; }\r
+        public string Token { get; set; } = "";\r
+    }\r
+\r
     public class Config\r
     {\r
         private readonly string _baseDir = AppDomain.CurrentDomain.BaseDirectory;\r
@@ -109,6 +115,7 @@ namespace KancolleSniffer
         public ProxyConfig Proxy { get; set; }\r
         public ShipListConfig ShipList { get; set; }\r
         public LogConfig Log { get; set; }\r
+        public KancolleDbConfig KancolleDb { get; set; }\r
 \r
         public Config()\r
         {\r
@@ -135,6 +142,7 @@ namespace KancolleSniffer
             Proxy = new ProxyConfig();\r
             ShipList = new ShipListConfig();\r
             Log = new LogConfig();\r
+            KancolleDb = new KancolleDbConfig();\r
             ConvertPath(PrependBaseDir);\r
         }\r
 \r
index da4993f..86ceb95 100644 (file)
@@ -109,6 +109,11 @@ namespace KancolleSniffer
             this.textBoxOutput = new System.Windows.Forms.TextBox();\r
             this.label11 = new System.Windows.Forms.Label();\r
             this.buttonOutputDir = new System.Windows.Forms.Button();\r
+            this.tabPageCollabo = new System.Windows.Forms.TabPage();\r
+            this.groupBoxKancolleDb = new System.Windows.Forms.GroupBox();\r
+            this.textBoxKancolleDbToken = new System.Windows.Forms.TextBox();\r
+            this.label8 = new System.Windows.Forms.Label();\r
+            this.checkBoxKancolleDbOn = new System.Windows.Forms.CheckBox();\r
             this.tabPageDebug = new System.Windows.Forms.TabPage();\r
             this.buttonPlayDebugLog = new System.Windows.Forms.Button();\r
             this.buttonDebugLogOpenFile = new System.Windows.Forms.Button();\r
@@ -138,6 +143,8 @@ namespace KancolleSniffer
             this.tabPageLog.SuspendLayout();\r
             this.groupBox1.SuspendLayout();\r
             ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaterialLogInterval)).BeginInit();\r
+            this.tabPageCollabo.SuspendLayout();\r
+            this.groupBoxKancolleDb.SuspendLayout();\r
             this.tabPageDebug.SuspendLayout();\r
             this.SuspendLayout();\r
             // \r
@@ -152,6 +159,7 @@ namespace KancolleSniffer
             this.tabControl.Controls.Add(this.tabPageVersion);\r
             this.tabControl.Controls.Add(this.tabPageProxy);\r
             this.tabControl.Controls.Add(this.tabPageLog);\r
+            this.tabControl.Controls.Add(this.tabPageCollabo);\r
             this.tabControl.Controls.Add(this.tabPageDebug);\r
             this.tabControl.Location = new System.Drawing.Point(7, 6);\r
             this.tabControl.Multiline = true;\r
@@ -819,6 +827,54 @@ namespace KancolleSniffer
             this.buttonOutputDir.UseVisualStyleBackColor = true;\r
             this.buttonOutputDir.Click += new System.EventHandler(this.buttonOutputDir_Click);\r
             // \r
+            // tabPageCollabo\r
+            // \r
+            this.tabPageCollabo.Controls.Add(this.groupBoxKancolleDb);\r
+            this.tabPageCollabo.Location = new System.Drawing.Point(4, 40);\r
+            this.tabPageCollabo.Name = "tabPageCollabo";\r
+            this.tabPageCollabo.Size = new System.Drawing.Size(256, 158);\r
+            this.tabPageCollabo.TabIndex = 8;\r
+            this.tabPageCollabo.Text = "連携";\r
+            this.tabPageCollabo.UseVisualStyleBackColor = true;\r
+            // \r
+            // groupBoxKancolleDb\r
+            // \r
+            this.groupBoxKancolleDb.Controls.Add(this.textBoxKancolleDbToken);\r
+            this.groupBoxKancolleDb.Controls.Add(this.label8);\r
+            this.groupBoxKancolleDb.Controls.Add(this.checkBoxKancolleDbOn);\r
+            this.groupBoxKancolleDb.Location = new System.Drawing.Point(11, 11);\r
+            this.groupBoxKancolleDb.Name = "groupBoxKancolleDb";\r
+            this.groupBoxKancolleDb.Size = new System.Drawing.Size(234, 48);\r
+            this.groupBoxKancolleDb.TabIndex = 0;\r
+            this.groupBoxKancolleDb.TabStop = false;\r
+            this.groupBoxKancolleDb.Text = "艦これ統計データベース";\r
+            // \r
+            // textBoxKancolleDbToken\r
+            // \r
+            this.textBoxKancolleDbToken.Location = new System.Drawing.Point(135, 18);\r
+            this.textBoxKancolleDbToken.Name = "textBoxKancolleDbToken";\r
+            this.textBoxKancolleDbToken.Size = new System.Drawing.Size(93, 19);\r
+            this.textBoxKancolleDbToken.TabIndex = 2;\r
+            // \r
+            // label8\r
+            // \r
+            this.label8.AutoSize = true;\r
+            this.label8.Location = new System.Drawing.Point(73, 21);\r
+            this.label8.Name = "label8";\r
+            this.label8.Size = new System.Drawing.Size(63, 12);\r
+            this.label8.TabIndex = 1;\r
+            this.label8.Text = "アクセスキー:";\r
+            // \r
+            // checkBoxKancolleDbOn\r
+            // \r
+            this.checkBoxKancolleDbOn.AutoSize = true;\r
+            this.checkBoxKancolleDbOn.Location = new System.Drawing.Point(6, 20);\r
+            this.checkBoxKancolleDbOn.Name = "checkBoxKancolleDbOn";\r
+            this.checkBoxKancolleDbOn.Size = new System.Drawing.Size(67, 16);\r
+            this.checkBoxKancolleDbOn.TabIndex = 0;\r
+            this.checkBoxKancolleDbOn.Text = "送信する";\r
+            this.checkBoxKancolleDbOn.UseVisualStyleBackColor = true;\r
+            // \r
             // tabPageDebug\r
             // \r
             this.tabPageDebug.Controls.Add(this.buttonPlayDebugLog);\r
@@ -969,6 +1025,9 @@ namespace KancolleSniffer
             this.groupBox1.ResumeLayout(false);\r
             this.groupBox1.PerformLayout();\r
             ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaterialLogInterval)).EndInit();\r
+            this.tabPageCollabo.ResumeLayout(false);\r
+            this.groupBoxKancolleDb.ResumeLayout(false);\r
+            this.groupBoxKancolleDb.PerformLayout();\r
             this.tabPageDebug.ResumeLayout(false);\r
             this.tabPageDebug.PerformLayout();\r
             this.ResumeLayout(false);\r
@@ -1052,5 +1111,10 @@ namespace KancolleSniffer
         private System.Windows.Forms.RadioButton radioButtonResultRankWhenClick;\r
         private System.Windows.Forms.RadioButton radioButtonResultRankAlways;\r
         private System.Windows.Forms.CheckBox checkBoxPresetAkashi;\r
+        private System.Windows.Forms.TabPage tabPageCollabo;\r
+        private System.Windows.Forms.GroupBox groupBoxKancolleDb;\r
+        private System.Windows.Forms.TextBox textBoxKancolleDbToken;\r
+        private System.Windows.Forms.Label label8;\r
+        private System.Windows.Forms.CheckBox checkBoxKancolleDbOn;\r
     }\r
 }
\ No newline at end of file
index 45c40a4..871c008 100644 (file)
@@ -85,6 +85,8 @@ namespace KancolleSniffer
             LoadProxySettings();\r
             LoadLogSettings();\r
             LoadDebugSettings();\r
+            checkBoxKancolleDbOn.Checked = _config.KancolleDb.On;\r
+            textBoxKancolleDbToken.Text = _config.KancolleDb.Token;\r
         }\r
 \r
         private void LoadProxySettings()\r
@@ -147,6 +149,8 @@ namespace KancolleSniffer
             if (!ApplyLogSettings(server))\r
                 DialogResult = DialogResult.None;\r
             ApplyDebugSettings();\r
+            _config.KancolleDb.On = checkBoxKancolleDbOn.Checked;\r
+            _config.KancolleDb.Token = textBoxKancolleDbToken.Text;\r
 \r
             _config.TopMost = checkBoxTopMost.Checked;\r
             _config.HideOnMinimized = checkBoxHideOnMinimized.Checked;\r
diff --git a/KancolleSniffer/KancolleDb.cs b/KancolleSniffer/KancolleDb.cs
new file mode 100644 (file)
index 0000000..84fc100
--- /dev/null
@@ -0,0 +1,118 @@
+// Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
+// \r
+// This program is part of KancolleSniffer.\r
+//\r
+// KancolleSniffer is free software: you can redistribute it and/or modify\r
+// it under the terms of the GNU General Public License as published by\r
+// the Free Software Foundation, either version 3 of the License, or\r
+// (at your option) any later version.\r
+//\r
+// This program is distributed in the hope that it will be useful,\r
+// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+// GNU General Public License for more details.\r
+//\r
+// You should have received a copy of the GNU General Public License\r
+// along with this program; if not, see <http://www.gnu.org/licenses/>.\r
+\r
+using System;\r
+using System.Collections.Concurrent;\r
+using System.Collections.Generic;\r
+using System.Collections.Specialized;\r
+using System.Net;\r
+using System.Text.RegularExpressions;\r
+using System.Threading.Tasks;\r
+\r
+namespace KancolleSniffer\r
+{\r
+    public class KancolleDb\r
+    {\r
+        private readonly BlockingCollection<Tuple<string, string, string>> _queue =\r
+            new BlockingCollection<Tuple<string, string, string>>(10);\r
+\r
+        private bool _started;\r
+        private string _token;\r
+\r
+        public void Start(string token)\r
+        {\r
+            _token = token;\r
+            if (_started)\r
+                return;\r
+            _started = true;\r
+            Task.Run(() =>\r
+            {\r
+                try\r
+                {\r
+                    while (true)\r
+                    {\r
+                        var e = _queue.Take();\r
+                        try\r
+                        {\r
+                            using (var wc = new WebClient())\r
+                            {\r
+                                var values = new NameValueCollection\r
+                                {\r
+                                    {"token", _token},\r
+                                    {"agent", "KdpR5STmwYTaFpNCbD4N"},\r
+                                    {"url", e.Item1},\r
+                                    {"requestbody", e.Item2},\r
+                                    {"responsebody", e.Item3}\r
+                                };\r
+                                wc.UploadValues("http://api.kancolle-db.net/2/", values);\r
+                            }\r
+                        }\r
+                        catch (WebException)\r
+                        {\r
+                        }\r
+                    }\r
+                }\r
+                catch (InvalidOperationException)\r
+                {\r
+                }\r
+            });\r
+        }\r
+\r
+        private readonly HashSet<string> _urlSet = new HashSet<string>()\r
+        {\r
+            "/kcsapi/api_port/port",\r
+            "/kcsapi/api_get_member/ship2",\r
+            "/kcsapi/api_get_member/ship3",\r
+            "/kcsapi/api_get_member/slot_item",\r
+            "/kcsapi/api_get_member/kdock",\r
+            "/kcsapi/api_get_member/mapinfo",\r
+            "/kcsapi/api_req_hensei/change",\r
+            "/kcsapi/api_req_kousyou/createship",\r
+            "/kcsapi/api_req_kousyou/getship",\r
+            "/kcsapi/api_req_kousyou/createitem",\r
+            "/kcsapi/api_req_map/start",\r
+            "/kcsapi/api_req_map/next",\r
+            "/kcsapi/api_req_map/select_eventmap_rank",\r
+            "/kcsapi/api_req_sortie/battle",\r
+            "/kcsapi/api_req_battle_midnight/battle",\r
+            "/kcsapi/api_req_battle_midnight/sp_midnight",\r
+            "/kcsapi/api_req_sortie/night_to_day",\r
+            "/kcsapi/api_req_sortie/battleresult",\r
+            "/kcsapi/api_req_combined_battle/battle",\r
+            "/kcsapi/api_req_combined_battle/airbattle",\r
+            "/kcsapi/api_req_combined_battle/midnight_battle",\r
+            "/kcsapi/api_req_combined_battle/battleresult",\r
+            "/kcsapi/api_req_sortie/airbattle",\r
+            "/kcsapi/api_req_combined_battle/battle_water",\r
+            "/kcsapi/api_req_combined_battle/sp_midnight"\r
+        };\r
+\r
+        public void Send(string url, string request, string response)\r
+        {\r
+            if (!_urlSet.Contains(url))\r
+                return;\r
+            request = Regex.Replace(request, @"&api%5Ftoken=[^&]+|api%5Ftoken=[^&]+&?", "");\r
+            response = response.Replace("svdata=", "");\r
+            _queue.TryAdd(new Tuple<string, string, string>(url, request, response));\r
+        }\r
+\r
+        public void Stop()\r
+        {\r
+            _queue.CompleteAdding();\r
+        }\r
+    }\r
+}
\ No newline at end of file
index 5d12b8c..1a2625f 100644 (file)
@@ -84,6 +84,7 @@
     <Compile Include="ItemTreeView.cs">\r
       <SubType>Component</SubType>\r
     </Compile>\r
+    <Compile Include="KancolleDb.cs" />\r
     <Compile Include="Logger.cs" />\r
     <Compile Include="LogServer.cs" />\r
     <Compile Include="MaterialInfo.cs" />\r
index 5498b1d..59c700d 100644 (file)
@@ -60,6 +60,7 @@ namespace KancolleSniffer
         private readonly ErrorDialog _errorDialog = new ErrorDialog();\r
         private bool _missionFinishTimeMode;\r
         private bool _ndockFinishTimeMode;\r
+        private readonly KancolleDb _kancolleDb = new KancolleDb();\r
 \r
         public MainForm()\r
         {\r
@@ -107,6 +108,8 @@ namespace KancolleSniffer
                 ShowServerError(url, request, response);\r
                 return;\r
             }\r
+            if (_config.KancolleDb.On)\r
+                _kancolleDb.Send(url, request, response);\r
             response = UnescapeString(response.Remove(0, "svdata=".Length));\r
             WriteDebugLog(url, request, response);\r
             try\r
@@ -206,6 +209,8 @@ namespace KancolleSniffer
             _sniffer.LoadState();\r
             ApplyProxySetting();\r
             SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;\r
+            if (_config.KancolleDb.On)\r
+                _kancolleDb.Start(_config.KancolleDb.Token);\r
         }\r
 \r
         private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)\r
@@ -254,6 +259,7 @@ namespace KancolleSniffer
             if (_config.Proxy.Auto)\r
                 _systemProxy.RestoreSettings();\r
             SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;\r
+            _kancolleDb.Stop();\r
         }\r
 \r
         private void ShutdownProxy()\r
@@ -308,6 +314,8 @@ namespace KancolleSniffer
             _sniffer.Achievement.ResetHours = _config.ResetHours;\r
             labelAkashiRepair.Visible = labelAkashiRepairTimer.Visible =\r
                 labelPresetAkashiTimer.Visible = _config.UsePresetAkashi;\r
+            if (_config.KancolleDb.On)\r
+                _kancolleDb.Start(_config.KancolleDb.Token);\r
         }\r
 \r
         public void ApplyDebugLogSetting()\r