Add server state mapping for web integration.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.alttd.chat.objects.chat_log;
|
||||
|
||||
import com.alttd.chat.objects.BatchInsertable;
|
||||
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
|
||||
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogTypeMapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.alttd.chat.objects.chat_log;
|
||||
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.ChatLogQueries;
|
||||
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogType;
|
||||
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogTypeMapper;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -18,7 +20,7 @@ import java.util.concurrent.*;
|
||||
@Slf4j
|
||||
public class ChatLogHandler {
|
||||
|
||||
private final ChatLogWebHandler chatLogWebHandler = new ChatLogWebHandler();
|
||||
private final WebHandler webHandler = new WebHandler();
|
||||
private static ChatLogHandler instance = null;
|
||||
private ScheduledExecutorService executorService = null;
|
||||
|
||||
@@ -156,7 +158,7 @@ public class ChatLogHandler {
|
||||
blocked
|
||||
);
|
||||
addLog(chatLog);
|
||||
chatLogWebHandler.forwardChatLogToWeb(chatLog);
|
||||
webHandler.forwardChatLogToWeb(chatLog);
|
||||
}
|
||||
|
||||
public CompletableFuture<List<ChatLog>> retrieveChatLogs(UUID uuid, Duration duration, String server) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alttd.chat.objects.chat_log;
|
||||
|
||||
import com.alttd.altitudeweb.invoker.ApiCallback;
|
||||
import com.alttd.altitudeweb.invoker.ApiException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class SilentApiHandler implements ApiCallback<Void> {
|
||||
|
||||
@Override
|
||||
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
log.error("Failed to update server states", e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(Void result, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
|
||||
}
|
||||
|
||||
}
|
||||
+15
-5
@@ -5,9 +5,11 @@ import com.alttd.altitudeweb.invoker.ApiClient;
|
||||
import com.alttd.altitudeweb.invoker.ApiException;
|
||||
import com.alttd.altitudeweb.model.ChatMessageDto;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.objects.chat_log.mapper.chat_log.ChatLogMapper;
|
||||
import com.alttd.chat.objects.chat_log.mapper.server_state.ServerMapper;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -17,17 +19,17 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class ChatLogWebHandler {
|
||||
@Slf4j
|
||||
public class WebHandler {
|
||||
|
||||
private static final int MAX_QUEUE_SIZE = 100;
|
||||
private static final Logger log = LoggerFactory.getLogger(ChatLogWebHandler.class);
|
||||
|
||||
private final Queue<ChatLog> queue = new ConcurrentLinkedQueue<>();
|
||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
private final ChatApi chatApi;
|
||||
|
||||
public ChatLogWebHandler() {
|
||||
public WebHandler() {
|
||||
ApiClient apiClient = new ApiClient();
|
||||
apiClient.setBasePath(Config.CHAT_WEB_SERVER_BASE_URL);
|
||||
|
||||
@@ -46,6 +48,14 @@ public class ChatLogWebHandler {
|
||||
triggerSend();
|
||||
}
|
||||
|
||||
public void updateServerState(String serverName, List<Player> playerList) {
|
||||
try {
|
||||
chatApi.updateServerStatesAsync(ServerMapper.toDto(serverName, playerList), new SilentApiHandler());
|
||||
} catch (ApiException e) {
|
||||
log.error("Failed to update server state", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void triggerSend() {
|
||||
if (sending) {
|
||||
return;
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package com.alttd.chat.objects.chat_log;
|
||||
package com.alttd.chat.objects.chat_log.mapper.chat_log;
|
||||
|
||||
import com.alttd.altitudeweb.model.ChatMessageDto;
|
||||
import com.alttd.chat.objects.chat_log.ChatLog;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alttd.chat.objects.chat_log;
|
||||
package com.alttd.chat.objects.chat_log.mapper.chat_log;
|
||||
|
||||
public enum ChatLogType {
|
||||
PUBLIC,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alttd.chat.objects.chat_log;
|
||||
package com.alttd.chat.objects.chat_log.mapper.chat_log;
|
||||
|
||||
import com.alttd.altitudeweb.model.ChatMessageDto;
|
||||
import lombok.experimental.UtilityClass;
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.alttd.chat.objects.chat_log.mapper.server_state;
|
||||
|
||||
import com.alttd.altitudeweb.model.ServerDto;
|
||||
import com.alttd.altitudeweb.model.ServerStateDto;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@UtilityClass
|
||||
public class ServerMapper {
|
||||
|
||||
public static ServerStateDto toDto(String serverName, List<Player> playerList) {
|
||||
ServerStateDto serverStateDto = new ServerStateDto();
|
||||
ServerDto serverDto = new ServerDto();
|
||||
|
||||
serverDto.setName(serverName);
|
||||
serverDto.setPlayers(playerList.stream().map(UserMapper::toDto).toList());
|
||||
serverStateDto.setServers(List.of(serverDto));
|
||||
return serverStateDto;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.alttd.chat.objects.chat_log.mapper.server_state;
|
||||
|
||||
import com.alttd.altitudeweb.model.UserDto;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@UtilityClass
|
||||
public class UserMapper {
|
||||
public static UserDto toDto(Player player) {
|
||||
UserDto userDto = new UserDto();
|
||||
userDto.setName(player.getName());
|
||||
userDto.setUuid(player.getUniqueId());
|
||||
userDto.setStyledName(GsonComponentSerializer.gson().serialize(player.displayName()));
|
||||
return userDto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user