Skip to content

Instantly share code, notes, and snippets.

@torgro
Last active Dec 24, 2019
Embed
What would you like to do?
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
}
}
@torgro

This comment has been minimized.

Copy link
Owner Author

@torgro torgro commented Mar 7, 2016

image

@torgro

This comment has been minimized.

Copy link
Owner Author

@torgro torgro commented Mar 7, 2016

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment