TowerManager.java 5.14 KB
Newer Older
Akka's avatar
Akka committed
1
2
3
package emu.grasscutter.game.tower;

import dev.morphia.annotations.Entity;
4
import dev.morphia.annotations.Transient;
Akka's avatar
Akka committed
5
import emu.grasscutter.data.GameData;
6
7
8
import emu.grasscutter.data.def.TowerLevelData;
import emu.grasscutter.game.dungeons.DungeonSettleListener;
import emu.grasscutter.game.dungeons.TowerDungeonSettleListener;
Akka's avatar
Akka committed
9
import emu.grasscutter.game.player.Player;
10
import emu.grasscutter.server.packet.send.PacketCanUseSkillNotify;
11
import emu.grasscutter.server.packet.send.PacketTowerCurLevelRecordChangeNotify;
Akka's avatar
Akka committed
12
import emu.grasscutter.server.packet.send.PacketTowerEnterLevelRsp;
13
import emu.grasscutter.server.packet.send.PacketTowerLevelStarCondNotify;
Akka's avatar
Akka committed
14

15
import java.util.HashMap;
Akka's avatar
Akka committed
16
import java.util.List;
17
import java.util.Map;
Akka's avatar
Akka committed
18
19
20

@Entity
public class TowerManager {
Akka's avatar
Akka committed
21
    @Transient private Player player;
Akka's avatar
Akka committed
22

Akka's avatar
Akka committed
23
24
25
    public TowerManager(Player player) {
        this.player = player;
    }
Akka's avatar
Akka committed
26

27
28
29
30
    public void setPlayer(Player player) {
        this.player = player;
    }

31
32
33
    /**
     * the floor players chose
     */
34
    private int currentFloorId;
Akka's avatar
Akka committed
35
    private int currentLevel;
36
37
38
    @Transient
    private int currentLevelId;

39
40
41
42
43
    /**
     * floorId - Record
     */
    private Map<Integer, TowerLevelRecord> recordMap;

44
45
46
47
48
49
    @Transient
    private int entryScene;

    public int getCurrentFloorId() {
        return currentFloorId;
    }
Akka's avatar
Akka committed
50

51
52
53
54
55
56
57
58
59
60
    public int getCurrentLevelId(){
        return this.currentLevelId + currentLevel;
    }

    /**
     * form 1-3
     */
    public int getCurrentLevel(){
        return currentLevel + 1;
    }
61
    private static final List<DungeonSettleListener> towerDungeonSettleListener = List.of(new TowerDungeonSettleListener());
62
63
64
65
66
67
68
69
70

    public Map<Integer, TowerLevelRecord> getRecordMap() {
        if(recordMap == null){
            recordMap = new HashMap<>();
            recordMap.put(1001, new TowerLevelRecord(1001));
        }
        return recordMap;
    }

Akka's avatar
Akka committed
71
72
73
    public void teamSelect(int floor, List<List<Long>> towerTeams) {
        var floorData = GameData.getTowerFloorDataMap().get(floor);

74
75
76
77
78
79
80
81
82
83
84
        this.currentFloorId = floorData.getFloorId();
        this.currentLevel = 0;
        this.currentLevelId = GameData.getTowerLevelDataMap().values().stream()
                .filter(x -> x.getLevelId() == floorData.getLevelId() && x.getLevelIndex() == 1)
                .findFirst()
                .map(TowerLevelData::getID)
                .orElse(0);

        if (entryScene == 0){
            entryScene = player.getSceneId();
        }
Akka's avatar
Akka committed
85

Akka's avatar
Akka committed
86
87
88
89
90
        player.getTeamManager().setupTemporaryTeam(towerTeams);
    }


    public void enterLevel(int enterPointId) {
91
        var levelData = GameData.getTowerLevelDataMap().get(getCurrentLevelId());
92

93
        var dungeonId = levelData.getDungeonId();
94
95

        notifyCurLevelRecordChange();
Akka's avatar
Akka committed
96
97
        // use team user choose
        player.getTeamManager().useTemporaryTeam(0);
98
        player.getServer().getDungeonManager().handoffDungeon(player, dungeonId,
99
100
101
102
103
104
                towerDungeonSettleListener);

        // make sure user can exit dungeon correctly
        player.getScene().setPrevScene(entryScene);
        player.getScene().setPrevScenePoint(enterPointId);

105
        player.getSession().send(new PacketTowerEnterLevelRsp(currentFloorId, getCurrentLevel()));
106
107
        // stop using skill
        player.getSession().send(new PacketCanUseSkillNotify(false));
108
109
        // notify the cond of stars
        player.getSession().send(new PacketTowerLevelStarCondNotify(currentFloorId, getCurrentLevel()));
110
111
112
    }

    public void notifyCurLevelRecordChange(){
113
        player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(currentFloorId, getCurrentLevel()));
114
    }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    public void notifyCurLevelRecordChangeWhenDone(int stars){
        if(!recordMap.containsKey(currentFloorId)){
            recordMap.put(currentFloorId,
                    new TowerLevelRecord(currentFloorId).setLevelStars(getCurrentLevelId(),stars));
        }else{
            recordMap.put(currentFloorId,
                    recordMap.get(currentFloorId).setLevelStars(getCurrentLevelId(),stars));
        }

        this.currentLevel++;

        if(!hasNextLevel()){
            // set up the next floor
            recordMap.put(getNextFloorId(), new TowerLevelRecord(getNextFloorId()));
            player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(getNextFloorId(), 1));
        }else{
            player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(currentFloorId, getCurrentLevel()));
        }
133
134
135
136
137
    }
    public boolean hasNextLevel(){
        return this.currentLevel < 3;
    }
    public int getNextFloorId() {
138
139
140
141
        return player.getServer().getTowerScheduleManager().getNextFloorId(this.currentFloorId);
    }
    public boolean hasNextFloor(){
        return player.getServer().getTowerScheduleManager().getNextFloorId(this.currentFloorId) > 0;
142
    }
Akka's avatar
Akka committed
143

144
145
    public void clearEntry() {
        this.entryScene = 0;
Akka's avatar
Akka committed
146
    }
147
148
149
150
151
152
153
154

    public boolean canEnterScheduleFloor(){
        if(!recordMap.containsKey(player.getServer().getTowerScheduleManager().getLastEntranceFloor())){
            return false;
        }
        return recordMap.get(player.getServer().getTowerScheduleManager().getLastEntranceFloor())
                .getStarCount() >= 6;
    }
Akka's avatar
Akka committed
155
}