TowerScheduleManager.java 3.03 KB
Newer Older
1
2
3
package emu.grasscutter.game.tower;

import emu.grasscutter.Grasscutter;
4
import emu.grasscutter.data.DataLoader;
5
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.data.excels.TowerScheduleData;
7
8
9
import emu.grasscutter.server.game.GameServer;

import java.io.FileReader;
10
11
import java.io.InputStreamReader;
import java.io.Reader;
12
import java.util.ArrayList;
13
14
import java.util.List;

15
16
import static emu.grasscutter.Configuration.*;

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class TowerScheduleManager {
    private final GameServer gameServer;

    public GameServer getGameServer() {
        return gameServer;
    }

    public TowerScheduleManager(GameServer gameServer) {
        this.gameServer = gameServer;
        this.load();
    }

    private TowerScheduleConfig towerScheduleConfig;

    public synchronized void load(){
32
        try (Reader fileReader = new InputStreamReader(DataLoader.load("TowerSchedule.json"))) {
33
34
35
36
37
38
39
40
41
42
43
44
45
            towerScheduleConfig = Grasscutter.getGsonFactory().fromJson(fileReader, TowerScheduleConfig.class);
        } catch (Exception e) {
            Grasscutter.getLogger().error("Unable to load tower schedule config.", e);
        }
    }

    public TowerScheduleConfig getTowerScheduleConfig() {
        return towerScheduleConfig;
    }

    public TowerScheduleData getCurrentTowerScheduleData(){
        var data = GameData.getTowerScheduleDataMap().get(towerScheduleConfig.getScheduleId());
        if(data == null){
Akka's avatar
Akka committed
46
47
            Grasscutter.getLogger().error("Could not get current tower schedule data by schedule id {}, please check your resource files",
                    towerScheduleConfig.getScheduleId());
48
        }
49
        
50
51
52
        return data;
    }

53
54
55
56
57
58
    public List<Integer> getAllFloors() {
        List<Integer> floors = new ArrayList<>(this.getCurrentTowerScheduleData().getEntranceFloorId());
        floors.addAll(this.getScheduleFloors());
        return floors;
    }

59
60
61
62
63
64
    public List<Integer> getScheduleFloors() {
        return getCurrentTowerScheduleData().getSchedules().get(0).getFloorList();
    }

    public int getNextFloorId(int floorId){
        var entranceFloors = getCurrentTowerScheduleData().getEntranceFloorId();
65
        var scheduleFloors = getScheduleFloors();
66
        var nextId = 0;
67
        
68
69
70
71
72
73
        // find in entrance floors first
        for(int i=0;i<entranceFloors.size()-1;i++){
            if(floorId == entranceFloors.get(i)){
                nextId = entranceFloors.get(i+1);
            }
        }
74
        
75
76
77
        if(floorId == entranceFloors.get(entranceFloors.size()-1)){
            nextId = scheduleFloors.get(0);
        }
78
        
79
80
81
        if(nextId != 0){
            return nextId;
        }
82
        
83
        // find in schedule floors
84
        for(int i=0; i < scheduleFloors.size() - 1; i++){
85
            if(floorId == scheduleFloors.get(i)){
86
                nextId = scheduleFloors.get(i + 1);
87
            }
88
        }return nextId;
89
90
91
    }

    public Integer getLastEntranceFloor() {
92
        return getCurrentTowerScheduleData().getEntranceFloorId().get(getCurrentTowerScheduleData().getEntranceFloorId().size() - 1);
93
94
    }
}