Skip to content

Commit

Permalink
Automatic extraction of version from csproj, refactoring, better auto…
Browse files Browse the repository at this point in the history
…matic naming of sets, automatic download of BIG puzzle csv file.
  • Loading branch information
Dumuzy committed Dec 11, 2022
1 parent 2f4197a commit 13adcc2
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 12 deletions.
14 changes: 13 additions & 1 deletion create-pack.cmd
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
rem
rem This script packs together a CPPecker_...zip package.
rem It takes the version number from ChessPuzzlePecker.csproj.
rem To work, this script needs tclsh.exe and 7za.exe in PATH.

@echo off
set projfile=src\ChessUI\ChessPuzzlePecker.csproj
rem The following seems to be the way to go to put the result of a command into
rem a variable in cmd-scripts. It is truly astonishing.
for /f usebackq %%i in (`tclsh extract-vers.tcl %projfile%`) do (
set vers=%%i
)

set vers=1.0.1
set temp0=C:\tmp\cpp\
set packname=CPPecker_%vers%
set tempdir=%temp0%%packname%



md %tempdir%
del /Q %tempdir%\*.*
del /Q %tempdir%\..\*.zip
Expand Down
36 changes: 36 additions & 0 deletions extract-vers.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file extracts and prints the VersionPrefix from a csproj file.
# Must be called like this: tclsh extract-vers.tcl xyz.csproj

proc readLines { fileName error } {
upvar $error err
if { [file exists $fileName] == 0 } {
set err ERR_FILE_NOT_FOUND
return 0
}
set f [open $fileName]
set lineList [read $f]
close $f
set ll [split $lineList \n]
set ll [lreplace $ll end end]
return $ll
}

proc getValueFromKey {lines key default} {
upvar $lines ll
foreach line $ll {
#puts "getValueFromIniLines 1.1 line=$line"
if { [regexp "<$key>(.*)</$key>" $line r r1] !=0 } {
#puts "getValueFromIniLines 1.2"
return $r1
}
}
return $default
}

set filename [lindex $argv 0]
#puts "LOOKING FOR VERS in $filename"
set err {}
set ll [readLines $filename err]
#puts LL=$ll
set vers [getValueFromKey ll {VersionPrefix} {0}]
puts $vers
4 changes: 2 additions & 2 deletions src/ChessUI/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ private void btLichess_Click(object sender, EventArgs e)

