LoginSignup
0
0

More than 1 year has passed since last update.

データサイエンスでDota2強くなるかも説(1)~環境設定~

Last updated at Posted at 2022-09-01

1. はじめに

最近,Dota2を始めましたが全く勝てません
ハードボットにボコボコにされます.

色々と調べても「死ぬな」くらいのことしか分らず苦戦しています.

1.1. データサイエンスでDota2強くなるかも説

そこで,データサイエンスの力を借りて,どのような状況なら勝っているか?や前回に比べてどのように振舞ったから勝てたのか?ということを数値化して分析していけば強くなるのでは!と考えました.本企画はその仮説を検証していく企画です.

1.2. 今回の概要

Dota2の情報をPythonで取得するまでをやっていきます.

2. Game State Integrationの設定

Game State Integration(以下GSI)はゲーム中のデータを取得するための公式APIで,こちらを使っていきます.

2.1. GSIファイルの作成

gamestate_integration_py.cfgというファイル名で作成します.

"Python Dota 2 GSI Integration"
{
    "uri"       "http://localhost:3000"
    "timeout"   "5.0"
    "buffer"    "0.1"
    "throttle"  "0.1"
    "heartbeat" "30.0"
    "data"
    {
        "provider"  "1"
        "map"       "1"
        "player"    "1"
        "hero"      "1"
        "abilities" "1"
        "items"     "1"
    }
}

こちらのファイルをDota2のcfgフォルダにgamestate_integrationフォルダを作成してコピーします.

cfgフォルダはsteamの画面からメニューを開いて,ローカルファイル,参照でDota2のフォルダに移動してから

file

dota 2 beta\game\dota\cfgこの場所にあります.
このフォルダにgamestate_integrationフォルダを作成して

file

gamestate_integration_py.cfgファイルをコピーします.

file

2.2. 起動設定

メニューの一般から起動オプション-gamestateintegrationを付けます.

file

3. Pythonとの連携

3.1. パッケージのインストール

dota2gsiをインストールします.

pip install dota2gsi==0.1

3.2. サンプルコード

体力やマナなど,基本的な情報を取得するコードです.

import dota2gsi

def demo_handle_state(last_state, state):
    # Use nested gets to safely extract data from the state
    hero_name = state.get('hero', {}).get('name')
    health_percent = state.get('hero', {}).get('health_percent')
    max_health = state.get('hero', {}).get('max_health')
    max_health = state.get('hero', {}).get('max_health')
    health = state.get('hero', {}).get('health')
    level = state.get('hero', {}).get('level')
    mana = state.get('hero', {}).get('mana')
    mana_percent = state.get('hero', {}).get('mana_percent')
    gold = state.get('player', {}).get('gold')
    xpm = state.get('player', {}).get('xpm')
    
    clock_time = state.get('map', {}).get('clock_time')
    
    # If the attributes exist, print them
    if health_percent and max_health:
        health = int(max_health * health_percent/100)
        # print(f"{hero_name}'s current health: {health}/{max_health}")
        print("{}:{}, {}:{}, {}:{}, {}:{},  {}:{},  {}:{}, ".format("clock_time", clock_time, "health", health, "gold", gold, "mana", mana, "level", level, "xpm", xpm))
        
server = dota2gsi.Server(ip='0.0.0.0', port=3000)
server.on_update(demo_handle_state)
server.start()

3.3. データのID

サンプルコードで使用しているIDの詳細はこちらにあります.

3.4. 実行結果

実行するとこんな感じで結構早い周期でデータを取得することができます.

(dota2) D:\Local_Project\5000_HaMaruki\5000.004_Dota2\Dota2-GSI-Science>python demo.py
DotA 2 GSI server listening on 0.0.0.0:3000 - CTRL+C to stop
clock_time:-58, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-57, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-57, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-56, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-55, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-55, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-55, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-55, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-55, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-55, health:640, gold:250, mana:291,  level:1,  xpm:0, 
clock_time:-55, health:640, gold:250, mana:291,  level:1,  xpm:0, 
Server stopped.

4. プログラム

コードはこちらです.

5. おわりに

こんかいは無事にDota2とPythonを連携させることができました.
次回はもう少し取得するデータを拡張し,データを保存する機構を作っていこうと思います.

最新情報

速報はTwitterやブログにて

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0