การสร้างเกมแนว Football Manager ด้วย Spring Boot

บทนำ

เกมแนว Football Manager เป็นเกมที่ผู้เล่นรับบทบาทเป็นผู้จัดการทีมฟุตบอล โดยต้องบริหารทีม ซื้อ-ขายนักเตะ วางแผนการเล่น และติดตามผลการแข่งขัน การสร้างเกมประเภทนี้ใน Spring Boot ต้องการการออกแบบที่คำนึงถึงการจัดการข้อมูลอย่างมีประสิทธิภาพ เช่น สถิติการแข่งขัน ข้อมูลนักเตะ และผลลัพธ์ของเกม ในบทความนี้ เราจะพูดถึงการออกแบบโครงสร้างพื้นฐานและฟีเจอร์หลักของเกม


การออกแบบโครงสร้างพื้นฐาน

1. การเลือกเทคโนโลยี
  • Spring Boot: สำหรับการพัฒนา Backend
  • Hibernate/JPA: สำหรับการจัดการฐานข้อมูล
  • H2/MySQL: ฐานข้อมูลสำหรับเก็บข้อมูลนักเตะ ทีม และสถิติ
  • Thymeleaf/React/Angular: สำหรับส่วนของ Frontend (ตามความถนัด)
2. โครงสร้างฐานข้อมูล
  • ตารางนักเตะ (Player):

    CREATE TABLE Player (
        id BIGINT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100),
        position VARCHAR(20),
        age INT,
        overall_rating DECIMAL(5,2),
        potential_rating DECIMAL(5,2),
        experience INT,
        training_level INT,
        team_id BIGINT,
        FOREIGN KEY (team_id) REFERENCES Team(id)
    );
    
  • ตารางทีม (Team):

    CREATE TABLE Team (
        id BIGINT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100),
        budget DECIMAL(15,2),
        manager_name VARCHAR(100)
    );
    
  • ตารางการแข่งขัน (Match):

    CREATE TABLE Match (
        id BIGINT AUTO_INCREMENT PRIMARY KEY,
        home_team_id BIGINT,
        away_team_id BIGINT,
        home_score INT,
        away_score INT,
        match_date DATE,
        FOREIGN KEY (home_team_id) REFERENCES Team(id),
        FOREIGN KEY (away_team_id) REFERENCES Team(id)
    );
    
  • ตารางการซื้อขายนักเตะ (Transfer):

    CREATE TABLE Transfer (
        id BIGINT AUTO_INCREMENT PRIMARY KEY,
        player_id BIGINT,
        from_team_id BIGINT,
        to_team_id BIGINT,
        transfer_fee DECIMAL(15,2),
        transfer_date DATE,
        FOREIGN KEY (player_id) REFERENCES Player(id),
        FOREIGN KEY (from_team_id) REFERENCES Team(id),
        FOREIGN KEY (to_team_id) REFERENCES Team(id)
    );
    
  • ตารางการฝึกซ้อม (Training):

    CREATE TABLE Training (
        id BIGINT AUTO_INCREMENT PRIMARY KEY,
        player_id BIGINT,
        training_type VARCHAR(50),
        training_date DATE,
        training_result DECIMAL(5,2),
        FOREIGN KEY (player_id) REFERENCES Player(id)
    );
    

การพัฒนา Backend

1. การสร้าง Entity
@Entity
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String position;
    private int age;
    private double overallRating;
    private double potentialRating;
    private int experience;
    private int trainingLevel;

    @ManyToOne
    @JoinColumn(name = "team_id")
    private Team team;
}
@Entity
public class Team {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private double budget;
    private String managerName;

    @OneToMany(mappedBy = "team")
    private List<Player> players;
}
2. การสร้าง Repository
@Repository
public interface PlayerRepository extends JpaRepository<Player, Long> {
    List<Player> findByTeamId(Long teamId);
}

@Repository
public interface TeamRepository extends JpaRepository<Team, Long> {
}

@Repository
public interface MatchRepository extends JpaRepository<Match, Long> {
}

@Repository
public interface TransferRepository extends JpaRepository<Transfer, Long> {
}

@Repository
public interface TrainingRepository extends JpaRepository<Training, Long> {
}
3. การพัฒนาฟีเจอร์หลัก
  • การสร้างทีมและเพิ่มนักเตะ
@Service
public class TeamService {

    @Autowired
    private TeamRepository teamRepository;

    public Team createTeam(Team team) {
        return teamRepository.save(team);
    }
}

@Service
public class PlayerService {

    @Autowired
    private PlayerRepository playerRepository;

    public Player addPlayerToTeam(Player player, Long teamId) {
        Team team = teamRepository.findById(teamId)
                     .orElseThrow(() -> new RuntimeException("Team not found"));
        player.setTeam(team);
        return playerRepository.save(player);
    }
}
  • การจำลองการแข่งขัน
@Service
public class MatchService {

    @Autowired
    private TeamRepository teamRepository;
    @Autowired
    private MatchRepository matchRepository;

