4 个版本
0.1.4 | 2024年6月30日 |
---|---|
0.1.3 | 2023年7月5日 |
0.1.2 | 2023年7月4日 |
0.1.1 | 2022年11月14日 |
0.1.0 |
|
#496 in 网络编程
90KB
2.5K SLoC
dotagsi
使用 Rust 将 Dota 2 的游戏状态集成。提供一个服务器,用于监听由 Dota 2 发送的 JSON 事件。
需求
集成需要
配置文件可以具有任何名称,但必须以 gamestate_integration_
为前缀。例如,gamestate_integration_test.cfg
将位于
- 在 Linux 中:
~/.steam/steam/steamapps/common/dota 2 beta/game/dota/cfg/gamestate_integration_test.cfg
- 在 Windows 中:
D:\Steam\steamapps\common\dota 2 beta\csgo\cfg\gamestate_integration_test.cfg
以下是一个示例配置文件
"dota2-gsi Configuration"
{
"uri" "http://127.0.0.1:53000/"
"timeout" "5.0"
"buffer" "0.1"
"throttle" "0.1"
"heartbeat" "30.0"
"data"
{
"buildings" "1"
"provider" "1"
"map" "1"
"player" "1"
"hero" "1"
"abilities" "1"
"items" "1"
"draft" "1"
"wearables" "1"
}
"auth"
{
"token" "abcdefghijklmopqrstuvxyz123456789"
}
}
请注意配置文件中使用的 URI,因为它必须与创建新的 GSIServer
时使用的 URI 相同。
示例
示例展示了如何实现处理程序来解析游戏状态数据,并对其进行任何操作。
Echoslam:回显服务器接收到的数据
此程序使用提供的组件模型尝试解析服务器接收到的 JSON。请参阅完整程序 src/bin/echoslam.rs
我们只需定义两个回显处理程序作为
use dota::{components::GameState, GSIServer};
/// Echo back Dota GameState integration state.
async fn echo_gamestate_handler(gs: GameState) {
println!("{}", gs);
}
/// Echo back raw JSON events.
async fn echo_json_handler(value: serde_json::Value) {
println!("{}", value);
}
使用命令行参数初始化 GSIServer
let server = GSIServer::new(&args.uri);
并在运行时将处理程序传递给服务器
if args.raw {
server.run(echo_json_handler).await?;
} else {
server.run(echo_gamestate_handler).await?;
}
我们定义了一个命令行标志,用于确定我们在回显之前尝试解析 JSON 数据还是传递原始 JSON。
此程序与 dotagsi
一起提供,可以使用以下方式编译
cargo build --release --bin echoslam
依赖项
~8–19MB
~233K SLoC