aboutsummaryrefslogtreecommitdiff
path: root/src/c/event.c
blob: 36b05290b716da7f9dde2cef69cce0b7336349db (plain)
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
/*
 * Copyright (C) 2026 Reiner Herrmann
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "event.h"

#define TIME2MIN(hh,mm) (hh * 60 + mm)

#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))

static const struct Gw2Event event_list[] = {
	/* Core Tyria */
	{
		.name = "Admiral Taidha Covington",
		.utc_offset = TIME2MIN(00,00),
		.category = CORE,
		.location = "Bloodtide Coast",
		.schedule_hours = 3,
	},
	{
		.name = "Svanir Shaman Chief",
		.utc_offset = TIME2MIN(00,15),
		.category = CORE,
		.location = "Wayfarer Foothills",
		.schedule_hours = 2,
	},
	{
		.name = "Megadestroyer",
		.utc_offset = TIME2MIN(00,30),
		.category = CORE,
		.location = "Mount Maelstrom",
		.schedule_hours = 3,
	},
	{
		.name = "Fire Elemental",
		.utc_offset = TIME2MIN(00,45),
		.category = CORE,
		.location = "Metrica Province",
		.schedule_hours = 2,
	},
	{
		.name = "The Shatterer",
		.utc_offset = TIME2MIN(01,00),
		.category = CORE,
		.location = "Blazeridge Steppes",
		.schedule_hours = 3,
	},
	{
		.name = "Great Jungle Worm",
		.utc_offset = TIME2MIN(01,15),
		.category = CORE,
		.location = "Caledon Forest",
		.schedule_hours = 2,
	},
	{
		.name = "Modnir Ulgoth",
		.utc_offset = TIME2MIN(01,30),
		.category = CORE,
		.location = "Harathi Hinterlands",
		.schedule_hours = 3,
	},
	{
		.name = "Shadow Behemoth",
		.utc_offset = TIME2MIN(01,45),
		.category = CORE,
		.location = "Queensdale",
		.schedule_hours = 2,
	},
	{
		.name = "Golem Mark II",
		.utc_offset = TIME2MIN(02,00),
		.category = CORE,
		.location = "Mount Maelstrom",
		.schedule_hours = 3,
	},
	{
		.name = "Claw of Jormag",
		.utc_offset = TIME2MIN(02,30),
		.category = CORE,
		.location = "Frostgorge Sound",
		.schedule_hours = 3,
	},
	{
		.name = "Ley-Line Anomaly",
		.utc_offset = TIME2MIN(00,20),
		.category = CORE,
		.location = "Timberline Falls",
		.schedule_hours = 6,
	},
	{
		.name = "Ley-Line Anomaly",
		.utc_offset = TIME2MIN(02,20),
		.category = CORE,
		.location = "Iron Marches",
		.schedule_hours = 6,
	},
	{
		.name = "Ley-Line Anomaly",
		.utc_offset = TIME2MIN(04,20),
		.category = CORE,
		.location = "Gendarran Fields",
		.schedule_hours = 6,
	},
	{
		.name = "Fractal Incursion",
		.utc_offset = TIME2MIN(00,00),
		.category = CORE,
		.location = "Kessex Hills",
		.schedule_hours = 4,
	},
	{
		.name = "Fractal Incursion",
		.utc_offset = TIME2MIN(01,00),
		.category = CORE,
		.location = "Diessa Plateau",
		.schedule_hours = 4,
	},
	{
		.name = "Fractal Incursion",
		.utc_offset = TIME2MIN(02,00),
		.category = CORE,
		.location = "Brisban Wildlands",
		.schedule_hours = 4,
	},
	{
		.name = "Fractal Incursion",
		.utc_offset = TIME2MIN(03,00),
		.category = CORE,
		.location = "Snowden Drifts",
		.schedule_hours = 4,
	},

	/* Living World Season 1 */
	{
		.name = "Twisted Marionette",
		.utc_offset = TIME2MIN(00,00),
		.category = LWS1,
		.location = "Eye of the North",
		.schedule_hours = 2,
	},
	{
		.name = "Battle for Lion's Arch",
		.utc_offset = TIME2MIN(00,30),
		.category = LWS1,
		.location = "Eye of the North",
		.schedule_hours = 2,
	},
	{
		.name = "Tower of Nightmares",
		.utc_offset = TIME2MIN(01,30),
		.category = LWS1,
		.location = "Eye of the North",
		.schedule_hours = 2,
	},

	/* Living World Season 2 */
	{
		.name = "Sandstorm",
		.utc_offset = TIME2MIN(00,40),
		.category = LWS2,
		.location = "Dry Top",
		.schedule_hours = 1,
	},

	/* Living World Season 3 */
	{
		.name = "New Loamhurst",
		.utc_offset = TIME2MIN(01,45),
		.category = LWS3,
		.location = "Lake Doric",
		.schedule_hours = 2,
	},
	{
		.name = "Noran's Homestead",
		.utc_offset = TIME2MIN(00,30),
		.category = LWS3,
		.location = "Lake Doric",
		.schedule_hours = 2,
	},
	{
		.name = "Saidra's Haven",
		.utc_offset = TIME2MIN(01,00),
		.category = LWS3,
		.location = "Lake Doric",
		.schedule_hours = 2,
	},

	/* Living World Season 4 */
	{
		.name = "Palawadan",
		.utc_offset = TIME2MIN(01,45),
		.category = LWS4,
		.location = "Domain of Istan",
		.schedule_hours = 2,
	},
	{
		.name = "Death-Branded Shatterer",
		.utc_offset = TIME2MIN(01,15),
		.category = LWS4,
		.location = "Jahai Bluffs",
		.schedule_hours = 2,
	},
	{
		.name = "The Oil Floes",
		.utc_offset = TIME2MIN(00,45),
		.category = LWS4,
		.location = "Thunderhead Peaks",
		.schedule_hours = 2,
	},
	{
		.name = "Thunderhead Keep",
		.utc_offset = TIME2MIN(01,45),
		.category = LWS4,
		.location = "Thunderhead Peaks",
		.schedule_hours = 2,
	},

	/* Ice Brood Saga */
	{
		.name = "Effigy",
		.utc_offset = TIME2MIN(00,10),
		.category = IBS,
		.location = "Grothmar Valley",
		.schedule_hours = 2,
	},
	{
		.name = "Doomlore Shrine",
		.utc_offset = TIME2MIN(00,38),
		.category = IBS,
		.location = "Grothmar Valley",
		.schedule_hours = 2,
	},
	{
		.name = "Ooze Pits",
		.utc_offset = TIME2MIN(01,05),
		.category = IBS,
		.location = "Grothmar Valley",
		.schedule_hours = 2,
	},
	{
		.name = "Metal Concert",
		.utc_offset = TIME2MIN(01,40),
		.category = IBS,
		.location = "Grothmar Valley",
		.schedule_hours = 2,
	},
	{
		.name = "Storms of Winter",
		.utc_offset = TIME2MIN(01,45),
		.category = IBS,
		.location = "Bjora Marches",
		.schedule_hours = 2,
	},
	{
		.name = "Drakkar",
		.utc_offset = TIME2MIN(01,05),
		.category = IBS,
		.location = "Bjora Marches",
		.schedule_hours = 2,
	},

	/* Heart of Thorns */
	{
		.name = "Chak Gerent",
		.utc_offset = TIME2MIN(00,30),
		.category = HOT,
		.location = "Tangled Depths",
		.schedule_hours = 2,
	},
	{
		.name = "Octovine",
		.utc_offset = TIME2MIN(01,00),
		.category = HOT,
		.location = "Auric Basin",
		.schedule_hours = 2,
	},
	{
		.name = "Dragon's Stand",
		.utc_offset = TIME2MIN(01,30),
		.category = HOT,
		.location = "Dragon's Stand",
		.schedule_hours = 2,
	},
	{
		.name = "Night and the Enemy",
		.utc_offset = TIME2MIN(01,45),
		.category = HOT,
		.location = "Verdant Brink",
		.schedule_hours = 2,
	},

	/* Path of Fire */
	{
		.name = "Casino Blitz",
		.utc_offset = TIME2MIN(00,05),
		.category = POF,
		.location = "Crystal Oasis",
		.schedule_hours = 2,
	},
	{
		.name = "Buried Treasure",
		.utc_offset = TIME2MIN(01,00),
		.category = POF,
		.location = "Desert Highlands",
		.schedule_hours = 2,
	},
	{
		.name = "The Path to Ascension",
		.utc_offset = TIME2MIN(01,30),
		.category = POF,
		.location = "Elon Riverlands",
		.schedule_hours = 2,
	},
	{
		.name = "Maws of Torment",
		.utc_offset = TIME2MIN(01,00),
		.category = POF,
		.location = "The Desolation",
		.schedule_hours = 2,
	},
	{
		.name = "Junundu Rising",
		.utc_offset = TIME2MIN(00,30),
		.category = POF,
		.location = "The Desolation",
		.schedule_hours = 1,
	},
	{
		.name = "Forged with Fire",
		.utc_offset = TIME2MIN(00,00),
		.category = POF,
		.location = "Domain of Vabbi",
		.schedule_hours = 1,
	},
	{
		.name = "Serpents' Ire",
		.utc_offset = TIME2MIN(00,30),
		.category = POF,
		.location = "Domain of Vabbi",
		.schedule_hours = 2,
	},
	{
		.name = "Dragonstorm",
		.utc_offset = TIME2MIN(01,00),
		.category = POF,
		.location = "Eye of the North",
		.schedule_hours = 2,
	},

	/* End of Dragons */
	{
		.name = "Aetherblade Assault",
		.utc_offset = TIME2MIN(01,30),
		.category = EOD,
		.location = "Seitung Province",
		.schedule_hours = 2,
	},
	{
		.name = "Kaineng Blackout",
		.utc_offset = TIME2MIN(00,00),
		.category = EOD,
		.location = "New Kaineng City",
		.schedule_hours = 2,
	},
	{
		.name = "Gang War",
		.utc_offset = TIME2MIN(00,30),
		.category = EOD,
		.location = "The Echovald Wilds",
		.schedule_hours = 2,
	},
	{
		.name = "Aspenwood",
		.utc_offset = TIME2MIN(01,40),
		.category = EOD,
		.location = "The Echovald Wilds",
		.schedule_hours = 2,
	},
	{
		.name = "Battle for the Jade Sea",
		.utc_offset = TIME2MIN(01,00),
		.category = EOD,
		.location = "Dragon's End",
		.schedule_hours = 2,
	},

	/* Secrets of the Obscure */
	{
		.name = "Unlocking the Wizard's Tower",
		.utc_offset = TIME2MIN(01,00),
		.category = SOTO,
		.location = "Skywatch Archipelago",
		.schedule_hours = 2,
	},
	{
		.name = "Defense of Amnytas",
		.utc_offset = TIME2MIN(00,00),
		.category = SOTO,
		.location = "Amnytas",
		.schedule_hours = 2,
	},
	{
		.name = "Outer Nayos",
		.utc_offset = TIME2MIN(01,30),
		.category = SOTO,
		.location = "The Wizard's Tower",
		.schedule_hours = 3,
	},

	/* Janthir Wilds */
	{
		.name = "Of Mists and Monsters",
		.utc_offset = TIME2MIN(00,30),
		.category = JW,
		.location = "Janthir Syntri",
		.schedule_hours = 2,
	},
	{
		.name = "A Titanic Voyage",
		.utc_offset = TIME2MIN(01,20),
		.category = JW,
		.location = "Bava Nisos",
		.schedule_hours = 2,
	},
	{
		.name = "Mount Balrior",
		.utc_offset = TIME2MIN(00,00),
		.category = JW,
		.location = "Lowland Shore",
		.schedule_hours = 3,
	},

	/* Visions of Eternity */
	{
		.name = "Hammerhart Rumble!",
		.utc_offset = TIME2MIN(00,40),
		.category = VOE,
		.location = "Shipwreck Strand",
		.schedule_hours = 2,
	},
	{
		.name = "Secrets of the Weald",
		.utc_offset = TIME2MIN(01,40),
		.category = VOE,
		.location = "Starlit Weald",
		.schedule_hours = 2,
	},
	{
		.name = "Shackles of the Ancients",
		.utc_offset = TIME2MIN(01,10),
		.category = VOE,
		.location = "Eternity's Garden",
		.schedule_hours = 2,
	},
};

