forked from BieremaBoyzProgramming/bbpPairings
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
executable file
·1034 lines (983 loc) · 27.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of BBP Pairings, a Swiss-system chess tournament engine
* Copyright (C) 2016 Jeremy Bierema
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 3.0, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <chrono>
#include <exception>
#ifdef EXPERIMENTAL_FILESYSTEM
#include <experimental/filesystem>
#define FILESYSTEM_NS std::experimental::filesystem
#else
#ifdef FILESYSTEM
#include <filesystem>
#define FILESYSTEM_NS std::filesystem
#endif
#endif
#include <fstream>
#include <iostream>
#include <list>
#include <memory>
#include <new>
#include <random>
#include <stdexcept>
#include <string>
#include <utility>
#include "fileformats/generatorconfiguration.h"
#include "fileformats/trf.h"
#include "fileformats/types.h"
#include "swisssystems/common.h"
#include "tournament/checker.h"
#include "tournament/generator.h"
#include "tournament/tournament.h"
#include "utility/uintstringconversion.h"
#define NO_VALID_PAIRING 1
#define UNEXPECTED_ERROR 2
#define INVALID_REQUEST 3
#define LIMIT_EXCEEDED 4
#define FILE_ERROR 5
#define STRINGIFY(x) #x
#define STRINGIFY_MACRO(x) STRINGIFY(x)
namespace
{
/**
* Print the build info to the ostream.
*/
void printProgramInfo(std::ostream &ostream, bool checkComponents = true)
{
#ifdef OMIT_BURSTEIN
#define CUSTOM_BUILD
#endif
#ifdef OMIT_DUTCH
#define CUSTOM_BUILD
#endif
#ifdef OMIT_GENERATOR
#define CUSTOM_BUILD
#endif
#ifdef OMIT_CHECKER
#define CUSTOM_BUILD
#endif
#ifdef MAX_PLAYERS
#define CUSTOM_BUILD
#endif
#ifdef MAX_POINTS
#define CUSTOM_BUILD
#endif
#ifdef MAX_RATING
#define CUSTOM_BUILD
#endif
#ifdef MAX_ROUNDS
#define CUSTOM_BUILD
#endif
ostream
<< "BBP Pairings (https://github.com/BieremaBoyzProgramming/bbpPairings) "
"- "
STRINGIFY_MACRO(VERSION_INFO)
" (Built "
<< (checkComponents
? ""
#ifdef CUSTOM_BUILD
"with customized settings"
#endif
#ifndef FILESYSTEM_NS
#ifdef CUSTOM_BUILD
" and "
#endif
"without file path manipulation, "
#else
#ifdef CUSTOM_BUILD
", "
#endif
#endif
: "")
<< __DATE__
" "
__TIME__
")";
}
void relativizePath(std::string &filename, const std::string &pathBase)
{
#ifdef FILESYSTEM_NS
if (!FILESYSTEM_NS::path(filename).has_parent_path())
{
filename =
FILESYSTEM_NS::absolute(
filename,
FILESYSTEM_NS::path(pathBase).remove_filename()
).string();
}
#endif
}
void getChecklistFilename(
std::string &checklistFilename,
const bool userSpecifiedFilename,
const std::string &baseFilename)
{
if (userSpecifiedFilename)
{
relativizePath(checklistFilename, baseFilename);
}
else
{
#ifdef FILESYSTEM_NS
checklistFilename =
FILESYSTEM_NS::path(baseFilename).replace_extension("list").string();
#else
assert(false);
#endif
}
}
std::unique_ptr<std::ofstream> openChecklist(
const std::string &filename,
const bool userSpecifiedFilename,
const std::string &baseFilename)
{
std::unique_ptr<std::ofstream> result;
std::string relativizedFilename = filename;
#ifdef FILESYSTEM_NS
try
{
#endif
getChecklistFilename(
relativizedFilename,
userSpecifiedFilename,
baseFilename);
result.reset(new std::ofstream(relativizedFilename));
if (!*result)
{
std::cerr << "The checklist file ("
<< filename
<< ") could not be opened."
<< std::endl;
result.reset();
}
#ifdef FILESYSTEM_NS
}
catch (const FILESYSTEM_NS::filesystem_error &)
{
std::cerr
<< "Error inferring the path to the checklist."
<< std::endl;
}
#endif
return result;
}
void closeChecklist(std::ofstream *const stream, const std::string &filename)
{
if (stream)
{
// Append the build information, and check for errors.
printProgramInfo(*stream, false);
stream->close();
if (!*stream)
{
std::cerr << "Error while writing to checklist file "
<< filename
<< '.'
<< std::endl;
}
}
}
}
int main(const int argc, char**const argv)
{
try
{
// Check which optional arguments are present and whether the args conform
// to one of the accepted patterns.
const bool printInfo = argc <= 1 || argv[1] == std::string("-r");
const std::string swissSystemString(
argc <= 1 + printInfo ? "" : argv[1u + printInfo]);
const swisssystems::SwissSystem swissSystem =
argc <= 1 + printInfo ? swisssystems::NONE
#ifndef OMIT_DUTCH
: swissSystemString == "--dutch" ? swisssystems::DUTCH
#endif
#ifndef OMIT_BURSTEIN
: swissSystemString == "--burstein" ? swisssystems::BURSTEIN
#endif
#ifndef OMIT_FAST
: swissSystemString == "--fast" ? swisssystems::FAST
#endif
: swisssystems::NONE;
int processedArgCount = 2 + printInfo;
const char *inputFilename;
#ifndef OMIT_CHECKER
const bool checkPairings =
argc >= 2 + processedArgCount
&& argv[1u + processedArgCount] == std::string("-c");
if (checkPairings)
{
inputFilename = argv[processedArgCount];
processedArgCount += 2;
}
#endif
std::string outputFilename;
const bool pairingsOutputFile =
argc >= 3 + processedArgCount
&& argv[2u + processedArgCount] != std::string("-l");
const bool doPairings =
argc >= 2 + processedArgCount + pairingsOutputFile
&& argv[1u + processedArgCount] == std::string("-p");
if (doPairings)
{
inputFilename = argv[processedArgCount];
processedArgCount += 2;
if (pairingsOutputFile)
{
outputFilename = argv[processedArgCount];
++processedArgCount;
}
}
#ifndef OMIT_GENERATOR
const char *seedString;
const bool modelFile =
argc >= 1 + processedArgCount
&& argv[processedArgCount] != std::string("-g");
const bool configurationFile =
argc >= 2 + processedArgCount + modelFile
&& argv[1u + processedArgCount + modelFile] != std::string("-o");
const bool seed =
argc >= 5 + processedArgCount + modelFile + configurationFile
&& argv[3u + processedArgCount + modelFile + configurationFile]
== std::string("-s");
const bool generateTournament =
(!modelFile || !configurationFile)
&& argc
>= 3 + processedArgCount + modelFile + configurationFile + 2 * seed
&& argv[1u + processedArgCount + modelFile + configurationFile]
== std::string("-o");
if (generateTournament)
{
if (modelFile)
{
inputFilename = argv[processedArgCount];
++processedArgCount;
}
++processedArgCount;
if (configurationFile)
{
inputFilename = argv[processedArgCount];
++processedArgCount;
}
++processedArgCount;
outputFilename = argv[processedArgCount];
++processedArgCount;
if (seed)
{
++processedArgCount;
seedString = argv[processedArgCount];
++processedArgCount;
}
}
#endif
const bool checklist =
#ifdef FILESYSTEM_NS
argc >= 1 + processedArgCount
#else
argc >= 2 + processedArgCount
#endif
&& argv[processedArgCount] == std::string("-l");
std::string checklistFilename;
bool checklistCustomFilename{ };
if (checklist)
{
++processedArgCount;
checklistCustomFilename = processedArgCount < argc;
if (checklistCustomFilename)
{
checklistFilename = argv[processedArgCount];
++processedArgCount;
}
}
if (
argc > 1 + printInfo
&& (swissSystem == swisssystems::NONE
|| (unsigned int)doPairings
#ifndef OMIT_CHECKER
+ checkPairings
#endif
#ifndef OMIT_GENERATOR
+ generateTournament
#endif
!= 1
|| processedArgCount != argc))
{
// Invalid command.
printProgramInfo(std::cerr);
const char*const swissSystemSyntax =
#ifndef OMIT_DUTCH
#ifndef OMIT_BURSTEIN
"("
#endif
"--dutch"
#endif
#ifndef OMIT_BURSTEIN
#ifndef OMIT_DUTCH
" | "
#endif
"--burstein"
#ifndef OMIT_DUTCH
")"
#endif
#endif
;
const char*const checklistString =
#ifdef FILESYSTEM_NS
"[-l [check-list-file]]";
#else
"[-l check-list-file]";
#endif
std::cerr << std::endl
<< std::endl
<< "Command line argument syntax:"
<< std::endl
<< argv[0]
<< " [-r]"
<< std::endl
#ifndef OMIT_CHECKER
<< argv[0]
<< " [-r] "
<< swissSystemSyntax
<< " input-file -c "
<< checklistString
<< std::endl
#endif
<< argv[0]
<< " [-r] "
<< swissSystemSyntax
<< " input-file -p [output-file] "
<< checklistString
<< std::endl
#ifndef OMIT_GENERATOR
<< argv[0]
<< " [-r] "
<< swissSystemSyntax
<< " (model-file -g | -g [config-file]) -o trf_file [-s random_seed] "
<< checklistString
<< std::endl
#endif
;
return INVALID_REQUEST;
}
if (printInfo)
{
// Print build info.
printProgramInfo(std::cout);
std::cout << std::endl;
}
#ifndef OMIT_CHECKER
if (checkPairings)
{
// Input a tournament and check that the pairings are correct.
std::ifstream inputStream(inputFilename);
try
{
// Read the tournament.
tournament::Tournament tournament;
try
{
tournament = fileformats::trf::readFile(inputStream, false);
}
catch (const fileformats::FileFormatException &exception)
{
std::cerr << "Error parsing file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
catch (const fileformats::FileReaderException &exception)
{
std::cerr << "Error reading file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return FILE_ERROR;
}
std::unique_ptr<std::ofstream> checklistStream;
if (checklist)
{
checklistStream =
openChecklist(
checklistFilename,
checklistCustomFilename,
inputFilename);
}
// Check that the pairings are correct.
try
{
tournament::checker::check(
tournament,
swissSystem,
checklistStream.get(),
#ifdef FILESYSTEM_NS
FILESYSTEM_NS::path(inputFilename).stem().string());
#else
inputFilename);
#endif
}
catch (const swisssystems::UnapplicableFeatureException &exception)
{
std::cerr << "Error checking file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
std::cout << std::endl;
closeChecklist(checklistStream.get(), checklistFilename);
}
catch (const tournament::BuildLimitExceededException &exception)
{
std::cerr << "Error processing file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return LIMIT_EXCEEDED;
}
catch (const std::length_error &)
{
std::cerr
<< "Error processing file "
<< inputFilename
<< ": The build does not support tournaments this large."
<< std::endl;
return LIMIT_EXCEEDED;
}
catch (const std::bad_alloc &)
{
std::cerr
<< "Error processing file "
<< inputFilename
<< ": The program ran out of memory."
<< std::endl;
return LIMIT_EXCEEDED;
}
}
#endif
if (doPairings)
{
// Input a tournament file, and compute the pairings of the next round.
try
{
std::ifstream inputStream(inputFilename);
// Read the tournament.
tournament::Tournament tournament;
try
{
tournament = fileformats::trf::readFile(inputStream, true);
}
catch (const fileformats::FileFormatException &exception)
{
std::cerr << "Error parsing file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
catch (const fileformats::FileReaderException &exception)
{
std::cerr << "Error reading file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return FILE_ERROR;
}
if (tournament.initialColor == tournament::COLOR_NONE)
{
std::cerr << "Error while parsing "
<< inputFilename
<< ": Please configure the initial piece colors."
<< std::endl;
return INVALID_REQUEST;
}
tournament.updateRanks();
tournament.computePlayerData();
// Add default accelerations.
const swisssystems::Info &info = swisssystems::getInfo(swissSystem);
if (tournament.defaultAcceleration)
{
for (
tournament::round_index round_index{ };
round_index <= tournament.playedRounds;
++round_index)
{
info.updateAccelerations(tournament, round_index);
}
}
// Open the output file, if specified.
std::ofstream outputFileStream;
std::ostream *outputStream = &std::cout;
if (pairingsOutputFile)
{
#ifdef FILESYSTEM_NS
try
{
#endif
relativizePath(outputFilename, inputFilename);
#ifdef FILESYSTEM_NS
}
catch (const FILESYSTEM_NS::filesystem_error &)
{
std::cerr
<< "Error extracting the directory of the input file."
<< std::endl;
return FILE_ERROR;
}
#endif
outputFileStream = std::ofstream(outputFilename);
if (!outputFileStream)
{
std::cerr << "The output file ("
<< outputFilename
<< ") could not be opened."
<< std::endl;
return FILE_ERROR;
}
outputStream = &outputFileStream;
}
// Open the checklist output file, if requested.
std::unique_ptr<std::ofstream> checklistStream;
if (checklist)
{
checklistStream =
openChecklist(
checklistFilename,
checklistCustomFilename,
inputFilename);
}
// Compute the matching.
std::list<swisssystems::Pairing> pairs;
try
{
pairs =
info.computeMatching(std::move(tournament), checklistStream.get());
}
catch (const swisssystems::NoValidPairingException &exception)
{
std::cerr << "Error while pairing "
<< inputFilename
<< ": No valid pairing exists: "
<< exception.what()
<< std::endl;
return NO_VALID_PAIRING;
}
catch (const swisssystems::UnapplicableFeatureException &exception)
{
std::cerr << "Error while pairing "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
closeChecklist(checklistStream.get(), checklistFilename);
swisssystems::sortResults(pairs, tournament);
// Output the pairs.
*outputStream << pairs.size() << std::endl;
for (const swisssystems::Pairing &pair : pairs)
{
*outputStream << pair.white + 1u
<< ' '
<< (pair.white == pair.black
? "0"
: utility::uintstringconversion::toString(pair.black + 1u))
<< std::endl;
}
if (pairingsOutputFile)
{
// Check for errors.
outputFileStream.close();
if (!outputFileStream)
{
std::cerr << "Error while writing to output file "
<< outputFilename
<< '.'
<< std::endl;
}
}
}
catch (const tournament::BuildLimitExceededException &exception)
{
std::cerr << "Error processing file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return LIMIT_EXCEEDED;
}
catch (const std::length_error &)
{
std::cerr
<< "Error processing file "
<< inputFilename
<< ": The build does not support tournaments this large."
<< std::endl;
return LIMIT_EXCEEDED;
}
catch (const std::bad_alloc &)
{
std::cerr
<< "Error processing file "
<< inputFilename
<< ": The program ran out of memory."
<< std::endl;
return LIMIT_EXCEEDED;
}
}
#ifndef OMIT_GENERATOR
else if (generateTournament)
{
// Generate a random tournament from configuration options or a model
// tournament.
// Choose a random seed, using the (high-precision) time and, if possible,
// a nondeterministic source of randomness.
try
{
std::minstd_rand::result_type seedValue;
if (seed)
{
try
{
seedValue =
utility::uintstringconversion
::parse<decltype(seedValue)>(seedString);
}
catch (const std::out_of_range &)
{
std::cerr << "The seed must be between 0 and "
<< utility::uintstringconversion
::toString(~decltype(seedValue){ })
<< '.'
<< std::endl;
return LIMIT_EXCEEDED;
}
}
else
{
const std::chrono::high_resolution_clock::duration::rep now =
std::chrono::high_resolution_clock::now().time_since_epoch()
.count();
try
{
std::random_device random;
if (random.entropy())
{
const decltype(random() + now) initializer[]{ random(), now };
std::seed_seq(
initializer,
initializer + sizeof(initializer) / sizeof(*initializer)
).generate(&seedValue, 1 + &seedValue);
}
else
{
std::seed_seq({ now }).generate(&seedValue, 1 + &seedValue);
}
}
catch (const std::exception &exception)
{
std::seed_seq({ now }).generate(&seedValue, 1 + &seedValue);
}
}
std::minstd_rand engine(seedValue);
tournament::generator::MatchesConfiguration matchesConfiguration;
fileformats::trf::FileData fileData;
if (modelFile)
{
try
{
std::ifstream inputStream(inputFilename);
// Read the model tournament.
tournament::Tournament tournament;
try
{
tournament =
fileformats::trf::readFile(inputStream, false, &fileData);
}
catch (const fileformats::FileFormatException &exception)
{
std::cerr << "Error parsing file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
catch (fileformats::FileReaderException &exception)
{
std::cerr << "Error reading file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return FILE_ERROR;
}
// Verify that ratings are nonzero.
for (const tournament::Player &player : tournament.players)
{
if (player.isValid && !player.rating)
{
std::cerr << "Error processing file "
<< inputFilename
<< ": All players must have meaningful (nonzero) ratings."
<< std::endl;
return INVALID_REQUEST;
}
}
// Compute the configuration options for generating the matches.
matchesConfiguration =
tournament::generator
::MatchesConfiguration(std::move(tournament));
}
catch (const tournament::BuildLimitExceededException &exception)
{
std::cerr << "Error processing file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return LIMIT_EXCEEDED;
}
catch (const std::length_error &)
{
std::cerr
<< "Error processing file "
<< inputFilename
<< ": The build does not support tournaments this large."
<< std::endl;
return LIMIT_EXCEEDED;
}
catch (const std::bad_alloc &)
{
std::cerr
<< "Error processing file "
<< inputFilename
<< ": The program ran out of memory."
<< std::endl;
return LIMIT_EXCEEDED;
}
}
else
{
// Compute random configuration options.
tournament::generator::Configuration configuration(engine);
if (configurationFile)
{
try
{
std::ifstream inputStream(inputFilename);
// Override configuration options using a file.
fileformats::generatorconfiguration::readFile(
configuration,
inputStream);
}
catch (const fileformats::FileFormatException &exception)
{
std::cerr << "Error parsing configuration file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
catch (const fileformats::FileReaderException &exception)
{
std::cerr << "Error while reading configuration file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return FILE_ERROR;
}
catch (const tournament::BuildLimitExceededException &exception)
{
std::cerr << "Error processing configuration file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return LIMIT_EXCEEDED;
}
}
// Randomly generate the player ratings.
try
{
matchesConfiguration =
tournament::generator::MatchesConfiguration(
std::move(configuration),
engine);
}
catch (
const tournament::generator::BadConfigurationException &exception)
{
std::cerr << "Error while processing configuration file "
<< inputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
}
// Open the output file.
if (modelFile || configurationFile)
{
#ifdef FILESYSTEM_NS
try
{
#endif
relativizePath(outputFilename, inputFilename);
#ifdef FILESYSTEM_NS
}
catch (const FILESYSTEM_NS::filesystem_error &)
{
std::cerr
<< "Error extracting the directory of the input file."
<< std::endl;
return FILE_ERROR;
}
#endif
}
std::ofstream outputStream(outputFilename);
if (!outputStream.good())
{
std::cerr << "The output file ("
<< outputFilename
<< ") could not be opened."
<< std::endl;
return FILE_ERROR;
}
std::unique_ptr<std::ofstream> checklistStream;
if (checklist)
{
checklistStream =
openChecklist(
checklistFilename,
checklistCustomFilename,
outputFilename);
}
// Write the seed to the output file in advance so we can reproduce
// discovered bugs.
fileformats::trf::writeSeed(outputStream, seedValue);
int result = 0;
// Pair the tournament, randomly generating each match.
tournament::Tournament tournament;
try
{
tournament::generator
::generateTournament(
tournament,
std::move(matchesConfiguration),
swissSystem,
engine,
checklistStream.get());
}
catch (const swisssystems::NoValidPairingException &exception)
{
std::cerr << "Error generating "
<< outputFilename
<< ": "
<< exception.what()
<< std::endl;
result = NO_VALID_PAIRING;
}
catch (const swisssystems::UnapplicableFeatureException &exception)
{
std::cerr << "Error generating "
<< outputFilename
<< ": "
<< exception.what()
<< std::endl;
return INVALID_REQUEST;
}
closeChecklist(checklistStream.get(), checklistFilename);
// Output the generated tournament.
try
{
if (modelFile)
{
fileformats::trf
::writeFile(outputStream, tournament, std::move(fileData));
}
else
{
fileformats::trf::writeFile(outputStream, tournament);
}
}
catch (const fileformats::LimitExceededException &exception)
{
std::cerr << "Error writing tournament to "
<< outputFilename
<< ": "
<< exception.what()
<< std::endl;
return LIMIT_EXCEEDED;
}
// Check for errors.
outputStream.close();
if (!outputStream)
{
std::cerr << "Error while writing to "
<< outputFilename
<< '.'
<< std::endl;
return FILE_ERROR;
}
return result;
}
catch (const tournament::BuildLimitExceededException &exception)
{
std::cerr << "Error processing file "
<< outputFilename
<< ": "
<< exception.what()