Skip to content

Commit

Permalink
Ship Change Implementation & Ship Construction Fix
Browse files Browse the repository at this point in the history
* Ship change use packets that doesn't have any info in SvData but instead has it in the request body.
* Ship construction has been broken up between different data sets.
* We now have real time ship swapping in KCV (swaps, adds to, and removes ships from fleets)
* Also now has fixed construction menus.
* This does not update instant build/repair numbers.
  • Loading branch information
Zharay committed Apr 28, 2014
1 parent 6e83f72 commit 4b17195
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 12 deletions.
28 changes: 27 additions & 1 deletion Grabacr07.KanColleWrapper/Dockyard.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
Expand All @@ -16,6 +17,8 @@ namespace Grabacr07.KanColleWrapper
/// </summary>
public class Dockyard : NotificationObject
{
private readonly Homeport homeport;

#region Dock 変更通知プロパティ

private MemberTable<BuildingDock> _Docks;
Expand All @@ -35,13 +38,27 @@ public MemberTable<BuildingDock> Docks

#endregion

internal Dockyard(KanColleProxy proxy)
internal Dockyard(Homeport parent, KanColleProxy proxy)
{
this.homeport = parent;
this.Docks = new MemberTable<BuildingDock>();

proxy.ApiSessionSource.Where(x => x.PathAndQuery == "/kcsapi/api_get_member/kdock")
.TryParse<kcsapi_kdock[]>()
.Subscribe(this.Update);

proxy.ApiSessionSource.Where(x => x.PathAndQuery == "/kcsapi/api_req_kousyou/getship")
.TryParse<kcsapi_getship>()
.Subscribe(x =>
{
this.Update(x.api_kdock);
this.homeport.AddShip(new Ship(this.homeport, x.api_ship));
});

proxy.ApiSessionSource.Where(x => x.PathAndQuery == "/kcsapi/api_req_kousyou/createship_speedchange")
.Select(x => { SvData<kcsapi_createship_speedchange> result; return SvData.TryParse(x, out result) ? result : null; })
.Where(x => x != null && x.IsSuccess)
.Subscribe(x => this.ChangeSpeed(x.RequestBody));
}

private void Update(kcsapi_kdock[] source)
Expand All @@ -60,5 +77,14 @@ private void Update(kcsapi_kdock[] source)
this.Docks = new MemberTable<BuildingDock>(source.Select(x => new BuildingDock(x)));
}
}

private void ChangeSpeed(NameValueCollection RawRequest)
{
int api_kdock_id = Int32.Parse(RawRequest["api_kdock_id"]);
int api_highspeed = Int32.Parse(RawRequest["api_highspeed"]);

if (api_highspeed > 0 && this.Docks[api_kdock_id] != null)
this.Docks[api_kdock_id].SetComplete();
}
}
}
96 changes: 93 additions & 3 deletions Grabacr07.KanColleWrapper/Homeport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
Expand Down Expand Up @@ -179,7 +181,7 @@ internal Homeport(KanColleProxy proxy)
this.Fleets = new MemberTable<Fleet>();
this.SlotItems = new MemberTable<SlotItem>();
this.UseItems = new MemberTable<UseItem>();
this.Dockyard = new Dockyard(proxy);
this.Dockyard = new Dockyard(this, proxy);
this.Repairyard = new Repairyard(this, proxy);
this.Logger = new Logger(proxy);
this.Quests = new Quests(proxy);
Expand Down Expand Up @@ -221,6 +223,7 @@ internal Homeport(KanColleProxy proxy)
this.UpdateShips(x.Data);
this.UpdateFleets(x.Fleets);
});

