2022-07-20 10:17:59 +00:00
|
|
|
package emu.grasscutter.game.systems;
|
2022-07-02 13:43:22 +00:00
|
|
|
|
|
|
|
import emu.grasscutter.Grasscutter;
|
|
|
|
import emu.grasscutter.data.DataLoader;
|
|
|
|
import emu.grasscutter.game.player.Player;
|
|
|
|
import emu.grasscutter.game.world.World;
|
|
|
|
import emu.grasscutter.net.proto.AnnounceDataOuterClass;
|
2022-07-19 05:47:38 +00:00
|
|
|
import emu.grasscutter.server.game.BaseGameSystem;
|
2022-07-02 13:43:22 +00:00
|
|
|
import emu.grasscutter.server.game.GameServer;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketServerAnnounceNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketServerAnnounceRevokeNotify;
|
|
|
|
import emu.grasscutter.utils.Utils;
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.Data;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.experimental.FieldDefaults;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@Getter
|
2022-07-19 05:47:38 +00:00
|
|
|
public class AnnouncementSystem extends BaseGameSystem {
|
|
|
|
private final Map<Integer, AnnounceConfigItem> announceConfigItemMap;
|
2022-07-21 07:21:22 +00:00
|
|
|
|
|
|
|
public AnnouncementSystem(GameServer server) {
|
2022-07-19 05:47:38 +00:00
|
|
|
super(server);
|
|
|
|
this.announceConfigItemMap = new HashMap<>();
|
2022-07-02 13:43:22 +00:00
|
|
|
loadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
private int loadConfig() {
|
2022-08-09 12:14:44 +00:00
|
|
|
try {
|
|
|
|
List<AnnounceConfigItem> announceConfigItems = DataLoader.loadList("Announcement.json", AnnounceConfigItem.class);
|
2022-07-02 13:43:22 +00:00
|
|
|
|
2022-07-19 05:47:38 +00:00
|
|
|
announceConfigItemMap.clear();
|
2022-07-02 13:43:22 +00:00
|
|
|
announceConfigItems.forEach(i -> announceConfigItemMap.put(i.getTemplateId(), i));
|
|
|
|
} catch (Exception e) {
|
|
|
|
Grasscutter.getLogger().error("Unable to load server announce config.", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return announceConfigItemMap.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Player> getOnlinePlayers() {
|
|
|
|
return getServer().getWorlds().stream()
|
|
|
|
.map(World::getPlayers)
|
|
|
|
.flatMap(Collection::stream)
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void broadcast(List<AnnounceConfigItem> tpl) {
|
2022-07-21 07:21:22 +00:00
|
|
|
if (tpl == null || tpl.size() == 0) {
|
2022-07-02 13:43:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var list = tpl.stream()
|
|
|
|
.map(AnnounceConfigItem::toProto)
|
|
|
|
.map(AnnounceDataOuterClass.AnnounceData.Builder::build)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
getOnlinePlayers().forEach(i -> i.sendPacket(new PacketServerAnnounceNotify(list)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int refresh() {
|
|
|
|
return loadConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void revoke(int tplId) {
|
|
|
|
getOnlinePlayers().forEach(i -> i.sendPacket(new PacketServerAnnounceRevokeNotify(tplId)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Data
|
|
|
|
@FieldDefaults(level = AccessLevel.PRIVATE)
|
|
|
|
public class AnnounceConfigItem{
|
|
|
|
int templateId;
|
|
|
|
AnnounceType type;
|
|
|
|
int frequency;
|
|
|
|
String content;
|
|
|
|
Date beginTime;
|
|
|
|
Date endTime;
|
|
|
|
boolean tick;
|
|
|
|
int interval;
|
|
|
|
|
2022-07-21 07:21:22 +00:00
|
|
|
public AnnounceDataOuterClass.AnnounceData.Builder toProto() {
|
2022-07-02 13:43:22 +00:00
|
|
|
var proto = AnnounceDataOuterClass.AnnounceData.newBuilder();
|
|
|
|
|
|
|
|
proto.setConfigId(templateId)
|
|
|
|
// I found the time here is useless
|
|
|
|
.setBeginTime(Utils.getCurrentSeconds() + 1)
|
|
|
|
.setEndTime(Utils.getCurrentSeconds() + 10);
|
|
|
|
|
2022-07-21 07:21:22 +00:00
|
|
|
if (type == AnnounceType.CENTER) {
|
2022-07-02 13:43:22 +00:00
|
|
|
proto.setCenterSystemText(content)
|
|
|
|
.setCenterSystemFrequency(frequency)
|
|
|
|
;
|
2022-07-21 07:21:22 +00:00
|
|
|
}else {
|
2022-07-02 13:43:22 +00:00
|
|
|
proto.setCountDownText(content)
|
|
|
|
.setCountDownFrequency(frequency)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum AnnounceType{
|
|
|
|
CENTER, COUNTDOWN
|
|
|
|
}
|
|
|
|
}
|