星际争霸2 sc2replay录像回放python库sc2reader常用函数总结
2021-05-29

安装

pip install sc2reader

使用

import sc2reader
replay = sc2reader.load_replay('video.SC2Replay', load_level=2)

level的值可设置为0到4,越大再入的信息越多。

player

replay.player[key].name 玩家游戏昵称
replay.player[key].pick_race 玩家的种族

一般pvp的时候 key 可选为 1或者2

event

replay的所有内容记录在replay.events结构中
单位创建,单位移动,单位死亡等等。

def get_event(replay):
	for event in replay.events:
		event_names = set([event.name for event in replay.events])
	events_of_type = {name: [] for name in event_names}
	for event in replay.events:
		events_of_type[event.name].append(event)
	return events_of_type

chat event

for msg_event in replay.messages:
		print msg_event.text

显示游戏中玩家的聊天信息

unit event

def unit_done_list(events_of_type):
	unit_born_events = events_of_type["UnitBornEvent"]
	for ube in unit_born_events:
		print ube.unit_controller.name.encode("utf-8") +" born " + str(ube.unit.name) + " at " + str(ube.second / 60) + ":" + str(ube.second % 60)

显示玩家的建造顺序

example

main.py如下

#coding=utf-8
#documents = https://readthedocs.org/projects/sc2reader/downloads/pdf/latest/
import sc2reader
import sys
import sc2fliter

def show_player_info(replay):
	print "    " + replay.player[1].name.encode("utf-8") + " vs " + replay.player[2].name.encode("utf-8")
	print "    " + replay.player[1].pick_race + " vs " + replay.player[2].pick_race
	print " "

def show_chat(replay):
	for msg_event in replay.messages:
		print "    " + msg_event.player.name.encode("utf-8") +" : " + msg_event.text.encode("utf-8")
	print " "

def get_event(replay):
	for event in replay.events:
		event_names = set([event.name for event in replay.events])
	events_of_type = {name: [] for name in event_names}
	for event in replay.events:
		events_of_type[event.name].append(event)
	return events_of_type

def show_base_info(relpay):
	show_player_info(replay)
	show_chat(replay)
	

def unit_done_list(events_of_type,uc=None,n=None):
	unit_born_events = events_of_type["UnitBornEvent"]
	for ube in unit_born_events:
		if sc2fliter.baseFliter(ube.unit_controller,ube.second,ube.unit.name) and sc2fliter.highFliter(ube.unit_controller.pid,ube.unit.name,uc,n):
			print ube.unit_controller.name.encode("utf-8") +" born " + str(ube.unit.name) + " at " + str(ube.second / 60) + ":" + str(ube.second % 60)

if __name__ == '__main__':
	filepath = sys.argv[1]
	replay = sc2reader.load_replay(filepath, load_level=4)
	show_base_info(replay)
	events_of_type = get_event(replay)
	unit_done_list(events_of_type,1)

sc2fliter.py为

import sc2unit
def baseFliter(unit_controller,second,name):
	if unit_controller == None:
		return False
	if second == 0:
		return False
	if str(name) in sc2unit.blacklist:
		return False
	return True

def highFliter(uc,n,unit_controller=None,name =None):
	if unit_controller != None and  name != None:
		if uc == unit_controller and n == name:
			return True
		else:
			return False
	if unit_controller != None:
		if uc == unit_controller:
			return True
		else:
			return False
	if name != None:
		if n == name:
			return True
		else:
			return False
	return True


最终运行结果

Dark建造顺序