proxy.ApiSessionSource.Where(x => x.PathAndQuery == "/kcsapi/api_get_member/ship3")
.TryParse<kcsapi_ship3>()
.Subscribe(x =>
Expand All @@ -244,6 +247,12 @@ internal Homeport(KanColleProxy proxy)
proxy.ApiSessionSource.Where(x => x.PathAndQuery == "/kcsapi/api_get_member/deck_port")
.TryParse<kcsapi_deck[]>()
.Subscribe(this.UpdateFleets);

proxy.ApiSessionSource.Where(x => x.PathAndQuery == "/kcsapi/api_req_hensei/change")
.Select(x => { SvData<kcsapi_change> result; return SvData.TryParse(x, out result) ? result : null; })
.Where(x => x != null && x.IsSuccess)
.Subscribe(x => this.ChangeShips(x.RequestBody));

this.Rankings = new Rankings(proxy);
this.Logger = new Logger(proxy);
this.Translations = new Translations();
Expand Down Expand Up @@ -286,7 +295,7 @@ private void UpdateShips(kcsapi_ship2[] source)
private void Charge(kcsapi_charge charge)
{
if (charge == null) return;

foreach (var ship in charge.api_ship)
{
var target = this.Ships[ship.api_id];
Expand All @@ -297,5 +306,86 @@ private void Charge(kcsapi_charge charge)

foreach (var f in Fleets.Values) f.UpdateShips();
}

private void ChangeShips(NameValueCollection RawRequest)
{

int api_id = Int32.Parse(RawRequest["api_id"]); // Fleet ID
int api_ship_idx = Int32.Parse(RawRequest["api_ship_idx"]); // Fleet slot index
int api_ship_id = Int32.Parse(RawRequest["api_ship_id"]); // Ship ID

Ship theShip = this.Ships[Int32.Parse(RawRequest["api_ship_id"])];
Ship shipAtIndex = null;
Fleet prevFleet = null;
int prevShipIndex = -1;

if (theShip == null)
{
if (api_ship_id < 0)
{
// Removing a ship only
List<Ship> fleetShips = this.Fleets[api_id].Ships.ToList();
fleetShips.RemoveAt(api_ship_idx);
this.Fleets[api_id].Ships = fleetShips.ToArray();
this.Fleets[api_id].UpdateShips();
}

return;
}

// Search the fleets to find whether it is already in a fleet
foreach (var fleet in this.Fleets.Values)
{
prevShipIndex = Array.IndexOf(fleet.Ships, theShip);
if (prevShipIndex >= 0)
{
prevFleet = fleet;
break;
}
}

// Figure out if there already is a ship occupying the index
if (this.Fleets[api_id].Ships.Length - 1 >= api_ship_idx)
{
shipAtIndex = this.Fleets[api_id].Ships[api_ship_idx];
}

if (shipAtIndex != null && prevFleet != null)
{
// Swap ships between fleets
prevFleet.Ships[prevShipIndex] = shipAtIndex;
this.Fleets[api_id].Ships[api_ship_idx] = theShip;
}
else
{
if (prevFleet != null)
{
// Remove it from the previous fleet
List<Ship> prevFleetShips = prevFleet.Ships.ToList();
prevFleetShips.RemoveAt(prevShipIndex);
prevFleet.Ships = prevFleetShips.ToArray();
}

// Plainly add it to fleet.
if (api_ship_idx > this.Fleets[api_id].Ships.Length - 1)
{
List<Ship> fleetShips = this.Fleets[api_id].Ships.ToList();
fleetShips.Add(theShip);
this.Fleets[api_id].Ships = fleetShips.ToArray();
}
else
{
this.Fleets[api_id].Ships[api_ship_idx] = theShip;
}
}

foreach (var f in Fleets.Values) f.UpdateShips();
}

public void AddShip(Ship ship)
{
this.Ships.Add(ship.Id, ship);
this.RaisePropertyChanged("Ships");
}
}
}
}
2 changes: 2 additions & 0 deletions Grabacr07.KanColleWrapper/KanColleWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@
<Compile Include="Models\QuestState.cs" />
<Compile Include="Models\QuestType.cs" />
<Compile Include="Models\Ranking.cs" />
<Compile Include="Models\Raw\kcsapi_change.cs" />
<Compile Include="Models\Raw\kcsapi_charge.cs" />
<Compile Include="Models\Raw\kcsapi_battleresult.cs" />
<Compile Include="Models\Raw\kcsapi_createitem.cs" />
<Compile Include="Models\Raw\kcsapi_getship.cs" />
<Compile Include="Models\Raw\kcsapi_createship.cs" />
<Compile Include="Models\Raw\kcsapi_port.cs" />
<Compile Include="Models\Raw\kcsapi_quest.cs" />
Expand Down
7 changes: 6 additions & 1 deletion Grabacr07.KanColleWrapper/MemberTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Grabacr07.KanColleWrapper
/// <typeparam name="TValue">ユーザー データの型。</typeparam>
public class MemberTable<TValue> : IReadOnlyDictionary<int, TValue> where TValue : class, IIdentifiable
{
private readonly IDictionary<int, TValue> dictionary;
private IDictionary<int, TValue> dictionary;

/// <summary>
/// テーブルから指定した ID の要素を取得します。ID が存在しない場合は null を返します。
Expand All @@ -33,6 +33,11 @@ public MemberTable(IEnumerable<TValue> source)
this.dictionary = source.ToDictionary(x => x.Id);
}

public void Add(int Key, TValue Value)
{
this.dictionary.Add(Key, Value);
}

#region IReadOnlyDictionary<TK, TV> members

public IEnumerator<KeyValuePair<int, TValue>> GetEnumerator()
Expand Down
6 changes: 6 additions & 0 deletions Grabacr07.KanColleWrapper/Models/BuildingDock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,11 @@ protected override void Tick()
this.Remaining = null;
}
}

internal void SetComplete()
{
this.State = BuildingDockState.Completed;
this.CompleteTime = null;
}
}
}
4 changes: 2 additions & 2 deletions Grabacr07.KanColleWrapper/Models/Raw/kcsapi_battleresult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class kcsapi_battleresult
public string api_quest_name { get; set; }
public int api_quest_level { get; set; }
public kcsapi_enemyinfo api_enemy_info { get; set; }
public kcsapi_getship api_get_ship { get; set; }
public kcsapi_getship2 api_get_ship { get; set; }
}

public class kcsapi_getship
public class kcsapi_getship2
{
public int api_ship_id { get; set; }
public string api_ship_name { get; set; }
Expand Down
12 changes: 12 additions & 0 deletions Grabacr07.KanColleWrapper/Models/Raw/kcsapi_change.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Grabacr07.KanColleWrapper.Models.Raw
{
// ReSharper disable InconsistentNaming
public class kcsapi_change { }
// ReSharper restore InconsistentNaming
}
10 changes: 5 additions & 5 deletions Grabacr07.KanColleWrapper/Models/Raw/kcsapi_createship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace Grabacr07.KanColleWrapper.Models.Raw
{
// ReSharper disable InconsistentNaming
public class kcsapi_createship
{
}
// ReSharper restore InconsistentNaming
// ReSharper disable InconsistentNaming
public class kcsapi_createship { }

public class kcsapi_createship_speedchange { }
// ReSharper restore InconsistentNaming
}
18 changes: 18 additions & 0 deletions Grabacr07.KanColleWrapper/Models/Raw/kcsapi_getship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Grabacr07.KanColleWrapper.Models.Raw
{
// ReSharper disable InconsistentNaming
public class kcsapi_getship
{
public int api_id { get; set; }
public kcsapi_kdock[] api_kdock { get; set; }
public kcsapi_ship2 api_ship { get; set; }
public kcsapi_slotitem api_slotitem { get; set; }
}
// ReSharper restore InconsistentNaming
}

0 comments on commit 4b17195

Please sign in to comment.