TowerManager.java 5.12 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.*;
Akka's avatar
Akka committed
11

12
import java.util.HashMap;
Akka's avatar
Akka committed
13
import java.util.List;
14
import java.util.Map;
Akka's avatar
Akka committed
15
16
17

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

Akka's avatar
Akka committed
20
21
22
    public TowerManager(Player player) {
        this.player = player;
    }
Akka's avatar
Akka committed
23

24
25
26
27
    public void setPlayer(Player player) {
        this.player = player;
    }

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

36
37
38
39
40
    /**
     * floorId - Record
     */
    private Map<Integer, TowerLevelRecord> recordMap;

41
42
43
44
45
46
    @Transient
    private int entryScene;

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

48
49
50
51
52
53
54
55
56
57
    public int getCurrentLevelId(){
        return this.currentLevelId + currentLevel;
    }

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

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

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

71
72
73
74
75
76
77
78
79
80
81
        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
82

Akka's avatar
Akka committed
83
84
85
86
87
        player.getTeamManager().setupTemporaryTeam(towerTeams);
    }


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

90
        var dungeonId = levelData.getDungeonId();
91
92

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

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

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

    public void notifyCurLevelRecordChange(){
110
        player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(currentFloorId, getCurrentLevel()));
111
    }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    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()));
        }
130
131
132
133
134
    }
    public boolean hasNextLevel(){
        return this.currentLevel < 3;
    }
    public int getNextFloorId() {
135
136
137
138
        return player.getServer().getTowerScheduleManager().getNextFloorId(this.currentFloorId);
    }
    public boolean hasNextFloor(){
        return player.getServer().getTowerScheduleManager().getNextFloorId(this.currentFloorId) > 0;
139
    }
Akka's avatar
Akka committed
140

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

    public boolean canEnterScheduleFloor(){
        if(!recordMap.containsKey(player.getServer().getTowerScheduleManager().getLastEntranceFloor())){
            return false;
        }
        return recordMap.get(player.getServer().getTowerScheduleManager().getLastEntranceFloor())
                .getStarCount() >= 6;
    }
152
153
154
155
156
157

    public void mirrorTeamSetUp(int teamId) {
        // use team user choose
        player.getTeamManager().useTemporaryTeam(teamId);
        player.sendPacket(new PacketTowerMiddleLevelChangeTeamNotify());
    }
Akka's avatar
Akka committed
158
}