Skip to content

Commit

Permalink
* リモート設定URL更新
Browse files Browse the repository at this point in the history
* リモート取得エラー結果をキャッシュするよう変更
* ゲージ番号を空にできるよう変更
  • Loading branch information
veigr committed Sep 19, 2018
1 parent 20fa718 commit 8a59d45
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 70 deletions.
1 change: 1 addition & 0 deletions EventMapHpViewer/EventMapHpViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="KanColleViewer.Composition, Version=1.4.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
12 changes: 6 additions & 6 deletions EventMapHpViewer/Models/MapData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace EventMapHpViewer.Models
{
public class MapData
{
private readonly RemoteSettingsClient client = new RemoteSettingsClient();
public int Id { get; set; }
public int IsCleared { get; set; }
public int IsExBoss { get; set; }
Expand Down Expand Up @@ -76,31 +77,30 @@ public async Task<RemainingCount> GetRemainingCount()
if (MapHpSettings.UseLocalBossSettings)
{
var settings = BossSettingsWrapper.FromSettings.List
.Where(x => x.Id == this.Id)
.Where(x => x.MapId == this.Id)
.Where(x => x.Rank == (int)this.Eventmap.SelectedRank)
.Where(x => x.GaugeNum == (this.Eventmap.GaugeNum ?? 1))
.Where(x => x.GaugeNum == this.Eventmap.GaugeNum)
.ToArray();
if (settings.Any())
return this.CalculateRemainingCount(settings);
}
else
{
var client = new RemoteSettingsClient();
var remoteBossData = await client.GetSettings<Raw.map_exboss[]>(
RemoteSettingsClient.BuildBossSettingsUrl(
MapHpSettings.RemoteBossSettingsUrl,
this.Id,
(int)this.Eventmap.SelectedRank,
this.Eventmap.GaugeNum ?? 1));
this.Eventmap.GaugeNum ?? 1)); // GaugeNum がない場合どうしよう?とりあえず1
client.CloseConnection();

if (remoteBossData == null)
return null;

if (!remoteBossData.Any(x => x.isLast))
if (!remoteBossData.Any(x => x.last))
return null;

if(!remoteBossData.Any(x => !x.isLast))
if(!remoteBossData.Any(x => !x.last))
return null;

return this.CalculateRemainingCount(BossSettingsWrapper.Parse(remoteBossData)); //イベント海域(リモートデータ)
Expand Down
23 changes: 5 additions & 18 deletions EventMapHpViewer/Models/Raw/map_exboss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,10 @@ namespace EventMapHpViewer.Models.Raw
{
class map_exboss
{
public bool isLast { get; set; }
public Ship ship { get; set; }

public class Ship
{
//public string shipName { get; set; }
//public int shipId { get; set; }
//public int shipLv { get; set; }
public int maxhp { get; set; }
//public Slot[] slot { get; set; }
//public int[] param { get; set; }
}

//public class Slot
//{
// public int slotitemId { get; set; }
// public string slotitemName { get; set; }
//}
public int mapid { get; set; }
public int rank { get; set; }
public int? gauge { get; set; }
public int maxhp { get; set; }
public bool last { get; set; }
}
}
11 changes: 8 additions & 3 deletions EventMapHpViewer/Models/Settings/AutoCalcTpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ public AutoCalcTpSettings()

private AutoCalcTpSettings(string stypeTp, string slotitemTp, string shipTp)
{
this.ShipTypeTp = !string.IsNullOrEmpty(stypeTp) ? DynamicJson.Parse(stypeTp) : Default.ShipTypeTp;
this.SlotItemTp = !string.IsNullOrEmpty(slotitemTp) ? DynamicJson.Parse(slotitemTp) : Default.SlotItemTp;
this.ShipTp = !string.IsNullOrEmpty(shipTp) ? DynamicJson.Parse(shipTp) : Default.ShipTp;
try { this.ShipTypeTp = DynamicJson.Parse(stypeTp); }
catch { this.ShipTypeTp = Default.ShipTypeTp; }

try { this.SlotItemTp = DynamicJson.Parse(slotitemTp); }
catch { this.SlotItemTp = Default.SlotItemTp; }

try { this.ShipTp = DynamicJson.Parse(shipTp); }
catch { this.ShipTp = Default.ShipTp; }
}

private AutoCalcTpSettings(IEnumerable<TpSetting> stypeTp, IEnumerable<TpSetting> slotitemTp, IEnumerable<TpSetting> shipTp)
Expand Down
41 changes: 34 additions & 7 deletions EventMapHpViewer/Models/Settings/BossSettingsWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@ private set

public BossSettingsWrapper(string json = "")
{
this.List = !string.IsNullOrEmpty(json)
? DynamicJson.Parse(json)
: new ObservableSynchronizedCollection<BossSetting>();
try
{
BossSettingForParse[] parsed = DynamicJson.Parse(json);
this.List = new ObservableSynchronizedCollection<BossSetting>(parsed.Select(x => x.ToValue()));
}
catch
{
this.List = new ObservableSynchronizedCollection<BossSetting>();
}
}

public static IEnumerable<BossSetting> Parse(IEnumerable<Raw.map_exboss> source)
{
return source.Select(x => new BossSetting
{
BossHP = x.ship.maxhp,
IsLast = x.isLast,
MapId = x.mapid,
Rank = x.rank,
GaugeNum = x.gauge,
BossHP = x.maxhp,
IsLast = x.last,
});
}

Expand All @@ -49,13 +58,31 @@ public static BossSettingsWrapper FromSettings

public class BossSetting
{
public int Id { get; set; }
public int MapId { get; set; }
public int Rank { get; set; }
public int GaugeNum { get; set; }
public int? GaugeNum { get; set; }
public int BossHP { get; set; }
public bool IsLast { get; set; }
}

public class BossSettingForParse
{
public int MapId { get; set; }
public int Rank { get; set; }
public string GaugeNum { get; set; } // DynamicJson は int? に Parse できない
public int BossHP { get; set; }
public bool IsLast { get; set; }
public BossSetting ToValue()
=> new BossSetting
{
MapId = this.MapId,
Rank = this.Rank,
GaugeNum = int.TryParse(this.GaugeNum, out var num) ? num : (int?)null,
BossHP = this.BossHP,
IsLast = this.IsLast,
};
}

static class BossSettingsWrapperExtensions
{
public static void Save(this BossSettingsWrapper settings)
Expand Down
2 changes: 1 addition & 1 deletion EventMapHpViewer/Models/Settings/MapHpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static class MapHpSettings

public static SerializableProperty<string> RemoteBossSettingsUrl { get; }
= new SerializableProperty<string>(GetKey(), roamingProvider,
"https://kctadil-admin.azurewebsites.net/admin/maphp/{version}/{id}/{rank}/{gaugeNum}"
"https://kctadilstorage.blob.core.windows.net/viewer/maphp/{mapId}/{rank}/{gaugeNum}.json"
) { AutoSave = true };

#endregion
Expand Down
39 changes: 31 additions & 8 deletions EventMapHpViewer/Models/Settings/RemoteSettingsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -19,14 +20,18 @@ class RemoteSettingsClient
{
private HttpClient client;

private ConcurrentDictionary<string, DateTimeOffset> lastModified;
private readonly ConcurrentDictionary<string, DateTimeOffset> lastModified;

private ConcurrentDictionary<string, object> caches;
private readonly ConcurrentDictionary<string, object> caches;

private TimeSpan cacheTtl;

private bool updating;

private static readonly object errorObject = new object();

public bool IsCacheError { get; set; } = true;

#if DEBUG
public RemoteSettingsClient() : this(TimeSpan.FromSeconds(10)) { }
#else
Expand Down Expand Up @@ -62,11 +67,12 @@ public async Task<T> GetSettings<T>(string url)

if (DateTimeOffset.Now - lm < this.cacheTtl)
{
object value;
if (this.caches.TryGetValue(url, out value)
&& value is T)
if (this.caches.TryGetValue(url, out var value))
{
return (T)value;
if (value is T)
return (T)value;
else if (value == errorObject)
return null;
}
}
this.updating = true;
Expand All @@ -85,6 +91,7 @@ public async Task<T> GetSettings<T>(string url)
if (!response.IsSuccessStatusCode)
{
// 200 じゃなかった
this.CacheError(url, lm);
return null;
}

Expand All @@ -97,6 +104,13 @@ public async Task<T> GetSettings<T>(string url)
catch (HttpRequestException)
{
// HTTP リクエストに失敗した
this.CacheError(url, lm);
return null;
}
catch
{
// 不正な JSON 等
this.CacheError(url, lm);
return null;
}
finally
Expand All @@ -105,6 +119,14 @@ public async Task<T> GetSettings<T>(string url)
}
}

private void CacheError(string url, DateTimeOffset lastModified)
{
if (!this.IsCacheError)
return;
this.lastModified.TryUpdate(url, DateTimeOffset.Now, lastModified);
this.caches.AddOrUpdate(url, errorObject, (_, __) => errorObject);
}

public void CloseConnection()
{
this.client?.Dispose();
Expand Down Expand Up @@ -152,7 +174,7 @@ public static string BuildBossSettingsUrl(string url, int id, int rank, int gaug
return BuildUrl(url, new Dictionary<string, string>
{
{ "version", $"{MapHpViewer.version}" },
{ "id", id.ToString() },
{ "mapId", id.ToString() },
{ "rank", rank.ToString() },
{ "gaugeNum", gaugeNum.ToString() },
});
Expand All @@ -164,7 +186,8 @@ public static string BuildUrl(string url, IDictionary<string, string> placeHolde
return url;
foreach(var placeHolder in placeHolders)
{
url = url.Replace($"{{{placeHolder.Key}}}", placeHolder.Value);
var regex = new Regex($"{{{placeHolder.Key}}}", RegexOptions.IgnoreCase | RegexOptions.Singleline);
url = regex.Replace(url, placeHolder.Value);
}
return url;
}
Expand Down
36 changes: 18 additions & 18 deletions EventMapHpViewer/ViewModels/Settings/BossSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public bool UseLocalBossSettings
}
#endregion

#region Id
private int _Id;
public int Id
#region MapId
private int _MapId;
public int MapId
{
get => this._Id;
get => this._MapId;
set
{
if (value == this._Id)
if (value == this._MapId)
return;
this._Id = value;
this._MapId = value;
this.RaisePropertyChanged();
}
}
Expand Down Expand Up @@ -100,8 +100,8 @@ public int Rank
#endregion

#region GaugeNum
private int _GaugeNum;
public int GaugeNum
private string _GaugeNum;
public string GaugeNum
{
get => this._GaugeNum;
set
Expand Down Expand Up @@ -148,15 +148,15 @@ public bool IsLast
public bool IsAddEnabled
{
get => !this.Settings.List.Any(
x => x.Id == this.Id
x => x.MapId == this.MapId
&& x.Rank == this.Rank
&& x.GaugeNum == this.GaugeNum
&& x.GaugeNum.ToString() == this.GaugeNum
&& x.BossHP == this.BossHP
&& x.IsLast == this.IsLast
)
&& this.Id != default
&& this.MapId != default
&& this.Rank != default
&& this.GaugeNum != default
// && this.GaugeNum != default // 空もありとする
&& this.BossHP != default;
}
#endregion
Expand All @@ -178,15 +178,15 @@ public BossSetting SelectedBossSetting
this._SelectedBossSetting = value;
if(value != null)
{
this.Id = value.Id;
this.MapId = value.MapId;
this.Rank = value.Rank;
this.GaugeNum = value.GaugeNum;
this.GaugeNum = value.GaugeNum.ToString();
this.BossHP = value.BossHP;
this.IsLast = value.IsLast;
}
else
{
this.Id = default;
this.MapId = default;
this.Rank = default;
this.GaugeNum = default;
this.BossHP = default;
Expand Down Expand Up @@ -234,7 +234,7 @@ public BossSettingsViewModel()
this.Settings = BossSettingsWrapper.FromSettings;

this.BossSettings = this.Settings.List
.ToSyncedSortedObservableCollection(x => $"{x.Id:D4}{x.Rank:D2}{x.GaugeNum:D2}{(x.IsLast ? 1 : 0)}{x.BossHP:D4}")
.ToSyncedSortedObservableCollection(x => $"{x.MapId:D4}{x.Rank:D2}{x.GaugeNum ?? 0:D2}{(x.IsLast ? 1 : 0)}{x.BossHP:D4}")
.ToSyncedSynchronizationContextCollection(SynchronizationContext.Current)
.ToSyncedReadOnlyNotifyChangedCollection();
}
Expand All @@ -243,9 +243,9 @@ public void Add()
{
var newValue = new BossSetting
{
Id = this.Id,
MapId = this.MapId,
Rank = this.Rank,
GaugeNum = this.GaugeNum,
GaugeNum = int.TryParse(this.GaugeNum, out var num) ? num : (int?)null,
BossHP = this.BossHP,
IsLast = this.IsLast
};
Expand Down
1 change: 0 additions & 1 deletion EventMapHpViewer/ViewModels/ToolViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public ToolViewModel(MapInfoProxy proxy)

public void Initialize()
{
Debug.WriteLine("ToolViewModel: Initialize()");
KanColleClient.Current.Homeport.Organization
.Subscribe(nameof(Organization.Fleets), this.UpdateFleets, false)
.Subscribe(nameof(Organization.Combined), this.UpdateTransportCapacity, false)
Expand Down
Loading

0 comments on commit 8a59d45

Please sign in to comment.