Skip to content

Commit

Permalink
chore: move fishing to handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyupe committed Feb 17, 2025
1 parent 18d832a commit 1bdd4a1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
66 changes: 66 additions & 0 deletions Cafe.Matcha/Network/Handler/FishingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.Network.Handler
{
using System;
using Cafe.Matcha.Constant;
using Cafe.Matcha.DTO;
using Cafe.Matcha.Utils;

internal class FishingHandler : AbstractHandler
{
public FishingHandler(Action<BaseDTO> fireEvent) : base(fireEvent)
{
}

public override bool Handle(Packet packet)
{
// Bite
if (packet.MatchaOpcode == MatchaOpcode.EventPlay)
{
if (packet.Length != 72)
{
return false;
}

var targetActorId = packet.Target;
var fishActorId = packet.ReadUInt32(0);

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

var type = (FishEventType)packet.ReadUInt16(12);
var biteType = packet.ReadUInt16(28);

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

var biteTypeParsed = ((FishEventBiteType)biteType) switch
{
FishEventBiteType.Light => 1,
FishEventBiteType.Medium => 2,
FishEventBiteType.Big => 3,
_ => 0,
};

if (biteTypeParsed != 0)
{
fireEvent(new FishBiteDTO()
{
Time = Helper.Now,
Type = 3
});
}

return true;
}

return false;
}
}
}
40 changes: 0 additions & 40 deletions Cafe.Matcha/Network/NetworkMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,46 +502,6 @@ private bool HandleMessageByOpcode(Packet packet)
Instance = contentId
});
}
else if (opcode == MatchaOpcode.EventPlay)
{
if (packet.Length != 72)
{
return false;
}

var targetActorId = packet.Target;
var fishActorId = BitConverter.ToUInt32(data, 0);

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

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

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

var biteTypeParsed = ((FishEventBiteType)biteType) switch
{
FishEventBiteType.Light => 1,
FishEventBiteType.Medium => 2,
FishEventBiteType.Big => 3,
_ => 0,
};

if (biteTypeParsed != 0)
{
FireEvent(new FishBiteDTO()
{
Time = Helper.Now,
Type = 3
});
}
}
else if (opcode == MatchaOpcode.ItemInfo)
{
if (packet.Length != 96)
Expand Down
5 changes: 5 additions & 0 deletions Cafe.Matcha/Network/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public byte[] GetRawData()
return Bytes.Skip(HeaderLength).ToArray();
}

public uint ReadUInt16(int startIndex)
{
return BitConverter.ToUInt16(Bytes, startIndex + HeaderLength);
}

public uint ReadUInt32(int startIndex)
{
return BitConverter.ToUInt32(Bytes, startIndex + HeaderLength);
Expand Down

0 comments on commit 1bdd4a1

Please sign in to comment.