1
0
Fork 0
forked from mc/VTools

分开关机信息,合并邻近的日志输出和join/left,增加永久性关闭关机计时器(直到下次开机)的隐藏命令

This commit is contained in:
Sodium-Aluminate 2023-12-21 17:52:27 +08:00
parent cba3248484
commit 7581ef4595
2 changed files with 58 additions and 42 deletions

View file

@ -9,6 +9,7 @@ import de.strifel.VTools.listeners.TGBridge;
import okhttp3.*; import okhttp3.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
public class ServerCloser { public class ServerCloser {
@ -140,7 +141,7 @@ public class ServerCloser {
while (!lock.isEmpty()) { while (!lock.isEmpty()) {
lock.poll().cancel(); lock.poll().cancel();
} }
lock.add(new Counter(INSTANCE, 1).start()); lock.add(new Counter(INSTANCE, 1,"收到关机命令已经过了 %s 分钟,即将关机。").start());
} }
} }
@ -149,7 +150,15 @@ public class ServerCloser {
while (!lock.isEmpty()) { while (!lock.isEmpty()) {
lock.poll().cancel(); lock.poll().cancel();
} }
lock.add(new Counter(INSTANCE, 60).start()); lock.add(new Counter(INSTANCE, 60, "虽然关机被取消,但 %s 分钟内依旧没有玩家上线,即将关机。").start());
}
}
public void noShutdown(){
synchronized (lock){
while (!lock.isEmpty()) {
lock.poll().cancel();
}
} }
} }
@ -158,11 +167,15 @@ public class ServerCloser {
private final int totalMin; private final int totalMin;
private int minLeft; private int minLeft;
private boolean canceled = false; private boolean canceled = false;
private final String reason;
protected Counter(ServerCloser instance, int minute) { protected Counter(ServerCloser instance, int minute) {this(instance, minute, null);}
protected Counter(ServerCloser instance, int minute, String reason) {
this.instance = instance; this.instance = instance;
totalMin = minute; totalMin = minute;
minLeft = minute; minLeft = minute;
this.reason = Objects.requireNonNullElse(reason, "距离上一个玩家离开已经过了 %s 分钟,即将关机。");
} }
protected synchronized void cancel() { protected synchronized void cancel() {
@ -199,12 +212,12 @@ public class ServerCloser {
minLeft--; minLeft--;
switch (minLeft) { switch (minLeft) {
case 1 -> { case 1 -> {
String msg = "服务器即将在一分钟后关机"; String msg = "服务器即将在一分钟后关机,使用 /fuck 以取消。";
instance.plugin.logger.info(msg); instance.plugin.logger.info(msg);
TGBridge.log(msg); TGBridge.log(msg);
} }
case 0 -> { case 0 -> {
String msg = "ServerCloser: 距离上一个玩家离开已经过了 %s 分钟,即将关机。".formatted(totalMin); String msg = "ServerCloser: " + reason.formatted(totalMin);
instance.plugin.logger.info(msg); instance.plugin.logger.info(msg);
TGBridge.log(msg); TGBridge.log(msg);
instance.close(); instance.close();

View file

@ -94,7 +94,7 @@ public class TGBridge {
Message message = update.message(); Message message = update.message();
if (message.chat() == null || message.chat().id() != CHAT_ID || message.from() == null) continue; if (message.chat() == null || message.chat().id() != CHAT_ID || message.from() == null) continue;
currentAnnounce.abort(); mergeMessage.abort();
String text = ""; String text = "";
Message replyTo = message.replyToMessage(); Message replyTo = message.replyToMessage();
@ -181,10 +181,15 @@ public class TGBridge {
} }
case "/fuck" -> { case "/fuck" -> {
if (server.getAllPlayers().isEmpty()) { if (server.getAllPlayers().isEmpty()) {
if("fuck".equalsIgnoreCase(arg)){
ServerCloser.INSTANCE.noShutdown();
outbound("shutdown timer disabled until next player join & left.");
}else {
ServerCloser.INSTANCE.slowShutdown(); ServerCloser.INSTANCE.slowShutdown();
outbound("shutdown timer has been set to 60 minutes."); outbound("shutdown timer has been set to 60 minutes.");
}
} else { } else {
outbound("still player online, can't shutdown."); outbound("still player online, will not shutdown.");
} }
} }
default -> {} default -> {}
@ -332,11 +337,11 @@ public class TGBridge {
} }
public static void error(String context) { public static void error(String context) {
INSTANCE.outbound("*" + MarkdownString.escapeStr(context) + "*", ParseMode.MarkdownV2); INSTANCE.appendMessage("*" + MarkdownString.escapeStr(context) + "*");
} }
public static void log(String context) { public static void log(String context) {
INSTANCE.outbound("_" + MarkdownString.escapeStr(context) + "_", ParseMode.MarkdownV2); INSTANCE.appendMessage("_" + MarkdownString.escapeStr(context) + "_");
} }
protected void outbound(String content) {outbound(content, (ParseMode) null);} protected void outbound(String content) {outbound(content, (ParseMode) null);}
@ -383,7 +388,7 @@ public class TGBridge {
if (lastDisconnect.equals(username)) { if (lastDisconnect.equals(username)) {
lastDisconnect = ""; lastDisconnect = "";
} }
joinLeftAnnounce(String.format("%s joined the proxy", username)); joinLeftAnnounce(String.format("`%s` joined the server\\.", MarkdownString.escapeStr(username)));
} }
updateRequests.add(new UpdateRequest()); updateRequests.add(new UpdateRequest());
} }
@ -396,8 +401,8 @@ public class TGBridge {
String username = event.getPlayer().getUsername(); String username = event.getPlayer().getUsername();
if (username != null) { if (username != null) {
lastDisconnect = username; lastDisconnect = username;
joinLeftAnnounce(String.format("`%s` left the server\\.", MarkdownString.escapeStr(username)));
} }
joinLeftAnnounce(String.format("%s left the proxy", username));
} }
updateRequests.add(new UpdateRequest()); updateRequests.add(new UpdateRequest());
} }
@ -411,8 +416,7 @@ public class TGBridge {
private boolean PROXY_SHUT_DOWN = false; private boolean PROXY_SHUT_DOWN = false;
private static class UpdateRequest { private static class UpdateRequest {}
}
private LinkedBlockingQueue<UpdateRequest> updateRequests = new LinkedBlockingQueue<>(); private LinkedBlockingQueue<UpdateRequest> updateRequests = new LinkedBlockingQueue<>();
@ -446,27 +450,26 @@ public class TGBridge {
if (PROXY_SHUT_DOWN) { if (PROXY_SHUT_DOWN) {
return; return;
} }
String oldestMessage = null;
try { try {
oldestMessage = announceQueue.take(); String oldestMessage = announceQueue.take();
appendMessage(oldestMessage);
} catch (InterruptedException ignored) {} } catch (InterruptedException ignored) {}
if (!currentAnnounce.isValid()) {
currentAnnounce = new JoinLeftAnnounceMessage(oldestMessage);
continue;
}
ArrayList<String> messages = new ArrayList<>(announceQueue.size() + 1);
messages.add(oldestMessage);
while (!announceQueue.isEmpty()) {
try {
messages.add(announceQueue.take());
} catch (InterruptedException ignored) {}
}
currentAnnounce.addLines(messages);
} }
}).start(); }).start();
} }
private static final Object mergeMessageLock = new Object();
private void appendMessage(String message) {
synchronized (mergeMessageLock) {
if (!mergeMessage.isValid()) {
mergeMessage = new MergeMessage(message);
} else {
mergeMessage.addLines(message);
}
}
}
private static void sleep(int millis) { private static void sleep(int millis) {
try { try {
Thread.sleep(millis); Thread.sleep(millis);
@ -506,7 +509,7 @@ public class TGBridge {
} }
private class JoinLeftAnnounceMessage { private class MergeMessage {
private int messageId; private int messageId;
private long time; private long time;
private long timeMinute; private long timeMinute;
@ -526,21 +529,21 @@ public class TGBridge {
abort = true; abort = true;
} }
protected JoinLeftAnnounceMessage(String firstMessage) { protected MergeMessage(String firstMessage) {
text = new StringBuilder(firstMessage); text = new StringBuilder(firstMessage);
time = System.currentTimeMillis(); time = System.currentTimeMillis();
timeMinute = time / MINUTE; timeMinute = time / MINUTE;
sendAnnounceMessage(); send();
} }
private void sendAnnounceMessage() { private void send() {
if (bot == null) { if (bot == null) {
messageId = -1; messageId = -1;
return; return;
} }
SendResponse response; SendResponse response;
try { try {
response = bot.execute(new SendMessage(CHAT_ID, text.toString())); response = bot.execute(new SendMessage(CHAT_ID, text.toString()).parseMode(ParseMode.MarkdownV2));
} catch (RuntimeException e) { } catch (RuntimeException e) {
messageId = -1; messageId = -1;
@ -553,7 +556,7 @@ public class TGBridge {
messageId = response.message().messageId(); messageId = response.message().messageId();
} }
protected JoinLeftAnnounceMessage() { protected MergeMessage() {
messageId = 0; messageId = 0;
time = 0; time = 0;
timeMinute = 0; timeMinute = 0;
@ -561,14 +564,14 @@ public class TGBridge {
abort = false; abort = false;
} //dummy } //dummy
private void addLines(List<String> messages) { private void addLines(String... messages) {
for (String message : messages) { for (String message : messages) {
text.append('\n').append(message); text.append('\n').append(message);
} }
updateAnnounceMessage(); update();
} }
private void updateAnnounceMessage() { private void update() {
if (!isValid()) { if (!isValid()) {
plugin.logger.error("message should only push to a valid object"); plugin.logger.error("message should only push to a valid object");
return; return;
@ -578,12 +581,12 @@ public class TGBridge {
return; return;
} }
try { try {
bot.execute(new EditMessageText(CHAT_ID, messageId, text.toString())); bot.execute(new EditMessageText(CHAT_ID, messageId, text.toString()).parseMode(ParseMode.MarkdownV2));
} catch (RuntimeException ignored) {} } catch (RuntimeException ignored) {}
} }
} }
private JoinLeftAnnounceMessage currentAnnounce = new JoinLeftAnnounceMessage(); private MergeMessage mergeMessage = new MergeMessage();
private LinkedBlockingQueue<String> announceQueue = new LinkedBlockingQueue<>(); private LinkedBlockingQueue<String> announceQueue = new LinkedBlockingQueue<>();
private void joinLeftAnnounce(String message) { private void joinLeftAnnounce(String message) {