Last active
December 24, 2019 16:55
-
-
Save torgro/4b8aa80ad5b9b2da351b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ChessScores | |
{ | |
[cmdletbinding()] | |
Param( | |
[Parameter(ValueFromPipeline)] | |
[string]$RootPath | |
) | |
Begin { | |
$SharpCode = @' | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
public static class ChessResult | |
{ | |
public static System.Collections.Hashtable GetResults(string fullFileName) | |
{ | |
System.Collections.Hashtable hash = new System.Collections.Hashtable(); | |
hash.Add("White", 0); | |
hash.Add("Black", 0); | |
hash.Add("Draw", 0); | |
List<byte> ByteList = new List<byte>(); | |
var allBytes = System.IO.File.ReadAllBytes(fullFileName); | |
int resultsBracketIndex = 0; | |
int white = 0; | |
int black = 0; | |
int draw = 0; | |
string resultString = string.Empty; | |
for (int i = 0; i < allBytes.Length; i++) | |
{ | |
if (allBytes[i] == 91 && allBytes[(i + 1)] == 82 && allBytes[(i + 2)] == 101) | |
{ | |
resultsBracketIndex = i; | |
} | |
if (allBytes[i] == 93 && resultsBracketIndex != 0) | |
{ | |
resultsBracketIndex = 0; | |
ByteList.Add(allBytes[i]); | |
int indexx = ByteList.IndexOf(34); | |
if (ByteList[indexx + 1] == 49 && ByteList[indexx + 2] == 45) white++; | |
if (ByteList[indexx + 1] == 48 && ByteList[indexx + 2] == 45) black++; | |
if (ByteList[indexx + 1] == 49 && ByteList[indexx + 2] == 47) draw++; | |
ByteList.Clear(); | |
} | |
if (resultsBracketIndex != 0) | |
{ | |
ByteList.Add(allBytes[i]); | |
} | |
} | |
hash["White"] = white; | |
hash["Black"] = black; | |
hash["Draw"] = draw; | |
return hash; | |
} | |
} | |
'@ | |
Add-Type $SharpCode -ErrorAction SilentlyContinue | |
$hash = @{ | |
White = 0 | |
Black = 0 | |
Draw = 0 | |
} | |
[gc]::Collect() | |
} | |
Process { | |
$Files = Get-ChildItem -Path $RootPath -Filter *.pgn -Recurse | |
foreach($file in $Files) | |
{ | |
$FileResult = [ChessResult]::GetResults($file.fullname) | |
$hash["White"] += $FileResult["White"] | |
$hash["Black"] += $FileResult["Black"] | |
$hash["Draw"] += $FileResult["Draw"] | |
} | |
} | |
End { | |
return $hash | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment