feat: add 10 min close room
This commit is contained in:
parent
50cdb30fa0
commit
e560f071ad
@ -26,6 +26,8 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
@ -254,5 +256,12 @@ public class ChessController {
|
||||
log.info("send ping to session {}", value.getId());
|
||||
value.getAsyncRemote().sendPing(Unpooled.buffer().writeLong(System.currentTimeMillis()).nioBuffer());
|
||||
}
|
||||
Instant instant = new Date().toInstant();
|
||||
Instant before10Min = instant.plus(-10, ChronoUnit.MINUTES);
|
||||
rooms.entrySet().stream()
|
||||
.filter(e -> e.getValue().getState().equals(Room.RoomState.CREATED))
|
||||
.filter(e -> e.getValue().getLastStateChange().toInstant().isBefore(before10Min))
|
||||
.map(Map.Entry::getKey)
|
||||
.forEach(rooms::remove);
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,10 @@ package org.mmga.clubs.entities.chess;
|
||||
|
||||
import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import jakarta.websocket.Session;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.mmga.clubs.entities.chess.packet.PlayerJoinRoomPacket;
|
||||
import org.mmga.clubs.entities.chess.packet.PlayerLeavePacket;
|
||||
import org.mmga.clubs.entities.chess.packet.PlayerSideAllocationPacket;
|
||||
@ -12,10 +14,7 @@ import org.mmga.clubs.entities.chess.packet.RoomInfoPacket;
|
||||
import org.mmga.clubs.entities.user.User;
|
||||
import org.mmga.clubs.utils.WebSocketUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
@Data
|
||||
public class Room {
|
||||
@ -30,13 +29,22 @@ public class Room {
|
||||
private boolean isWhiteAcceptRestart;
|
||||
private boolean isBlackAcceptRestart;
|
||||
private boolean canWhiteDown;
|
||||
@Setter(value = AccessLevel.PRIVATE)
|
||||
private Date lastStateChange;
|
||||
|
||||
public Room() {
|
||||
this.id = UUID.randomUUID();
|
||||
this.state = RoomState.CREATED;
|
||||
this.lastStateChange = new Date();
|
||||
resetPieces();
|
||||
}
|
||||
|
||||
public void setState(RoomState state) {
|
||||
if (state == this.state) return;
|
||||
this.state = state;
|
||||
this.lastStateChange = new Date();
|
||||
}
|
||||
|
||||
public void resetPieces() {
|
||||
this.canWhiteDown = false;
|
||||
this.pieces = new byte[16][16];
|
||||
|
Loading…
x
Reference in New Issue
Block a user