Ladder continuation

This commit is contained in:
Michael Wain 2024-06-24 22:14:31 +03:00
parent 08bc4b85f1
commit 798beb9b15
33 changed files with 346 additions and 61 deletions

@ -8,7 +8,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@SpringBootApplication
@EnableScheduling

File diff suppressed because one or more lines are too long

@ -4,6 +4,7 @@ import com.alterdekim.hearthhack.entity.RoomPlayer;
import com.alterdekim.hearthhack.service.RoomPlayerService;
import com.alterdekim.hearthhack.service.RoomService;
import com.alterdekim.hearthhack.service.UserService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
@ -26,22 +27,30 @@ public class GamePool {
@Autowired
private RoomPlayerService roomPlayerService;
private ConcurrentHashMap<Long, GameRoom> games = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, GameRoom> games = new ConcurrentHashMap<>();
private static final int PLAYERS_COUNT = 2;
private static final int PLAYERS_COUNT = 1; // TODO: MAKE 2 PLAYERS FOR PRODUCTION
@Scheduled(fixedRate = 1000)
@Scheduled(fixedRate = 200)
private void refreshRooms() {
roomService.getAll()
.forEach(r -> {
if( roomPlayerService.findByRoomId(r.getId()).size() != PLAYERS_COUNT ) return;
log.info("Got room!");
List<RoomPlayer> players = roomPlayerService.findByRoomId(r.getId());
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());
games.put(r.getId(), new GameRoom(players, userService));
});
}
@PostConstruct
private void clearRooms() {
roomService.clear();
roomPlayerService.clear();
}
public Boolean containsPlayer(Long userId) {
return games.keySet()
.stream()
@ -64,4 +73,8 @@ public class GamePool {
)
.findFirst();
}
public GameRoom getGameRoomById(Long id) {
return games.get(id);
}
}

@ -1,19 +1,29 @@
package com.alterdekim.hearthhack.component;
import com.alterdekim.hearthhack.entity.RoomPlayer;
import com.alterdekim.hearthhack.entity.User;
import com.alterdekim.hearthhack.service.UserService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@ToString
@Slf4j
@AllArgsConstructor
public class GameRoom {
@Getter
private List<RoomPlayer> players;
private final List<RoomPlayer> players;
private UserService userService;
private final UserService userService;
@Getter
private final String password;
public GameRoom(List<RoomPlayer> players, UserService userService, String password) {
this.players = players;
this.userService = userService;
this.password = password;
log.info("New GameRoom!");
}
}

