Skip to content

Commit

Permalink
Guards in PawnGetPartyPawnDataHandler (?), QuestGetSetQuestListHandle…
Browse files Browse the repository at this point in the history
…r, and QuestStateManager. Better logging for ResponseErrorExceptions.
  • Loading branch information
RyanYappert committed Nov 9, 2024
1 parent 45d3dff commit 18b709b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
8 changes: 7 additions & 1 deletion Arrowgene.Ddon.GameServer/Characters/EquipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ public bool CanMeetStorageRequirements(DdonGameServer server, GameClient client,
// Check removals caused by equipped an ensemble
foreach (var changeItem in changeCharacterEquipList)
{
// This is actually a removal, so skip it.
if (changeItem.EquipItemUId.Length == 0)
{
continue;
}

var itemInfo = server.ItemManager.LookupInfoByUID(server, changeItem.EquipItemUId);
if (itemInfo.SubCategory == ItemSubCategory.EquipEnsemble)
{
Expand All @@ -354,7 +360,7 @@ public bool CanMeetStorageRequirements(DdonGameServer server, GameClient client,
}
}

return slotsRequired >= freeSlots;
return slotsRequired <= freeSlots;
}

public uint CalculateItemRank(DdonGameServer server, CharacterCommon characterCommon)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ public PawnGetPartyPawnDataHandler(DdonGameServer server) : base(server)
public override S2CPawnGetPartyPawnDataRes Handle(GameClient client, C2SPawnGetPartyPawnDataReq packet)
{
// var owner = Server.CharacterManager.SelectCharacter(packet.Structure.CharacterId);
GameClient owner = this.Server.ClientLookup.GetClientByCharacterId(packet.CharacterId);
GameClient owner = this.Server.ClientLookup.GetClientByCharacterId(packet.CharacterId)
?? throw new ResponseErrorException(ErrorCode.ERROR_CODE_CHARACTER_PARAM_NOT_FOUND);
// TODO: Move this to a function or lookup class
List<Pawn> pawns = owner.Character.Pawns.Concat(client.Character.RentedPawns).ToList();

Pawn pawn = pawns
.Where(pawn => pawn.PawnId == packet.PawnId)
.FirstOrDefault() ?? throw new ResponseErrorException(ErrorCode.ERROR_CODE_PAWN_NOT_FOUNDED);
.Find(pawn => pawn.PawnId == packet.PawnId)
?? throw new ResponseErrorException(ErrorCode.ERROR_CODE_PAWN_NOT_FOUNDED);

var res = new S2CPawnGetPartyPawnDataRes();
res.CharacterId = pawn.CharacterId;
Expand Down
38 changes: 15 additions & 23 deletions Arrowgene.Ddon.GameServer/Handler/QuestGetSetQuestListHandler.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
using Arrowgene.Buffers;
using Arrowgene.Ddon.GameServer.Characters;
using Arrowgene.Ddon.GameServer.Dump;
using Arrowgene.Ddon.GameServer.Party;
using Arrowgene.Ddon.GameServer.Quests;
using Arrowgene.Ddon.Server;
using Arrowgene.Ddon.Server.Network;
using Arrowgene.Ddon.Shared.Asset;
using Arrowgene.Ddon.Shared.Entity.PacketStructure;
using Arrowgene.Ddon.Shared.Entity.Structure;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Ddon.Shared.Model.Quest;
using Arrowgene.Ddon.Shared.Network;
using Arrowgene.Logging;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Arrowgene.Ddon.GameServer.Handler
{
public class QuestGetSetQuestListHandler : GameStructurePacketHandler<C2SQuestGetSetQuestListReq>
public class QuestGetSetQuestListHandler : GameRequestPacketHandler<C2SQuestGetSetQuestListReq, S2CQuestGetSetQuestListRes>
{
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(QuestGetQuestPartyBonusListHandler));

public QuestGetSetQuestListHandler(DdonGameServer server) : base(server)
{
}

public override void Handle(GameClient client, StructurePacket<C2SQuestGetSetQuestListReq> packet)
public override S2CQuestGetSetQuestListRes Handle(GameClient client, C2SQuestGetSetQuestListReq request)
{
// client.Send(GameFull.Dump_132);

Expand All @@ -47,14 +36,14 @@ public override void Handle(GameClient client, StructurePacket<C2SQuestGetSetQue
// Populate state for all quests currently in progress by the player
foreach (var questScheduleId in client.Party.QuestState.GetActiveQuestScheduleIds())
{
var quest = client.Party.QuestState.GetQuest(questScheduleId);
if (!QuestManager.IsWorldQuest(quest.QuestId))
Quest quest = client.Party.QuestState.GetQuest(questScheduleId);
if (quest is null || !QuestManager.IsWorldQuest(quest.QuestId))
{
continue;
}

var questStats = client.Party.Leader.Client.Character.CompletedQuests.GetValueOrDefault(quest.QuestId);
var questState = client.Party.QuestState.GetQuestState(quest);
CompletedQuest questStats = client.Party.Leader?.Client.Character.CompletedQuests.GetValueOrDefault(quest.QuestId);
QuestState questState = client.Party.QuestState.GetQuestState(quest);

res.SetQuestList.Add(new CDataSetQuestList()
{
Expand All @@ -63,20 +52,23 @@ public override void Handle(GameClient client, StructurePacket<C2SQuestGetSetQue
IsDiscovery = (questStats == null) ? quest.IsDiscoverable : true,
ClearCount = (questStats == null) ? 0 : questStats.ClearCount
},
Param = quest.ToCDataQuestList(questState.Step),
Param = quest.ToCDataQuestList(questState?.Step ?? 0),
});
}

foreach (var questScheduleId in client.Party.QuestState.AreaQuests(packet.Structure.DistributeId))
foreach (var questScheduleId in client.Party.QuestState.AreaQuests(request.DistributeId))
{
if (client.Party.QuestState.IsQuestActive(questScheduleId) || client.Party.QuestState.IsCompletedWorldQuest(questScheduleId))
Quest quest = QuestManager.GetQuestByScheduleId(questScheduleId);

if (quest is null
|| client.Party.QuestState.IsQuestActive(questScheduleId)
|| client.Party.QuestState.IsCompletedWorldQuest(questScheduleId))
{
// Skip quests already populated or completed
continue;
}

var quest = QuestManager.GetQuestByScheduleId(questScheduleId);
var questStats = client.Party.Leader.Client.Character.CompletedQuests.GetValueOrDefault(quest.QuestId);
CompletedQuest questStats = client.Party.Leader?.Client.Character.CompletedQuests.GetValueOrDefault(quest.QuestId);
res.SetQuestList.Add(new CDataSetQuestList()
{
Detail = new CDataSetQuestDetail()
Expand Down Expand Up @@ -109,7 +101,7 @@ public override void Handle(GameClient client, StructurePacket<C2SQuestGetSetQue

client.Party.SendToAll(ntc);

client.Send(res);
return res;
}
}
}
6 changes: 6 additions & 0 deletions Arrowgene.Ddon.GameServer/Quests/QuestStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ public void SetInstanceEnemies(Quest quest, StageId stageId, ushort subGroupId,
{
lock (ActiveQuests)
{
if (!ActiveQuests[quest.QuestScheduleId].QuestEnemies.ContainsKey(stageId))
{
// Why does this keep failing?
ActiveQuests[quest.QuestScheduleId].QuestEnemies[stageId] = new();
Logger.Error($"Unprepared enemy location {stageId} for schedule {quest.QuestScheduleId}.");
}
ActiveQuests[quest.QuestScheduleId].QuestEnemies[stageId][subGroupId] = enemies;
}
}
Expand Down
5 changes: 5 additions & 0 deletions Arrowgene.Ddon.Server/Network/RequestPacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Arrowgene.Ddon.Shared.Entity;
using Arrowgene.Ddon.Shared.Model;
using Arrowgene.Ddon.Shared.Network;
using Arrowgene.Logging;

namespace Arrowgene.Ddon.Server.Network
{
Expand All @@ -11,6 +12,8 @@ public abstract class RequestPacketHandler<TClient, TReqStruct, TResStruct> : St
where TReqStruct : class, IPacketStructure, new()
where TResStruct : ServerResponse, new()
{
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(RequestPacketHandler<TClient, TReqStruct, TResStruct>));

protected RequestPacketHandler(DdonServer<TClient> server) : base(server)
{
}
Expand All @@ -28,6 +31,8 @@ public sealed override void Handle(TClient client, StructurePacket<TReqStruct> r
{
response = new TResStruct();
response.Error = (uint) ex.ErrorCode;
var message = ex.Message.Length > 0 ? ("\n\tMessage: " + ex.Message) : "";
Logger.Error(client, $"{ex.ErrorCode} thrown when handling {typeof(TReqStruct)}{message}.");
client.Send(response);
}
catch (Exception)
Expand Down

0 comments on commit 18b709b

Please sign in to comment.