    public Match simulateMatch(Long homeTeamId, Long awayTeamId) {
        Team homeTeam = teamRepository.findById(homeTeamId)
                          .orElseThrow(() -> new RuntimeException("Home team not found"));
        Team awayTeam = teamRepository.findById(awayTeamId)
                          .orElseThrow(() -> new RuntimeException("Away team not found"));

        double homeStrength = homeTeam.getPlayers().stream()
                              .mapToDouble(player -> player.getOverallRating() + player.getExperience() * 0.1)
                              .average()
                              .orElse(0);
        double awayStrength = awayTeam.getPlayers().stream()
                              .mapToDouble(player -> player.getOverallRating() + player.getExperience() * 0.1)
                              .average()
                              .orElse(0);

        int homeScore = (int) (Math.random() * (homeStrength / 10));
        int awayScore = (int) (Math.random() * (awayStrength / 10));

        Match match = new Match();
        match.setHomeTeam(homeTeam);
        match.setAwayTeam(awayTeam);
        match.setHomeScore(homeScore);
        match.setAwayScore(awayScore);
        match.setMatchDate(LocalDate.now());

        return matchRepository.save(match);
    }
}
  • ระบบตลาดนักเตะ
@Service
public class TransferService {

    @Autowired
    private PlayerRepository playerRepository;
    @Autowired
    private TeamRepository teamRepository;
    @Autowired
    private TransferRepository transferRepository;

    public Transfer transferPlayer(Long playerId, Long toTeamId, double transferFee) {
        Player player = playerRepository.findById(playerId)
                        .orElseThrow(() -> new RuntimeException("Player not found"));
        Team toTeam = teamRepository.findById(toTeamId)
                       .orElseThrow(() -> new RuntimeException("Team not found"));
        Team fromTeam = player.getTeam();

        if (toTeam.getBudget() < transferFee) {
            throw new RuntimeException("Not enough budget");
        }

        toTeam.setBudget(toTeam.getBudget() - transferFee);
        fromTeam.setBudget(fromTeam.getBudget() + transferFee);
        player.setTeam(toTeam);

        Transfer transfer = new Transfer();
        transfer.setPlayer(player);
        transfer.setFromTeam(fromTeam);
        transfer.setToTeam(toTeam);
        transfer.setTransferFee(transferFee);
        transfer.setTransferDate(LocalDate.now());

        return transferRepository.save(transfer);
    }
}
  • ระบบการฝึกซ้อม
@Service
public class TrainingService {

    @Autowired
    private PlayerRepository playerRepository;
    @Autowired
    private TrainingRepository trainingRepository;

    public Training trainPlayer(Long playerId, String trainingType) {
        Player player = playerRepository.findById(playerId)
                        .orElseThrow(() -> new RuntimeException("Player not found"));

        double trainingResult = Math.random() * 5; // ผลลัพธ์การฝึกซ้อมแบบสุ่ม
        player.setOverallRating(player.getOverallRating() + trainingResult);
        player.setExperience(player.getExperience() + 1);
        playerRepository.save(player);

        Training training = new Training();
        training.setPlayer(player);
        training.setTrainingType(trainingType);
        training.setTrainingDate(LocalDate.now());
        training.setTrainingResult(trainingResult);

        return trainingRepository.save(training);
    }
}

การพัฒนา Frontend

1. การแสดงข้อมูลทีมและนักเตะ
  • สร้างหน้าที่แสดงรายละเอียดของทีมและนักเตะในแต่ละทีม เช่น จำนวนงบประมาณ รายชื่อนักเตะ และตำแหน่ง
2. การจำลองการแข่งขัน
  • สร้างฟอร์มให้ผู้เล่นเลือกทีมเหย้าและทีมเยือน และแสดงผลลัพธ์การแข่งขันที่ได้จาก Backend
3. ระบบตลาดนักเตะ
  • สร้างหน้าให้ผู้ใช้สามารถเลือกนักเตะและทีมเป้าหมาย รวมถึงกรอกค่าตัวนักเตะ
4. ระบบการฝึกซ้อม
  • สร้างหน้าสำหรับการเลือกนักเตะและประเภทการฝึกซ้อม และแสดงผลลัพธ์ของการฝึกซ้อม

การขยายฟีเจอร์

  1. ระบบลีก: เพิ่มตารางการแข่งขันแบบลีก และคำนวณคะแนนในตาราง
  2. ระบบการเงิน: เพิ่มการจัดการรายได้และรายจ่ายของทีม
  3. ระบบการบาดเจ็บของนักเตะ: เพิ่มความสมจริงในการแข่งขัน
  4. ระบบวิเคราะห์สถิติ: เพิ่มการวิเคราะห์สถิติของทีมและนักเตะเพื่อการวางแผนที่ดีขึ้น

สรุป

การสร้างเกมแนว Football Manager ด้วย Spring Boot ต้องการการออกแบบโครงสร้างฐานข้อมูลที่เหมาะสมและการจัดการข้อมูลที่มีประสิทธิภาพ การใช้ Spring Boot และเทคโนโลยีที่เกี่ยวข้องช่วยให้การพัฒนาเป็นไปอย่างรวดเร็วและยืดหยุ่น คุณสามารถเพิ่มฟีเจอร์เพิ่มเติมได้ตามความต้องการ เช่น ระบบฝึกซ้อม ตลาดนักเตะ หรือการแข่งขันในลีก เพื่อเพิ่มความสนุกและความท้าทายให้กับเกมของคุณ

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

จากสมศรี 422R สู่หุ่นยนต๋ในยุค llm