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

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;

13
14
import static emu.grasscutter.Configuration.*;

BaiSugar's avatar
BaiSugar committed
15
16
17
18
public final class AnnouncementHandler implements HttpContextHandler {
    @Override
    public void handle(Request request, Response response) throws IOException {//event
        if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnContent")) {
19
            response.send("{\"retcode\":0,\"message\":\"OK\",\"data\":" + readToString(new File(DATA("GameAnnouncement.json"))) +"}");
BaiSugar's avatar
BaiSugar committed
20
        } else if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnList")) {
21
            String data = readToString(new File(DATA("GameAnnouncementList.json"))).replace("System.currentTimeMillis()",String.valueOf(System.currentTimeMillis()));
BaiSugar's avatar
BaiSugar committed
22
23
24
            response.send("{\"retcode\":0,\"message\":\"OK\",\"data\": "+data +"}");
        }
    }
25
    @SuppressWarnings("ResultOfMethodCallIgnored")
BaiSugar's avatar
BaiSugar committed
26
    private static String readToString(File file) {
27
28
        long length = file.length();
        byte[] content = new byte[(int) length];
BaiSugar's avatar
BaiSugar committed
29
30
        try {
            FileInputStream in = new FileInputStream(file);
31
32
33
            in.read(content); in.close();
        } catch (IOException ignored) {
            Grasscutter.getLogger().warn("File not found: " + file.getAbsolutePath());
BaiSugar's avatar
BaiSugar committed
34
        }
35
36
        
        return new String(content);
BaiSugar's avatar
BaiSugar committed
37
38
    }
}