-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
515 lines (420 loc) · 10.6 KB
/
cmd.c
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
#include <stdlib.h>
#include <unistd.h>
//#include <string.h>
#include "players.h"
#include "cmd.h"
#include "draw.h"
#include "nou.h"
#include "utils.h"
#define _SLAVE_IGNORE 0
const char* errmsg[] = {
[EINVALIDCMD] = "Invalid command",
[ENOSUCHCARDNUM] = "No such playable matching number",
[ENOSUCHCARDSUIT] = "No such playable matching suit",
[EMULCARDNUM] = "Multiple matches for number were found",
[EMULCARDSUIT] = "Multiple matches for suit were found",
[EMULCARD] = "Multiple matches for card were found",
[ENOSUCHCARDID] = "No such playable matching id",
[EMISSINGSUIT] = "Missing suit for special card",
[EILLEGAL] = "Command plays with illegal card",
[EMIXING] = "Wrong syntax. Tried to mix",
[EMULSUIT] = "Multiple card suits were found for command",
[EMULNUM] = "Multiple card numbers were found for command",
[ENOPREVCMD] = "No previous command to repeat",
[ENOLEGAL] = "No legal command found",
[EMULLEGAL] = "Multiple legal commands found",
[E3LEGAL] = "More than 3 legal cards were found",
};
const char* infomsg[] = {
[IFOUNDCARD] = "A legal card was found while running the `t'",
//[ISUITS] = "spades: `s', clubs: `c', hearts: `h', diamonds: `d'",
};
int MSGERRCODE = EOK, MSGINFOCODE = IOK;
static inline Number findcard_number (char buf[2]) {
short easy = *(short*) buf;
if (! easy) return NONUMBER;
else {
if (buf[0] == '1' && buf[1] == '0')
return _10;
switch (buf[0]) {
case 'A': return _A;
case 'K': return _K;
case 'Q': return _Q;
case 'J': return _J;
case 'B': return _B;
case 'C': return _C;
}
return (ATOI (buf[0]) - 1);
}
}
static inline Suit findcard_suit (char buf) {
switch (buf) {
case 's': return SPADES;
case 'c': return CLUBS;
case 'h': return HEARTS;
case 'd': return DIAMONDS;
default: return NOSUIT;
}
}
typedef struct {
CmdStat master;
bool slave;
uint target;
} CmdStatFindcard;
static CmdStatFindcard findcard (Player* p,
Number num,
Suit suit,
enum err* ecode) {
CmdStatFindcard ret = {0};
uint pcardi = p->cardi;
Card card;
// chose the part of the function to execute
if (num == NONUMBER) goto by_suit;
else if (suit == NOSUIT) goto by_num;
for (uint i = 0; i < pcardi; i++) {
card = index (deckr.deck, p->cards[i], deckr.cards);
if (card.number == num && card.suit == suit) {
MAYBESLAVE (ret, EMULCARD);
ret.target = (i+1);
}
}
if (! ret.slave) ECODESLAVE (ret, EILLEGAL);
goto done;
by_num:
for (uint i = 0; i < pcardi; i++) {
card = index(deckr.deck, p->cards[i], deckr.cards);
if (card.number == num) {
MAYBESLAVE (ret, EMULCARDNUM);
ret.target = (i+1);
}
}
if (! ret.slave) ECODESLAVE (ret, ENOSUCHCARDNUM);
goto done;
by_suit:
for (uint i = 0; i < pcardi; i++) {
card = index(deckr.deck, p->cards[i], deckr.cards);
if (card.suit == suit) {
MAYBESLAVE (ret, EMULCARDSUIT);
ret.target = (i+1);
}
}
if (! ret.slave) ECODESLAVE (ret, ENOSUCHCARDSUIT);
done:
return ret;
}
typedef struct {
CmdStat master;
enum {
_SLAVE_GOTO_DONE = 1, _SLAVE_RETURN
} slave;
} CmdStatEasy;
static CmdStatEasy cmdparse_easyones (char* buf, Cmd* lastcmd, Cmd* cmd, enum err* ecode) {
short easy = *(short*)buf;
CmdStatEasy ret = {0};
// NOTE: I'm not sure why `read' inverts the bytes so I just
// invert them back. If anyone encounters a bug with this,
// comment out the next 3 lines
short __little_endian = (easy & 0x00ff) << 8;
easy >>= 8;
easy |= __little_endian;
switch (easy) {
case CMDQUIT:
ret.master = CQUIT;
ret.slave = _SLAVE_RETURN;
break;
case CMDNONE:
if (lastcmd->ac.cmd == NOCMD) {
*ecode = ENOPREVCMD;
ret.master = CINVALID;
ret.slave = _SLAVE_RETURN;
}
else {
*cmd = *lastcmd;
ret.master = COK;
ret.slave = _SLAVE_GOTO_DONE;
}
break;
}
return ret;
}
typedef struct {
CmdStat master;
enum { _SLAVE_GOTO_INVALID_PREFIX = 1, } slave;
uint num;
} CmdStatNum;
static CmdStatNum cmdparse_number (char* buf, enum err* ecode) {
CmdStatNum ret = {0};
uint d = 0, id = 0, pos = 1;
uint i = 0;
char c = buf[i];
// `0' prefix
if (buf[i] == '0') {
goto invalid_prefix;
}
// dirty hack for `pos'
INC (c, buf, i);
// find out how many digits we have
ITER (c, buf, i, CMDBUFF) {
if (NUMBER (c)) {
pos *= 10, d++;
}
else {
break;
}
}
if (d > MAXCARDSLEN) {
invalid_prefix:
*ecode = ENOSUCHCARDID;
ret.master = CINVALID;
ret.slave = _SLAVE_GOTO_INVALID_PREFIX;
return ret;
}
for (uint i = 0; (i <= d) && (pos); i++) {
c = buf[i];
id += ATOI (c)*pos;
pos /= 10;
}
ret.num = id;
return ret;
}
typedef struct {
CmdStat master;
enum { _SLAVE_LEGAL = 1, } slave;
uint target;
} CmdStatLegal;
static CmdStatLegal cmdparse_legal (Player* p, enum err* ecode) {
CmdStatLegal ret = {0};
uint pcardi = p->cardi;
Card card;
for (uint i = 0; i < pcardi; i++) {
card = index (deckr.deck, p->cards[i], deckr.cards);
if (legal (card, *top)) {
MAYBESLAVE (ret, EMULLEGAL);
ret.target = (i+1);
}
}
if (ret.slave == _SLAVE_IGNORE)
*ecode = ENOLEGAL;
return ret;
}
static CmdStatLegal cmdparse_legalacc (Player* p, enum err* ecode) {
CmdStatLegal ret = {0};
uint pcardi = p->cardi;
Card card;
for (uint i = 0; i < pcardi; i++) {
card = index (deckr.deck, p->cards[i], deckr.cards);
// give the player a change to counteract
if (legal (card, *top)) {
ret.master = COK;
ret.slave = _SLAVE_LEGAL;
goto done;
}
}
ret.master = CACC;
ret.slave = _SLAVE_IGNORE;
done:
return ret;
}
static CmdStat cmdparse (Cmd* cmd, enum err* ecode) {
static Cmd lastcmd = {.ac = {.cmd = NOCMD}};
struct promise {
bool _play_num, _play_suit, _play_special;
bool __10;
bool __PAD__[4];
} promise = {0};
struct val {
char play_suit;
char* play_number;
} val = {0};
char _val_play_number[2] = {0,0};
val.play_number = _val_play_number;
char* cmdbuf = cmd->cmdstr;
uint i = 0;
char c = cmdbuf[i];
// look for the easy ones first
CmdStatEasy stateasy = cmdparse_easyones (cmdbuf, &lastcmd, cmd, ecode);
if (stateasy.slave != _SLAVE_IGNORE) {
switch (stateasy.slave) {
case _SLAVE_GOTO_DONE:
goto done;
case _SLAVE_RETURN:
return stateasy.master;
}
}
// prefixing <.>: take by amount
if (cmdbuf[i] == '.') {
INC (c, cmdbuf, i);
if (_DONE (c)) {
cmd->ac.cmd = TAKE;
cmd->ac.am = 1;
lastcmd = *cmd;
goto done;
}
else {
CmdStatNum statnum = cmdparse_number (cmdbuf+1, ecode);
// clear overshot
if (statnum.num > (deckr.cards - (deckr.played + deckr.playing))) {
ECODE (EINVALIDCMD);
}
if (statnum.slave == _SLAVE_IGNORE) {
cmd->ac.cmd = TAKE;
cmd->ac.am = statnum.num;
lastcmd = *cmd;
}
return statnum.master;
}
}
// prefixing <:>: ignore otherwise legal plays and do accumulative action
else if (cmdbuf[i] == ':') {
INC (c, cmdbuf, i);
if (! _DONE (c) || ! acc) {
ECODE (EINVALIDCMD);
}
else {
// NOTE: so that `:' still updates the display correclty
return CACCDRAW;
}
}
// prefixing <,>: play by card id
else if (cmdbuf[i] == ',') {
INC (c, cmdbuf, i);
if (_DONE (c)) {
cmd->ac.cmd = PLAY;
cmd->ac.target = 1;
lastcmd = *cmd;
goto done;
}
else {
CmdStatNum statnum = cmdparse_number (cmdbuf+1, ecode);
// clear overshot
if (statnum.num > cmd->p->cardi)
ECODE (EINVALIDCMD);
if (statnum.slave == _SLAVE_IGNORE) {
cmd->ac.cmd = PLAY;
cmd->ac.target = statnum.num;
lastcmd = *cmd;
}
else goto statnum_master;
Card card = index (deckr.deck,
cmd->p->cards[statnum.num-1],
deckr.cards);
if (card.suit == SPECIAL) {
c = numpm[card.number][0];
goto _special;
}
else statnum_master: return statnum.master;
}
}
// prefixing < >: play the only legal move
else if (cmdbuf[i] == ' ') {
INC (c, cmdbuf, i);
CmdStatLegal statlegal = cmdparse_legal (cmd->p, ecode);
if (statlegal.slave == _SLAVE_LEGAL) {
cmd->ac.cmd = PLAY;
cmd->ac.target = statlegal.target;
lastcmd = *cmd;
}
else goto statlegal_master;
Card card = index (deckr.deck,
cmd->p->cards[statlegal.target],
deckr.cards);
if (card.suit == SPECIAL) {
c = numpm[card.number][0];
goto _special;
}
else statlegal_master: return statlegal.master;
}
cmd->ac.cmd = PLAY;
// prefixing <>: play by card attribute
ITER (c, cmdbuf, i, CMDBUFF) {
if (_DONE (c)) {
break;
}
if (_SUIT (c)) {
// `1' followed by suit: `1' is not a card number
if (promise.__10) ECODE (ENOSUCHCARDNUM);
MAYBE (promise._play_suit, EMULSUIT);
val.play_suit = c;
}
else if (_NUMBER (c)) {
// <special> is not followed by a number
if (promise._play_special) ECODE (EMISSINGSUIT);
if (c == '0' && ! promise.__10) ECODE (ENOSUCHCARDNUM); // `0'-prefix
else if (c == '1') {
MAYBE (promise.__10, ENOSUCHCARDNUM);
val.play_number[0] = c;
}
else if (promise.__10) {
if (c != '0') ECODE (ENOSUCHCARDNUM);
else promise.__10 = 0;
goto maybe_num;
}
else {
maybe_num:
MAYBE (promise._play_num, EMULCARDNUM);
*(val.play_number + (val.play_number[0] != '\0')) = c;
}
}
else if (_SPECIAL (c)) _special: {
// <special> precedes all
if (promise._play_suit | promise._play_num | promise._play_special)
ECODE (EMULNUM);
else {
MAYBE (promise._play_special, EMULSUIT);
*(val.play_number + (val.play_number[0] != '\0')) = c;
}
}
else ECODE (EINVALIDCMD);
}
if (promise._play_special && ! promise._play_suit) ECODE (EMISSINGSUIT);
CmdStatFindcard statfindcard = findcard (cmd->p,
findcard_number (val.play_number),
(promise._play_special?
(csuit = findcard_suit (val.play_suit), NOSUIT):
findcard_suit (val.play_suit)),
ecode);
if (statfindcard.slave != _SLAVE_IGNORE) {
cmd->ac.target = statfindcard.target;
goto done;
}
else return statfindcard.master;
done:
return COK;
}
void msgsend_err (enum err ecode) {
error_display (errmsg[ecode]);
}
void msgsend_info (enum info icode) {
return; // TODO: <-
}
void cmdread (Cmd* cmd) {
enum err ecode;
char* cmdbuf = cmd->cmdstr;
uint _read;
// send accumalative callback only if there are no cards available to be played
if (acc) {
CmdStatLegal statlegalacc = cmdparse_legalacc (cmd->p, &ecode);
if (statlegalacc.slave == _SLAVE_IGNORE) {
// TODO: send an info message
cmd->status = statlegalacc.master;
return;
}
}
parse:
ecode = EOK;
_read = read (0, cmdbuf, CMDBUFF);
// handle C-d (aka '\0' terminated read callbacks)
if (! _read) {
goto parse;
}
else if (cmdbuf[_read-1] != '\n') {
fix_display (); // TODO: <-
}
cmdbuf[_read] = '\0';
cmd->status = cmdparse (cmd, &ecode);
// something went wrong
if (ecode != EOK) {
error_display (errmsg[ecode]);
goto parse;
}
}