Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Kancolle Server Handling #515

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 3 additions & 107 deletions ElectronicObserver/Data/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ElectronicObserver.Window.Dialog.QuestTrackerManager.Enums;
using System.Linq;
using ElectronicObserver.Window.Dialog.QuestTrackerManager.Enums;
using ElectronicObserverTypes;

namespace ElectronicObserver.Data;
Expand Down Expand Up @@ -993,112 +994,7 @@ public static string GetExpeditionResult(int value)
#endregion

#region Servers
public class KCServer(int num, string name, string jp, string ip)
{
public int Num { get; set; } = num;
public string Name { get; set; } = name;
public string Jp { get; set; } = jp;
public string Ip { get; set; } = ip;
}

public static KCServer getKCServer(int num)
{
switch (num)
{
case 1:
return new KCServer(num, "Yokosuka Naval District", "横須賀鎮守府", "203.104.209.71");
case 2:
return new KCServer(num, "Kure Naval District", "呉鎮守府", "203.104.209.87");
case 3:
return new KCServer(num, "Sasebo Naval District", "佐世保鎮守府", "125.6.184.215");
case 4:
return new KCServer(num, "Maizuru Naval District", "舞鶴鎮守府", "203.104.209.183");
case 5:
return new KCServer(num, "Ominato Guard District", "大湊警備府", "203.104.209.150");
case 6:
return new KCServer(num, "Truk Anchorage", "トラック泊地", "203.104.209.134");
case 7:
return new KCServer(num, "Lingga Anchorage", "リンガ泊地", "203.104.209.167");
case 8:
return new KCServer(num, "Rabaul Naval Base", "ラバウル基地", "203.104.209.199");
case 9:
return new KCServer(num, "Shortland Anchorage", "ショートランド泊地", "125.6.189.7");
case 10:
return new KCServer(num, "Buin Naval Base", "ブイン基地", "125.6.189.39");
case 11:
return new KCServer(num, "Tawi-Tawi Anchorage", "タウイタウイ泊地", "125.6.189.71");
case 12:
return new KCServer(num, "Palau Anchorage", "パラオ泊地", "125.6.189.103");
case 13:
return new KCServer(num, "Brunei Anchorage", "ブルネイ泊地", "125.6.189.135");
case 14:
return new KCServer(num, "Hitokappu Bay Anchorage", "単冠湾泊地", "125.6.189.167");
case 15:
return new KCServer(num, "Paramushir Anchorage", "幌筵泊地", "125.6.189.215");
case 16:
return new KCServer(num, "Sukumo Bay Anchorage", "宿毛湾泊地", "125.6.189.247");
case 17:
return new KCServer(num, "Kanoya Airfield", "鹿屋基地", "203.104.209.23");
case 18:
return new KCServer(num, "Iwagawa Airfield", "岩川基地", "203.104.209.39");
case 19:
return new KCServer(num, "Saiki Bay Anchorage", "佐伯湾泊地", "203.104.209.55");
case 20:
return new KCServer(num, "Hashirajima Anchorage", "柱島泊地", "203.104.209.102");
default:
return new KCServer(0, "", "", "");
}
}

public static KCServer getKCServer(string ip)
{
switch (ip)
{
case "203.104.209.71":
return getKCServer(1);
case "203.104.209.87":
return getKCServer(2);
case "125.6.184.215":
return getKCServer(3);
case "203.104.209.183":
return getKCServer(4);
case "203.104.209.150":
return getKCServer(5);
case "203.104.209.134":
return getKCServer(6);
case "203.104.209.167":
return getKCServer(7);
case "203.104.209.199":
return getKCServer(8);
case "125.6.189.7":
return getKCServer(9);
case "125.6.189.39":
return getKCServer(10);
case "125.6.189.71":
return getKCServer(11);
case "125.6.189.103":
return getKCServer(12);
case "125.6.189.135":
return getKCServer(13);
case "125.6.189.167":
return getKCServer(14);
case "125.6.189.215":
return getKCServer(15);
case "125.6.189.247":
return getKCServer(16);
case "203.104.209.23":
return getKCServer(17);
case "203.104.209.39":
return getKCServer(18);
case "203.104.209.55":
return getKCServer(19);
case "203.104.209.102":
return getKCServer(20);
default:
return new KCServer(0, "", "", "");
}
}


public static string getRank(int num)
{
switch (num)
Expand Down
2 changes: 1 addition & 1 deletion ElectronicObserver/Data/DiscordRPC/DiscordRpcModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class DiscordRpcModel
{
public string TopDisplayText { get; set; } = "";

public List<string> BottomDisplayText { get; set; } = new List<string>();
public List<string> BottomDisplayText { get; set; } = [];

public string LargeImageHoverText { get; set; } = "";

Expand Down
1 change: 0 additions & 1 deletion ElectronicObserver/Data/DiscordRPC/EoToDiscordRpcClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using DiscordRPC;
using ElectronicObserver.Observer;

namespace ElectronicObserver.Data.DiscordRPC;

Expand Down
6 changes: 2 additions & 4 deletions ElectronicObserver/Data/KCDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ElectronicObserver.Data.Quest;
using ElectronicObserver.Data.Translation;
using ElectronicObserver.Data.TsunDbSubmission;
using ElectronicObserver.Services;
using ElectronicObserver.Window.Dialog.QuestTrackerManager;
using ElectronicObserverTypes;
using ElectronicObserverTypes.Data;
Expand Down Expand Up @@ -162,10 +163,7 @@ public sealed class KCDatabase : IKCDatabase
public DataAndTranslationManager Translation { get; private set; }
public PoiDbSubmissionService PoiDbSubmission { get; private set; }

/// <summary>
/// Current server
/// </summary>
public KCServer Server { get; set; }
public ServerManager ServerManager { get; } = new();

/// <summary>
/// 艦隊編成プリセットデータ
Expand Down
9 changes: 9 additions & 0 deletions ElectronicObserver/Data/KCServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ElectronicObserver.Data;

public record KCServer
{
public required int Num { get; set; }
public required string Name { get; set; }
public required string Jp { get; set; }
public required string Ip { get; set; }
}
23 changes: 12 additions & 11 deletions ElectronicObserver/Observer/APIObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -854,20 +854,16 @@ private async Task ProxyOnBeforeResponse(object sender, SessionEventArgs e)
await ApiProcessingChannel.Writer.WriteAsync(() => LoadRequest(url, body));
}

//debug
//Utility.Logger.Add( 1, baseurl );

if (baseurl == ("/gadgets/makeRequest"))
if (KCDatabase.Instance.ServerManager.CurrentServer is null && baseurl.Contains("/gadget_html5/js/kcs_const.js"))
{
KCDatabase db = KCDatabase.Instance;
if (db.Server is null)
string body = await e.GetResponseBodyAsString();

_ = Task.Run(() =>
{
string body = await e.GetResponseBodyAsString();
string url = body.Split('/')[2];
url = url.Split('\\')[0];
KCDatabase.Instance.ServerManager.LoadServerList(body);
});

db.Server = Constants.getKCServer(url);
}
return;
}

//response
Expand Down Expand Up @@ -979,6 +975,11 @@ private async Task ProxyOnBeforeResponse(object sender, SessionEventArgs e)
if (ServerAddress == null && baseurl.Contains("/kcsapi/"))
{
ServerAddress = e.HttpClient.Request.Host;

if (!string.IsNullOrEmpty(ServerAddress))
{
KCDatabase.Instance.ServerManager.LoadCurrentServer(ServerAddress);
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions ElectronicObserver/Observer/kcsapi/api_port/port.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace ElectronicObserver.Observer.kcsapi.api_port;

public class port : APIBase
{


public override void OnResponseReceived(dynamic data)
{

Expand Down Expand Up @@ -75,9 +73,9 @@ public override void OnResponseReceived(dynamic data)
dataForWS.ImageKey = Utility.Configuration.Config.Control.UseFlagshipIconForRPC ? db.Fleet[1].MembersInstance[0].ShipID.ToString() : "kc_logo_512x512";
dataForWS.CurrentShipId = db.Fleet[1].MembersInstance[0].ShipID;

if (db.Admiral.Senka != null && db.Server?.Name != null)
if (db.Admiral.Senka != null && db.ServerManager.CurrentServer is not null)
{
dataForWS.BottomDisplayText.Add(string.Format(ObserverRes.ServerRank, db.Admiral.Senka, db.Server.Name));
dataForWS.BottomDisplayText.Add(string.Format(ObserverRes.ServerRank, db.Admiral.Senka, db.ServerManager.CurrentServer.Name));
}

if (!string.IsNullOrEmpty(dataForWS.MapInfo))
Expand Down
113 changes: 113 additions & 0 deletions ElectronicObserver/Services/ServerManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ElectronicObserver.Data;

namespace ElectronicObserver.Services;

public partial class ServerManager
{
public List<KCServer> Servers { get; set; } = [];

public KCServer? CurrentServer { get; private set; }

[GeneratedRegex("ConstServerInfo.World_([0-9]+)[ ]+ = \"(.+)\"", RegexOptions.Multiline)]
private static partial Regex ServerRegex();

private string? ServerUrl { get; set; }

public void LoadServerList(string constBody)
{
Servers.Clear();

MatchCollection matches = ServerRegex().Matches(constBody);

foreach (GroupCollection groups in matches.Where(m => m.Success).Select(m => m.Groups))
{
if (int.TryParse(groups[1].Value, out int num))
{
Servers.Add(new()
{
Num = num,
Ip = groups[2].Value,
Jp = GetJapaneseName(num),
Name = GetEnglishName(num),
});
}
}

if (!string.IsNullOrEmpty(ServerUrl))
{
LoadCurrentServer(ServerUrl);
ServerUrl = null;
}
}

public void LoadCurrentServer(string serverUrl)
{
if (Servers.Count is 0)
{
ServerUrl = serverUrl;
}
else
{
CurrentServer = Servers.FirstOrDefault(server => server.Ip.Contains(serverUrl)) ?? new()
{
Ip = serverUrl,
Jp = "",
Name = "",
Num = 0,
};
}
}

private string GetJapaneseName(int num) => num switch
{
1 => "横須賀鎮守府",
2 => "呉鎮守府",
3 => "佐世保鎮守府",
4 => "舞鶴鎮守府",
5 => "大湊警備府",
6 => "トラック泊地",
7 => "リンガ泊地",
8 => "ラバウル基地",
9 => "ショートランド泊地",
10 => "ブイン基地",
11 => "タウイタウイ泊地",
12 => "パラオ泊地",
13 => "ブルネイ泊地",
14 => "単冠湾泊地",
15 => "幌筵泊地",
16 => "宿毛湾泊地",
17 => "鹿屋基地",
18 => "岩川基地",
19 => "佐伯湾泊地",
20 => "柱島泊地",
_ => "",
};

private string GetEnglishName(int num) => num switch
{
1 => "Yokosuka Naval District",
2 => "Kure Naval District",
3 => "Sasebo Naval District",
4 => "Maizuru Naval District",
5 => "Ominato Guard District",
6 => "Truk Anchorage",
7 => "Lingga Anchorage",
8 => "Rabaul Naval Base",
9 => "Shortland Anchorage",
10 => "Buin Naval Base",
11 => "Tawi-Tawi Anchorage",
12 => "Palau Anchorage",
13 => "Brunei Anchorage",
14 => "Hitokappu Bay Anchorage",
15 => "Paramushir Anchorage",
16 => "Sukumo Bay Anchorage",
17 => "Kanoya Airfield",
18 => "Iwagawa Airfield",
19 => "Saiki Bay Anchorage",
20 => "Hashirajima Anchorage",
_ => "",
};
}