GameMainQuest.java 4.26 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.quest;

Akka's avatar
Akka committed
3
import java.util.*;
Melledy's avatar
Melledy committed
4

5
import emu.grasscutter.server.packet.send.PacketCodexDataUpdateNotify;
Melledy's avatar
Melledy committed
6
7
8
9
10
11
12
import org.bson.types.ObjectId;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Indexed;
import dev.morphia.annotations.Transient;
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
13
14
import emu.grasscutter.data.binout.MainQuestData;
import emu.grasscutter.data.excels.RewardData;
Melledy's avatar
Melledy committed
15
16
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.game.props.ActionReason;
Melledy's avatar
Melledy committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import emu.grasscutter.game.quest.enums.ParentQuestState;
import emu.grasscutter.game.quest.enums.QuestState;
import emu.grasscutter.net.proto.ChildQuestOuterClass.ChildQuest;
import emu.grasscutter.net.proto.ParentQuestOuterClass.ParentQuest;
import emu.grasscutter.net.proto.QuestOuterClass.Quest;
import emu.grasscutter.server.packet.send.PacketFinishedParentQuestUpdateNotify;
import emu.grasscutter.server.packet.send.PacketQuestListUpdateNotify;
import emu.grasscutter.server.packet.send.PacketQuestProgressUpdateNotify;
import emu.grasscutter.utils.Utils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

@Entity(value = "quests", useDiscriminator = false)
public class GameMainQuest {
	@Id private ObjectId id;
Akka's avatar
Akka committed
33

Melledy's avatar
Melledy committed
34
35
	@Indexed private int ownerUid;
	@Transient private Player owner;
Akka's avatar
Akka committed
36

Melledy's avatar
Melledy committed
37
	private Map<Integer, GameQuest> childQuests;
Akka's avatar
Akka committed
38

Melledy's avatar
Melledy committed
39
40
41
42
	private int parentQuestId;
	private int[] questVars;
	private ParentQuestState state;
	private boolean isFinished;
Akka's avatar
Akka committed
43
44
    List<QuestGroupSuite> questGroupSuites;

Melledy's avatar
Melledy committed
45
46
	@Deprecated // Morphia only. Do not use.
	public GameMainQuest() {}
Akka's avatar
Akka committed
47

Melledy's avatar
Melledy committed
48
49
50
51
52
53
54
	public GameMainQuest(Player player, int parentQuestId) {
		this.owner = player;
		this.ownerUid = player.getUid();
		this.parentQuestId = parentQuestId;
		this.childQuests = new HashMap<>();
		this.questVars = new int[5];
		this.state = ParentQuestState.PARENT_QUEST_STATE_NONE;
Akka's avatar
Akka committed
55
        this.questGroupSuites = new ArrayList<>();
Melledy's avatar
Melledy committed
56
57
58
59
60
	}

	public int getParentQuestId() {
		return parentQuestId;
	}
Akka's avatar
Akka committed
61

Melledy's avatar
Melledy committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	public int getOwnerUid() {
		return ownerUid;
	}

	public Player getOwner() {
		return owner;
	}

	public void setOwner(Player player) {
		if (player.getUid() != this.getOwnerUid()) return;
		this.owner = player;
	}

	public Map<Integer, GameQuest> getChildQuests() {
		return childQuests;
	}
Akka's avatar
Akka committed
78

Melledy's avatar
Melledy committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	public GameQuest getChildQuestById(int id) {
		return this.getChildQuests().get(id);
	}

	public int[] getQuestVars() {
		return questVars;
	}

	public ParentQuestState getState() {
		return state;
	}

	public boolean isFinished() {
		return isFinished;
	}

Akka's avatar
Akka committed
95
96
97
98
99
    public List<QuestGroupSuite> getQuestGroupSuites() {
        return questGroupSuites;
    }

    public void finish() {
Melledy's avatar
Melledy committed
100
101
		this.isFinished = true;
		this.state = ParentQuestState.PARENT_QUEST_STATE_FINISHED;
Akka's avatar
Akka committed
102

Melledy's avatar
Melledy committed
103
		this.getOwner().getSession().send(new PacketFinishedParentQuestUpdateNotify(this));
104
		this.getOwner().getSession().send(new PacketCodexDataUpdateNotify(this));
Akka's avatar
Akka committed
105

106
		this.save();
Akka's avatar
Akka committed
107

Melledy's avatar
Melledy committed
108
109
110
111
		// Add rewards
		MainQuestData mainQuestData = GameData.getMainQuestDataMap().get(this.getParentQuestId());
		for (int rewardId : mainQuestData.getRewardIdList()) {
			RewardData rewardData = GameData.getRewardDataMap().get(rewardId);
Akka's avatar
Akka committed
112

Melledy's avatar
Melledy committed
113
114
115
			if (rewardData == null) {
				continue;
			}
Akka's avatar
Akka committed
116

Melledy's avatar
Melledy committed
117
118
			getOwner().getInventory().addItemParamDatas(rewardData.getRewardItemList(), ActionReason.QuestReward);
		}
Akka's avatar
Akka committed
119
120
121
122
123
124

        // handoff main quest
        if(mainQuestData.getSuggestTrackMainQuestList() != null){
            Arrays.stream(mainQuestData.getSuggestTrackMainQuestList())
                .forEach(getOwner().getQuestManager()::startMainQuest);
        }
Melledy's avatar
Melledy committed
125
126
127
128
129
130
131
132
133
134
135
	}

	public void save() {
		DatabaseHelper.saveQuest(this);
	}

	public ParentQuest toProto() {
		ParentQuest.Builder proto = ParentQuest.newBuilder()
				.setParentQuestId(getParentQuestId())
				.setIsFinished(isFinished())
				.setParentQuestState(getState().getValue());
Akka's avatar
Akka committed
136

Melledy's avatar
Melledy committed
137
138
139
140
141
		for (GameQuest quest : this.getChildQuests().values()) {
			ChildQuest childQuest = ChildQuest.newBuilder()
					.setQuestId(quest.getQuestId())
					.setState(quest.getState().getValue())
					.build();
Akka's avatar
Akka committed
142

Melledy's avatar
Melledy committed
143
144
			proto.addChildQuestList(childQuest);
		}
Akka's avatar
Akka committed
145

Melledy's avatar
Melledy committed
146
147
148
149
150
151
152
153
154
		if (getQuestVars() != null) {
			for (int i : getQuestVars()) {
				proto.addQuestVar(i);
			}
		}

		return proto.build();
	}
}