private void FillPuzzleSetsComboBox()
{
var pzls = Directory.EnumerateFiles(".", "*.pzl");
var pzls = Directory.EnumerateFiles(".", "*" + PuzzleSet.FileExt);
foreach (var pzl in pzls)
cbPuzzleSets.Items.Add(Path.GetFileNameWithoutExtension(pzl));
}
Expand Down Expand Up @@ -544,7 +544,7 @@ private void btCreatePuzzleSet_Click(object sender, EventArgs e)
{
if (_puzzleSet == null)
_puzzleSet = new PuzzleSet("MyPuzzles-1", 100,
"fork master:70;masterVsMaster:10;hangingPiece:20;", 1800, 2150, 1000,
"fork master:70;masterVsMaster:10;hangingPiece:20;", 1800, 2150, PuzzleSet.StartPuzzleNumRandom,
PuzzleDbProvider.GetLichessCsvFile);

var ib = new InputBox(_puzzleSet);
Expand Down
8 changes: 8 additions & 0 deletions src/ChessUI/InputBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ private void btOk_Click(object sender, EventArgs e)
tbNameOfSet.Text = motifs[0];
}
tbNameOfSet.Text += $"-{tbLowerRating.Value}-{tbUpperRating.Value}";
if (File.Exists(tbNameOfSet.Text + PuzzleSet.FileExt))
foreach (var ext in "abcdefghijklmnopqrstuvwxyz".ToCharArray())
if (!File.Exists(tbNameOfSet.Text + "-" + ext + PuzzleSet.FileExt))
{
tbNameOfSet.Text += "-" + ext;
break;
}

}
var ps = new PuzzleSet(tbNameOfSet.Text, Helper.ToInt(tbNumPuzzles.Text), filters,
tbLowerRating.Value, tbUpperRating.Value, Helper.ToInt(tbStartAtNumber.Text),
Expand Down
6 changes: 4 additions & 2 deletions src/ChessUI/PuzzleDbProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ static private string GetUncompressedCsvGz()

static private string TryDownloadLichessPart()
{
var licPartFile = "lic_part_puzzle-100000.csv";
var licPartFile = "lichess_db_puzzle.csv";
if (!File.Exists(licPartFile))
{
var licPartUrls = @"
http://schachclub-ittersbach.de/wordpress/wp-content/uploads/2022/10/lic_part_puzzle-100000.csv.gz"
http://99-developer-tools.com/wp-content/uploads/2022/12/lichess_db_puzzle.csv.gz
http://schachclub-ittersbach.de/wordpress/wp-content/uploads/2022/10/lic_part_puzzle-100000.csv.gz
"
.SplitToLines();
foreach (var url in licPartUrls)
{
Expand Down
35 changes: 28 additions & 7 deletions src/ChessUI/PuzzleSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public override string ToString()
return s;
}

public const int StartPuzzleNumRandom = -5;

public const string FileExt = ".pzl";

private void ShufflePuzzles() => Puzzles = Puzzles.OrderBy(p => rand.Next()).ToLi();

#region SearchPuzzles
Expand All @@ -108,18 +112,25 @@ void SearchPuzzles()
var fileName = DbFileName;
if (fileName == null)
return;
double startPuzzleRatio = rand.NextDouble(),
fileSize = new System.IO.FileInfo(fileName).Length, readSize = 0;
for (int i = 0; puzzles.Count != NumPuzzlesWanted && i < 3; ++i)
{
int j = 0;
double minReadSize = startPuzzleRatio * fileSize;
foreach (string line in File.ReadLines(fileName))
if (j++ >= startPuzzleNum)
{
if ((startPuzzleNum != StartPuzzleNumRandom && j++ >= startPuzzleNum) ||
(startPuzzleNum == StartPuzzleNumRandom && readSize >= minReadSize))
if (IsAllowed(line, puzzles))
{
var pu = new Puzzle(line);
puzzles.Add(pu.LichessId, pu);
if (puzzles.Count >= NumPuzzlesWanted)
break;
}
readSize += line.Length;
}
// for debugging
foreach (var f in Filters)
{
Expand All @@ -128,11 +139,21 @@ void SearchPuzzles()
}
if (puzzles.Count < NumPuzzlesWanted)
{
if (i % 2 == 0 && LowerRating >= 550)
LowerRating -= 50;
else if (i % 2 == 1 && UpperRating <= 3150)
UpperRating += 50;
startPuzzleNum = 0;
if (startPuzzleNum == StartPuzzleNumRandom)
{
startPuzzleRatio = i == 0 ? startPuzzleRatio / 4 : rand.NextDouble();
readSize = 0;
}
else
startPuzzleNum = 0;

if (i > 0)
{
if (i % 2 == 0 && UpperRating <= 3150)
UpperRating += 50;
else if (i % 2 == 1 && LowerRating >= 550)
LowerRating -= 50;
}
}
}
this.Puzzles = puzzles.Values.ToLi();
Expand Down Expand Up @@ -242,7 +263,7 @@ Li<string> HeaderToLines()
readonly string Name, DbFileName;
string FileName => SFileName(Name);

static string SFileName(string name) => Helper.MakeFilenameSafe(name) + ".pzl";
static string SFileName(string name) => Helper.MakeFilenameSafe(name) + FileExt;
static Random rand = new Random();
#endregion Fields
}
Expand Down

0 comments on commit 13adcc2

Please sign in to comment.