@@ -36,6 +36,25 @@ static const char SQL_init_games_table[] =
36
36
"owner integer NOT NULL REFERENCES users (uid), " "ts timestamp NOT NULL, "
37
37
"start_ts timestamp NOT NULL" ");" ;
38
38
39
+ static const char SQL_init_binge_table [] =
40
+ "CREATE TABLE bingeboard ("
41
+ "gid SERIAL PRIMARY KEY REFERENCES games (gid), "
42
+ "level integer NOT NULL, "
43
+ "hp integer NOT NULL, "
44
+ "max_hp integer NOT NULL, "
45
+ "gold integer NOT NULL, "
46
+ "moves integer NOT NULL, "
47
+ "energy integer NOT NULL, "
48
+ "max_energy integer NOT NULL, "
49
+ "attrib_str integer NOT NULL, "
50
+ "attrib_int integer NOT NULL, "
51
+ "attrib_wis integer NOT NULL, "
52
+ "attrib_dex integer NOT NULL, "
53
+ "attrib_con integer NOT NULL, "
54
+ "attrib_cha integer NOT NULL, "
55
+ "score integer NOT NULL "
56
+ ");" ;
57
+
39
58
static const char SQL_init_topten_table [] =
40
59
"CREATE TABLE topten(" "gid integer PRIMARY KEY REFERENCES games (gid), "
41
60
"points integer NOT NULL, " "hp integer NOT NULL, "
@@ -81,16 +100,36 @@ static const char SQL_add_game[] =
81
100
"VALUES ($1::text, $2::text, $3::text, $4::text, $5::text, "
82
101
"$6::integer, 1, 1, $7::integer, $8::text, $9::text, 'now', 'now')" ;
83
102
103
+ static const char SQL_add_binge_entry [] =
104
+ "INSERT INTO bingeboard ("
105
+ "gid, level, hp, max_hp, gold, moves, energy, max_energy, attrib_str, "
106
+ "attrib_int, attrib_wis, attrib_dex, attrib_con, attrib_cha, score) "
107
+ "VALUES ($1::integer, $2::integer, $3::integer, $4::integer, $5::integer, "
108
+ "$6::integer, $7::integer, $8::integer, $9::integer, $10::integer, $11::integer, "
109
+ "$12::integer, $13::integer, $14::integer, $15::integer)" ;
110
+
84
111
static const char SQL_delete_game [] =
85
112
"DELETE FROM games WHERE owner = $1::integer AND gid = $2::integer;" ;
86
113
114
+ static const char SQL_delete_binge_entry [] =
115
+ "DELETE FROM bingeboard WHERE gid = $1::integer;" ;
116
+
87
117
static const char SQL_last_game_id [] = "SELECT currval('games_gid_seq');" ;
88
118
89
119
static const char SQL_update_game [] =
90
120
"UPDATE games "
91
121
"SET ts = 'now', moves = $2::integer, depth = $3::integer, level_desc = "
92
122
"$4::text WHERE gid = $1::integer;" ;
93
123
124
+ static const char SQL_update_binge_entry [] =
125
+ "UPDATE bingeboard "
126
+ "SET level = $2::integer, hp = $3::integer, max_hp = $4::integer, "
127
+ "gold = $5::integer, moves = $6::integer, energy = $7::integer, "
128
+ "max_energy = $8::integer, attrib_str = $9::integer, attrib_int = $10::integer, "
129
+ "attrib_wis = $11::integer, attrib_dex = $12::integer, attrib_con = $13::integer, "
130
+ "attrib_cha = $14::integer, score = $15::integer WHERE gid = $1::integer;" ;
131
+
132
+
94
133
static const char SQL_set_game_done [] =
95
134
"UPDATE games " "SET done = TRUE " "WHERE gid = $1::integer;" ;
96
135
@@ -208,6 +247,7 @@ check_database(void)
208
247
*/
209
248
if (!check_create_table ("users" , SQL_init_user_table ) ||
210
249
!check_create_table ("games" , SQL_init_games_table ) ||
250
+ !check_create_table ("bingeboard" , SQL_init_binge_table ) ||
211
251
!check_create_table ("topten" , SQL_init_topten_table ))
212
252
goto err ;
213
253
@@ -446,6 +486,60 @@ db_add_new_game(int uid, const char *filename, const char *role,
446
486
return gid ;
447
487
}
448
488
489
+ void
490
+ db_add_binge_entry (int gid , int level , int hp , int max_hp , int gold , int moves ,
491
+ int energy , int max_energy , int attrib_str , int attrib_int ,
492
+ int attrib_wis , int attrib_dex , int attrib_con , int attrib_cha ,
493
+ int score )
494
+ {
495
+ PGresult * res ;
496
+ char gid_str [16 ],
497
+ level_str [16 ],
498
+ hp_str [16 ],
499
+ max_hp_str [16 ],
500
+ gold_str [16 ],
501
+ moves_str [16 ],
502
+ energy_str [16 ],
503
+ max_energy_str [16 ],
504
+ attrib_str_str [16 ],
505
+ attrib_int_str [16 ],
506
+ attrib_wis_str [16 ],
507
+ attrib_dex_str [16 ],
508
+ attrib_con_str [16 ],
509
+ attrib_cha_str [16 ],
510
+ score_str [16 ];
511
+
512
+ const char * const params [] = {gid_str , level_str , hp_str , max_hp_str ,
513
+ gold_str , moves_str , energy_str , max_energy_str , attrib_str_str ,
514
+ attrib_int_str , attrib_wis_str , attrib_dex_str , attrib_con_str ,
515
+ attrib_cha_str , score_str
516
+ };
517
+ const int paramFormats [15 ] = { 0 };
518
+
519
+ snprintf (gid_str , sizeof (gid_str ), "%d" , gid );
520
+ snprintf (level_str , sizeof (level_str ), "%d" , level );
521
+ snprintf (hp_str , sizeof (hp_str ), "%d" , hp );
522
+ snprintf (max_hp_str , sizeof (max_hp_str ), "%d" , max_hp );
523
+ snprintf (gold_str , sizeof (gold_str ), "%d" , gold );
524
+ snprintf (moves_str , sizeof (moves_str ), "%d" , moves );
525
+ snprintf (energy_str , sizeof (energy_str ), "%d" , energy );
526
+ snprintf (max_energy_str , sizeof (max_energy_str ), "%d" , max_energy );
527
+ snprintf (attrib_str_str , sizeof (attrib_str_str ), "%d" , attrib_str );
528
+ snprintf (attrib_int_str , sizeof (attrib_int_str ), "%d" , attrib_int );
529
+ snprintf (attrib_wis_str , sizeof (attrib_wis_str ), "%d" , attrib_wis );
530
+ snprintf (attrib_dex_str , sizeof (attrib_dex_str ), "%d" , attrib_dex );
531
+ snprintf (attrib_con_str , sizeof (attrib_con_str ), "%d" , attrib_con );
532
+ snprintf (attrib_cha_str , sizeof (attrib_cha_str ), "%d" , attrib_cha );
533
+ snprintf (score_str , sizeof (score_str ), "%d" , score );
534
+
535
+ res =
536
+ PQexecParams (conn , SQL_add_binge_entry , 15 , NULL , params , NULL , paramFormats ,
537
+ 0 );
538
+ if (PQresultStatus (res ) != PGRES_COMMAND_OK ) {
539
+ log_msg ("db_add_binge_entry error while adding (%d): %s" , gid , PQerrorMessage (conn ));
540
+ PQclear (res );
541
+ }
542
+ }
449
543
450
544
void
451
545
db_update_game (int game , int moves , int depth , const char * levdesc )
@@ -467,6 +561,60 @@ db_update_game(int game, int moves, int depth, const char *levdesc)
467
561
PQclear (res );
468
562
}
469
563
564
+ void
565
+ db_update_binge_entry (int gid , int level , int hp , int max_hp , int gold , int moves ,
566
+ int energy , int max_energy , int attrib_str , int attrib_int ,
567
+ int attrib_wis , int attrib_dex , int attrib_con , int attrib_cha ,
568
+ int score )
569
+ {
570
+ PGresult * res ;
571
+ char gid_str [16 ],
572
+ level_str [16 ],
573
+ hp_str [16 ],
574
+ max_hp_str [16 ],
575
+ gold_str [16 ],
576
+ moves_str [16 ],
577
+ energy_str [16 ],
578
+ max_energy_str [16 ],
579
+ attrib_str_str [16 ],
580
+ attrib_int_str [16 ],
581
+ attrib_wis_str [16 ],
582
+ attrib_dex_str [16 ],
583
+ attrib_con_str [16 ],
584
+ attrib_cha_str [16 ],
585
+ score_str [16 ];
586
+
587
+ const char * const params [] = {gid_str , level_str , hp_str , max_hp_str ,
588
+ gold_str , moves_str , energy_str , max_energy_str , attrib_str_str ,
589
+ attrib_int_str , attrib_wis_str , attrib_dex_str , attrib_con_str ,
590
+ attrib_cha_str , score_str
591
+ };
592
+ const int paramFormats [15 ] = { 0 };
593
+
594
+ snprintf (gid_str , sizeof (gid_str ), "%d" , gid );
595
+ snprintf (level_str , sizeof (level_str ), "%d" , level );
596
+ snprintf (hp_str , sizeof (hp_str ), "%d" , hp );
597
+ snprintf (max_hp_str , sizeof (max_hp_str ), "%d" , max_hp );
598
+ snprintf (gold_str , sizeof (gold_str ), "%d" , gold );
599
+ snprintf (moves_str , sizeof (moves_str ), "%d" , moves );
600
+ snprintf (energy_str , sizeof (energy_str ), "%d" , energy );
601
+ snprintf (max_energy_str , sizeof (max_energy_str ), "%d" , max_energy );
602
+ snprintf (attrib_str_str , sizeof (attrib_str_str ), "%d" , attrib_str );
603
+ snprintf (attrib_int_str , sizeof (attrib_int_str ), "%d" , attrib_int );
604
+ snprintf (attrib_wis_str , sizeof (attrib_wis_str ), "%d" , attrib_wis );
605
+ snprintf (attrib_dex_str , sizeof (attrib_dex_str ), "%d" , attrib_dex );
606
+ snprintf (attrib_con_str , sizeof (attrib_con_str ), "%d" , attrib_con );
607
+ snprintf (attrib_cha_str , sizeof (attrib_cha_str ), "%d" , attrib_cha );
608
+ snprintf (score_str , sizeof (score_str ), "%d" , score );
609
+
610
+ res =
611
+ PQexecParams (conn , SQL_update_binge_entry , 15 , NULL , params , NULL , paramFormats ,
612
+ 0 );
613
+ if (PQresultStatus (res ) != PGRES_COMMAND_OK )
614
+ log_msg ("update_binge_entry error: %s" , PQerrorMessage (conn ));
615
+ PQclear (res );
616
+ }
617
+
470
618
void
471
619
db_delete_game (int uid , int gid )
472
620
{
@@ -487,6 +635,25 @@ db_delete_game(int uid, int gid)
487
635
PQclear (res );
488
636
}
489
637
638
+ void
639
+ db_delete_binge_entry (int gid )
640
+ {
641
+ PGresult * res ;
642
+ char gid_str [16 ];
643
+ const char * const params [] = { gid_str };
644
+ const int paramFormats [] = { 0 };
645
+
646
+ snprintf (gid_str , sizeof (gid_str ), "%d" , gid );
647
+
648
+ res =
649
+ PQexecParams (conn , SQL_delete_binge_entry , 1 , NULL , params , NULL , paramFormats ,
650
+ 0 );
651
+ if (PQresultStatus (res ) != PGRES_COMMAND_OK )
652
+ log_msg ("db_delete_binge_entry error: %s" , PQerrorMessage (conn ));
653
+
654
+ PQclear (res );
655
+ }
656
+
490
657
491
658
static struct gamefile_info *
492
659
db_game_name_core (int completed , int uid , int gid , int limit , int * count )
0 commit comments