ok-ef 开发者快速开始

July 18, 2026 · View on GitHub

返回:文档索引 / README

1. 从源码运行

项目当前要求
操作系统Windows 10/11;交互、截图和登录流程依赖 Win32
Python3.12;CI 和 China 打包配置均固定为 3.12
权限建议管理员权限运行终端/IDE,以与游戏权限一致
游戏窗口16:9,最低 1600x900;见 src/config.py
git clone --recurse-submodules https://github.com/AliceJump/ok-end-field.git
Set-Location ok-end-field
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install --upgrade pip
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
.\.venv\Scripts\python.exe main_debug.py

若 clone 时漏了子模块:

git submodule update --init --recursive

main.pymain_debug.py 都会先调用 install_startup_patches(),再构造 ok.OK(config);Debug 入口只额外设置 config["debug"] = True

2. 新增一次性任务

最小任务放在 src/tasks/onetime/MyTask.py

from src.core.BaseEfTask import BaseEfTask


class MyTask(BaseEfTask):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.name = "我的任务"
        self.description = "执行一个可重复验证的步骤"
        self.default_config.update({"等待秒数": 1})
        self.config_description.update({
            "等待秒数": "操作完成后的等待时间。",
        })

    def run(self):
        self.ensure_main()
        self.log_info("任务开始")
        self.sleep(self.config.get("等待秒数", 1))

src/config.pyconfig["onetime_tasks"] 中注册模块路径和类名:

["src.tasks.onetime.MyTask", "MyTask"],

不要直接赋值 self.default_config = {...};多重继承任务需要保留 MRO 中其它 Mixin 已注册的配置。

3. 新增触发式任务

from ok import TriggerTask

from src.core.BaseEfTask import BaseEfTask


class MyTriggerTask(BaseEfTask, TriggerTask):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.name = "我的触发任务"
        self.description = "后台检查条件并执行动作"

    def run(self):
        if self.find_one("some_feature"):
            self.log_info("检测到目标")

注册到 config["trigger_tasks"]

["src.tasks.trigger.MyTriggerTask", "MyTriggerTask"],

现有组合可作为 MRO 参考:AutoCombatTask(BattleMixin, TriggerTask)ItemNavigatorTask(WsPositionMixin, BaseEfTask, TriggerTask)。业务 Mixin 已继承 BaseEfTask 时不需要再次显式列出 BaseEfTask

4. 复用现有能力

核心能力现在位于 src/core/base_mixin/,业务能力位于 src/tasks/mixin/

from src.tasks.mixin.battle_mixin import BattleMixin
from src.tasks.mixin.map_mixin import MapMixin


class MyBattleTask(MapMixin, BattleMixin):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.name = "地图战斗任务"

    def run(self):
        self.ensure_main()
        self.ensure_map()

常用规则:

  • Feature 使用 FeatureList 或其中存在的字符串名称;find_feature 返回列表,find_one 返回首项。
  • OCR 文本使用 self.lang.<module>.<key>,资源写入统一的 assets/lang/<module>.json
  • 可改键操作用 press_keypress_industry_keypress_combat_key;参数传默认按键值。
  • self.click(box) 可点击识别框;当前项目封装不使用 click(box=...) 示例。
  • login_flow(username) 选择登录界面的最近账号,不输入密码。

5. 验证

运行全部测试:

.\.venv\Scripts\python.exe -m unittest discover -s tests -p "Test*.py"

仓库的 run_tests.ps1 会逐个运行 tests/*.py,也可使用:

.\run_tests.ps1

新增语言引用后至少运行:

.\.venv\Scripts\python.exe -m unittest tests.TestCheckLang
.\.venv\Scripts\python.exe -m unittest tests.TestPoLocaleConsistency

涉及窗口、OCR 或游戏状态的行为仍需用 main_debug.py 实机验证;测试集中既有纯离线测试,也有依赖样本图片和 ok-script 测试工具的测试,不能概括为全部不需要项目资源。

相关文档