Skip to content

Getting Started

Pham Anh Tu edited this page Jun 25, 2025 · 43 revisions

<---Home

Tài liệu này mô tả các bước cài đặt game & chạy SDK để kết nối.

Bước 1: Set up phần mềm

Screenshot 2024-08-23 112420

  • Download file Codefest.jar từ bản release version mới nhất (là một file java chứa các thư viện có sẵn để hỗ trợ cho người chơi code) : Download Jar

Bước 2: Tạo Project

  • Mở Intellij vừa dược tải về, chọn New Project

image

  • Đặt tên cho Project và bấm nút Create, lưu ý hãy chọn phiên bản JDK từ 20 trở lên

image

  • Khi này bạn sẽ được giao diện của Project

image

Bước 3: Import file jar

  • Ở góc trên bên trái màn hình chọn Menu sau đó chọn mục Project Structure

Screenshot 2024-08-23 115123

  • Nếu không thấy bạn có thể chọn Setting ở góc trên phải màn hình và chọn Project Structure

Screenshot 2024-08-23 120831

  • Cửa sổ mới sẽ mở ra, sau đó bạn chọn mục Modules -> Libraries

image

  • Chọn dấu + và chọn Java

image

  • Chọn file CodeFest.jar bạn vừa tải về trong folder Downloads sau đó chọn OK

image

  • Sau đó bạn sẽ thêm được file jar vào trong project, bạn chọn Apply sau đó OK

image

  • Khi này bạn đã thêm file jar thành công

image

Bước 4: Connect server

  • Copy đoạn code sau paste vào file Main.java trong Project của bạn
import io.socket.emitter.Emitter;
import jsclub.codefest.sdk.model.GameMap;
import jsclub.codefest.sdk.Hero;

import java.io.IOException;

public class Main {
    private static final String SERVER_URL = "https://cf25-server.jsclub.dev";
    private static final String GAME_ID = "";
    private static final String PLAYER_NAME = "";
    private static final String SECRET_KEY = "*secret-key được BTC cung cấp*";


    public static void main(String[] args) throws IOException {
        Hero hero = new Hero(GAME_ID, PLAYER_NAME, SECRET_KEY);

        Emitter.Listener onMapUpdate = new Emitter.Listener() {
            @Override
            public void call(Object... args) {

                GameMap gameMap = hero.getGameMap();
                gameMap.updateOnUpdateMap(args[0]);
                
            }
        };

        hero.setOnMapUpdate(onMapUpdate);
        hero.start(SERVER_URL);
    }
}
  • Sau đó mở game lên, giao diện của game sẽ như này

image

  • Lúc này trên màn hình sẽ có game ID, lúc này là 189926 Sau đó bạn sẽ nhập game id vào code trong file main của bạn
private static final String GAME_ID = "189926"; 
  • Bộ phận kĩ thuật sẽ cung cấp cho các đội chơi SECRET_KEY cho từng con bot, bạn nhập vào phần tương ứng trong code
    private static final String SECRET_KEY = "key_duoc_cap";
  • Sau đó bạn run Project

image

  • Project sẽ log ra connected to server, lúc này bạn đã thành công connect to server

  • Lúc này trong game sẽ hiện lên người chơi với tên tương ứng trong code

image

Bước 5: Chạy thử bot

  • Để chắc chắn con bot của bạn có thể thực hiện hành động trong game, mình sẽ thử cho nó đi lấy súng
  • Sử dụng if-else để nó sẽ là mỗi condition(step) thực hiện một action
  • Lúc đấy toàn bộ code của bạn sẽ như này
import io.socket.emitter.Emitter;
import jsclub.codefest.sdk.Hero;
import jsclub.codefest.sdk.algorithm.PathUtils;
import jsclub.codefest.sdk.base.Node;
import jsclub.codefest.sdk.model.GameMap;
import jsclub.codefest.sdk.model.players.Player;
import jsclub.codefest.sdk.model.weapon.Weapon;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    private static final String SERVER_URL = "https://cf25-server.jsclub.dev";
    private static final String GAME_ID = "";
    private static final String PLAYER_NAME = "";
    private static final String SECRET_KEY = "*secret-key được BTC cung cấp*";


    public static void main(String[] args) throws IOException {
        Hero hero = new Hero(GAME_ID, PLAYER_NAME, SECRET_KEY);
        Emitter.Listener onMapUpdate = new MapUpdateListener(hero);

        hero.setOnMapUpdate(onMapUpdate);
        hero.start(SERVER_URL);
    }
}

class MapUpdateListener implements Emitter.Listener {
    private final Hero hero;

    public MapUpdateListener(Hero hero) {
        this.hero = hero;
    }

    @Override
    public void call(Object... args) {
        try {
            if (args == null || args.length == 0) return;

            GameMap gameMap = hero.getGameMap();
            gameMap.updateOnUpdateMap(args[0]);
            Player player = gameMap.getCurrentPlayer();

            if (player == null || player.getHealth() == 0) {
                System.out.println("Player is dead or data is not available.");
                return;
            }

            List<Node> nodesToAvoid = getNodesToAvoid(gameMap);

            if (hero.getInventory().getGun() == null) {
                handleSearchForGun(gameMap, player, nodesToAvoid);
            }


        } catch (Exception e) {
            System.err.println("Critical error in call method: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void handleSearchForGun(GameMap gameMap, Player player, List<Node> nodesToAvoid) throws IOException {
        System.out.println("No gun found. Searching for a gun.");
        String pathToGun = findPathToGun(gameMap, nodesToAvoid, player);

        if (pathToGun != null) {
            if (pathToGun.isEmpty()) {
                hero.pickupItem();
            } else {
                hero.move(pathToGun);
            }
        }
    }

    private List<Node> getNodesToAvoid(GameMap gameMap) {
        List<Node> nodes = new ArrayList<>(gameMap.getListIndestructibles());

        nodes.removeAll(gameMap.getObstaclesByTag("CAN_GO_THROUGH"));
        nodes.addAll(gameMap.getOtherPlayerInfo());
        return nodes;
    }

    private String findPathToGun(GameMap gameMap, List<Node> nodesToAvoid, Player player) {
        Weapon nearestGun = getNearestGun(gameMap, player);
        if (nearestGun == null) return null;
        return PathUtils.getShortestPath(gameMap, nodesToAvoid, player, nearestGun, false);
    }

    private Weapon getNearestGun(GameMap gameMap, Player player) {
        List<Weapon> guns = gameMap.getAllGun();
        Weapon nearestGun = null;
        double minDistance = Double.MAX_VALUE;

        for (Weapon gun : guns) {
            double distance = PathUtils.distance(player, gun);
            if (distance < minDistance) {
                minDistance = distance;
                nearestGun = gun;
            }
        }
        return nearestGun;
    }

}
  • Do mình vừa sửa code nên mình phải chạy lại code

  • Để nhanh hơn thì mình tắt đi bật lại game, sau đó thao tác lại bước 4

  • Do cần phải có nhiều hơn 1 người mới vào chơi được nên bạn chọn Add player sau đó chọn Play

  • Như vậy là mình đã hoàn thành xong các hướng dẫn cần thiết để có thể chơi game

Clone this wiki locally