-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
1163 lines (1031 loc) · 39.7 KB
/
database.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
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
#include "database.h"
#include <QApplication>
#include <QDir>
#include <QDebug>
#include <QDateTime>
#include <QMessageBox>
#include <iostream>
Database::Database(QObject* parent)
: QObject(parent)
{
}
QString Database::gamerTagReplacements(const QMap<QString, QString> gamertag_replacements, const QString& player_name)
{
if (gamertag_replacements.contains(player_name.toLower()))
{
std::cout << "Replacing " << player_name.toStdString() << " with " << gamertag_replacements[player_name.toLower()].toStdString() << std::endl;
return gamertag_replacements[player_name.toLower()];
}
else
return player_name;
}
QSqlDatabase& Database::db(const QString& db_path)
{
static QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
if ( db.databaseName().isEmpty() )
{
db.setDatabaseName(db_path);
}
qDebug() << db.databaseName();
if( !db.open() )
{
qDebug() << db.lastError().text();
qFatal( "Failed to connect to database!" );
}
qDebug( "Connected!" );
return db;
}
void Database::initDB(const QString& cfg_file)
{
QString db_path;
QFileInfo cfg_fileInfo(cfg_file);
if (cfg_file.isEmpty() || !cfg_fileInfo.exists())
{
db_path = QApplication::instance()->applicationDirPath() +
QDir::separator() + "stats.sqlite";
}
else
{
db_path = cfg_fileInfo.absolutePath() +
QDir::separator() + "stats.sqlite";
}
db(db_path); // init db filepath
createTableConfig();
createTablePlayers();
createTableTournaments();
createTableElo();
createTableSessions();
createTableMatches();
}
void Database::createTableConfig()
{
QSqlDatabase& db = Database::db();
QSqlQuery query;
if ( !db.tables().contains( "config" ) )
{
if ( !query.exec( "CREATE TABLE config ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name varchar(255) NOT NULL UNIQUE, "
"val varchar(255) NOT NULL)" ) )
{ qDebug() << query.lastError().text(); }
setConfigVal("eloK", 25);
}
}
void Database::setConfigVal(const QString name, const QVariant val)
{
QSqlQuery query;
query.prepare("INSERT INTO config (name,val) VALUES(:name,:val)");
query.bindValue(":name", name);
query.bindValue(":val", val.toString());
if ( !query.exec() ){ qDebug() << "setConfigVal failed!"; }
}
bool Database::getConfigVal(const QString name, QVariant &val)
{
QSqlQuery query;
query.prepare( "SELECT val FROM config WHERE name = :name" );
query.bindValue( ":name", name );
query.exec();
if( query.next() ) { val = query.value(0); return true; }
return false;
}
void Database::createTablePlayers()
{
QSqlDatabase& db = Database::db();
QSqlQuery query;
if ( !db.tables().contains( "players" ) )
{
if ( !query.exec( "CREATE TABLE players ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"tag varchar(255) NOT NULL, "
"name varchar(255) )" ) )
{ qDebug() << query.lastError().text(); }
}
}
void Database::createTableTournaments()
{
QSqlDatabase& db = Database::db();
QSqlQuery query;
if ( !db.tables().contains( "tournaments" ) )
{
if ( !query.exec( "CREATE TABLE tournaments ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name varchar(255) )" ) )
{ qDebug() << query.lastError().text(); }
}
}
void Database::createTableElo()
{
QSqlDatabase& db = Database::db();
QSqlQuery query;
if ( !db.tables().contains( "elo" ) )
{
if ( !query.exec( "CREATE TABLE elo ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"player_id INTEGER NOT NULL UNIQUE, "
"player_tag varchar(255) NOT NULL, "
"rating_start INTEGER NOT NULL, "
"rating INTEGER NOT NULL, "
"set_wins INTEGER NOT NULL, "
"set_losses INTEGER NOT NULL, "
"match_wins INTEGER NOT NULL, "
"match_losses INTEGER NOT NULL, "
"attendances INTEGER NOT NULL)"))//, "
//"FOREIGN KEY(player_id) REFERENCES players(id) ON UPDATE CASCADE )" ) )
{ qDebug() << query.lastError().text(); }
}
}
void Database::createTableSessions()
{
QSqlDatabase& db = Database::db();
QSqlQuery query;
if ( !db.tables().contains( "sessions" ) )
{
if ( !query.exec( "CREATE TABLE sessions ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name varchar(255) NOT NULL, "
"challonge_id INTEGER NOT NULL, "
"tournament_id INTEGER NOT NULL, "
"date varchar(255) NOT NULL, "
"url varchar(1024) )" ))//,"
//"FOREIGN KEY(tournament_id) REFERENCES tournaments(id) ON UPDATE CASCADE" ) )
{ qDebug() << query.lastError().text(); }
}
}
void Database::createTableMatches()
{
QSqlDatabase& db = Database::db();
QSqlQuery query;
if ( !db.tables().contains( "matches" ) )
{
if ( !query.exec( "CREATE TABLE matches ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"session_id int NOT NULL, "
"round int NOT NULL, "
"suggested_play_order int NOT NULL, "
"p1 int NOT NULL, "
"p2 int NOT NULL, "
"p1_wins int NOT NULL, "
"p2_wins int NOT NULL )" ) ) //,"
//"FOREIGN KEY(session_id) REFERENCES sessions(id) ON UPDATE CASCADE, "
//"FOREIGN KEY(p1) REFERENCES players(id) ON UPDATE CASCADE, "
//"FOREIGN KEY(p1) REFERENCES players(id) ON UPDATE CASCADE )" ) )
{ qDebug() << query.lastError().text(); }
}
}
bool Database::tournamentExists( const QString& name )
{
QSqlQuery query;
query.prepare( "SELECT name FROM tournaments WHERE name = :name COLLATE NOCASE" );
query.bindValue( ":name", name );
query.exec();
if( query.next() ) { return true; }
return false;
}
QStringList Database::tournaments( )
{
QStringList list_tournaments;
QSqlQuery query;
if ( !query.exec( "SELECT name FROM tournaments" ) )
{ qDebug() << "tournaments() failed: " << query.lastError(); }
while ( query.next() ){ list_tournaments << query.value(0).toString(); }
return list_tournaments;
}
bool Database::addTournament( const QString& name )
{
if ( tournamentExists( name ) ) { return false; }
QSqlQuery query;
query.prepare( "INSERT INTO tournaments (name) VALUES( :name)" );
query.bindValue(":name", name );
if ( !query.exec() )
{ qDebug() << "addTournament failed: " << query.lastError(); }
return true;
}
int Database::tournamentID( const QString& name )
{
QSqlQuery query;
query.prepare( "SELECT id FROM tournaments WHERE name = :name COLLATE NOCASE" );
query.bindValue( ":name", name );
query.exec();
if( query.next() ) { return query.value(0).toInt(); }
return -1;
}
QString Database::tournamentName( const int id )
{
QSqlQuery query;
query.prepare( "SELECT name FROM tournaments WHERE id = :id COLLATE NOCASE" );
query.bindValue( ":id", id );
query.exec();
if( query.next() ) { return query.value(0).toString(); }
return "";
}
bool Database::addPlayerToEloRating(const int player_id)
{
QSqlQuery query;
query.prepare( "INSERT INTO elo "
"(player_id, player_tag, "
"rating_start, rating, "
"set_wins, set_losses, "
"match_wins, match_losses, attendances) "
"VALUES"
"(:player_id, :player_tag, "
":rating_start, :rating, "
":set_wins, :set_losses, "
":match_wins, :match_losses, :attendances) " );
query.bindValue(":player_id", player_id);
query.bindValue(":player_tag", Database::playerTag(player_id));
query.bindValue(":rating_start", 1500);
query.bindValue(":rating", 1500);
query.bindValue(":set_wins", 0);
query.bindValue(":set_losses", 0);
query.bindValue(":match_wins", 0);
query.bindValue(":match_losses", 0);
query.bindValue(":attendances", 0);
if ( !query.exec() )
{
qDebug() << "addPlayerToEloRating failed: " << query.lastError();
return false;
}
return true;
}
bool Database::clearEloRating()
{
QSqlQuery query;
if ( !query.exec( "UPDATE elo SET rating=rating_start") ) return false;
if ( !query.exec( "UPDATE elo SET set_wins=0") ) return false;
if ( !query.exec( "UPDATE elo SET set_losses=0") ) return false;
if ( !query.exec( "UPDATE elo SET match_wins=0") ) return false;
if ( !query.exec( "UPDATE elo SET match_losses=0") ) return false;
if ( !query.exec( "UPDATE elo SET attendances=0") ) return false;
return true;
}
bool Database::eloData(std::vector<EloData>& vecEloData)
{
vecEloData.clear();
QSqlQuery query;
query.prepare( "SELECT player_id, rating_start, rating, set_wins, "
"set_losses, match_wins, match_losses, attendances FROM elo" );
query.exec();
while( query.next() )
{
EloData eloData_;
eloData_.player_id = query.value(0).toInt();
eloData_.rating_start = query.value(1).toInt();
eloData_.rating = query.value(2).toInt();
eloData_.set_wins = query.value(3).toInt();
eloData_.set_losses = query.value(4).toInt();
eloData_.match_wins = query.value(5).toInt();
eloData_.match_losses = query.value(6).toInt();
eloData_.attendances = query.value(7).toInt();
vecEloData.push_back(eloData_);
}
if ( vecEloData.empty() )
return false;
return true;
}
bool Database::eloData(std::map<int,EloData>& mapEloData)
{
std::vector<EloData> vecEloData;
if ( !Database::eloData(vecEloData) ) return false;
mapEloData.clear();
for ( int i = 0; i < static_cast<int>(vecEloData.size()); i++ )
{
mapEloData[vecEloData[i].player_id] = vecEloData[i];
}
if ( mapEloData.empty() ) return false;
return true;
}
void Database::saveEloToTable( const std::map<int,EloData>& mapEloData )
{
QSqlQuery query;
for ( std::map<int,EloData>::const_iterator it = mapEloData.begin();
it != mapEloData.end(); ++it )
{
query.prepare( "UPDATE elo SET rating = :rating, set_wins = :set_wins,"
"set_losses = :set_losses, match_wins = :match_wins, "
"match_losses = :match_losses, attendances = :attendances "
"WHERE player_id = :player_id");
query.bindValue(":rating", it->second.rating);
query.bindValue(":set_wins", it->second.set_wins);
query.bindValue(":set_losses", it->second.set_losses);
query.bindValue(":match_wins", it->second.match_wins);
query.bindValue(":match_losses", it->second.match_losses);
query.bindValue(":attendances", it->second.attendances);
query.bindValue(":player_id", it->second.player_id);
if ( !query.exec() )
{ qDebug() << "saveEloToTable failed: " << query.lastError(); }
}
}
bool Database::playerExists( const QString& tag )
{
QSqlQuery query;
query.prepare( "SELECT tag FROM players WHERE tag = :tag COLLATE NOCASE" );
query.bindValue( ":tag", tag.toLower() );
query.exec();
if( query.next() ) { return true; }
return false;
}
QStringList Database::players( )
{
QStringList players;
QSqlQuery query;
if ( !query.exec( "SELECT tag FROM players" ) )
{ qDebug() << "players() failed: " << query.lastError(); }
while ( query.next() ){ players << query.value(0).toString(); }
players.sort();
return players;
}
QStringList Database::playerNames( )
{
QStringList players;
QSqlQuery query;
if ( !query.exec( "SELECT name FROM players" ) )
{ qDebug() << "players() failed: " << query.lastError(); }
while ( query.next() ){ players << query.value(0).toString(); }
players.sort();
return players;
}
QStringList Database::players( const QString& session_name )
{
QStringList ret;
if ( !sessionExists(session_name) ){ return ret; }
QSet<QString> players_in_session;
QSqlQuery query;
query.prepare("SELECT p1, p2 FROM matches WHERE session_id = :session_id");
query.bindValue( ":session_id", sessionID(session_name) );
if ( !query.exec( ) )
{ qDebug() << "players(session_name) failed: " << query.lastError(); }
while ( query.next() )
{
players_in_session.insert(query.value(0).toString());
players_in_session.insert(query.value(1).toString());
}
ret = players_in_session.toList(); ret.sort();
return ret;
}
QString Database::playerTag( const int player_id )
{
QSqlQuery query;
query.prepare( "SELECT tag FROM players where id = :player_id" );
query.bindValue(":player_id", player_id);
if ( !query.exec() )
{ qDebug() << "playerTag(player_id) failed: " << query.lastError(); }
if ( query.next() ){ return query.value(0).toString(); }
return "";
}
QString Database::playerName( const int player_id )
{
QSqlQuery query;
query.prepare( "SELECT name FROM players where id = :player_id" );
query.bindValue(":player_id", player_id);
if ( !query.exec() )
{ qDebug() << "playerName(player_id) failed: " << query.lastError(); }
if ( query.next() ){ return query.value(0).toString(); }
return "";
}
int Database::playerID( const QString& tag )
{
QSqlQuery query;
query.prepare( "SELECT id FROM players WHERE tag = :tag" );
query.bindValue(":tag", tag.toLower());
if ( !query.exec() )
{ qDebug() << "playerID() failed: " << query.lastError(); }
if ( query.next() ){ return query.value(0).toInt(); }
return -1;
}
bool Database::addPlayer( const QString& tag, const QString& name )
{
if (tag.compare("angelos", Qt::CaseInsensitive) == 0)
std::cout << "addPlayer: " << tag.toStdString() << std::endl;
if ( playerExists( tag ) ) { return false; }
QSqlQuery query;
query.prepare( "INSERT INTO players (tag, name) VALUES( :tag, :name)" );
query.bindValue(":tag", tag.toLower());
query.bindValue(":name", name.isEmpty() ? tag : name );
if ( !query.exec() )
{ qDebug() << "addPlayer failed: " << query.lastError(); }
if ( !query.exec("SELECT last_insert_rowid()") )
{ qDebug() << "addPlayer SELECT last_insert_rowid() failed: " << query.lastError(); }
int last_row_id = -1;
if ( query.next() ){ last_row_id = query.value(0).toInt(); }
if ( !addPlayerToEloRating(last_row_id) )
return false;
return true;
}
bool Database::sessionExists( const QString& name )
{
QSqlQuery query;
query.prepare( "SELECT name FROM sessions WHERE name = :name COLLATE NOCASE" );
query.bindValue( ":name", name );
query.exec();
if( query.next() ) { return true; }
return false;
}
bool Database::sessionExistsByChallongeID(const int challonge_id)
{
QSqlQuery query;
query.prepare( "SELECT id FROM sessions WHERE challonge_id = :challonge_id COLLATE NOCASE" );
query.bindValue( ":challonge_id", challonge_id );
query.exec();
if( query.next() ) { return true; }
return false;
}
QStringList Database::sessions( )
{
QStringList sessions;
QSqlQuery query;
if ( !query.exec( "SELECT name FROM sessions" ) )
{ qDebug() << "sessions() failed: " << query.lastError(); }
while ( query.next() ){ sessions << query.value(0).toString(); }
return sessions;
}
QStringList Database::sessionsByTournamentID( const int tournament_id )
{
QStringList sessions;
QSqlQuery query;
query.prepare( "SELECT name FROM sessions WHERE tournament_id = :tournament_id" );
query.bindValue( ":tournament_id", tournament_id );
if ( !query.exec() )
{ qDebug() << "sessionsByTournamentID() failed: " << query.lastError(); }
while ( query.next() ){ sessions << query.value(0).toString(); }
return sessions;
}
bool Database::addSession(
const QString& name,
const int challonge_id,
const int tournament_id,
const QString& date,
const QString& url )
{
if ( sessionExists(name) ) return false;
if ( sessionExistsByChallongeID(challonge_id) )
{
QMessageBox::warning( nullptr, "Error", "Cannot import session, has been imported already!" );
return false;
}
QString _date(date);
if ( _date.isEmpty() )
{
_date = QDateTime::currentDateTime().toString("yyyy-MM-dd");
}
QSqlQuery query;
query.prepare( "INSERT INTO sessions (name, challonge_id, tournament_id, date, url) "
"VALUES( :name, :challonge_id, :tournament_id, :date, :url)" );
query.bindValue(":name", name);
query.bindValue(":challonge_id", challonge_id);
query.bindValue(":tournament_id", tournament_id);
query.bindValue(":date", _date);
query.bindValue(":url", url);
if ( !query.exec() )
{ qDebug() << "addSession failed: " << query.lastError(); return false; }
return true;
}
bool Database::updateSession( const int id,
const QString& name,
const int challonge_id,
const int tournament_id,
const QString& date,
const QString& url)
{
if ( id < 0 ) return false;
if ( !sessionExists(name) ) return false;
QSqlQuery query;
query.prepare( "UPDATE sessions SET name = :name, "
"challonge_id = :challonge_id, "
"tournament_id = :tournament_id, "
"date = :date, url = :url "
"WHERE id = :id" );
query.bindValue(":name", name);
query.bindValue(":challonge_id", challonge_id);
query.bindValue(":tournament_id", tournament_id);
query.bindValue(":date", date);
query.bindValue(":url", url);
query.bindValue(":id", id );
if ( !query.exec() )
{ qDebug() << "updateSession failed: " << query.lastError(); return false; }
return true;
}
bool Database::deleteSession( const QString& name )
{
if ( !sessionExists(name) ) return false;
quint32 id = sessionID(name);
QSqlQuery query;
query.prepare( "Delete FROM sessions WHERE id = :id" );
query.bindValue(":id", id);
if ( !query.exec() )
{ qDebug() << "deleteSession failed: " << query.lastError(); return false; }
query.prepare( "Delete FROM matches WHERE session_id = :id" );
query.bindValue(":id", id);
if ( !query.exec() )
{ qDebug() << "deleteSession failed: " << query.lastError(); return false; }
return true;
}
int Database::sessionID( const QString& session_name )
{
if ( !sessionExists(session_name) ) return -1;
QSqlQuery query;
query.prepare( "SELECT id FROM sessions WHERE name = :name" );
query.bindValue(":name", session_name);
if ( !query.exec() )
{ qDebug() << "sessionID failed: " << query.lastError(); }
if ( query.next() ) return query.value(0).toInt();
return -1;
}
int Database::sessionChallongeID( const QString& session_name )
{
if ( !sessionExists(session_name) ) return -1;
QSqlQuery query;
query.prepare( "SELECT challonge_id FROM sessions WHERE name = :name" );
query.bindValue(":name", session_name);
if ( !query.exec() )
{ qDebug() << "sessionChallongeID failed: " << query.lastError(); }
if ( query.next() ) return query.value(0).toInt();
return -1;
}
int Database::sessionTournamentID( const QString& session_name )
{
if ( !sessionExists(session_name) ) return -1;
QSqlQuery query;
query.prepare( "SELECT tournament_id FROM sessions WHERE name = :name" );
query.bindValue(":name", session_name);
if ( !query.exec() )
{ qDebug() << "sessionTournamentID failed: " << query.lastError(); }
if ( query.next() ) return query.value(0).toInt();
return -1;
}
QString Database::sessionDate( const QString& session_name )
{
if ( !sessionExists(session_name) ) return "";
QSqlQuery query;
query.prepare( "SELECT date FROM sessions WHERE name = :name" );
query.bindValue(":name", session_name);
if ( !query.exec() )
{ qDebug() << "sessionDate failed: " << query.lastError(); }
if ( query.next() ) return query.value(0).toString();
return "";
}
QString Database::sessionURL( const QString& session_name )
{
if ( !sessionExists(session_name) ) return "";
QSqlQuery query;
query.prepare( "SELECT url FROM sessions WHERE name = :name" );
query.bindValue(":name", session_name);
if ( !query.exec() )
{ qDebug() << "sessionURL failed: " << query.lastError(); }
if ( query.next() ) return query.value(0).toString();
return "";
}
bool Database::addOrUpdateMatch(
const QString& session_name,
const int challonge_id,
const int session_tournament_id,
const QString& p1_tag,
const QString& p1_name,
const QString& p2_tag,
const QString& p2_name,
const int round,
const quint32 suggested_play_order,
const int p1_wins,
const int p2_wins)
{
if ( !sessionExists(session_name) ){ addSession(session_name, session_tournament_id, challonge_id); }
if ( !playerExists(p1_tag) ){ addPlayer(p1_tag, p1_name); }
if ( !playerExists(p2_tag) ){ addPlayer(p2_tag, p2_name); }
const int session_id = sessionID(session_name);
if ( session_id < 0 )
{ qDebug() << "addOrUpdateMatch failed! Invalid session id."; return false; }
if ( p1_wins == p2_wins )
{
qDebug() << "Adding or Updating Match failed!",
"Cannot insert a tie for elo ranking! " + p1_tag + " vs " + p2_tag;
return false;
}
if ( p1_wins < 0 || p2_wins < 0 )
{
qDebug() << "Adding or Updating Match failed!",
"Cannot insert a DQ/Leave for elo ranking! " + p1_tag + " vs " + p2_tag;
return false;
}
QSqlQuery query;
quint32 match_id, suggested_play_order_(suggested_play_order);
int p1_wins_, p2_wins_;
int round_(round);
bool matchExists = matchResults_p( session_name, p1_tag, p2_tag, round_,
suggested_play_order_, p1_wins_, p2_wins_, match_id);
query.clear();
if ( matchExists )
{
// if ( !query.prepare( "UPDATE matches SET session_id = :session_id, round = :round, "
// "suggested_play_order = :suggested_play_order, "
// "p1 = :p1, p2 = :p2, p1_wins = :p1_wins, p2_wins = :p2_wins "
// "WHERE id = :match_id" ) )
// { qDebug() << "addorUpdateMatch failed: " << query.lastError(); return false; }
if ( !query.prepare( "UPDATE matches SET p1_wins = :p1_wins, p2_wins = :p2_wins "
"WHERE id = :match_id" ) )
{ qDebug() << "addorUpdateMatch failed: " << query.lastError(); return false; }
query.bindValue(":p1_wins", p1_wins);
query.bindValue(":p2_wins", p2_wins);
query.bindValue(":match_id", match_id );
}
else
{
if ( !query.prepare( "INSERT INTO matches (session_id, round, suggested_play_order, p1, p2, p1_wins, p2_wins) "
"VALUES( :session_id, :round, :suggested_play_order, :p1, :p2, :p1_wins, :p2_wins )" ) )
{ qDebug() << "addorUpdateMatch failed: " << query.lastError(); return false; }
query.bindValue(":session_id", session_id);
query.bindValue(":round", round);
query.bindValue(":suggested_play_order", suggested_play_order);
query.bindValue(":p1", p1_tag);
query.bindValue(":p2", p2_tag);
query.bindValue(":p1_wins", p1_wins);
query.bindValue(":p2_wins", p2_wins);
}
// query.bindValue(":session_id", session_id);
// query.bindValue(":round", round);
// query.bindValue(":suggested_play_order", suggested_play_order);
// query.bindValue(":p1", p1);
// query.bindValue(":p2", p2);
// query.bindValue(":p1_wins", p1_wins);
// query.bindValue(":p2_wins", p2_wins);
// if ( matchExists ) { query.bindValue(":match_id", match_id ); }
if ( !query.exec() )
{ qDebug() << "addorUpdateMatch failed: " << query.lastError(); return false; }
return true;
}
bool Database::matchResults( const QString& p1_tag,
const QString& p2_tag,
int& p1_set_wins,
int& p2_set_wins,
int& p1_match_wins,
int& p2_match_wins)
{
p1_set_wins = 0;
p2_set_wins = 0;
p1_match_wins = 0;
p2_match_wins = 0;
{
QSqlQuery query;
query.prepare("SELECT p1_wins, p2_wins FROM matches WHERE "
"p1 LIKE :p1 AND p2 LIKE :p2");
query.bindValue( ":p1", p1_tag );
query.bindValue( ":p2", p2_tag );
if ( !query.exec( ) )
{ qDebug() << "matchResults_p() failed: " << query.lastError(); return false; }
while ( query.next() )
{
int p1_wins = query.value(0).toInt();
int p2_wins = query.value(1).toInt();
if ( p1_wins > p2_wins )
p1_set_wins++;
else
p2_set_wins++;
p1_match_wins += p1_wins;
p2_match_wins += p2_wins;
}
}
{
QSqlQuery query;
query.prepare("SELECT p1_wins, p2_wins FROM matches WHERE "
"p1 LIKE :p1 AND p2 LIKE :p2");
query.bindValue( ":p2", p1_tag );
query.bindValue( ":p1", p2_tag );
if ( !query.exec( ) )
{ qDebug() << "matchResults_p() failed: " << query.lastError(); return false; }
while ( query.next() )
{
int p1_wins = query.value(1).toInt();
int p2_wins = query.value(0).toInt();
if ( p1_wins > p2_wins )
p1_set_wins++;
else
p2_set_wins++;
p1_match_wins += p1_wins;
p2_match_wins += p2_wins;
}
}
return true;
}
bool Database::matchResults( const QString& session_name,
const QString& p1_tag,
const QString& p2_tag,
int& round,
quint32& suggested_play_order,
int& p1_wins,
int& p2_wins)
{
if ( !sessionExists(session_name) || !playerExists(p1_tag) || !playerExists(p2_tag) )
return false;
quint32 match_id = -1;
if ( !matchResults_p(session_name, p1_tag, p2_tag, round,
suggested_play_order, p1_wins, p2_wins, match_id ) )
{ return false; }
return true;
}
bool Database::matchResults_p( const QString& session_name,
const QString& p1,
const QString& p2,
int& round,
quint32& suggested_play_order,
int& p1_wins,
int& p2_wins,
quint32& match_id )
{
QSqlQuery query;
query.prepare("SELECT id, round, suggested_play_order, p1_wins, p2_wins FROM matches WHERE "
"session_id = :session_id AND p1 = :p1 AND p2 = :p2 AND "
"round = :round AND "
"suggested_play_order = :suggested_play_order");
query.bindValue( ":session_id", sessionID(session_name) );
query.bindValue( ":p1", p1 );
query.bindValue( ":p2", p2 );
query.bindValue( ":round", round );
query.bindValue( ":suggested_play_order", suggested_play_order );
if ( !query.exec( ) )
{ qDebug() << "matchResults_p() failed: " << query.lastError(); return false; }
if ( query.next() )
{
match_id = query.value(0).toInt();
round = query.value(1).toInt();
suggested_play_order = query.value(2).toInt();
p1_wins = query.value(3).toInt();
p2_wins = query.value(4).toInt();
return true;
}
// try another p1 / p2 combination
query.prepare("SELECT id, round, suggested_play_order, p1_wins, p2_wins FROM matches WHERE "
"session_id = :session_id AND p1 = :p1 AND p2 = :p2 AND "
"round = :round AND "
"suggested_play_order = :suggested_play_order");
query.bindValue( ":session_id", sessionID(session_name) );
query.bindValue( ":p1", p2 );
query.bindValue( ":p2", p1 );
query.bindValue( ":round", round );
query.bindValue( ":suggested_play_order", suggested_play_order );
if ( !query.exec( ) )
{ qDebug() << "matchResults_p() failed: " << query.lastError(); return false; }
if ( query.next() )
{
match_id = query.value(0).toInt();
round = query.value(1).toInt();
suggested_play_order = query.value(2).toInt();
p2_wins = query.value(3).toInt();
p1_wins = query.value(4).toInt();
return true;
}
return false;
}
bool Database::matchResults( const QString& session_name,
std::vector<MatchData>& match_data)
{
match_data.clear();
if ( !sessionExists(session_name) ){ return false; }
QSqlQuery query;
query.prepare("SELECT id, round, suggested_play_order, p1, p2, p1_wins, p2_wins FROM matches WHERE "
"session_id = :session_id");
query.bindValue( ":session_id", sessionID(session_name) );
if ( !query.exec( ) )
{ qDebug() << "matchResults() failed: " << query.lastError(); return false; }
while ( query.next() )
{
//int match_id = query.value(0).toInt();
MatchData matchDate(
query.value(1).toInt(),
query.value(2).toInt(),
PlayerData( Database::playerID(query.value(3).toString()), query.value(3).toString().toLower(), query.value(3).toString() ),
PlayerData( Database::playerID(query.value(4).toString()), query.value(4).toString().toLower(), query.value(4).toString() ),
query.value(5).toInt(),
query.value(6).toInt() );
match_data.push_back( matchDate );
}
return true;
}
//void Database::obtainPools( const QString& session_name,
// std::vector< std::vector< StandingData > >& poolStandings )
//{
// poolStandings.clear();
// if ( Database::sessionType(session_name) != SessionType::Pools ) return;
// std::vector<MatchData> match_data;
// matchResults( session_name, match_data );
// QStringList player_data = players( session_name );
// // construct pools
// std::vector< QSet< QString > > pools, pools_;
// // add to "pools_"
// for ( size_t i = 0; i < match_data.size(); i++ )
// {
// const MatchData& match_date = match_data[i];
// QString p1_name = match_date.p1.tag;
// QString p2_name = match_date.p2.tag;
// int p1_pool(-1), p2_pool(-1);
// for ( size_t i = 0; i < pools_.size(); i++ )
// {
// const QSet<QString>& pool = pools_[i];
// if ( pool.contains(p1_name) ) { p1_pool = i; }
// else if ( pool.contains(p2_name) ) { p2_pool = i; }
// }
// if ( p1_pool < 0 && p2_pool < 0 )
// {
// pools_.push_back( QSet<QString>() );
// pools_.back().insert( p1_name );
// pools_.back().insert( p2_name );
// }
// else if ( p1_pool >= 0 ){ pools_[p1_pool].insert( p2_name ); }
// else if ( p2_pool >= 0 ){ pools_[p1_pool].insert( p1_name ); }
// }
// // merge "pools"
// for ( size_t i = 0; i < pools_.size(); i++ )
// {
// int pool_found_id = -1;
// const QSet< QString >& pool_ = pools_[i];
// for ( QSet< QString >::const_iterator it = pool_.begin();
// it != pool_.end(); ++it )
// {
// const QString& player_ = *it;
// for ( size_t k = 0; k < pools.size(); k++ )
// {
// if ( pools[k].contains(player_) )
// { pool_found_id = k; break; }
// }
// }
// if ( pool_found_id < 0 )
// {
// pools.push_back( pool_ );
// }
// else
// {
// for ( QSet< QString >::const_iterator it = pool_.begin();
// it != pool_.end(); ++it )
// { pools[pool_found_id].insert( *it ); }
// }
// }
// QMap< QString, StandingData > standings_map;
// for ( size_t i = 0; i < match_data.size(); i++ )
// {
// const MatchData& matchDate = match_data[i];
// {
// StandingData& standingDate = standings_map[ matchDate.p1.tag ];
// standingDate.player = matchDate.p1.tag;
// if ( matchDate.p1_wins > matchDate.p2_wins )
// {
// standingDate.won_against.insert( matchDate.p2.tag );
// standingDate.set_wins++;
// }
// else { standingDate.set_losses++; }
// standingDate.game_wins += matchDate.p1_wins;
// standingDate.game_losses += matchDate.p2_wins;
// }
// {
// StandingData& standingDate = standings_map[ matchDate.p2.tag ];
// standingDate.player = matchDate.p2.tag;
// if ( matchDate.p2_wins > matchDate.p1_wins )
// {
// standingDate.won_against.insert( matchDate.p1.tag );
// standingDate.set_wins++;
// }
// else { standingDate.set_losses++; }
// standingDate.game_wins += matchDate.p2_wins;
// standingDate.game_losses += matchDate.p1_wins;
// }
// }
// poolStandings.resize(pools.size());
// for ( QMap< QString, StandingData >::const_iterator it = standings_map.begin();
// it != standings_map.end(); ++it )
// {
// int pool_idx = 0;
// for ( size_t j = 0; j < pools.size(); j++ )
// {
// if ( pools[j].contains( it.key() ) )
// { pool_idx = j; break; }
// }
// poolStandings[pool_idx].push_back( it.value() );
// }
//}