AnnouncementHandler.java 1.55 KB
Newer Older
BaiSugar's avatar
BaiSugar committed
1
2
3
4
5
6
7
8
package emu.grasscutter.server.dispatch;

import emu.grasscutter.Grasscutter;
import express.http.HttpContextHandler;
import express.http.Request;
import express.http.Response;

import java.io.IOException;
Hotaru's avatar
Hotaru committed
9
10
11
12
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
BaiSugar's avatar
BaiSugar committed
13
14
import java.util.Objects;

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

BaiSugar's avatar
BaiSugar committed
17
18
public final class AnnouncementHandler implements HttpContextHandler {
    @Override
Hotaru's avatar
Hotaru committed
19
    public void handle(Request request, Response response) throws IOException {
BaiSugar's avatar
BaiSugar committed
20
        if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnContent")) {
Hotaru's avatar
Hotaru committed
21
22
23
            String data = readToString(Paths.get(DATA("GameAnnouncement.json")));

            response.send("{\"retcode\":0,\"message\":\"OK\",\"data\":" + data + "}");
BaiSugar's avatar
BaiSugar committed
24
        } else if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnList")) {
Hotaru's avatar
Hotaru committed
25
26
27
28
            String data = readToString(Paths.get(DATA("GameAnnouncementList.json")))
                .replace("System.currentTimeMillis()", String.valueOf(System.currentTimeMillis()));

            response.send("{\"retcode\":0,\"message\":\"OK\",\"data\": " + data + "}");
BaiSugar's avatar
BaiSugar committed
29
30
        }
    }
Hotaru's avatar
Hotaru committed
31
32
33
34

    private static String readToString(Path path) {
        String content = "";

BaiSugar's avatar
BaiSugar committed
35
        try {
Hotaru's avatar
Hotaru committed
36
            content = Files.readString(path, StandardCharsets.UTF_8);
37
        } catch (IOException ignored) {
Hotaru's avatar
Hotaru committed
38
            Grasscutter.getLogger().warn("Unable to open file " + path.toAbsolutePath());
BaiSugar's avatar
BaiSugar committed
39
        }
40
        
Hotaru's avatar
Hotaru committed
41
        return content;
BaiSugar's avatar
BaiSugar committed
42
43
    }
}