summaryrefslogtreecommitdiff
path: root/stat.c
blob: d60df312eb9d02772ad3dcb360e4a03ff819c28e (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
/*
 * Created by yuuta on 4/5/22.
 */

#include "logic.h"
#include "log.h"
#include "tdutils.h"
#include "db.h"

#include <stdlib.h>
#include <string.h>

static char *praises[] = {
        "普天之下,莫非 K 土。TGCN 共有 %d 位 K 教授的虔诚学生卖了 %d 句菜。",
        "率土之滨,莫非 K 臣。TGCN 共有 %d 位 K 教授的虔诚学生卖了 %d 句菜。",
};

void stat(struct TdMessage *msg) {
    int r;
    char m[1024];
    sqlite3_stmt *stmt = NULL;
    if ((r = sqlite3_prepare_v2(db, "SELECT COUNT(user), SUM(i) FROM stats;", -1, &stmt, NULL))) {
        goto sql_err;
        sql_err:
        {
            snprintf(m, 1024, "Cannot query statistics: %s.", sqlite3_errstr(r));
            LOGEV("%s", m);
            if (stmt) sqlite3_finalize(stmt);
            goto send;
        }
    }
    if ((r = sqlite3_step(stmt)) != SQLITE_ROW) goto sql_err;
    snprintf(m, 1024, praises[rand() % (sizeof(praises) / sizeof(char *))],
             sqlite3_column_int(stmt, 0),
             sqlite3_column_int(stmt, 1));
    sqlite3_finalize(stmt);
    stmt = NULL;

    if ((r = sqlite3_prepare_v2(db, "SELECT COUNT(t) FROM says;", -1, &stmt, NULL))) goto sql_err;
    if ((r = sqlite3_step(stmt)) != SQLITE_ROW) goto sql_err;
    size_t len = strlen(m);
    snprintf(&m[len], 1024 - len, "\n\n共有 %d 句教授语录。", sqlite3_column_int(stmt, 0));
    sqlite3_finalize(stmt);
    goto send;
    send:
    td_send(TdCreateObjectSendMessage(msg->chat_id_,
                                      0,
                                      msg->id_,
                                      TdCreateObjectMessageSendOptions(false, false, true, NULL),
                                      NULL,
                                      (struct TdInputMessageContent *)
                                              TdCreateObjectInputMessageText(
                                                      TdCreateObjectFormattedText(m,
                                                                                  (struct TdVectorTextEntity *)
                                                                                          TdCreateObjectVectorObject(
                                                                                                  0, NULL)
                                                      ),
                                                      false,
                                                      false)),
            NULL, NULL);
}