gamedigを使う
まずはインストール。
ここにあるように、
npm install gamedig -g
今回はコマンドラインから使うので、上記で。CLI(Command Line Interface)からですね。
基本的な使い方は、
After installing gamedig globally, you can call gamedig via the command line:
gamedig --type minecraft mc.example.com:11234
# Alternatively, if you don't want to install gamedig globally, you can run it with npx:
npx gamedig --type minecraft mc.example.com:11234
The output of the command will be in JSON format.
Additional advanced parameters can be passed in as well:
--debug: Print debugging information, useful when stating an issue.
--pretty: Outputs the JSON format nicely.
--requestRules: Request Valve games rules.
--givenPortOnly: Run the query with the specified port only (if any).
--socketTimeout N: Specifies socket timeout (where N is a number, e.g. 5000).
... and the rest in the same format.
ですが、サポートしているgametypeは、
にフルリストがあります。今回はなじみのあるBattleField2142をターゲットとします。
nao@ghost:~$ gamedig --type battlefield2142 216.128.141.114:29902
{"name":"BF2142 Reclamation US","map":"Suez_Canal_coop","password":false,"raw":{"playerTeamInfo":{"":[{"player":"Mr Slave","score":"6","ping":"0","team":"1","deaths":"2","pid":"0","skill":"2"},{"player":"Woodland Critters","score":"4","ping":"0","team":"2","deaths":"2","pid":"0","skill":"4"},{"player":" DANYOSTUDIOS","score":"4","ping":"42","team":"1","deaths":"0","pid":"11267","skill":"3"},{"player":"Towlie Wannagithigh","score":"4","ping":"0","team":"1","deaths":"2","pid":"0","skill":"2"},{"player":"OJ Simpson","score":"4","ping":"0","team":"1","deaths":"1","pid":"0","skill":"1"},{"player":"[FLOT] Cobalt60-JaGr","score":"3","ping":"47","team":"2","deaths":"1","pid":"55514","skill":"3"},{"player":" Supatimmey","score":"3","ping":"198","team":"1","deaths":"1","pid":"10034455","skill":"3"},{"player":"RIP EternalOldSmoke","score":"3","ping":"0","team":"1","deaths":"2","pid":"0","skill":"2"},{"player":"Fannie Licker","score":"3","ping":"0","team":"1","deaths":"1","pid":"0","skill":"1"},{"player":"TREEFIDY AirJerr","score":"2","ping":"40","team":"2","deaths":"1","pid":"11141","skill":"2"},{"player":"Ivana Humpalot","score":"2","ping":"0","team":"1","deaths":"0","pid":"0","skill":"1"},{"player":"Goolius Boozler","score":"2","pi
以下略
ご丁寧に、JSONで出力してきます。ので、これをパースするのはいつものjqで扱うのが吉ですかね。少し見やすくすると、冒頭は、
{
"name": "BF2142 Reclamation US",
"map": "Fall_of_Berlin_coop",
"password": false,
"raw": {
"playerTeamInfo": {
"": [
{
"player": "D-Isiah TBillings-Clyde",
"score": "23",
"ping": "0",
"team": "2",
"deaths": "5",
"pid": "0",
"skill": "10"
},
最後尾の方は、
"version": "1.10.112.0",
"maxplayers": 64,
"numplayers": 0,
"players": [
{
"name": "D-Isiah TBillings-Clyde",
"raw": {
"score": 23,
"ping": 0,
"team": 2,
"deaths": 5,
"pid": 0,
"skill": "10"
}
},
{
"name": "[DonorBot] Anonymous",
"raw": {
"score": 21,
"ping": 0,
"team": 1,
"deaths": 5,
"pid": 0,
"skill": "13"
}
},
以下略
bot君達もリストアップされていますが、彼らの”pid”は0なので、区別できます。逆にpidが非ゼロならば、joinしている人間プレイヤーなので、これもリストしますかね。で、できあがったスクリプトは、
#!/bin/bash
first=`date '+%Y/%m/%d %H:%M:%S'`
once=$(/usr/local/bin/gamedig --type battlefield2142 216.128.141.114:29902)
second=$(echo $once | /usr/bin/jq -r '[.map,.numplayers] | join(",")')
third=$(echo $once | /usr/bin/jq -r '.players[] | select(.raw.pid != 0)' | jq -r '.name' )
if [ -n "$third" ]; then
echo $first,$second,$third
else
echo $first,$second
fi
これを起動すると、
nao@ghost:~$ ./rec
2026/02/04 16:28:56,Fall_of_Berlin_coop,1, NoomPhohdee
が得られます。瞬時値をここで表示してもしょうがないので、cronで定期的に起動して、結果をテキストファイルへ書いていきます。いつものようにWindowsからもアクセスできるようなディレクトリのファイルに追記していきます。例は、
2026/01/31 11:30:01,Liberation_of_Leipzig_coop,0
2026/01/31 11:40:01,Liberation_of_Leipzig_coop,0
2026/01/31 11:50:01,Liberation_of_Leipzig_coop,0
2026/01/31 12:00:01,Liberation_of_Leipzig_coop,3,Damo Buscopain1 Dano Ackerman Arthus
2026/01/31 12:10:01,Liberation_of_Leipzig_coop,3,Damo Buscopain1 SgtSlaughter08 Dano
2026/01/31 12:20:01,Liberation_of_Leipzig_coop,3,Damo Buscopain1 Dano SgtSlaughter08
2026/01/31 12:30:01,Suez_Canal_coop,2,[PNW] Drflipz SgtSlaughter08
2026/01/31 12:40:01,Suez_Canal_coop,2, SgtSlaughter08 [PNW] Drflipz
2026/01/31 12:50:01,Suez_Canal_coop,4, SgtSlaughter08 [PNW] Drflipz DANYOSTUDIOS TREEFIDY AirJerr
2026/01/31 13:00:01,Belgrade_coop,5,[PNW] Drflipz TREEFIDY AirJerr RampageCyborg DANYOSTUDIOS frostylemons
2026/01/31 13:10:01,Camp_Gibraltar_2_coop,5,TREEFIDY AirJerr DANYOSTUDIOS RampageCyborg frostylemons meier
2026/01/31 13:20:01,Camp_Gibraltar_2_coop,7,TREEFIDY AirJerr Heat-Seeker frostylemons [FLOT] Cobalt60-JaGr DANYOSTUDIOS lillyishot meier
2026/01/31 13:30:01,Camp_Gibraltar_2_coop,8,TREEFIDY AirJerr frostylemons Heat-Seeker lillyishot DANYOSTUDIOS [FLOT] Cobalt60-JaGr meier jake_burst
2026/01/31 13:40:01,Hamburg_Harbour_coop,7, frostylemons DANYOSTUDIOS TREEFIDY AirJerr Heat-Seeker [FLOT] Cobalt60-JaGr meier lillyishot
2026/01/31 13:50:01,Hamburg_Harbour_coop,9, lillyishot frostylemons Heat-Seeker TREEFIDY AirJerr DANYOSTUDIOS [FLOT] Cobalt60-JaGr nibba228 meier jsmitherzz
2026/01/31 14:00:01,Camp_Gibraltar,9, lillyishot Heat-Seeker frostylemons nibba228 [FLOT] Cobalt60-JaGr jsmitherzz TREEFIDY AirJerr meier DANYOSTUDIOS
2026/01/31 14:10:01,street,9, frostylemons jsmitherzz Heat-Seeker TREEFIDY AirJerr nibba228 lillyishot [FLOT] Cobalt60-JaGr DANYOSTUDIOS meier
2026/01/31 14:20:01,street,9, frostylemons lillyishot TREEFIDY AirJerr jsmitherzz nibba228 [FLOT] Cobalt60-JaGr Heat-Seeker meier DANYOSTUDIOS
2026/01/31 14:30:01,street,9, frostylemons lillyishot jsmitherzz TREEFIDY AirJerr nibba228 [FLOT] Cobalt60-JaGr Heat-Seeker meier DANYOSTUDIOS
2026/01/31 14:40:01,Zero1,9, frostylemons jsmitherzz nibba228 [FLOT] Cobalt60-JaGr DANYOSTUDIOS TREEFIDY AirJerr meier Heat-Seeker lillyishot
2026/01/31 14:50:01,Suez_Picnic,8, frostylemons Heat-Seeker jsmitherzz lillyishot [FLOT] Cobalt60-JaGr TREEFIDY AirJerr DANYOSTUDIOS nibba228
2026/01/31 15:00:01,Suez_Picnic,8, frostylemons [FLOT] Cobalt60-JaGr lillyishot jsmitherzz DANYOSTUDIOS nibba228 Heat-Seeker TREEFIDY AirJerr
2026/01/31 15:10:01,Suez_Picnic,9, frostylemons Heat-Seeker [FLOT] Cobalt60-JaGr jsmitherzz nibba228 DANYOSTUDIOS Aglooka Castell TREEFIDY AirJerr
2026/01/31 15:20:01,2142mini_battle,2,TREEFIDY AirJerr Aglooka
あまり頻繁にinquiryするとサーバーの負荷になりかねないので、10分に1回にしています。9人集まると、まあまあですかね。無理矢理gamedigをWindows上で動かすことも可能だと思いますが、C++ Builder CEでUDPClientないしServer使って自前で処理するのが吉だと思います。以前に例示した記憶もありますが、ソースが見つかったら記事にするかもしれません。


コメント