-
Notifications
You must be signed in to change notification settings - Fork 3
/
Seek.m
286 lines (252 loc) · 8.05 KB
/
Seek.m
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
/*
$Id$
Copyright 2006 Klaus Thul (klaus.thul@mac.com)
This file is part of Kibitz.
Kibitz is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
Kibitz 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 Kibitz; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "Seek.h"
NSString *StyleNames[] = {
@"Normal", @"Crazyhouse", @"Suicide", @"Wild"
};
@implementation Seek
/**
* Create seek object from FICS-style seek adversitement string
* Note, this is the "seekinfo" version documented at
* http://www.freechess.org/Help/HelpFiles/iv_seekinfo.html
*
* @param seekInfo Seek advertisement string
*/
+ (Seek *) seekFromSeekInfo: (const char *) seekInfo
{
Seek *s = [[Seek alloc] init];
[s autorelease];
NSString *seekInfoNS = @(seekInfo);
NSArray *parts = [seekInfoNS componentsSeparatedByString:@" "];
for (NSString *part in parts) {
NSArray *keyVal = [part componentsSeparatedByString:@"="];
if (keyVal.count != 2) {
continue;
}
NSString *key = keyVal[0];
NSString *value = keyVal[1];
if ([key isEqualToString:@"w"]) {
s->nameFrom = value;
[s->nameFrom retain];
} else if ([key isEqualToString:@"t"]) {
s->timeStart = value.intValue;
} else if ([key isEqualToString:@"i"]) {
s->timeIncrement = value.intValue;
} else if ([key isEqualToString:@"t"]) {
if ([value isEqualToString:@"r"])
s->rated = YES;
else if ([value isEqualToString:@"u"])
s->rated = NO;
} else if ([key isEqualToString:@"c"]) {
if ([value isEqualToString:@"?"])
s->wantsColor = WANTS_BOTH;
else if ([value isEqualToString:@"W"])
s->wantsColor = WANTS_WHITE;
else if ([value isEqualToString:@"B"])
s->wantsColor = WANTS_BLACK;
} else if ([key isEqualToString:@"a"]) {
if ([value isEqualToString:@"t"])
s->automatic = YES;
else if ([value isEqualToString:@"f"])
s->automatic = NO;
} else if ([key isEqualToString:@"f"]) {
if ([value isEqualToString:@"t"])
s->formulaChecked = YES;
else if ([value isEqualToString:@"f"])
s->formulaChecked = NO;
} else if ([key isEqualToString:@"ti"]) {
s->title = strtol(value.UTF8String, NULL, 16);
} else if ([key isEqualToString:@"rt"]) {
s->ratingValue = value.intValue;
} else if ([key isEqualToString:@"tp"]) {
s->typeOfPlay = value;
[s->typeOfPlay retain];
} else if ([key isEqualToString:@"rr"]) {
// TODO: implement Ratings range
}
}
return s;
}
- (Seek *) init
{
if ((self = [super init]) != nil) {
timeStart = 5;
timeIncrement = 0;
rated = YES;
wantsColor = WANTS_BOTH;
playStyle = STYLE_NORMAL;
automatic = YES;
ratingRangeLow = 0;
ratingRangeHigh = 9999;
formulaChecked = NO;
}
return self;
}
+ (Seek *) defaultSeek
{
return [[[Seek alloc] init] autorelease];
}
- (void) dealloc
{
[nameFrom release];
[typeOfPlay release];
[wildStyle release];
[super dealloc];
}
- (int) ratingValue
{
return ratingValue;
}
- (NSString *) nameFrom
{
return [[nameFrom retain] autorelease];
}
- (NSString *) typeOfPlay
{
return [[typeOfPlay retain] autorelease];
}
- (void) setWildStyle: (NSString *) style
{
[self willChangeValueForKey: @"description"];
[wildStyle release];
wildStyle = [style copy];
[self didChangeValueForKey: @"description"];
}
- (NSString *) wildStyle
{
return (wildStyle == nil) ? @"" : wildStyle;
}
- (BOOL) isWild
{
return playStyle == STYLE_WILD;
}
- (NSString *) description
{
NSString *style;
if (playStyle == STYLE_WILD)
style = [NSString stringWithFormat: @"Wild %@", [self wildStyle]];
else
style = StyleNames[playStyle];
return [NSString stringWithFormat: @"%@: %d+%d, %s, %s%s (%d-%d%s)", style,
timeStart, timeIncrement,
(rated) ? "r" : "u", (automatic) ? "a" : "m",
(wantsColor == WANTS_BOTH) ? "" : ((wantsColor == WANTS_WHITE) ? ", w" : ", b"),
ratingRangeLow, ratingRangeHigh, (formulaChecked) ? ", f" : ""];
}
- (void) setTimeStart: (int) t
{
[self willChangeValueForKey: @"description"];
timeStart = t;
[self didChangeValueForKey: @"description"];
}
- (void) setTimeIncrement: (int) t
{
[self willChangeValueForKey: @"description"];
timeIncrement = t;
[self didChangeValueForKey: @"description"];
}
- (void) setRated: (bool) r
{
[self willChangeValueForKey: @"description"];
rated = r;
[self didChangeValueForKey: @"description"];
}
- (void) setWantsColor: (enum WantsColor) c
{
[self willChangeValueForKey: @"description"];
wantsColor = c;
[self didChangeValueForKey: @"description"];
}
- (void) setAutomatic: (bool) a
{
[self willChangeValueForKey: @"description"];
automatic = a;
[self didChangeValueForKey: @"description"];
}
- (void) setFormulaChecked: (bool) c
{
[self willChangeValueForKey: @"description"];
formulaChecked = c;
[self didChangeValueForKey: @"description"];
}
- (void) setRatingRangeLow: (int) r
{
[self willChangeValueForKey: @"description"];
ratingRangeLow = r;
[self didChangeValueForKey: @"description"];
}
- (void) setRatingRangeHigh: (int) r
{
[self willChangeValueForKey: @"description"];
ratingRangeHigh = r;
[self didChangeValueForKey: @"description"];
}
- (void) setPlayStyle: (enum PlayStyle) s
{
[self willChangeValueForKey: @"isWild"];
[self willChangeValueForKey: @"description"];
playStyle = s;
[self didChangeValueForKey: @"isWild"];
[self didChangeValueForKey: @"description"];
}
- (NSString *) seekCommand
{
NSString *style;
switch (playStyle) {
case STYLE_WILD:
style = [NSString stringWithFormat: @"Wild %@ ", [self wildStyle]];
break;
case STYLE_CRAZYHOUSE:
style = @"crazyhouse ";
break;
case STYLE_SUICIDE:
style = @"suicide ";
break;
default:
style = @"";
}
return [NSString stringWithFormat: @"seek %d %d %s %s %@ %s %s %d-%d",
timeStart, timeIncrement, rated ? "rated" : "unrated",
(wantsColor == WANTS_BOTH) ? "" : ((wantsColor == WANTS_WHITE) ? "white" : "black"),
style, automatic ? "auto" : "manual", formulaChecked ? "formula" : "",
ratingRangeLow, ratingRangeHigh];
}
- (instancetype) initWithCoder: (NSCoder *) coder
{
if ((self = [super init]) != nil) {
[self setTimeStart: [coder decodeIntForKey: @"timeStart"]];
[self setTimeIncrement: [coder decodeIntForKey: @"timeIncrement"]];
[self setRated: [coder decodeBoolForKey: @"rated"]];
[self setWantsColor: [coder decodeIntForKey: @"wantsColor"]];
[self setAutomatic: [coder decodeBoolForKey: @"automatic"]];
[self setFormulaChecked: [coder decodeBoolForKey: @"formulaChecked"]];
[self setRatingRangeLow: [coder decodeIntForKey: @"ratingRangeLow"]];
[self setRatingRangeHigh: [coder decodeIntForKey: @"ratingRangeHigh"]];
[self setPlayStyle: [coder decodeIntForKey: @"playStyle"]];
[self setWildStyle: [coder decodeObjectForKey: @"wildStyle"]];
}
return self;
}
- (void) encodeWithCoder: (NSCoder *) coder
{
[coder encodeInt: timeStart forKey: @"timeStart"];
[coder encodeInt: timeIncrement forKey: @"timeIncrement"];
[coder encodeBool: rated forKey: @"rated"];
[coder encodeInt: wantsColor forKey: @"wantsColor"];
[coder encodeBool: automatic forKey: @"automatic"];
[coder encodeBool: formulaChecked forKey: @"formulaChecked"];
[coder encodeInt: ratingRangeLow forKey: @"ratingRangeLow"];
[coder encodeInt: ratingRangeHigh forKey: @"ratingRangeHigh"];
[coder encodeInt: playStyle forKey: @"playStyle"];
[coder encodeObject: wildStyle forKey: @"wildStyle"];
}
@end