-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
654 lines (548 loc) · 16.6 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
#include "door.h"
#include "yaml-cpp/yaml.h"
#include <chrono> // chrono::system_clock
#include <ctime> // localtime
#include <iomanip> // put_time
#include <iostream>
#include <random>
#include <string>
#include "db.h"
#include "deck.h"
#include "play.h"
#include "scores.h"
#include "starfield.h"
#include "utils.h"
#include "version.h"
#include <algorithm> // transform
// configuration here -- access via extern
YAML::Node config;
door::ANSIColor stringToANSIColor(std::string colorCode);
std::function<std::ofstream &(void)> get_logger;
std::function<void(void)> cls_display_starfield;
std::function<int(void)> press_a_key;
std::string return_current_time_and_date() {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
// ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
return ss.str();
}
int press_any_key(door::Door &door) {
door::ANSIColor green{door::COLOR::GREEN, door::ATTR::BOLD};
door::ANSIColor blue{door::COLOR::BLUE, door::ATTR::BOLD};
door::ANSIColor yellow{door::COLOR::YELLOW, door::ATTR::BOLD};
door::ANSIColor any_caps = green;
door::ANSIColor any_lower = yellow;
door::renderFunction any_color =
[&any_caps, &any_lower](const std::string &txt) -> door::Render {
door::Render r(txt);
// door::ANSIColor blue(door::COLOR::GREEN, door::ATTR::BOLD);
// door::ANSIColor cyan(door::COLOR::YELLOW, door::ATTR::BOLD);
for (char const &c : txt) {
if (isupper(c))
r.append(any_caps);
else
r.append(any_lower);
}
return r;
};
static std::default_random_engine generator;
static std::uniform_int_distribution<int> distribution(0, 1);
int use_set = distribution(generator);
std::string text = "Press a key to continue...";
door << door::reset;
any_color(text).output(door);
// << text;
int r;
int t = 0;
// alternate animation
#define ALT_ANIMATION
#ifdef ALT_ANIMATION
std::vector<std::pair<int, int>> words = find_words(text);
std::string work_text = text;
int text_length = text.size();
// current word
unsigned int word = 0;
std::string current_word = text.substr(words[word].first, words[word].second);
unsigned int wpos = 0;
int ms_sleep = 0;
int sleep_ms = 250;
t = 0;
while ((r = door.sleep_ms_key(sleep_ms)) == TIMEOUT) {
ms_sleep += sleep_ms;
if (ms_sleep > 1000) {
ms_sleep -= 1000;
t++;
if (t >= door.inactivity) {
// restore text
door << std::string(text_length, '\b');
any_color(text).output(door);
// << text;
door << door::nl;
return TIMEOUT;
}
}
++wpos;
if (wpos == current_word.size()) {
++word;
work_text = text;
wpos = 0;
if (word == words.size()) {
// we've reached the end
word = 0;
if (any_caps == green)
any_caps = blue;
else
any_caps = green;
door << std::string(text_length, '\b');
any_color(work_text).output(door);
current_word = text.substr(words[word].first, words[word].second);
continue;
}
current_word = text.substr(words[word].first, words[word].second);
}
int c = current_word[wpos];
while (!std::islower(c)) {
// It is not lower case
wpos++;
if (wpos == current_word.size()) {
// Ok, this word is done.
wpos = 0;
word++;
work_text = text;
if (word == words.size()) {
word = 0;
}
current_word = text.substr(words[word].first, words[word].second);
wpos = 0;
}
c = current_word[wpos];
}
// Ok, I found something that's lower case.
work_text[words[word].first + wpos] = std::toupper(c);
// std::toupper(words[word].first + wpos);
door << std::string(text_length, '\b');
any_color(work_text).output(door);
// << work_text;
}
// ok, restore the original string
door << std::string(text_length, '\b');
any_color(text).output(door);
//<< text;
return r;
#endif
#ifdef ALT_ANIMATION_2
// This can be cleaned up to be simpler -- and do the same effect. We don't
// need the words here, we're not really using them.
std::vector<std::pair<int, int>> words = find_words(text);
std::string work_text = text;
int text_length = text.size();
// current word
unsigned int word = 0;
std::string current_word = text.substr(words[word].first, words[word].second);
unsigned int wpos = 0;
// DOES NOT HONOR door.inactivity (AT THIS TIME)
while ((r = door.sleep_ms_key(200)) == TIMEOUT) {
++wpos;
if (wpos == current_word.size()) {
++word;
wpos = 0;
if (word == words.size())
word = 0;
current_word = text.substr(words[word].first, words[word].second);
}
int c = current_word[wpos];
while (!std::islower(c)) {
// It is not lower case
wpos++;
if (wpos == current_word.size()) {
// Ok, this word is done.
wpos = 0;
word++;
if (word == words.size())
word = 0;
current_word = text.substr(words[word].first, words[word].second);
wpos = 0;
}
c = current_word[wpos];
}
// Ok, I found something that's lower case.
work_text = text;
work_text[words[word].first + wpos] = std::toupper(c);
// std::toupper(words[word].first + wpos);
door << std::string(text_length, '\b') << work_text;
}
// ok, restore the original string
door << std::string(text_length, '\b') << text;
return r;
#endif
// animation spinner that reverses itself. /-\| and blocks.
t = 0;
int loop = 0;
const char *loop_chars[3][4] = {{"/", "-", "\\", "|"},
{"\xde", "\xdc", "\xdd", "\xdf"},
{"\u2590", "\u2584", "\u258c", "\u2580"}};
std::array<int, 11> sleeps = {1000, 700, 500, 300, 200, 200,
200, 300, 500, 700, 1000};
// start out at
int sleep_number = 5;
bool forward = true;
if (door::unicode) {
if (use_set == 1)
use_set = 2;
}
door << loop_chars[use_set][loop % 4];
ms_sleep = 0;
bool check_speed = false;
while ((r = door.sleep_ms_key(sleeps[sleep_number])) == TIMEOUT) {
ms_sleep += sleeps[sleep_number];
if (ms_sleep > 1000) {
ms_sleep -= 1000;
t++;
if (t >= door.inactivity) {
door << "\b \b";
door << door::nl;
return TIMEOUT;
}
}
if (forward) {
loop++;
if (loop == 4) {
loop = 0;
check_speed = true;
}
} else {
--loop;
if (loop < 0) {
loop = 3;
check_speed = true;
}
}
if (check_speed) {
sleep_number++;
if (sleep_number >= (int)sleeps.size()) {
sleep_number = 0;
forward = !forward;
}
check_speed = false;
}
door << "\b" << loop_chars[use_set][loop];
}
door << "\b \b";
door << door::nl;
return r;
}
// This does not seem to be working. I keep getting zero.
int opt_from_string(std::string colorCode) {
for (std::size_t pos = 0; pos != deck_colors.size(); ++pos) {
// if (caseInsensitiveStringCompare(colorCode, deck_colors[pos]) == 0) {
if (iequals(colorCode, deck_colors[pos])) {
return pos;
}
}
return 0;
}
int configure(door::Door &door, DBData &db) {
// pre-warm the parser
find_words(std::string(""));
auto menu = make_config_menu();
int r = 0;
bool save_deckcolor = false;
const char *deckcolor = "DeckColor";
std::string newColor;
while (r >= 0) {
if (save_deckcolor) {
door << menu;
db.setSetting(deckcolor, newColor);
save_deckcolor = false;
}
if (cls_display_starfield)
cls_display_starfield();
else
door << door::reset << door::cls;
r = menu.choose(door);
if (r > 0) {
door << door::reset << door::cls;
char c = menu.which(r - 1);
if (c == 'D') {
// Ok, deck colors
// get default
std::string currentDefault = db.getSetting(deckcolor, "ALL");
int currentOpt = opt_from_string(currentDefault);
door << door::reset << door::cls;
auto deck = make_deck_menu();
deck.defaultSelection(currentOpt);
if (cls_display_starfield)
cls_display_starfield();
else
door << door::reset << door::cls;
int newOpt = deck.choose(door);
door << door::reset << door::cls;
if (newOpt >= 0) {
newOpt--;
newColor = stringFromColorOptions(newOpt);
if (newOpt != currentOpt) {
door.log() << deckcolor << " was " << currentDefault << ", "
<< currentOpt << ". Now " << newColor << ", " << newOpt
<< std::endl;
save_deckcolor = true;
}
}
}
if (c == 'V') {
// view settings -- Sysop Configuration
if (cls_display_starfield)
cls_display_starfield();
else
door << door::reset << door::cls;
door::Panel config_panel = make_sysop_config();
// config_panel.set(1, 1);
door << config_panel << door::reset << door::nl;
r = press_a_key();
if (r < 0)
return r;
door << door::reset << door::cls;
}
if (c == 'Q') {
return r;
}
}
}
return r;
}
int main(int argc, char *argv[]) {
door::Door door("space-ace", argc, argv);
// store the door log so we can easily access it.
get_logger = [&door]() -> ofstream & { return door.log(); };
press_a_key = [&door]() -> int { return press_any_key(door); };
std::random_device rd;
std::mt19937 rng(rd());
Starfield mainfield(door, rng);
cls_display_starfield = [&mainfield]() -> void { mainfield.display(); };
/*
AnimatedStarfield amazing(door, rng);
amazing.display();
int res = -1;
while (res == -1) {
res = door.sleep_ms_key(250);
amazing.animate();
}
*/
DBData spacedb;
spacedb.setUser(door.username);
if (file_exists("space-ace.yaml")) {
config = YAML::LoadFile("space-ace.yaml");
}
bool update_config = false;
// populate with "good" defaults
if (!config["hands_per_day"]) {
config["hands_per_day"] = 3;
update_config = true;
}
if (!config["date_format"]) {
config["date_format"] = "%B %d"; // Month day or "%b %d,%Y" Mon,d YYYY
update_config = true;
}
if (!config["date_score"]) {
config["date_score"] = "%m/%d/%Y"; // or "%Y/%0m/%0d";
update_config = true;
}
if (!config["makeup_per_day"]) {
config["makeup_per_day"] = 5;
update_config = true;
}
if (!config["play_days_ahead"]) {
config["play_days_ahead"] = 2;
update_config = true;
}
if (!config["date_monthly"]) {
config["date_monthly"] = "%B %Y";
update_config = true;
}
if (!config["_seed"]) {
// pick numbers to use for seed
std::string seeds;
seeds += std::to_string(rng());
seeds += ",";
seeds += std::to_string(rng());
seeds += ",";
seeds += std::to_string(rng());
config["_seed"] = seeds;
update_config = true;
}
// save configuration -- something was updated
if (update_config) {
std::ofstream fout("space-ace.yaml");
fout << config << std::endl;
}
// retrieve lastcall
time_t last_call = std::stol(spacedb.getSetting("LastCall", "0"));
std::chrono::_V2::system_clock::time_point now =
std::chrono::system_clock::now();
// store now as lastcall
time_t now_t = std::chrono::system_clock::to_time_t(now);
spacedb.setSetting("LastCall", std::to_string(now_t));
// run maint
{
std::chrono::_V2::system_clock::time_point maint_date = now;
firstOfMonthDate(maint_date);
if (spacedb.expireScores(
std::chrono::system_clock::to_time_t(maint_date))) {
if (get_logger)
get_logger() << "maint completed" << std::endl;
door << "Thanks for waiting..." << door::nl;
}
}
// Have they used this door before?
if (last_call != 0) {
door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
<< "Welcome Back!" << door::nl;
auto nowClock = std::chrono::system_clock::from_time_t(now_t);
auto lastClock = std::chrono::system_clock::from_time_t(last_call);
auto delta = nowClock - lastClock;
// int days = chrono::duration_cast<chrono::days>(delta).count(); //
// c++ 20
int hours = chrono::duration_cast<chrono::hours>(delta).count();
int days = hours / 24;
int minutes = chrono::duration_cast<chrono::minutes>(delta).count();
int secs = chrono::duration_cast<chrono::seconds>(delta).count();
if (days > 1) {
door << door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD)
<< "It's been " << days << " days since you last played."
<< door::nl;
} else {
if (hours > 1) {
door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << hours
<< " hours since you last played." << door::nl;
} else {
if (minutes > 1) {
door << door::ANSIColor(door::COLOR::CYAN) << "It's been " << minutes
<< " minutes since you last played." << door::nl;
} else {
door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD)
<< "It's been " << secs << " seconds since you last played."
<< door::nl;
door << "It's like you never left." << door::nl;
}
}
}
if (press_a_key() < 0)
return 0;
}
int mx, my; // Max screen width/height
if (door.width == 0) {
// screen detect failed, use sensible defaults
door.width = mx = 80;
door.height = my = 23;
} else {
mx = door.width;
my = door.height;
}
// We assume here that the width and height aren't something crazy like
// 10x15. :P (or 24x923!)
display_starfield_space_ace(door, rng);
// for testing inactivity timeout
// door.inactivity = 10;
door::Panel timeout = make_timeout(mx, my);
door::Menu m = make_main_menu();
door::Panel about = make_about(); // 8 lines
door::Panel help = make_help();
// center the about box
about.set((mx - 60) / 2, (my - 9) / 2);
// center the help box
help.set((mx - 60) / 2, (my - 15) / 2);
/*
// animated starfield with about panel
AnimatedStarfield amazing(door, rng);
checkVisibleFunction about_visible = [&about](int x, int y) -> bool {
int px, py;
about.get(px, py);
int pw = about.getWidth();
int ph = about.getHeight();
if ((x >= px) and (x <= (px + pw + 1)) and (y >= py) and (y <= py + ph))
return false;
return true;
};
amazing.setVisible(about_visible);
amazing.regenerate();
amazing.display();
door << about << door::reset;
int res = -1;
while (res == -1) {
res = door.sleep_ms_key(250);
amazing.animate();
}
*/
PlayCards pc(door, spacedb, rng);
Starfield menu_stars(door, rng);
int r = 0;
while ((r >= 0) and (r != 6)) {
// starfield + menu ?
// display_starfield(door, rng);
menu_stars.display();
r = m.choose(door);
// need to reset the colors. (whoops!)
door << door::reset << door::cls; // door::nl;
// OK! The screen is blank at this point!
switch (r) {
case 1: // play game
{
// PlayCards pc(door, spacedb, rng);
r = pc.play();
// r = play_cards(door, spacedb, rng);
}; break;
case 2: // view scores
// door << door::cls;
{
Scores score(door, spacedb, menu_stars);
score.display_scores(door);
}
r = press_a_key();
break;
case 3: // configure
r = configure(door, spacedb);
// r = press_a_key();
break;
case 4: // help
// display_starfield(door, rng);
menu_stars.display();
door << help << door::nl;
r = press_a_key();
break;
case 5: // about
// display_starfield(door, rng);
menu_stars.display();
door << about << door::nl;
r = press_a_key();
break;
case 6: // quit
break;
}
}
if (r < 0) {
// DOOR_ERROR:
if (r == TIMEOUT) {
door.log() << "TIMEOUT" << std::endl;
door << timeout << door::reset << door::nl << door::nl;
} else {
if (r == OUTOFTIME) {
door.log() << "OUTTA TIME" << std::endl;
door::Panel notime = make_notime(mx, my);
door << notime << door::reset << door::nl;
}
}
return 0;
}
door << door::nl;
// door << door::reset << door::cls;
// display_starfield(door, rng);
mainfield.display();
door << m << door::reset << door::nl;
// Normal DOOR exit goes here...
door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
press_a_key = nullptr;
get_logger = nullptr;
cls_display_starfield = nullptr;
return 0;
}