PlayerOpenStateManager.java 6.4 KB
Newer Older
akatatsu27's avatar
akatatsu27 committed
1
2
3
package emu.grasscutter.game.player;

import dev.morphia.annotations.Entity;
4
5
6
7
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.excels.OpenStateData;
import emu.grasscutter.data.excels.OpenStateData.OpenStateCondType;
import emu.grasscutter.net.proto.RetcodeOuterClass.Retcode;
akatatsu27's avatar
akatatsu27 committed
8
9
import emu.grasscutter.server.packet.send.PacketOpenStateChangeNotify;
import emu.grasscutter.server.packet.send.PacketOpenStateUpdateNotify;
10
import emu.grasscutter.server.packet.send.PacketSetOpenStateRsp;
akatatsu27's avatar
akatatsu27 committed
11
12
13
14
15

import java.util.*;
import java.util.stream.Collectors;

@Entity
Melledy's avatar
Melledy committed
16
public class PlayerOpenStateManager extends BasePlayerDataManager {
17
18
19
20
    // Set of open states that are never unlocked, whether they fulfill the conditions or not.
    public static final Set<Integer> BLACKLIST_OPEN_STATES = Set.of(
    48      // blacklist OPEN_STATE_LIMIT_REGION_GLOBAL to make Meledy happy. =D Remove this as soon as quest unlocks are fully implemented.
    );
github-actions's avatar
github-actions committed
21

22
23
24
25
26
27
28
29
30
    // Set of open states that are set per default for all accounts. Can be overwritten by an entry in `map`.
    public static final Set<Integer> DEFAULT_OPEN_STATES = GameData.getOpenStateList().stream()
        .filter(s -> 
            s.isDefaultState()      // Actual default-opened states.
            || (s.getCond().stream().filter(c -> c.getCondType() == OpenStateCondType.OPEN_STATE_COND_PLAYER_LEVEL).count() == 0) // All states whose unlock we don't handle correctly yet.
            || s.getId() == 1 // Always unlock OPEN_STATE_PAIMON, otherwise the player will not have a working chat.
        )
        .filter(s -> !BLACKLIST_OPEN_STATES.contains(s.getId()))    // Filter out states in the blacklist.
        .map(s -> s.getId())
31
        .collect(Collectors.toSet());
32
33
34
35

    // Map of all open states that this player has.
    private Map<Integer, Integer> map;

akatatsu27's avatar
akatatsu27 committed
36
    public PlayerOpenStateManager(Player player) {
Melledy's avatar
Melledy committed
37
        super(player);
akatatsu27's avatar
akatatsu27 committed
38
39
    }

40
41
42
43
44
45
    public synchronized Map<Integer, Integer> getOpenStateMap() {
        // If no map currently exists, we create one.
        if (this.map == null) {
            this.map = new HashMap<>();
        }
        
github-actions's avatar
github-actions committed
46
47
        return this.map;
    }
48

49
50
51
52
53
    /**********
        Direct getters and setters for open states.
    **********/
    public int getOpenState(int openState) {
        return getOpenStateMap().getOrDefault(openState, 0);
akatatsu27's avatar
akatatsu27 committed
54
    }
github-actions's avatar
github-actions committed
55

56
57
58
    private void setOpenState(int openState, int value, boolean sendNotify) {
        int previousValue = this.getOpenStateMap().getOrDefault(openState, 0);

github-actions's avatar
github-actions committed
59
        if (value != previousValue) {
60
61
62
63
64
            this.getOpenStateMap().put(openState, value);

            if (sendNotify) {
                player.getSession().send(new PacketOpenStateChangeNotify(openState, value));
            }
akatatsu27's avatar
akatatsu27 committed
65
66
        }
    }
67
68
69
    private void setOpenState(int openState, int value) {
        this.setOpenState(openState, value, true);
    }
akatatsu27's avatar
akatatsu27 committed
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    /**********
        Condition checking for setting open states.
    **********/
    private boolean areConditionsMet(OpenStateData openState) {
        // Check all conditions and test if at least one of them is violated.
        for (var condition : openState.getCond()) {
            // For level conditions, check if the player has reached the necessary level.
            if (condition.getCondType() == OpenStateCondType.OPEN_STATE_COND_PLAYER_LEVEL) {
                if (this.player.getLevel() < condition.getParam()) {
                    return false;
                }
            }
            else if (condition.getCondType() == OpenStateCondType.OPEN_STATE_COND_QUEST) {
                // ToDo: Implement.
            }
            else if (condition.getCondType() == OpenStateCondType.OPEN_STATE_COND_PARENT_QUEST) {
                // ToDo: Implement.
            }
            else if (condition.getCondType() == OpenStateCondType.OPEN_STATE_OFFERING_LEVEL) {
                // ToDo: Implement.
            }
            else if (condition.getCondType() == OpenStateCondType.OPEN_STATE_CITY_REPUTATION_LEVEL) {
                // ToDo: Implement.
            }
akatatsu27's avatar
akatatsu27 committed
95
        }
96
97
98
        
        // Done. If we didn't find any violations, all conditions are met.
        return true;
akatatsu27's avatar
akatatsu27 committed
99
100
    }

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    /**********
        Setting open states from the client (via `SetOpenStateReq`).
    **********/
    public void setOpenStateFromClient(int openState, int value) {
        // Get the data for this open state.
        OpenStateData data = GameData.getOpenStateDataMap().get(openState);
        if (data == null) {
            this.player.sendPacket(new PacketSetOpenStateRsp(Retcode.RET_FAIL));
            return;
        }

        // Make sure that this is an open state that the client is allowed to set,
        // and that it doesn't have any further conditions attached.
        if (!data.isAllowClientOpen() || !this.areConditionsMet(data)) {
            this.player.sendPacket(new PacketSetOpenStateRsp(Retcode.RET_FAIL));
            return;
        }

        // Set.
        this.setOpenState(openState, value);
        this.player.sendPacket(new PacketSetOpenStateRsp(openState, value));
    }

    /**********
        Handler for player login.
    **********/
akatatsu27's avatar
akatatsu27 committed
127
    public void onPlayerLogin() {
128
129
130
131
132
        // Try unlocking open states on player login. This handles accounts where unlock conditions were
        // already met before certain open state unlocks were implemented.
        this.tryUnlockOpenStates(false);

        // Send notify to the client.
133
        player.getSession().send(new PacketOpenStateUpdateNotify(this));
akatatsu27's avatar
akatatsu27 committed
134
    }
135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    /**********
        Triggered unlocking of open states (unlock states whose conditions have been met.)
    **********/
    public void tryUnlockOpenStates(boolean sendNotify) {
        // Get list of open states that are not yet unlocked.
        var lockedStates = GameData.getOpenStateList().stream().filter(s -> this.getOpenStateMap().getOrDefault(s, 0) == 0).toList();

        // Try unlocking all of them.
        for (var state : lockedStates) {
            // To auto-unlock a state, it has to meet three conditions:
            // * it can not be a state that is unlocked by the client,
            // * it has to meet all its unlock conditions, and
            // * it can not be in the blacklist.
            if (!state.isAllowClientOpen() && this.areConditionsMet(state) && !BLACKLIST_OPEN_STATES.contains(state.getId())) {
                this.setOpenState(state.getId(), 1, sendNotify);
            }
        }
    }
    public void tryUnlockOpenStates() {
        this.tryUnlockOpenStates(true);
156
    }
github-actions's avatar
github-actions committed
157
}