Ladder continuation x2
This commit is contained in:
parent
798beb9b15
commit
8c2053e0fb
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,6 @@
|
||||
package com.alterdekim.hearthhack.component;
|
||||
|
||||
import com.alterdekim.hearthhack.entity.RoomPlayer;
|
||||
import com.alterdekim.hearthhack.service.RoomPlayerService;
|
||||
import com.alterdekim.hearthhack.dto.RoomPlayerDTO;
|
||||
import com.alterdekim.hearthhack.service.RoomService;
|
||||
import com.alterdekim.hearthhack.service.UserService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
@ -24,9 +23,6 @@ public class GamePool {
|
||||
@Autowired
|
||||
private RoomService roomService;
|
||||
|
||||
@Autowired
|
||||
private RoomPlayerService roomPlayerService;
|
||||
|
||||
private final ConcurrentHashMap<Long, GameRoom> games = new ConcurrentHashMap<>();
|
||||
|
||||
private static final int PLAYERS_COUNT = 1; // TODO: MAKE 2 PLAYERS FOR PRODUCTION
|
||||
@ -35,12 +31,11 @@ public class GamePool {
|
||||
private void refreshRooms() {
|
||||
roomService.getAll()
|
||||
.forEach(r -> {
|
||||
if( roomPlayerService.findByRoomId(r.getId()).size() != PLAYERS_COUNT ) return;
|
||||
if( r.getPlayers().size() != PLAYERS_COUNT ) return;
|
||||
log.info("Got room!");
|
||||
List<RoomPlayer> players = roomPlayerService.findByRoomId(r.getId());
|
||||
List<RoomPlayerDTO> players = r.getPlayers();
|
||||
log.info("Put room by id: {}", r.getId());
|
||||
games.put(r.getId(), new GameRoom(players, userService, r.getRoomPassword()));
|
||||
roomPlayerService.removeByRoomId(r.getId());
|
||||
roomService.removeRoom(r.getId());
|
||||
});
|
||||
}
|
||||
@ -48,30 +43,6 @@ public class GamePool {
|
||||
@PostConstruct
|
||||
private void clearRooms() {
|
||||
roomService.clear();
|
||||
roomPlayerService.clear();
|
||||
}
|
||||
|
||||
public Boolean containsPlayer(Long userId) {
|
||||
return games.keySet()
|
||||
.stream()
|
||||
.anyMatch(k -> games.get(k)
|
||||
.getPlayers()
|
||||
.stream()
|
||||
.anyMatch(p -> p.getId().longValue() == userId.longValue()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Optional<Long> getGameIdByPlayerId(Long userId) {
|
||||
return games.keySet()
|
||||
.stream()
|
||||
.filter(k -> games.get(k)
|
||||
.getPlayers()
|
||||
.stream()
|
||||
.anyMatch(p -> p.getId().longValue() == userId.longValue()
|
||||
)
|
||||
)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public GameRoom getGameRoomById(Long id) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.alterdekim.hearthhack.component;
|
||||
|
||||
import com.alterdekim.hearthhack.entity.RoomPlayer;
|
||||
import com.alterdekim.hearthhack.dto.RoomPlayerDTO;
|
||||
import com.alterdekim.hearthhack.service.UserService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
@ -13,14 +13,14 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class GameRoom {
|
||||
@Getter
|
||||
private final List<RoomPlayer> players;
|
||||
private final List<RoomPlayerDTO> players;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
@Getter
|
||||
private final String password;
|
||||
|
||||
public GameRoom(List<RoomPlayer> players, UserService userService, String password) {
|
||||
public GameRoom(List<RoomPlayerDTO> players, UserService userService, String password) {
|
||||
this.players = players;
|
||||
this.userService = userService;
|
||||
this.password = password;
|
||||
|
@ -2,7 +2,6 @@ package com.alterdekim.hearthhack.component;
|
||||
|
||||
import com.alterdekim.hearthhack.component.processor.*;
|
||||
import com.alterdekim.hearthhack.config.ObjectConfig;
|
||||
import com.alterdekim.hearthhack.service.RoomPlayerService;
|
||||
import com.alterdekim.hearthhack.service.RoomService;
|
||||
import com.alterdekim.hearthhack.service.UserService;
|
||||
import com.alterdekim.hearthhack.util.BattleNetPacket;
|
||||
@ -40,9 +39,6 @@ public class TcpConnection extends Thread {
|
||||
@Getter
|
||||
private final RoomService roomService;
|
||||
|
||||
@Getter
|
||||
private final RoomPlayerService roomPlayerService;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private Long userId;
|
||||
|
@ -5,7 +5,6 @@ import com.alterdekim.hearthhack.component.processor.Processor;
|
||||
import com.alterdekim.hearthhack.config.ObjectConfig;
|
||||
import com.alterdekim.hearthhack.config.ServerConfig;
|
||||
import com.alterdekim.hearthhack.reflect.ReflectionLoader;
|
||||
import com.alterdekim.hearthhack.service.RoomPlayerService;
|
||||
import com.alterdekim.hearthhack.service.RoomService;
|
||||
import com.alterdekim.hearthhack.service.UserService;
|
||||
import com.alterdekim.hearthhack.util.Util;
|
||||
@ -38,9 +37,6 @@ public class TcpServer extends ReflectionLoader<Processor> {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private RoomPlayerService roomPlayerService;
|
||||
|
||||
@Autowired
|
||||
private RoomService roomService;
|
||||
|
||||
@ -63,7 +59,7 @@ public class TcpServer extends ReflectionLoader<Processor> {
|
||||
|
||||
while(true) {
|
||||
SSLSocket s = (SSLSocket) serverSocket.accept();
|
||||
TcpConnection c = new TcpConnection(s, this.getParsers(), dbfConfig, userService, roomService, roomPlayerService);
|
||||
TcpConnection c = new TcpConnection(s, this.getParsers(), dbfConfig, userService, roomService);
|
||||
connections.add(c);
|
||||
c.start();
|
||||
log.info("New Connection Established From {}", s.getInetAddress().toString());
|
||||
|
@ -2,7 +2,7 @@ package com.alterdekim.hearthhack.component.processor;
|
||||
|
||||
import com.alterdekim.Protocol;
|
||||
import com.alterdekim.hearthhack.component.TcpConnection;
|
||||
import com.alterdekim.hearthhack.entity.Room;
|
||||
import com.alterdekim.hearthhack.dto.RoomDTO;
|
||||
import com.alterdekim.hearthhack.util.BattleNetPacket;
|
||||
import com.alterdekim.hearthhack.util.GameType;
|
||||
import com.google.protobuf.ByteString;
|
||||
@ -83,8 +83,10 @@ public class GameMasterProcessor extends Processor {
|
||||
);
|
||||
log.info("GGGG: {}", ci);*/
|
||||
String pwd = UUID.randomUUID().toString();
|
||||
Long roomId = conn.getRoomService().createRoom(new Room(pwd)); // TODO: make 1 player rooms check.
|
||||
conn.getRoomPlayerService().joinRoom(roomId, conn.getUserId()); // TODO: make 1 player rooms check.
|
||||
Long roomId = conn.getRoomService().createRoom(new RoomDTO(pwd)); // TODO: make 1 player rooms check.
|
||||
conn.getRoomService().joinRoom(roomId, conn.getUserId()); // TODO: make 1 player rooms check.
|
||||
|
||||
Thread.sleep(1000L);
|
||||
|
||||
Protocol.Notification n1 = Protocol.Notification.newBuilder()
|
||||
.setType("G_RESULT")
|
||||
|
@ -1,27 +1,26 @@
|
||||
package com.alterdekim.hearthhack.entity;
|
||||
package com.alterdekim.hearthhack.dto;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "room")
|
||||
public class Room {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public class RoomDTO {
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private List<RoomPlayerDTO> players;
|
||||
|
||||
private String roomPassword;
|
||||
|
||||
public Room(String roomPassword) {
|
||||
public RoomDTO(String roomPassword) {
|
||||
this.players = new ArrayList<>();
|
||||
this.roomPassword = roomPassword;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.alterdekim.hearthhack.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public class RoomPlayerDTO {
|
||||
private Long userId;
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package com.alterdekim.hearthhack.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "room_player")
|
||||
public class RoomPlayer {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Long roomId;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Long userId;
|
||||
|
||||
public RoomPlayer(Long roomId, Long userId) {
|
||||
this.roomId = roomId;
|
||||
this.userId = userId;
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
package com.alterdekim.hearthhack.repository;
|
||||
|
||||
import com.alterdekim.hearthhack.entity.RoomPlayer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface RoomPlayerRepository extends JpaRepository<RoomPlayer, Long> {
|
||||
|
||||
@Query(value = "SELECT new RoomPlayer(r.id, r.roomId, r.userId) FROM RoomPlayer r WHERE r.roomId = :roomId ORDER BY r.userId ASC")
|
||||
List<RoomPlayer> findByRoomId(@Param("roomId") Long roomId);
|
||||
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query(value = "DELETE FROM RoomPlayer r WHERE r.userId = :userId")
|
||||
void deleteAllByUserId(@Param("userId") Long userId);
|
||||
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query(value = "DELETE FROM RoomPlayer r WHERE r.roomId = :roomId")
|
||||
void removeByRoomId(@Param("roomId") Long roomId);
|
||||
|
||||
@Query(value = "SELECT r.roomId FROM RoomPlayer r WHERE r.userId = :userId ORDER BY r.roomId ASC LIMIT 1")
|
||||
Long hasUserId(@Param("userId") Long userId);
|
||||
}
|
||||
|
@ -1,18 +0,0 @@
|
||||
package com.alterdekim.hearthhack.repository;
|
||||
|
||||
import com.alterdekim.hearthhack.entity.Room;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Repository
|
||||
public interface RoomRepository extends JpaRepository<Room, Long> {
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Modifying
|
||||
@Query(value = "DELETE FROM room WHERE room.id NOT IN (SELECT t.room_id FROM (SELECT COUNT(id) as UID, room_id FROM room_player GROUP BY room_id) t)", nativeQuery = true)
|
||||
void clearEmptyRooms();
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.alterdekim.hearthhack.service;
|
||||
|
||||
import com.alterdekim.hearthhack.entity.RoomPlayer;
|
||||
import com.alterdekim.hearthhack.repository.RoomPlayerRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class RoomPlayerService implements IService {
|
||||
|
||||
private final RoomPlayerRepository repository;
|
||||
|
||||
public List<RoomPlayer> getAll() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public List<RoomPlayer> findByRoomId(Long roomId) {
|
||||
return repository.findByRoomId(roomId);
|
||||
}
|
||||
|
||||
public void joinRoom(Long id, Long userId) {
|
||||
repository.save(new RoomPlayer(id, userId));
|
||||
}
|
||||
|
||||
public void leaveByUserId(Long userId) {
|
||||
repository.deleteAllByUserId(userId);
|
||||
}
|
||||
|
||||
public Long hasUserId(Long userId) {
|
||||
return repository.hasUserId(userId);
|
||||
}
|
||||
|
||||
public void removeByRoomId(Long roomId) {
|
||||
repository.removeByRoomId(roomId);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
repository.deleteAll();
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,59 @@
|
||||
package com.alterdekim.hearthhack.service;
|
||||
|
||||
import com.alterdekim.hearthhack.entity.Room;
|
||||
import com.alterdekim.hearthhack.repository.RoomRepository;
|
||||
import com.alterdekim.hearthhack.dto.RoomDTO;
|
||||
import com.alterdekim.hearthhack.dto.RoomPlayerDTO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class RoomService implements IService {
|
||||
private final RoomRepository roomRepository;
|
||||
|
||||
public List<Room> getAll() {
|
||||
return roomRepository.findAll();
|
||||
private ConcurrentMap<Long, RoomDTO> map;
|
||||
private Long _id = 0L;
|
||||
|
||||
public RoomService() {
|
||||
this.map = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
public Optional<Room> findById(Long id) {
|
||||
return roomRepository.findById(id);
|
||||
public List<RoomDTO> getAll() {
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
|
||||
public Long createRoom(Room room) {
|
||||
return roomRepository.save(room).getId();
|
||||
public Optional<RoomDTO> findById(Long id) {
|
||||
return Optional.ofNullable(map.get(id));
|
||||
}
|
||||
|
||||
public Long createRoom(RoomDTO room) {
|
||||
this._id++;
|
||||
room.setId(_id);
|
||||
this.map.put(_id, room);
|
||||
return this._id;
|
||||
}
|
||||
|
||||
public void clearEmptyRooms() {
|
||||
roomRepository.clearEmptyRooms();
|
||||
this.map.keySet().stream()
|
||||
.filter(k -> map.get(k).getPlayers().isEmpty())
|
||||
.forEach(k -> map.remove(k));
|
||||
}
|
||||
|
||||
public void removeRoom(Long roomId) {
|
||||
roomRepository.deleteById(roomId);
|
||||
this.map.remove(roomId);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
roomRepository.deleteAll();
|
||||
map = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
public void joinRoom(Long roomId, Long userId) {
|
||||
RoomDTO r = map.get(roomId);
|
||||
r.getPlayers().add(new RoomPlayerDTO(userId));
|
||||
map.put(roomId, r);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user