/* these events have no regular schedule */
static const struct Gw2Event event_list_irregular[] = {
	{
		.name = "Tequatl the Sunless",
		.utc_offset = TIME2MIN(00,00),
		.category = CORE,
		.location = "Sparkfly Fen",
		.schedule_hours = 24,
	},
	{
		.name = "Tequatl the Sunless",
		.utc_offset = TIME2MIN(03,00),
		.category = CORE,
		.location = "Sparkfly Fen",
		.schedule_hours = 24,
	},
	{
		.name = "Tequatl the Sunless",
		.utc_offset = TIME2MIN(07,00),
		.category = CORE,
		.location = "Sparkfly Fen",
		.schedule_hours = 24,
	},
	{
		.name = "Tequatl the Sunless",
		.utc_offset = TIME2MIN(11,30),
		.category = CORE,
		.location = "Sparkfly Fen",
		.schedule_hours = 24,
	},
	{
		.name = "Tequatl the Sunless",
		.utc_offset = TIME2MIN(16,00),
		.category = CORE,
		.location = "Sparkfly Fen",
		.schedule_hours = 24,
	},
	{
		.name = "Tequatl the Sunless",
		.utc_offset = TIME2MIN(19,00),
		.category = CORE,
		.location = "Sparkfly Fen",
		.schedule_hours = 24,
	},
	{
		.name = "Karka Queen",
		.utc_offset = TIME2MIN(02,00),
		.category = CORE,
		.location = "Southsun Cove",
		.schedule_hours = 24,
	},
	{
		.name = "Karka Queen",
		.utc_offset = TIME2MIN(06,00),
		.category = CORE,
		.location = "Southsun Cove",
		.schedule_hours = 24,
	},
	{
		.name = "Karka Queen",
		.utc_offset = TIME2MIN(10,30),
		.category = CORE,
		.location = "Southsun Cove",
		.schedule_hours = 24,
	},
	{
		.name = "Karka Queen",
		.utc_offset = TIME2MIN(15,00),
		.category = CORE,
		.location = "Southsun Cove",
		.schedule_hours = 24,
	},
	{
		.name = "Karka Queen",
		.utc_offset = TIME2MIN(19,00),
		.category = CORE,
		.location = "Southsun Cove",
		.schedule_hours = 24,
	},
	{
		.name = "Karka Queen",
		.utc_offset = TIME2MIN(23,00),
		.category = CORE,
		.location = "Southsun Cove",
		.schedule_hours = 24,
	},
	{
		.name = "Triple Trouble",
		.utc_offset = TIME2MIN(01,00),
		.category = CORE,
		.location = "Bloodtide Coast",
		.schedule_hours = 24,
	},
	{
		.name = "Triple Trouble",
		.utc_offset = TIME2MIN(04,00),
		.category = CORE,
		.location = "Bloodtide Coast",
		.schedule_hours = 24,
	},
	{
		.name = "Triple Trouble",
		.utc_offset = TIME2MIN(8,00),
		.category = CORE,
		.location = "Bloodtide Coast",
		.schedule_hours = 24,
	},
	{
		.name = "Triple Trouble",
		.utc_offset = TIME2MIN(12,30),
		.category = CORE,
		.location = "Bloodtide Coast",
		.schedule_hours = 24,
	},
	{
		.name = "Triple Trouble",
		.utc_offset = TIME2MIN(17,00),
		.category = CORE,
		.location = "Bloodtide Coast",
		.schedule_hours = 24,
	},
	{
		.name = "Triple Trouble",
		.utc_offset = TIME2MIN(20,00),
		.category = CORE,
		.location = "Bloodtide Coast",
		.schedule_hours = 24,
	},
};

