aboutsummaryrefslogtreecommitdiff
path: root/src/UdpTasks/StatusChecker/playerinfoinsert.cpp
blob: c98b8b45eab411396d06b0709ad89754fb379191 (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
#include "statuschecker.h"

bool StatusChecker::insertPlayerInfo(const QString                 &serverAddress,
                                     const unsigned short          &serverPort,
                                     const QHash<QString, QString> &serverInfo)
{
    QSqlQuery q;
    int serverID = -1;

    // TODO: combine from 3 to 2 queries?
    // (query optimisation)

    // get server ID first
    QString selectString = "SELECT id FROM serverlist "
                           "WHERE ip = :ip AND queryport = :queryport ";

    q.prepare(selectString);
    q.bindValue(":ip",        serverAddress);
    q.bindValue(":queryport", serverPort);

    if ( ! q.exec() )
        return reportQuery(q);

    if ( q.next() ) // else serverID remains -1
        serverID = q.value(0).toUInt();

    // remove old player entries
    QString deleteString = "DELETE FROM playerinfo WHERE sid = :sid";
    q.prepare(deleteString);
    q.bindValue(":sid", serverID);
    if ( ! q.exec() )
        return reportQuery(q);

    // hotfix 4: some player data may be missing intermittently (e.g player 1, 2, 4, 5 )

    // regex to find player_%d
    static QRegularExpression p_regex("player_(\\d+)");

    // iterate all serverinfo fields for player_%d
    QHashIterator<QString, QString> infoIterator(serverInfo);
    while ( infoIterator.hasNext() )
    {
        infoIterator.next();

        // match player id in player_%d
        QRegularExpressionMatch p_match = p_regex.match( infoIterator.key() );
        if ( p_match.hasMatch()  )
        {
            // extract the player_id from matched string
            QString playerIndexString = p_match.captured(0);
            int playerIndex = playerIndexString.remove("player_").toInt();

            // and insert the whole player dataset into the db (no sanity check needed, to defaults)
            QString insertString = "INSERT INTO playerinfo "
                                   "(sid, name, team, frags, mesh, skin, face, ping, misc, dt_player) "
                                   "VALUES (:sid, :name, :team, :frags, :mesh, :skin, :face, :ping, :misc, :dt_player)";
            q.prepare(insertString);
            q.bindValue(":sid",   serverID);
            q.bindValue(":name",  serverInfo.value(QStringLiteral("player_%1").arg(playerIndex), "Player"));
            q.bindValue(":team",  serverInfo.value(QStringLiteral(  "team_%1").arg(playerIndex), "0"));
            q.bindValue(":frags", serverInfo.value(QStringLiteral( "frags_%1").arg(playerIndex), "0"));
            q.bindValue(":mesh",  serverInfo.value(QStringLiteral(  "mesh_%1").arg(playerIndex), "default"));
            q.bindValue(":skin",  serverInfo.value(QStringLiteral(  "skin_%1").arg(playerIndex), "default"));
            q.bindValue(":face",  serverInfo.value(QStringLiteral(  "face_%1").arg(playerIndex), "default"));
            q.bindValue(":ping",  serverInfo.value(QStringLiteral(  "ping_%1").arg(playerIndex), "0"));
            q.bindValue(":misc",  ""); // reserved for additional query info
            q.bindValue(":dt_player", QDateTime::currentSecsSinceEpoch() );
            if ( ! q.exec() )
                return reportQuery(q);
        } // hasMatch
    }

    return true;
}