-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSGFGenerator.cpp
executable file
·152 lines (127 loc) · 4.61 KB
/
SGFGenerator.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
/**
* @file SGFGenerator.cpp
* @ingroup Go-CamRecorder
* @author Dominique Vaufreydaz, personnal project
* @copyright All right reserved.
*/
#include "SGFGenerator.h"
/**
* @brief Open a new SGF file in a Folder. File name is generated with date, time and a random number to autorized multiple instance to run at the same time
on the same computer.
* @param Folder [in] Folder name to store SGF file
* @param Event [int] Event name (i.e. competition name)
* @param Event [int] Event name (i.e. competition name)
* @param Event [int] Event name (i.e. competition name)
* @param Round [int] Round turn
* @param Rule [int] Used rules (japanese, ...)
* @param Komi [int] Komi set.
* @param Date [int] Date as string
* @param Time [int] Time as string
* @param BlackPlayerName [int] Black player name
* @param WhitePlayerName [int] White player name
* @return true if SGF could be opened.
*/
bool SGFGenerator::Open( Omiscid::SimpleString Folder, Omiscid::SimpleString& Event, Omiscid::SimpleString& Round, Omiscid::SimpleString& Rule, Omiscid::SimpleString& Komi,
Omiscid::SimpleString& Date, Omiscid::SimpleString& Time, Omiscid::SimpleString& BlackPlayerName, Omiscid::SimpleString& WhitePlayerName )
{
Omiscid::SimpleString FileName;
// Generate filename from date, time and random numbers in order to prevent
// games with same file name
FileName += Date + "." + Time + "_";
// Generate 8 ramdom chararacters to prevent same name from different computer
for ( int i = 0; i < 10; i++ )
{
FileName += (char)('0' + (char)(Omiscid::random()%10));
}
FileName += ".sgf";
// Generate local file name, into Folder
// First, add '/' if mandatory
SGFFileName = Folder;
if ( SGFFileName[SGFFileName.GetLength()-1] != '/' )
{
SGFFileName += '/';
}
SGFFileName += FileName;
// Open localfile name
fout = fopen( SGFFileName.GetStr(), "wb" );
if ( fout == nullptr )
{
return false;
}
// Set no buffer to file, security in case of crash, state is in the file...
// At the end, we will need to rewrite the file with the result, if any...
setbuf( fout, NULL );
// Generate SGF Header
FileContent = GenerateSGFHeader( NumCells, FileName, Event, Round, Rule, Komi, Date, BlackPlayerName, WhitePlayerName );
// Write header of the file, need to check variation code for ST
fprintf( fout, "%s\n", FileContent.GetStr() );
// Init upload (if upload server and URL are not define, this call is effectless)
OnlineUploader.InitDistantSGF( NumCells, FileName, FileContent );
// FileContent must now have a '\n', maybe be done in a better way
FileContent += '\n';
return true;
}
/**
* @brief Open a new SGF file in a Folder. File name is generated with date, time and a random number to autorized multiple instance to run at the same time
on the same computer.
* @param Color [in] Color of move (Black/White)
* @param Col [int] Col number
* @param Row [int] Row number
* @param Comment [int] Comment to as to current move.
* @return true if move has been added.
*/
bool SGFGenerator::AddMove( int Color, int Col, int Row, Omiscid::SimpleString Comment /* = "" */ )
{
if ( fout == nullptr || Col >= NumCells || Row >= NumCells )
{
return false;
}
Omiscid::SimpleString CurrentMove = ";";
CurrentMove += (Color == StoneDetector::Black ? 'B' : 'W');
CurrentMove += '[';
CurrentMove += (char)('a'+Col);
CurrentMove += (char)('a'+Row);
CurrentMove += ']';
// Add comment if any
if ( Comment.GetLength() != 0 )
{
// Escape en tag in Comment, Comment is a local copy
CurrentMove += "C[" + Comment + "]";
}
// Add current move (and comment) to the file and later to the file content
// Print current move in file
fprintf( fout, "%s\n", CurrentMove.GetStr() );
// Add move to online SGF (if configured)
OnlineUploader.AddMove( CurrentMove );
// Add current move to the FileContent
FileContent += CurrentMove + "\n";
return true;
}
/**
* @brief Close SGF file. Add Result if any.
* @param Result [in] Result of the game.
*/
void SGFGenerator::Close( Omiscid::SimpleString& Result )
{
if ( fout != nullptr )
{
fprintf( fout, ")\n" );
fclose( fout );
fout = nullptr;
}
if ( Result.IsEmpty() == false )
{
// Reopen localfile name
fout = fopen( SGFFileName.GetStr(), "wb" );
if ( fout == nullptr )
{
fprintf( stderr, "Unable to store final game SGF with result\n" );
return;
}
FileContent.ReplaceFirst( "RE[?]", "RE[" + Result + "]" );
fprintf( fout, "%s)\n", FileContent.GetStr() );
fclose( fout );
}
// End distant SGF (if configured)
OnlineUploader.EndDistantSGF( Result );
}