static struct Gw2Event *all_events = NULL;
static int all_events_count = 0;
static int event_index = 0;

static inline uint16_t enabled_categories() {
	/* TODO: make this mask configurable */
	return 0xffff;
}

static inline bool is_category_enabled(enum Category category) {
	return enabled_categories() & (1 << category);
}

static int event_time_comp(const void *p1, const void *p2) {
	const struct Gw2Event *event1 = p1;
	const struct Gw2Event *event2 = p2;

	if (event1->utc_offset == event2->utc_offset) {
		/* if they start at the same time, sort by category */
		return event1->category - event2->category;
	}

	return (event1->utc_offset < event2->utc_offset) ? -1 : 1;
}

void cleanup_events() {
	if (all_events) {
		free(all_events);
		all_events = NULL;
	}
	all_events_count = 0;
	event_index = 0;
}

void init_events() {
	cleanup_events();

	/* count all events in a day */
	all_events_count = 0;
	for (size_t i=0; i<ARRAY_SIZE(event_list); i++) {
		if (!is_category_enabled(event_list[i].category)) {
			continue;
		}
		all_events_count += 24 / event_list[i].schedule_hours;
	}
	for (size_t i=0; i<ARRAY_SIZE(event_list_irregular); i++) {
		if (!is_category_enabled(event_list_irregular[i].category)) {
			continue;
		}
		all_events_count++;
	}

	all_events = calloc(all_events_count, sizeof(struct Gw2Event));

	/* create all individual events */
	int pos = 0;
	for (size_t i=0; i<ARRAY_SIZE(event_list); i++) {
		if (!is_category_enabled(event_list[i].category)) {
			continue;
		}
		const int daily_count = 24 / event_list[i].schedule_hours;
		for (int j=0; j<daily_count; j++) {
			memcpy(&all_events[pos], &event_list[i], sizeof(struct Gw2Event));
			all_events[pos].utc_offset += all_events[pos].schedule_hours * 60 * j;
			pos++;
		}
	}
	for (size_t i=0; i<ARRAY_SIZE(event_list_irregular); i++) {
		if (!is_category_enabled(event_list_irregular[i].category)) {
			continue;
		}
		memcpy(&all_events[pos], &event_list_irregular[i], sizeof(struct Gw2Event));
		pos++;
	}

	/* sort list by time */
	qsort(all_events, all_events_count, sizeof(struct Gw2Event), event_time_comp);

	update_event_index();
}

