TowerSystem.java 2.92 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
import emu.grasscutter.server.game.BaseGameSystem;
8
9
import emu.grasscutter.server.game.GameServer;

10
11
import static emu.grasscutter.config.Configuration.*;

12
import java.io.FileReader;
13
14
import java.io.InputStreamReader;
import java.io.Reader;
15
import java.util.ArrayList;
16
17
import java.util.List;

18
public class TowerSystem extends BaseGameSystem {
github-actions's avatar
github-actions committed
19

20
21
    public TowerSystem(GameServer server) {
        super(server);
22
23
24
25
26
        this.load();
    }

    private TowerScheduleConfig towerScheduleConfig;

github-actions's avatar
github-actions committed
27
    public synchronized void load() {
28
        try (Reader fileReader = DataLoader.loadReader("TowerSchedule.json")) {
29
30
31
32
33
34
35
36
37
38
            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;
    }

github-actions's avatar
github-actions committed
39
    public TowerScheduleData getCurrentTowerScheduleData() {
40
        var data = GameData.getTowerScheduleDataMap().get(towerScheduleConfig.getScheduleId());
github-actions's avatar
github-actions committed
41
        if (data == null) {
Akka's avatar
Akka committed
42
43
            Grasscutter.getLogger().error("Could not get current tower schedule data by schedule id {}, please check your resource files",
                    towerScheduleConfig.getScheduleId());
44
        }
github-actions's avatar
github-actions committed
45

46
47
48
        return data;
    }

49
50
51
52
53
54
    public List<Integer> getAllFloors() {
        List<Integer> floors = new ArrayList<>(this.getCurrentTowerScheduleData().getEntranceFloorId());
        floors.addAll(this.getScheduleFloors());
        return floors;
    }

55
56
57
58
    public List<Integer> getScheduleFloors() {
        return getCurrentTowerScheduleData().getSchedules().get(0).getFloorList();
    }

github-actions's avatar
github-actions committed
59
    public int getNextFloorId(int floorId) {
60
        var entranceFloors = getCurrentTowerScheduleData().getEntranceFloorId();
61
        var scheduleFloors = getScheduleFloors();
62
        var nextId = 0;
github-actions's avatar
github-actions committed
63

64
        // find in entrance floors first
github-actions's avatar
github-actions committed
65
66
        for (int i=0;i<entranceFloors.size()-1;i++) {
            if (floorId == entranceFloors.get(i)) {
67
68
69
                nextId = entranceFloors.get(i+1);
            }
        }
github-actions's avatar
github-actions committed
70
71

        if (floorId == entranceFloors.get(entranceFloors.size()-1)) {
72
73
            nextId = scheduleFloors.get(0);
        }
github-actions's avatar
github-actions committed
74
75

        if (nextId != 0) {
76
77
            return nextId;
        }
github-actions's avatar
github-actions committed
78

79
        // find in schedule floors
github-actions's avatar
github-actions committed
80
81
        for (int i=0; i < scheduleFloors.size() - 1; i++) {
            if (floorId == scheduleFloors.get(i)) {
82
                nextId = scheduleFloors.get(i + 1);
83
            }
84
        }return nextId;
85
86
87
    }

    public Integer getLastEntranceFloor() {
88
        return getCurrentTowerScheduleData().getEntranceFloorId().get(getCurrentTowerScheduleData().getEntranceFloorId().size() - 1);
89
90
    }
}