PlayerOpenStateManager.java 3.09 KB
Newer Older
akatatsu27's avatar
akatatsu27 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package emu.grasscutter.game.player;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Transient;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.OpenState;
import emu.grasscutter.server.packet.send.PacketOpenStateChangeNotify;
import emu.grasscutter.server.packet.send.PacketOpenStateUpdateNotify;
import lombok.Getter;

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

import static emu.grasscutter.game.props.OpenState.*;

@Entity
public class PlayerOpenStateManager {
    @Transient private Player player;
21
22
23
24
    
    // Map of all open states that this player has. Do not put default values here.
    private Map<Integer, Integer> map;
    
akatatsu27's avatar
akatatsu27 committed
25
26
27
28
29
30
31
     /*
    //DO NOT MODIFY. Based on conversation of official server and client, game version 2.7
    private static Set<OpenState> newPlayerOpenStates = Set.of(OPEN_STATE_DERIVATIVE_MALL,OPEN_STATE_PHOTOGRAPH,OPEN_STATE_BATTLE_PASS,OPEN_STATE_SHOP_TYPE_GENESISCRYSTAL,OPEN_STATE_SHOP_TYPE_RECOMMANDED,
        OPEN_STATE_SHOP_TYPE_GIFTPACKAGE,OPEN_STATE_GUIDE_RELIC_PROM,OPEN_STATE_GUIDE_TALENT,OPEN_STATE_SHOP_TYPE_BLACKSMITH,OPEN_STATE_SHOP_TYPE_PAIMON,OPEN_STATE_WEAPON_AWAKEN,
        OPEN_STATE_WEAPON_PROMOTE,OPEN_STATE_AVATAR_PROMOTE,OPEN_STATE_AVATAR_TALENT,OPEN_STATE_WEAPON_UPGRADE,OPEN_STATE_RESIN,OPEN_STATE_RELIQUARY_UPGRADE,
        OPEN_STATE_SHOP_TYPE_VIRTUAL_SHOP,OPEN_STATE_RELIQUARY_PROMOTE);
    */
32
33
34
    
    // For development. Remove entry when properly implemented
    // TODO - Set as boolean in OpenState
35
36
37
    public static final Set<OpenState> DEV_OPEN_STATES = Stream.of(OpenState.values())
        .filter(s -> s != OpenState.OPEN_STATE_NONE && s.getUnlockLevel() <= 1)
        .collect(Collectors.toSet());
akatatsu27's avatar
akatatsu27 committed
38
39
40
41
42
43
44
45
46

    public PlayerOpenStateManager(Player player) {
        this.player = player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

47
48
49
50
51
52
53
	public Map<Integer, Integer> getOpenStateMap() {
		if (this.map == null) this.map = new HashMap<>();
		return this.map;
	}

    public int getOpenState(OpenState openState) {
        return this.map.getOrDefault(openState.getValue(), 0);
akatatsu27's avatar
akatatsu27 committed
54
    }
55
    
akatatsu27's avatar
akatatsu27 committed
56
    public void setOpenState(OpenState openState, Integer value) {
57
        Integer previousValue = this.map.getOrDefault(openState.getValue(),0);
58
        if(value != previousValue) {
59
            this.map.put(openState.getValue(), value);
akatatsu27's avatar
akatatsu27 committed
60
61
62
63
64
65
66
67
68
69
70
            player.getSession().send(new PacketOpenStateChangeNotify(openState.getValue(),value));
        }
    }

    public void setOpenStates(Map<OpenState,Integer> openStatesChanged) {
        for(Map.Entry<OpenState, Integer> entry : openStatesChanged.entrySet()) {
            setOpenState(entry.getKey(), entry.getValue());
        }
    }

    public void onPlayerLogin() {
71
        player.getSession().send(new PacketOpenStateUpdateNotify(this));
akatatsu27's avatar
akatatsu27 committed
72
    }
73

74
    public void unlockLevelDependentStates() {
75
        Stream.of(OpenState.values())
76
            .filter(s -> s.getUnlockLevel() > 1 && s.getUnlockLevel() <= this.player.getLevel())
77
78
            .forEach(s -> this.setOpenState(s, 1));
    }
akatatsu27's avatar
akatatsu27 committed
79
}