const struct Gw2Event *get_next_event(int offset) {
	offset = (offset + event_index) % all_events_count;
	return &all_events[offset];
}

/* moves the index to the first event that is not yet running */
void update_event_index() {
	const time_t now = time(NULL);
	const struct tm *utc = gmtime(&now);
	const int utc_offset = utc->tm_hour * 60 + utc->tm_min;

	event_index = 0;
	for (int i=0; i<all_events_count; i++) {
		if (all_events[i].utc_offset >= utc_offset) {
			event_index = i;
			break;
		}
	}
}

int minutes_to_event(const struct Gw2Event *event) {
	const time_t now = time(NULL);
	const struct tm *utc = gmtime(&now);
	const int utc_offset = utc->tm_hour * 60 + utc->tm_min;

	return event->utc_offset - utc_offset;
}

GColor8 color_for_category(enum Category category) {
	switch(category) {
		case CORE:
		case LWS1:
			return GColorDarkCandyAppleRed;
		case LWS2:
			return GColorLimerick;
		case LWS3:
			return GColorMayGreen;
		case LWS4:
			return GColorLavenderIndigo;
		case IBS:
			return GColorVividCerulean;
		case HOT:
			return GColorSpringBud;
		case POF:
			return GColorPurpureus;
		case EOD:
			return GColorTiffanyBlue;
		case SOTO:
			return GColorChromeYellow;
		case JW:
			return GColorBabyBlueEyes;
		case VOE:
			return GColorRajah;
	}
	return GColorFashionMagenta;
}