Skip to content

Instantly share code, notes, and snippets.

@samunders-core
Created December 27, 2016 15:46
Show Gist options
  • Save samunders-core/278047fdf3607bec18b76933bb6b6990 to your computer and use it in GitHub Desktop.
Save samunders-core/278047fdf3607bec18b76933bb6b6990 to your computer and use it in GitHub Desktop.
parse OpenRa cnc 2 teams replay file for duration, team factions and outcome
#!/bin/sh
egrep -aHo '[-0-9a-zA-Z_@]+:.*' "$@" | \
awk '
$0 == $1 {
group=$1
next
}
{
print group""substr($0,index($0, ":") + 1)
}' | \
egrep -a '.*:(Player@[0-9]:((Faction)?Name|Outcome(TimestampUtc)?|Team)|\w+:\w+Utc):' | \
awk -F'[: ]+' '
function printName() {
if (2 != length(members)) {
printf("%s: Unsupported number of teams: %d\n", file, length(members)) > "/dev/stderr"
return;
} else if (!endTs) {
printf("%s: Crashed game replays unsupported\n", file) > "/dev/stderr"
return;
}
IGNORECASE=1
for (key in members) {
counts[outcome[key]] = asort(members[key])
teams[outcome[key]] = ""
for (i in members[key]) {
teams[outcome[key]] = teams[outcome[key]] "" members[key][i]
}
split("", members[key], ":")
}
IGNORECASE=0
printf("%s", substr(file, 1, index(file, ".") - 1) " " counts["Won"] "v" counts["Lost"] "@")
system("e=$(date -d \"" endTs "\" +%s); s=$(date -d \"" startTs "\" +%s); printf \"%02d:%02d\" $(((e-s)/60)) $((e - s - 60*((e-s)/60)))")
print " " teams["Won"] " vs. " teams["Lost"] "" substr(file, index(file, "."))
endTs=""
}
! file {
file=$1
}
file != $1 {
printName()
split("", members, ":")
file=$1
}
/:Name:/ {
name=$4
}
/:FactionName:/ {
name=name"("$4")"
}
/:Team:/ {
members[team=$4][name]=name
}
/:Outcome:/ {
outcome[team]=$4
}
/:StartTimeUtc:/ {
startTs=$4 " " gensub(/([0-9]+)-([0-9]+)-([0-9]+)/, "\\1:\\2:\\3", "g", $5)
}
/:OutcomeTimestampUtc:/ && "Won" == outcome[team] {
endTs=$4 " " gensub(/([0-9]+)-([0-9]+)-([0-9]+)/, "\\1:\\2:\\3", "g", $5)
}
END {
printName()
}
' | sed -re '
s/OpenRA-//;
s/\(Nod\)/(N)/g;
s/\(GDI\)/(G)/g;
s/(bartolomej|maco)/maco/Ig;
s/(ne)?[vw]educi/weduci/Ig;
s/(ja(k(ub)?|cob))/JAKUB/Ig;
s/e[vw]elyn/evelyn/Ig;
s/vlad(en)?o/vladeno/Ig;
'
# continue pipe to get unique players
# sed -re 's/^20.*v.\s//' -e 's/\((G|N)\)/\n/g' -e 's/(\s+vs.\s+|\.(ora)?rep)/\n/g' | sort | uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment