aboutsummaryrefslogtreecommitdiff
path: root/cd.c
blob: 9b875127b848f678aa0e6e4b1f89323e616d5a38 (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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <sqlite3.h>
#include "segmenttree.h"

int main(int argc, char* argv[]) {
	char *file = "cards";
	bool verbose = false;

	/* Proccess args */
	static struct option long_options[] = {
		{"file",    required_argument, NULL, 'f'},
		{"verbose", no_argument,       NULL, 'v'}
	};
	while (1) {
		int option_index = 0;
		int c = getopt_long(argc, argv, "f:v", long_options, &option_index);
		if (c == -1) break;
		switch (c) {
			case 'f': file = strdup(optarg); break;
			case 'v': verbose = true; break;
			default: abort();
		}
	}

	/* Seed the RNG */
	srand(time(0));

	/* Connect to db */
	sqlite3 *db;
	int rc = sqlite3_open(file, &db);
	if (rc) {
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		abort();
	}

	/* Get number of cards */
	sqlite3_stmt *stmt;
	rc = sqlite3_prepare_v3(db, "SELECT COUNT(*) FROM cards", -1, 0, &stmt, NULL);
	if (rc) {
    	fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db));
		abort();
	}
	sqlite3_step(stmt);
	int N = sqlite3_column_int(stmt, 0);
	sqlite3_finalize(stmt);

	/* Get card weights */
	rc = sqlite3_prepare_v3(db, "SELECT weight FROM cards", -1, 0, &stmt, NULL);
	if (rc) {
    	fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db));
		abort();
	}
	seg = malloc(4 * N * sizeof(int));
	build(stmt, 0, N - 1, 1);
	sqlite3_finalize(stmt);

	if (verbose) {
		for (int i = 0; i < N; i++) {
			printf("%d ", seg[i]);
		}
		printf("\n");
	}

	/* Disable input buffering */
	system("stty -F /dev/tty cbreak min 1");
	system("stty -F /dev/tty -echo");

	while (true) {
		int x = (long long)rand() * rand() % seg[1];
		int res[2];
		query(res, x, 0, N-1, 1);
		int w = res[0], i = res[1];

		if (verbose) {
			printf("%d %d %d %d\n", seg[1], x, w, i);
		}

		/* Get card contents from database */
		rc = sqlite3_prepare_v3(db, "SELECT key, val FROM cards WHERE idx=?", -1, 0, &stmt, NULL);
		if (rc) {
    		fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db));
			abort();
		}
		sqlite3_bind_int(stmt, 1, i);
		sqlite3_step(stmt);
		char *key, *val;
		key = strdup(sqlite3_column_text(stmt, 0));
		val = strdup(sqlite3_column_text(stmt, 1));
		sqlite3_finalize(stmt);

		printf("> %s\n", key);

		/* Wait for confirmation */
		char b = getchar();
		printf("%s\n", val);

		/* Read user input */
		b = getchar();
		if (b == 'y') {
			w >>= 1;
		}
		else if (b == 'n') {
			w <<= 3;
		}
		else {
			break;
		}

		/* Update segment tree and database */
		update(i, w, 0, N - 1, 1);
		rc = sqlite3_prepare_v3(db, "UPDATE cards SET weight=? WHERE idx=?", -1, 0, &stmt, NULL);
		if (rc) {
    		fprintf(stderr, "Database error: %s\n", sqlite3_errmsg(db));
			abort();
		}
		sqlite3_bind_int(stmt, 1, w);
		sqlite3_bind_int(stmt, 2, i);
		sqlite3_step(stmt);
		sqlite3_finalize(stmt);
	}

	/* Cleanup */
	system("stty -F /dev/tty echo");
	sqlite3_close(db);
}