@@ -733,41 +733,53 @@ export class LocationLifecycleService {
733
733
) {
734
734
// Exclude completed quests
735
735
const activeQuestIdsInProfile = profileQuests
736
- . filter ( ( quest ) => quest . status !== QuestStatus . Success )
736
+ . filter ( ( quest ) => ! [ QuestStatus . Success , QuestStatus . AvailableForStart ] . includes ( quest . status ) )
737
737
. map ( ( status ) => status . qid ) ;
738
738
739
739
// Get db details of quests we found above
740
- const questDb = Object . values ( this . databaseService . getQuests ( ) ) . filter ( ( x ) =>
741
- activeQuestIdsInProfile . includes ( x . _id ) ,
740
+ const questDb = Object . values ( this . databaseService . getQuests ( ) ) . filter ( ( quest ) =>
741
+ activeQuestIdsInProfile . includes ( quest . _id ) ,
742
742
) ;
743
743
744
744
for ( const lostItem of lostQuestItems ) {
745
- for ( const quest of questDb ) {
746
- // Find a quest in the db that has the lost item in one of its conditions that is also a 'find' type of condition
745
+ let matchingConditionId : string ;
746
+ // Find a quest that has a FindItem condition that has the list items tpl as a target
747
+ const matchingQuests = questDb . filter ( ( quest ) => {
747
748
const matchingCondition = quest . conditions . AvailableForFinish . find (
748
749
( questCondition ) =>
749
750
questCondition . conditionType === "FindItem" && questCondition . target . includes ( lostItem . _tpl ) ,
750
751
) ;
751
752
if ( ! matchingCondition ) {
752
753
// Quest doesnt have a matching condition
753
- continue ;
754
+ return false ;
754
755
}
755
756
756
- // We have a match, remove the condition id from profile to reset progress and let player pick item up again
757
- const profileQuestToUpdate = profileQuests . find ( ( questStatus ) => questStatus . qid === quest . _id ) ;
758
- if ( ! profileQuestToUpdate ) {
759
- // Profile doesnt have a matching quest
760
- continue ;
761
- }
757
+ // We found a condition, save id for later
758
+ matchingConditionId = matchingCondition . id ;
759
+ return true ;
760
+ } ) ;
762
761
763
- // Filter out the matching condition we found
764
- profileQuestToUpdate . completedConditions = profileQuestToUpdate . completedConditions . filter (
765
- ( conditionId ) => conditionId !== matchingCondition . id ,
762
+ // Fail if multiple were found
763
+ if ( matchingQuests . length !== 1 ) {
764
+ this . logger . error (
765
+ `Unable to fix quest item: ${ lostItem } , ${ matchingQuests . length } matching quests found, expected 1` ,
766
766
) ;
767
767
768
- // We found and updated the relevant quest, no more work to do with this lost item
769
- break ;
768
+ continue ;
770
769
}
770
+
771
+ const matchingQuest = matchingQuests [ 0 ] ;
772
+ // We have a match, remove the condition id from profile to reset progress and let player pick item up again
773
+ const profileQuestToUpdate = profileQuests . find ( ( questStatus ) => questStatus . qid === matchingQuest . _id ) ;
774
+ if ( ! profileQuestToUpdate ) {
775
+ // Profile doesnt have a matching quest
776
+ continue ;
777
+ }
778
+
779
+ // Filter out the matching condition we found
780
+ profileQuestToUpdate . completedConditions = profileQuestToUpdate . completedConditions . filter (
781
+ ( conditionId ) => conditionId !== matchingConditionId ,
782
+ ) ;
771
783
}
772
784
}
773
785
0 commit comments