-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.ixx
1577 lines (1258 loc) · 56.4 KB
/
Database.ixx
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ____ _ _
* | _ \ __ _| |_ __ _| |__ __ _ ___ ___
* | | | |/ _` | __/ _` | '_ \ / _` / __|/ _ \
* | |_| | (_| | || (_| | |_) | (_| \__ \ __/
* |____/ \__,_|\__\__,_|_.__/ \__,_|___/\___|
*
*
* The modules for objects which are saved to the database do not import the database themselves.
* Instead, the screen modules which load those classes also load the database module.
* The database module also loads all the modules whose classes must be saved to the database.
* So Limb does not have a save() function.
* Instead, the database will have a save() function for each class,
* and the screen modules can call THAT function when needed.
* The same process applies for getting data from the database,
* and using that data to create objects.
*/
module;
export module Database;
import <vector>;
import <cstdlib>;
import <iostream>;
import <unordered_map>;
import <fstream>;
import <string>;
#include "sqlite3.h"
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
import CharacterClasses;
import MapClasses;
import TypeStorage;
import LimbFormMasterList;
import FormFactory;
import GameState;
import UI;
using namespace std;
const char* dbPath() { return "data/databases/limbs_data.db"; }
export string stringFromUnsignedChar(const unsigned char* c_str) {
std::string str;
int i{ 0 };
while (c_str[i] != '\0') { str += c_str[i++]; }
return str;
}
/* Creates the database and tables if they don't exist. */
export void createDB() {
sqlite3* db;
/* Open database (create if does not exist). */
int dbFailed = sqlite3_open(dbPath(), &db);
cout << dbFailed << "\n";
if (dbFailed == 0) {
cout << "Opened Database Successfully." << "\n";
/* Get the schema file. */
string schemaFilename = "data/schema.sql";
ifstream schemaFile(schemaFilename);
if (schemaFile.is_open()) {
cout << "Opened schema file.\n";
/* Get the SQL string from the open file. */
string sql((istreambuf_iterator<char>(schemaFile)), istreambuf_iterator<char>());
char* errMsg = nullptr;
int returnCode = sqlite3_exec(db, sql.c_str(), NULL, NULL, &errMsg);
if (returnCode == SQLITE_OK) { cout << "Schema executed successfully." << endl; }
else {
cerr << "SQL error: " << errMsg << endl;
sqlite3_free(errMsg); }
}
else {
cerr << "Could not open file: " << schemaFilename << endl;
return;
}
}
else { cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl; }
/* Close database. */
sqlite3_close(db);
}
/**
*
*
*
* _ _ _ ____ _ _ _
* | | (_)_ __ ___ | |__ | _ \ ___| | __ _| |_ ___ __| |
* | | | | '_ ` _ \| '_ \ _____| |_) / _ \ |/ _` | __/ _ \/ _` |
* | |___| | | | | | | |_) |_____| _ < __/ | (_| | || __/ (_| |
* |_____|_|_| |_| |_|_.__/ _ |_| \_\___|_|\__,_|\__\___|\__,_|
* | ___| _ _ __ ___| |_(_) ___ _ __ ___
* | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __|
* | _|| |_| | | | | (__| |_| | (_) | | | \__ \
* |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
*
*
* Limb-Related Functions
*
*/
/* When a limb changes owner. */
export bool updateLimbOwner(int limbID, int newCharacterID) {
bool success = false;
sqlite3* db;
/* Open database. */
int dbFailed = sqlite3_open(dbPath(), &db);
cout << dbFailed << "\n";
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return success;
}
/* No need to change the map_slug because map only loads non-owned limbs. */
const char* updateSQL = "UPDATE limb SET character_id = ? WHERE id = ?;";
sqlite3_stmt* statement;
/* Prepare the statement. */
int returnCode = sqlite3_prepare_v2(db, updateSQL, -1, &statement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare LIMB UPDATE statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return success;
}
/* Bind the values. */
sqlite3_bind_int(statement, 1, newCharacterID);
sqlite3_bind_int(statement, 2, limbID);
cout << "Updated Limb ID: " << limbID << "\n";
/* Execute the statement. */
returnCode = sqlite3_step(statement);
if (returnCode != SQLITE_DONE) { cerr << "Update failed: " << sqlite3_errmsg(db) << endl; }
else { success = true; }
/* Finalize statement and close database. */
sqlite3_finalize(statement);
sqlite3_close(db);
return success;
}
/*
* When the list of roamingLimbs in the map have all moved to a new location,
* send them here and update the database.
*/
export void updateLimbsLocation(vector<Limb>& limbs) {
/* Open database. */
sqlite3* db;
char* errMsg = nullptr;
int dbFailed = sqlite3_open(dbPath(), &db);
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return;
}
const char* updateSQL = "UPDATE limb SET position_x = ?, position_y = ? WHERE id = ?;";
sqlite3_stmt* statement;
/* Prepare the statement. */
int returnCode = sqlite3_prepare_v2(db, updateSQL, -1, &statement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare LIMB POSITION UPDATE statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return;
}
/* Begin the transaction. */
sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, &errMsg);
for (int i = 0; i < limbs.size(); ++i) {
Point position = limbs[i].getPosition();
sqlite3_bind_int(statement, 1, position.x);
sqlite3_bind_int(statement, 2, position.y);
sqlite3_bind_int(statement, 3, limbs[i].getId());
if (sqlite3_step(statement) != SQLITE_DONE) {
cerr << "Update failed for LIMB: " << sqlite3_errmsg(db) << endl;
break;
}
sqlite3_reset(statement);
}
/* Finalize the statement, commit the transaction. */
sqlite3_finalize(statement);
sqlite3_exec(db, "COMMIT;", nullptr, nullptr, &errMsg);
sqlite3_close(db);
}
/**
*
*
* ____ _ _
* / ___| |__ __ _ _ __ __ _ ___| |_ ___ _ __
* | | | '_ \ / _` | '__/ _` |/ __| __/ _ \ '__|____
* | |___| | | | (_| | | | (_| | (__| || __/ | |_____|
* \____|_| |_|\__,_|_| \__,_|\___|\__\___|_|
* | _ \ ___| | __ _| |_ ___ __| |
* | |_) / _ \ |/ _` | __/ _ \/ _` |
* | _ < __/ | (_| | || __/ (_| |
* |_|_\_\___|_|\__,_|\__\___|\__,_|
* | ___| _ _ __ ___| |_(_) ___ _ __ ___
* | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __|
* | _|| |_| | | | | (__| |_| | (_) | | | \__ \
* |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
*
*
*/
/*
* Update all attributes of a character's limbs and their joints.
* To be used when SAVING limb setup in Character Creation Screen.
*/
export void updateCharacterLimbs(int characterId, int anchorLimbId, vector<Limb>& limbs) {
/* Open database. */
sqlite3* db;
char* errMsg = nullptr;
int dbFailed = sqlite3_open(dbPath(), &db);
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return;
}
/* First update the Character. */
const char* updateCharacterSQL = "UPDATE character SET anchor_limb_id = ? WHERE id = ?;";
sqlite3_stmt* updateCharacterstatement;
/* Prepare the statement. */
int returnCode = sqlite3_prepare_v2(db, updateCharacterSQL, -1, &updateCharacterstatement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare CHARACTER UPDATE statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return;
}
/* Bind the values. */
sqlite3_bind_int(updateCharacterstatement, 1, anchorLimbId);
sqlite3_bind_int(updateCharacterstatement, 2, characterId);
/* Execute the statement. */
returnCode = sqlite3_step(updateCharacterstatement);
if (returnCode != SQLITE_DONE) { cerr << "Update failed: " << sqlite3_errmsg(db) << endl; }
/* Finalize statement. */
sqlite3_finalize(updateCharacterstatement);
/*
*
* Now do a transaction and update all limbs and their joints.
*
*/
/* Begin the transaction. */
sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, &errMsg);
for (Limb& limb : limbs) {
const char* updateLimbSQL = "UPDATE limb SET map_slug = ?, hp_mod = ?, strength_mod = ?, "
"speed_mod = ?, intelligence_mod = ?, position_x = ?, position_y = ?, rotation_angle = ?, is_anchor = ?, "
"is_flipped = ?, character_id = ? WHERE id = ?;";
sqlite3_stmt* updateLimbStatement;
/* Prepare the LIMB statement (to be binded and reset for each Limb). */
returnCode = sqlite3_prepare_v2(db, updateLimbSQL, -1, &updateLimbStatement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare LIMB UPDATE statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return;
}
/* Bind the values for each limb. */
int isAnchorInt = limb.getIsAnchor() ? 1 : 0;
int isFlippedInt = limb.getIsFlipped() ? 1 : 0;
string mapSlugString = limb.getMapSlug();
const char* mapSlug = mapSlugString.c_str();
sqlite3_bind_text(updateLimbStatement, 1, mapSlug, -1, SQLITE_STATIC);
sqlite3_bind_int(updateLimbStatement, 2, limb.getHpMod());
sqlite3_bind_int(updateLimbStatement, 3, limb.getStrengthMod());
sqlite3_bind_int(updateLimbStatement, 4, limb.getSpeedMod());
sqlite3_bind_int(updateLimbStatement, 5, limb.getIntelligenceMod());
sqlite3_bind_int(updateLimbStatement, 6, limb.getPosition().x);
sqlite3_bind_int(updateLimbStatement, 7, limb.getPosition().y);
sqlite3_bind_int(updateLimbStatement, 8, limb.getRotationAngle());
sqlite3_bind_int(updateLimbStatement, 9, isAnchorInt);
sqlite3_bind_int(updateLimbStatement, 10, isFlippedInt);
sqlite3_bind_int(updateLimbStatement, 11, characterId);
sqlite3_bind_int(updateLimbStatement, 12, limb.getId());
/* Execute the statement. */
returnCode = sqlite3_step(updateLimbStatement);
if (returnCode != SQLITE_DONE) {
cerr << "Update Limb failed: " << sqlite3_errmsg(db) << endl;
}
else {
/* NOW update each JOINT for this LIMB. */
for (int i = 0; i < limb.getJoints().size(); ++i) {
Joint& joint = limb.getJoints()[i];
const char* updateJointSQL = "UPDATE joint SET vector_index = ?, modified_point_x = ?, modified_point_y = ?, "
"is_anchor = ?, connected_limb_id = ?, anchor_joint_index = ? WHERE id = ?;";
sqlite3_stmt* updateJointStatement;
/* Prepare the JOINT statement (to be binded and reset for each joint of each Limb). */
returnCode = sqlite3_prepare_v2(db, updateJointSQL, -1, &updateJointStatement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare JOINT UPDATE statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return;
}
Point modifiedPoint = joint.getPoint();
int isAnchorJointInt = joint.getIsAnchor() ? 1 : 0;
sqlite3_bind_int(updateJointStatement, 1, i);
sqlite3_bind_int(updateJointStatement, 2, modifiedPoint.x);
sqlite3_bind_int(updateJointStatement, 3, modifiedPoint.y);
sqlite3_bind_int(updateJointStatement, 4, isAnchorJointInt);
sqlite3_bind_int(updateJointStatement, 5, joint.getConnectedLimbId());
sqlite3_bind_int(updateJointStatement, 6, joint.getChildLimbAnchorJointIndex());
sqlite3_bind_int(updateJointStatement, 7, joint.getId());
/* Execute the statement. */
returnCode = sqlite3_step(updateJointStatement);
if (returnCode != SQLITE_DONE) { cerr << "Update Joint failed: " << sqlite3_errmsg(db) << endl; }
sqlite3_finalize(updateJointStatement);
}
}
sqlite3_finalize(updateLimbStatement);
}
sqlite3_exec(db, "COMMIT;", nullptr, nullptr, &errMsg);
sqlite3_close(db);
}
/* Update the MAP_SLUG of the Character player. */
export bool updatePlayerMap(string newMapSlug) {
bool success = false;
GameState& gameState = GameState::getInstance();
int playerID = gameState.getPlayerID();
/* Open database. */
sqlite3* db;
char* errMsg = nullptr;
int dbFailed = sqlite3_open(dbPath(), &db);
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return success;
}
const char* updateSQL = "UPDATE character SET map_slug = ? WHERE id = ?;";
sqlite3_stmt* statement;
/* Prepare the statement. */
int returnCode = sqlite3_prepare_v2(db, updateSQL, -1, &statement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare CHARACTER MAP_SLUG UPDATE statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return success;
}
/* Bind the values. */
sqlite3_bind_text(statement, 1, newMapSlug.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_int(statement, 2, playerID);
/* Execute the statement. */
returnCode = sqlite3_step(statement);
if (returnCode != SQLITE_DONE) { cerr << "Update failed: " << sqlite3_errmsg(db) << endl; }
else { success = true; }
/* Finalize statement and close database. */
sqlite3_finalize(statement);
sqlite3_close(db);
return success;
}
/*
* Same as loadPlayerMapCharacter but without map-related stuff.
* Should extract most of this to make a struct that serves them both!
* May use same system to create loadPlayerBattleCharacter too.
*/
export Character loadPlayerCharacter() {
GameState& gameState = GameState::getInstance();
int characterID = gameState.getPlayerID();
string name;
string mapSlug;
int anchorLimbID; /* Get from the limbs. */
int battleID; /* For later, when battles actually exist. */
bool isPlayer = true;
Character character = Character(CharacterType::None);
/* Open database. */
sqlite3* db;
char* errMsg = nullptr;
int dbFailed = sqlite3_open(dbPath(), &db);
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return character;
}
/* GET THE CHARACTER OBJECT. */
/* Create statement template for getting the character. */
const char* queryCharacterSQL = "SELECT * FROM character WHERE id = ?;";
sqlite3_stmt* characterStatement;
int returnCode = sqlite3_prepare_v2(db, queryCharacterSQL, -1, &characterStatement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare character retrieval statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return character;
}
/* Bind the id value. */
sqlite3_bind_int(characterStatement, 1, characterID);
/* Execute binded statement. */
if (sqlite3_step(characterStatement) != SQLITE_ROW) {
cerr << "Failed to retrieve MAP: " << sqlite3_errmsg(db) << endl;
return character;
}
name = stringFromUnsignedChar(sqlite3_column_text(characterStatement, 1));
anchorLimbID = sqlite3_column_int(characterStatement, 2);
mapSlug = stringFromUnsignedChar(sqlite3_column_text(characterStatement, 4));
battleID = sqlite3_column_int(characterStatement, 5);
/* Finalize statement. */
sqlite3_finalize(characterStatement);
/* GET THE LIMBS. */
/* Create statement template for querying Map objects with this slug. */
const char* queryLimbsSQL = "SELECT * FROM limb WHERE character_id = ?;";
sqlite3_stmt* queryLimbsStatement;
returnCode = sqlite3_prepare_v2(db, queryLimbsSQL, -1, &queryLimbsStatement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare limbs retrieval statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return character;
}
/* Bind the slug value. */
sqlite3_bind_int(queryLimbsStatement, 1, characterID);
/* Execute and iterate through results. */
while ((returnCode = sqlite3_step(queryLimbsStatement)) == SQLITE_ROW) {
int limbID = sqlite3_column_int(queryLimbsStatement, 0);
LimbForm limbForm = getLimbForm(stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 1)));
string mapSlug = stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 3));
int hpMod = sqlite3_column_int(queryLimbsStatement, 4);
int strengthMod = sqlite3_column_int(queryLimbsStatement, 5);
int speedMod = sqlite3_column_int(queryLimbsStatement, 6);
int intelligenceMod = sqlite3_column_int(queryLimbsStatement, 7);
int posX = sqlite3_column_int(queryLimbsStatement, 8);
int posY = sqlite3_column_int(queryLimbsStatement, 9);
int rotationAngle = sqlite3_column_int(queryLimbsStatement, 10);
bool isAnchor = sqlite3_column_int(queryLimbsStatement, 11) == 1;
bool isFlipped = sqlite3_column_int(queryLimbsStatement, 12) == 1;
string limbName = stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 13));
Point position = Point(posX, posY);
/* Get the JOINTS for this Limb. */
/* Start by querying the count to see how big the vector should be. */
const char* queryCountJointsSQL = "SELECT COUNT(*) FROM joint WHERE limb_id = ?;";
sqlite3_stmt* queryCountJointsStatement;
returnCode = sqlite3_prepare_v2(db, queryCountJointsSQL, -1, &queryCountJointsStatement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare JOINTS retrieval statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return character;
}
sqlite3_bind_int(queryCountJointsStatement, 1, limbID);
/* Execute count query. */
int jointCount = 0;
if (sqlite3_step(queryCountJointsStatement) == SQLITE_ROW) {
jointCount = sqlite3_column_int(queryCountJointsStatement, 0);
}
sqlite3_finalize(queryCountJointsStatement);
vector<Joint> joints(jointCount);
/* Now get the JOINTS themselves. */
const char* queryJointsSQL = "SELECT * FROM joint WHERE limb_id = ?;";
sqlite3_stmt* queryJointsStatement;
returnCode = sqlite3_prepare_v2(db, queryJointsSQL, -1, &queryJointsStatement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare blocks retrieval statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return character;
}
/* Bind the ID value. */
sqlite3_bind_int(queryJointsStatement, 1, limbID);
int jointsReturnCode;
while ((jointsReturnCode = sqlite3_step(queryJointsStatement)) == SQLITE_ROW) {
int jointID = sqlite3_column_int(queryJointsStatement, 0);
int vectorIndex = sqlite3_column_int(queryJointsStatement, 1);
Point pointForm = Point(
sqlite3_column_int(queryJointsStatement, 3),
sqlite3_column_int(queryJointsStatement, 4));
Point modifiedPoint = Point(
sqlite3_column_int(queryJointsStatement, 5),
sqlite3_column_int(queryJointsStatement, 6));
bool isAnchor = sqlite3_column_int(queryJointsStatement, 7) == 1;
int connectedLimbID = sqlite3_column_int(queryJointsStatement, 8);
int anchorJointIndex = sqlite3_column_int(queryJointsStatement, 9);
Joint joint = Joint(pointForm, modifiedPoint, isAnchor, connectedLimbID, anchorJointIndex, jointID);
if (vectorIndex < jointCount) { joints[vectorIndex] = joint; }
}
Limb limb = Limb(sqlite3_column_int(queryLimbsStatement, 0),
getLimbForm(stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 1))),
position,
joints
);
limb.setName(limbName);
limb.modifyHP(hpMod);
limb.modifyStrength(strengthMod);
limb.modifySpeed(speedMod);
limb.modifyIntelligence(intelligenceMod);
limb.rotate(rotationAngle);
limb.setFlipped(isFlipped);
limb.setAnchor(isAnchor);
limb.setMapSlug(mapSlug);
limb.setCharacterId(characterID);
limb.setId(limbID);
character.addLimb(limb);
cout << "added limb " << limbName << "\n";
}
cout << "Player has " << character.getLimbs().size() << " limbs (in DATABASE module)\n";
if (returnCode != SQLITE_DONE) {
cerr << "Execution failed: " << sqlite3_errmsg(db) << endl;
return character;
}
/* Finalize prepared statement. */
sqlite3_finalize(queryLimbsStatement);
character.setType(CharacterType::Player);
character.setName(name);
character.setId(characterID);
character.setAnchorLimbId(anchorLimbID);
cout << "Player has " << character.getLimbs().size() << " limbs in DB module\n";
return character;
}
/* Same as loadPlayerCharacter, but adding the map-related stuff. */
export MapCharacter loadPlayerMapCharacter() {
GameState& gameState = GameState::getInstance();
int characterID = gameState.getPlayerID();
string name;
string mapSlug;
int anchorLimbID; /* Get from the limbs. */
int battleID; /* For later, when battles actually exist. */
bool isPlayer = true;
MapCharacter character = MapCharacter(CharacterType::None);
/* Open database. */
sqlite3* db;
char* errMsg = nullptr;
int dbFailed = sqlite3_open(dbPath(), &db);
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return character;
}
/* GET THE CHARACTER OBJECT. */
/* Create statement template for getting the character. */
const char* queryCharacterSQL = "SELECT * FROM character WHERE id = ?;";
sqlite3_stmt* characterStatement;
int returnCode = sqlite3_prepare_v2(db, queryCharacterSQL, -1, &characterStatement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare character retrieval statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return character;
}
/* Bind the id value. */
sqlite3_bind_int(characterStatement, 1, characterID);
/* Execute binded statement. */
if (sqlite3_step(characterStatement) != SQLITE_ROW) {
cerr << "Failed to retrieve MAP: " << sqlite3_errmsg(db) << endl;
return character;
}
name = stringFromUnsignedChar(sqlite3_column_text(characterStatement, 1));
anchorLimbID = sqlite3_column_int(characterStatement, 2);
mapSlug = stringFromUnsignedChar(sqlite3_column_text(characterStatement, 4));
battleID = sqlite3_column_int(characterStatement, 5);
/* Finalize statement. */
sqlite3_finalize(characterStatement);
/* GET THE LIMBS. */
/* Create statement template for querying Map objects with this slug. */
const char* queryLimbsSQL = "SELECT * FROM limb WHERE character_id = ?;";
sqlite3_stmt* queryLimbsStatement;
returnCode = sqlite3_prepare_v2(db, queryLimbsSQL, -1, &queryLimbsStatement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare limbs retrieval statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return character;
}
/* Bind the slug value. */
sqlite3_bind_int(queryLimbsStatement, 1, characterID);
/* Execute and iterate through results. */
while ((returnCode = sqlite3_step(queryLimbsStatement)) == SQLITE_ROW) {
int limbID = sqlite3_column_int(queryLimbsStatement, 0);
LimbForm limbForm = getLimbForm(stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 1)));
string mapSlug = stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 3));
int hpMod = sqlite3_column_int(queryLimbsStatement, 4);
int strengthMod = sqlite3_column_int(queryLimbsStatement, 5);
int speedMod = sqlite3_column_int(queryLimbsStatement, 6);
int intelligenceMod = sqlite3_column_int(queryLimbsStatement, 7);
int posX = sqlite3_column_int(queryLimbsStatement, 8);
int posY = sqlite3_column_int(queryLimbsStatement, 9);
int rotationAngle = sqlite3_column_int(queryLimbsStatement, 10);
bool isAnchor = sqlite3_column_int(queryLimbsStatement, 11) == 1;
bool isFlipped = sqlite3_column_int(queryLimbsStatement, 12) == 1;
string limbName = stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 13));
Point limbPosition = Point(posX, posY);
/* Get the JOINTS for this Limb. */
/* Start by querying the count to see how big the vector should be. */
const char* queryCountJointsSQL = "SELECT COUNT(*) FROM joint WHERE limb_id = ?;";
sqlite3_stmt* queryCountJointsStatement;
returnCode = sqlite3_prepare_v2(db, queryCountJointsSQL, -1, &queryCountJointsStatement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare JOINTS retrieval statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return character;
}
sqlite3_bind_int(queryCountJointsStatement, 1, limbID);
/* Execute count query. */
int jointCount = 0;
if (sqlite3_step(queryCountJointsStatement) == SQLITE_ROW) {
jointCount = sqlite3_column_int(queryCountJointsStatement, 0);
}
sqlite3_finalize(queryCountJointsStatement);
vector<Joint> joints(jointCount);
/* Now get the JOINTS themselves. */
const char* queryJointsSQL = "SELECT * FROM joint WHERE limb_id = ?;";
sqlite3_stmt* queryJointsStatement;
returnCode = sqlite3_prepare_v2(db, queryJointsSQL, -1, &queryJointsStatement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare blocks retrieval statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return character;
}
/* Bind the ID value. */
sqlite3_bind_int(queryJointsStatement, 1, limbID);
int jointsReturnCode;
while ((jointsReturnCode = sqlite3_step(queryJointsStatement)) == SQLITE_ROW) {
int jointID = sqlite3_column_int(queryJointsStatement, 0);
int vectorIndex = sqlite3_column_int(queryJointsStatement, 1);
Point pointForm = Point(
sqlite3_column_int(queryJointsStatement, 3),
sqlite3_column_int(queryJointsStatement, 4));
Point modifiedPoint = Point(
sqlite3_column_int(queryJointsStatement, 5),
sqlite3_column_int(queryJointsStatement, 6));
bool isAnchor = sqlite3_column_int(queryJointsStatement, 7) == 1;
int connectedLimbID = sqlite3_column_int(queryJointsStatement, 8);
int anchorJointIndex = sqlite3_column_int(queryJointsStatement, 9);
Joint joint = Joint(pointForm, modifiedPoint, isAnchor, connectedLimbID, anchorJointIndex, jointID);
if (vectorIndex < jointCount) { joints[vectorIndex] = joint; }
}
Limb limb = Limb(sqlite3_column_int(queryLimbsStatement, 0),
getLimbForm(stringFromUnsignedChar(sqlite3_column_text(queryLimbsStatement, 1))),
limbPosition,
joints
);
limb.setName(limbName);
limb.modifyHP(hpMod);
limb.modifyStrength(strengthMod);
limb.modifySpeed(speedMod);
limb.modifyIntelligence(intelligenceMod);
limb.rotate(rotationAngle);
limb.setFlipped(isFlipped);
limb.setAnchor(isAnchor);
limb.setMapSlug(mapSlug);
limb.setCharacterId(characterID);
limb.setId(limbID);
character.addLimb(limb);
cout << "added limb " << limbName << "\n";
}
cout << "Player has " << character.getLimbs().size() << " limbs (in DATABASE module)\n";
if (returnCode != SQLITE_DONE) {
cerr << "Execution failed: " << sqlite3_errmsg(db) << endl;
return character;
}
/* Finalize prepared statement. */
sqlite3_finalize(queryLimbsStatement);
character.setType(CharacterType::Player);
character.setName(name);
character.setId(characterID);
character.setAnchorLimbId(anchorLimbID);
/*
* NOW get the blockPosition from the Map table.
* ( also, temporarily get the texture... will replace with uilding from limbs of course. )
*
* THE ABOVE CODE was all normal Character code.
* That code must be put into a struct which can be handed to any derivative class,
* to avoid repeating code.
* THAT FUNCTION will take a db object as a parameter.
*/
/* Create statement template for querying Map objects with this slug. */
const char* queryMapSQL = "SELECT character_x, character_y FROM map WHERE slug = ?;";
sqlite3_stmt* statement;
returnCode = sqlite3_prepare_v2(db, queryMapSQL, -1, &statement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare map retrieval statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return character;
}
/* Bind the slug value. */
sqlite3_bind_text(statement, 1, mapSlug.c_str(), -1, SQLITE_STATIC);
/* Execute binded statement. */
if (sqlite3_step(statement) != SQLITE_ROW) {
cerr << "Failed to retrieve MAP: " << sqlite3_errmsg(db) << endl;
return character;
}
Point characterPosition = Point(
sqlite3_column_int(statement, 0),
sqlite3_column_int(statement, 1)
);
/* Finally actually set the block position. */
character.setBlockPosition(characterPosition);
/* Finalize map ID retrieval statement. */
sqlite3_finalize(statement);
sqlite3_close(db);
cout << "We really reached the end of the character loading, so what's wrong?\n";
cout << "Player has " << character.getLimbs().size() << " limbs in DB module\n";
/* get character texture (MUST DELETE AFTER WE START DRAWING RAW LIMBS ONTO THE MAP INSTEAD.) */
UI& ui = UI::getInstance();
SDL_Surface* characterSurface = IMG_Load("assets/player_character.png");
SDL_Texture* characterTexture = SDL_CreateTextureFromSurface(ui.getMainRenderer(), characterSurface);
SDL_FreeSurface(characterSurface);
character.setTexture(characterTexture);
return character;
}
export int createPlayerCharacterOrGetID() {
/*
* First check if player character already exists.
* If it does, get the ID and return the ID.
* If it does not exist, create the player character and return the ID.
*/
int count = 0;
int playerID = -1;
/* Open database. */
sqlite3* db;
char* errMsg = nullptr;
int dbFailed = sqlite3_open(dbPath(), &db);
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return false;
}
/* Create statement template for querying the count. */
const char* queryCountSQL = "SELECT COUNT(*) FROM character WHERE is_player = 1;";
sqlite3_stmt* statement;
int returnCode = sqlite3_prepare_v2(db, queryCountSQL, -1, &statement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return false;
}
/* Execute statement. */
if (sqlite3_step(statement) == SQLITE_ROW) {
count = sqlite3_column_int(statement, 0);
}
/* Finalize statement. */
sqlite3_finalize(statement);
if (count > 0) {
/* Player character exists. Get the ID. */
/* Create statement template for querying the id. */
const char* queryIDSQL = "SELECT id FROM character WHERE is_player = 1;";
sqlite3_stmt* idStatement;
returnCode = sqlite3_prepare_v2(db, queryIDSQL, -1, &idStatement, nullptr);
if (returnCode != SQLITE_OK) {
std::cerr << "Failed to prepare statement: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return false;
}
/* Execute statement. */
if (sqlite3_step(idStatement) == SQLITE_ROW) {
playerID = sqlite3_column_int(idStatement, 0);
sqlite3_finalize(idStatement);
sqlite3_close(db);
cout << "Player ID retrieved: " << playerID << "\n";
return playerID;
}
/* Finalize statement. */
sqlite3_finalize(idStatement);
}
/* Player character does not exist. Create it. */
const char* newCharacterSQL = "INSERT INTO character (name, is_player) VALUES (?, ?);";
sqlite3_stmt* newCharStatement;
returnCode = sqlite3_prepare_v2(db, newCharacterSQL, -1, &newCharStatement, nullptr);
/* Bind values. */
const char* name = "Player";
int isPlayerBoolInt = 1;
sqlite3_bind_text(newCharStatement, 1, name, -1, SQLITE_STATIC);
sqlite3_bind_int(newCharStatement, 2, isPlayerBoolInt);
/* Execute the statement. */
returnCode = sqlite3_step(newCharStatement);
if (returnCode != SQLITE_DONE) { cerr << "Insert Player Character failed: " << sqlite3_errmsg(db) << endl; }
else {
/* Get the ID of the saved item. */
playerID = static_cast<int>(sqlite3_last_insert_rowid(db));
}
/* Close DB. */
sqlite3_finalize(newCharStatement);
sqlite3_close(db);
return playerID;
}
/**
*
*
* __ __ ____ _ _ _
* | \/ | __ _ _ __ | _ \ ___| | __ _| |_ ___ __| |
* | |\/| |/ _` | '_ \ _____| |_) / _ \ |/ _` | __/ _ \/ _` |
* | | | | (_| | |_) |_____| _ < __/ | (_| | || __/ (_| |
* |_|__|_|\__,_| .__/ _ |_| \_\___|_|\__,_|\__\___|\__,_|
* | ___| _ _|_| ___| |_(_) ___ _ __ ___
* | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __|
* | _|| |_| | | | | (__| |_| | (_) | | | \__ \
* |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
*
*
*/
export void updatePlayerMapLocation(string slugString, Point position) {
sqlite3* db;
const char* mapSlug = slugString.c_str();
/* Open database. */
int dbFailed = sqlite3_open(dbPath(), &db);
cout << dbFailed << "\n";
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return;
}
/* Create statement for updating player position in a Map table entry in the DB. */
const char* insertSQL = "UPDATE map SET character_x = ?, character_y = ? WHERE slug = ?;";
sqlite3_stmt* statement;
/* Prepare the statement. */
int returnCode = sqlite3_prepare_v2(db, insertSQL, -1, &statement, nullptr);
if (returnCode != SQLITE_OK) {
cerr << "Failed to prepare MAP update statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return;
}
/* Bind the data and execute the statement. */
sqlite3_bind_int(statement, 1, position.x);
sqlite3_bind_int(statement, 2, position.y);
sqlite3_bind_text(statement, 3, mapSlug, -1, SQLITE_STATIC);
returnCode = sqlite3_step(statement);
if (returnCode != SQLITE_DONE) {
cerr << "Insert failed for MAP: " << sqlite3_errmsg(db) << endl;
sqlite3_finalize(statement);
return;
}
/* Finalize statement. */
sqlite3_finalize(statement);
sqlite3_close(db);
}
/*
* When the player first enters a particular map, so the map is built for the first time,
* we send the built map here to be saved to the database.
* So we save the map itself, the blocks, the limbs, and the limbs' joints.
*/
export bool createNewMap(Map& map) {
bool success = false;
sqlite3* db;
string slugString = map.getForm().slug;
const char* mapSlug = slugString.c_str();
int playerX = map.getPlayerCharacter().getBlockX();
int playerY = map.getPlayerCharacter().getBlockY();
/* Open database. */
int dbFailed = sqlite3_open(dbPath(), &db);
cout << dbFailed << "\n";
if (dbFailed != 0) {
cerr << "Error opening DB: " << sqlite3_errmsg(db) << endl;
return success;
}
/* First save the Map object to the DB. */