@ -1,6 +1,7 @@
package com.alterdekim.hearthhack.component;
import com.alterdekim.hearthhack.config.ServerConfig;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
@ -25,6 +26,10 @@ public class GameServer {
@Autowired
private ServerConfig serverConfig;
@Getter
@Autowired
private GamePool gamePool;
@Scheduled(fixedDelay = 5000)
private void start() {
try {
@ -34,7 +39,7 @@ public class GameServer {
while(true) {
Socket client = this.serverSocket.accept();
log.info("New WOW Connection Established From {}", client.getInetAddress().toString());
GameConnection c = new GameConnection(client);
GameConnection c = new GameConnection(client, this);
this.connections.add(c);
c.start();
}

@ -4,14 +4,11 @@ import com.alterdekim.hearthhack.config.ObjectConfig;
import com.alterdekim.hearthhack.config.FS;
import com.alterdekim.hearthhack.config.ServerConfig;
import com.alterdekim.hearthhack.dbf.CardsDBF;
import com.alterdekim.hearthhack.game.GameTag;
import com.alterdekim.hearthhack.parser.CardsXmlParser;
import com.alterdekim.hearthhack.parser.DBFParser;
import com.alterdekim.hearthhack.util.CardsObject;
import com.alterdekim.hearthhack.util.FormattedCards;
import com.alterdekim.hearthhack.xml.CardsXML;
import com.alterdekim.hearthhack.xml.XMLEntity;
import com.alterdekim.hearthhack.xml.XMLTag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
@ -24,7 +21,6 @@ import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ -2,6 +2,8 @@ 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;
import lombok.Getter;
@ -35,6 +37,12 @@ public class TcpConnection extends Thread {
@Getter
private final UserService userService;
@Getter
private final RoomService roomService;
@Getter
private final RoomPlayerService roomPlayerService;
@Getter
@Setter
private Long userId;

@ -5,6 +5,8 @@ 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;
import lombok.extern.slf4j.Slf4j;
@ -36,6 +38,12 @@ public class TcpServer extends ReflectionLoader<Processor> {
@Autowired
private UserService userService;
@Autowired
private RoomPlayerService roomPlayerService;
@Autowired
private RoomService roomService;
@Scheduled(fixedDelay = 5000)
private void start() {
try {
@ -55,7 +63,7 @@ public class TcpServer extends ReflectionLoader<Processor> {
while(true) {
SSLSocket s = (SSLSocket) serverSocket.accept();
TcpConnection c = new TcpConnection(s, this.getParsers(), dbfConfig, userService);
TcpConnection c = new TcpConnection(s, this.getParsers(), dbfConfig, userService, roomService, roomPlayerService);
connections.add(c);
c.start();
log.info("New Connection Established From {}", s.getInetAddress().toString());

@ -5,10 +5,7 @@ import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.Util;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Slf4j
public class AccountProcessor extends Processor {

@ -4,10 +4,7 @@ package com.alterdekim.hearthhack.component.processor;
import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Slf4j
public class ChannelProcessor extends Processor {

@ -5,7 +5,6 @@ import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.Util;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Set;

@ -2,15 +2,13 @@ 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.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.GameType;
import com.alterdekim.hearthhack.util.Util;
import com.google.protobuf.ByteString;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
import static com.alterdekim.hearthhack.util.GameUtilities.generateSmallNotification;
import java.util.UUID;
@Slf4j
public class GameMasterProcessor extends Processor {
@ -84,6 +82,9 @@ public class GameMasterProcessor extends Processor {
n.getAttributeList().stream().filter(p -> p.hasName() && p.getName().equals("connection_info")).findFirst().get().getValue().getMessageValue()
);
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.
Protocol.Notification n1 = Protocol.Notification.newBuilder()
.setType("G_RESULT")
@ -127,7 +128,7 @@ public class GameMasterProcessor extends Processor {
)
.setHost("192.168.0.9")
.setPort(3724)
.setToken(ByteString.copyFromUtf8("EjcDeI"))
.setToken(ByteString.copyFromUtf8(pwd)) // password
.addAttribute(Protocol.Attribute
.newBuilder()
.setName("version")
@ -135,7 +136,7 @@ public class GameMasterProcessor extends Processor {
.addAttribute(Protocol.Attribute
.newBuilder()
.setName("game")
.setValue(Protocol.Variant.newBuilder().setIntValue(13697066L)))
.setValue(Protocol.Variant.newBuilder().setIntValue(roomId))) // game_id
.addAttribute(Protocol.Attribute
.newBuilder()
.setName("player")
@ -143,7 +144,7 @@ public class GameMasterProcessor extends Processor {
.addAttribute(Protocol.Attribute
.newBuilder()
.setName("id")
.setValue(Protocol.Variant.newBuilder().setIntValue(59414578L)))
.setValue(Protocol.Variant.newBuilder().setIntValue(conn.getUserId())))
.addAttribute(Protocol.Attribute
.newBuilder()
.setName("resumable")

@ -8,7 +8,7 @@ import com.alterdekim.hearthhack.reflect.ReflectionLoader;
import com.alterdekim.hearthhack.util.*;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.Optional;

@ -5,7 +5,6 @@ import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.reflect.AbstractParser;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.Compute32;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@ -4,11 +4,9 @@ import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.ClientRequestBody;
import com.alterdekim.hearthhack.util.Util;
import java.util.stream.IntStream;
import static com.alterdekim.hearthhack.util.GameUtilities.generateEmptyNotification;
import static com.alterdekim.hearthhack.util.GameUtilities.generateNotification;
public class GetAchieves extends ClientRequestParser {
@ -37,7 +35,7 @@ public class GetAchieves extends ClientRequestParser {
achieves.addList(Protocol.Achieve.newBuilder()
.setId(79)
.setProgress(10)
.setProgress(11)
.setActive(false)
.setAckProgress(10)
.setDoNotAck(true)

@ -5,16 +5,11 @@ import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.component.processor.Processor;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.ClientRequestBody;
import com.alterdekim.hearthhack.util.Util;
import com.google.protobuf.ByteString;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static com.alterdekim.hearthhack.util.GameUtilities.generateNotification;
@Slf4j
public class GetDeck extends ClientRequestParser {
@Override

@ -5,8 +5,6 @@ import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.ClientRequestBody;
import java.util.List;
public class SetCardSeen extends ClientRequestParser {
@Override
public void parse(BattleNetPacket packet, ClientRequestBody body, TcpConnection conn) throws Exception {

@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.stream.Collectors;
import static com.alterdekim.hearthhack.util.GameUtilities.generateEmptyNotification;
import static com.alterdekim.hearthhack.util.GameUtilities.generateNotification;
import static com.alterdekim.hearthhack.util.GetAccountInfoRequest.COLLECTION;

@ -1,7 +1,6 @@
package com.alterdekim.hearthhack.config;
import com.alterdekim.hearthhack.dbf.CardsDBF;
import com.alterdekim.hearthhack.util.CardsObject;
import com.alterdekim.hearthhack.util.FormattedCards;
import com.alterdekim.hearthhack.xml.CardsXML;
import lombok.Data;

@ -19,5 +19,9 @@ public class Room {
@Column(nullable = false)
private String roomPassword;
public Room(String roomPassword) {
this.roomPassword = roomPassword;
}
}

@ -1,6 +1,5 @@
package com.alterdekim.hearthhack.entity;
import com.alterdekim.hearthhack.dto.BoosterDTO;
import com.alterdekim.hearthhack.dto.UserCardDTO;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;

@ -1,6 +1,5 @@
package com.alterdekim.hearthhack.reflect;
import com.alterdekim.hearthhack.component.processor.Processor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;

@ -2,7 +2,6 @@ package com.alterdekim.hearthhack.repository;
import com.alterdekim.hearthhack.dto.BoosterDTO;
import com.alterdekim.hearthhack.entity.Booster;
import com.alterdekim.hearthhack.entity.HeroXP;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

@ -1,10 +1,7 @@
package com.alterdekim.hearthhack.repository;
import com.alterdekim.hearthhack.entity.CardBack;
import com.alterdekim.hearthhack.entity.HeroXP;
import jakarta.transaction.Transactional;
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;

@ -1,6 +1,5 @@
package com.alterdekim.hearthhack.repository;
import com.alterdekim.hearthhack.entity.Deck;
import com.alterdekim.hearthhack.entity.DeckContent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;

@ -1,7 +1,6 @@
package com.alterdekim.hearthhack.repository;
import com.alterdekim.hearthhack.entity.Deck;
import com.alterdekim.hearthhack.entity.HeroXP;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

@ -8,8 +8,6 @@ import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Repository
public interface RoomRepository extends JpaRepository<Room, Long> {

@ -1,7 +1,6 @@
package com.alterdekim.hearthhack.repository;
import com.alterdekim.hearthhack.dto.UserCardDTO;
import com.alterdekim.hearthhack.entity.User;
import com.alterdekim.hearthhack.entity.UserCard;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;

@ -36,5 +36,9 @@ public class RoomPlayerService implements IService {
public void removeByRoomId(Long roomId) {
repository.removeByRoomId(roomId);
}
public void clear() {
repository.deleteAll();
}
}

@ -32,4 +32,8 @@ public class RoomService implements IService {
public void removeRoom(Long roomId) {
roomRepository.deleteById(roomId);
}
public void clear() {
roomRepository.deleteAll();
}
}

@ -7,7 +7,6 @@ import com.alterdekim.hearthhack.dto.UserDTO;
import com.alterdekim.hearthhack.entity.*;
import com.alterdekim.hearthhack.repository.*;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@ -1,13 +1,10 @@
package com.alterdekim.hearthhack.xml;
import com.alterdekim.hearthhack.game.GameTag;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import lombok.*;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@ToString
@Getter

@ -3,7 +3,6 @@ package com.alterdekim.hearthhack.xml;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
import lombok.*;
import java.util.List;