Skip to content

Commit

Permalink
feat(network): add treasure result (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyupe committed Aug 29, 2021
1 parent d0928b1 commit 4e7dd37
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Cafe.Matcha/Cafe.Matcha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
<Compile Include="Constant\RequestType.cs" />
<Compile Include="Constant\Secret.cs" />
<Compile Include="Constant\Secret.Local.cs" />
<Compile Include="Constant\TreasureShiftingWheelResultType.cs" />
<Compile Include="DTO\TreasureResultDTO.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Network\State.cs" />
<Compile Include="Network\Universalis\Structures\UniversalisQueryResponse.cs" />
Expand Down
1 change: 1 addition & 0 deletions Cafe.Matcha/Constant/EventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum EventType
MiniCactpot,
Gearset,
TreasureSpot,
TreasureResult,
CompanyVoyageStatus,

DynamicEvent
Expand Down
12 changes: 12 additions & 0 deletions Cafe.Matcha/Constant/TreasureShiftingWheelResultType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Cafe.Matcha.Constant
{
internal enum TreasureShiftingWheelResultType
{
Low = 191,
Medium = 192,
High = 193,
Shift = 194,
Special = 195,
End = 196
}
}
25 changes: 25 additions & 0 deletions Cafe.Matcha/DTO/TreasureResultDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) FFCafe. All rights reserved.
// Licensed under the AGPL-3.0 license. See LICENSE file in the project root for full license information.

namespace Cafe.Matcha.DTO
{
using Cafe.Matcha.Constant;
using Newtonsoft.Json;

internal class TreasureResultDTO : BaseDTO
{
public override EventType EventType
{
get
{
return EventType.TreasureResult;
}
}

[JsonProperty("value")]
public string Value;

[JsonProperty("round")]
public int Round;
}
}
20 changes: 18 additions & 2 deletions Cafe.Matcha/Models/ConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,26 @@ public class ConfigOutput : BindingTarget

public class ConfigFormatter : BindingTarget
{
public ConfigFormatter() : this(null, null, null, null, null)
public ConfigFormatter() : this(null, null, null, null, null, null)
{
}

[JsonConstructor]
private ConfigFormatter(ConfigFormatterFate fate, ConfigFormatterFish fish, ConfigFormatterInstance instance, ConfigFormatterZone zone, ConfigFormatterCriticalEngagement criticalEngagement)
private ConfigFormatter(
ConfigFormatterFate fate,
ConfigFormatterFish fish,
ConfigFormatterInstance instance,
ConfigFormatterZone zone,
ConfigFormatterCriticalEngagement criticalEngagement,
ConfigFormatterTreasure treasure
)
{
Fate = fate ?? new ConfigFormatterFate();
Fish = fish ?? new ConfigFormatterFish();
Instance = instance ?? new ConfigFormatterInstance();
Zone = zone ?? new ConfigFormatterZone();
CriticalEngagement = criticalEngagement ?? new ConfigFormatterCriticalEngagement();
Treasure = treasure ?? new ConfigFormatterTreasure();
}

[JsonProperty("fate")]
Expand All @@ -169,6 +177,8 @@ private ConfigFormatter(ConfigFormatterFate fate, ConfigFormatterFish fish, Conf
public ConfigFormatterZone Zone { get; set; }
[JsonProperty("critical-engagement")]
public ConfigFormatterCriticalEngagement CriticalEngagement { get; set; }
[JsonProperty("treasure")]
public ConfigFormatterTreasure Treasure { get; set; }
}

public class ConfigFormatterFish : BindingTarget
Expand Down Expand Up @@ -196,6 +206,12 @@ public class ConfigFormatterCriticalEngagement : BindingTarget
public bool Name { get; set; } = true;
}

public class ConfigFormatterTreasure : BindingTarget
{
[JsonProperty("result")]
public bool Result { get; set; } = true;
}

public class ConfigFormatterInstance : BindingTarget
{
[JsonProperty("name")]
Expand Down
117 changes: 100 additions & 17 deletions Cafe.Matcha/Network/NetworkMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,86 @@ private void HandleMessage(byte[] message)
return;
}

var processed = HandleMessageByOpcode(message);
if (!processed)
{
TryHandleMessage(message);
}
}

private void TryHandleMessage(byte[] message)
{
var data = message.Skip(32).ToArray();
// Treasure Shifting Wheel Result
if (message.Length == 88)
{
var level = BitConverter.ToUInt32(data, 24);
if (
level == 7636061 || // G10 运河宝物库神殿
level == 8508181 // G12 梦羽宝殿
)
{
var result = (TreasureShiftingWheelResultType)data[40];
switch (result)
{
case TreasureShiftingWheelResultType.Low:
FireEvent(new TreasureResultDTO()
{
Value = "wheel-low"
});
break;
case TreasureShiftingWheelResultType.Medium:
FireEvent(new TreasureResultDTO()
{
Value = "wheel-medium"
});
break;
case TreasureShiftingWheelResultType.High:
FireEvent(new TreasureResultDTO()
{
Value = "wheel-high"
});
break;
case TreasureShiftingWheelResultType.Shift:
FireEvent(new TreasureResultDTO()
{
Value = "wheel-shift"
});
break;
case TreasureShiftingWheelResultType.Special:
FireEvent(new TreasureResultDTO()
{
Value = "wheel-special"
});
break;
case TreasureShiftingWheelResultType.End:
FireEvent(new TreasureResultDTO()
{
Value = "wheel-end"
});
break;
}
}
}
else if (message.Length == 96)
{
var flag = BitConverter.ToUInt32(data, 16);
if (flag == 0x04482c03)
{
FireEvent(new TreasureResultDTO()
{
Round = data[32] + 1,
Value = data[40] == 1 ? "gate-open" : "gate-fail"
});
}
}
}

private bool HandleMessageByOpcode(byte[] message)
{
if (!ToMatchaOpcode(BitConverter.ToUInt16(message, 18), out var opcode))
{
return;
return false;
}

Universalis.Client.HandlePacket(opcode, message);
Expand All @@ -75,7 +152,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 168)
{
return;
return false;
}

var category = BitConverter.ToUInt32(data, 0);
Expand All @@ -94,7 +171,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 64)
{
return;
return false;
}

var type = (ActorControlType)BitConverter.ToUInt16(data, 0);
Expand Down Expand Up @@ -170,7 +247,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 64)
{
return;
return false;
}

var roulette = BitConverter.ToUInt16(data, 2);
Expand All @@ -186,7 +263,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 176)
{
return;
return false;
}

var list = new List<CompanyVoyageStatusItem>();
Expand Down Expand Up @@ -218,7 +295,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 176)
{
return;
return false;
}

var list = new List<CompanyVoyageStatusItem>();
Expand Down Expand Up @@ -250,7 +327,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 128)
{
return;
return false;
}

State.Instance.ZoneId = BitConverter.ToUInt16(data, 2);
Expand All @@ -264,23 +341,23 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 72)
{
return;
return false;
}

var targetActorId = BitConverter.ToUInt32(message, 8);
var fishActorId = BitConverter.ToUInt32(data, 0);

if (targetActorId != fishActorId)
{
return;
return true;
}

var type = (FishEventType)BitConverter.ToUInt16(data, 12);
var biteType = BitConverter.ToUInt16(data, 28);

if (type != FishEventType.Bite)
{
return;
return true;
}

switch ((FishEventBiteType)biteType)
Expand Down Expand Up @@ -309,7 +386,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 48)
{
return;
return false;
}

var itemId = BitConverter.ToUInt32(data, 0);
Expand All @@ -327,7 +404,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 1560)
{
return;
return false;
}

var detail = new List<int>();
Expand Down Expand Up @@ -365,14 +442,14 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 96)
{
return;
return false;
}

// Filter out non-equipped items
var container = BitConverter.ToUInt16(data, 0x08);
if (container != 1000)
{
return;
return true;
}

var materias = new List<Materia>();
Expand All @@ -399,14 +476,14 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 80)
{
return;
return false;
}

// Filter out non-equipped items
var container = BitConverter.ToUInt16(data, 0x0c);
if (container != 1000)
{
return;
return true;
}

FireEvent(new GearsetDTO()
Expand All @@ -423,7 +500,7 @@ private void HandleMessage(byte[] message)
{
if (message.Length != 1016)
{
return;
return false;
}

const int offset = 0x40;
Expand Down Expand Up @@ -469,6 +546,12 @@ private void HandleMessage(byte[] message)
{
State.Instance.WorldId = BitConverter.ToUInt16(data, 4);
}
else
{
return false;
}

return true;
}

public delegate void ExceptionHandler(Exception e);
Expand Down
32 changes: 32 additions & 0 deletions Cafe.Matcha/Utils/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,36 @@ private static string GetFishText(FishBiteDTO dto)
return sb.ToString();
}

private static string GetTreasureResultText(TreasureResultDTO dto)
{
if (!FormatterConfig.Treasure.Result)
{
return null;
}

switch (dto.Value)
{
case "wheel-low":
return "下级召唤";
case "wheel-medium":
return "中级召唤";
case "wheel-high":
return "上级召唤";
case "wheel-shift":
return "召唤式变动";
case "wheel-special":
return "下级召唤";
case "wheel-end":
return "召唤失败";
case "gate-open":
return "开门";
case "gate-fail":
return "失败";
}

return null;
}

public static string GetEventText(BaseDTO dto)
{
switch (dto.EventType)
Expand Down Expand Up @@ -191,6 +221,8 @@ public static string GetEventText(BaseDTO dto)
return zoneText;
case EventType.DynamicEvent:
return GetDynamicEventText((DynamicEventDTO)dto);
case EventType.TreasureResult:
return GetTreasureResultText((TreasureResultDTO)dto);
default:
return string.Format("{0} {1}", Enum.GetName(typeof(EventType), dto.EventType), dto.ToJSON());
}
Expand Down
Loading

0 comments on commit 4e7dd37

Please sign in to comment.