-
Notifications
You must be signed in to change notification settings - Fork 5
/
rent.c
207 lines (189 loc) · 6.54 KB
/
rent.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pcre.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <time.h>
#include "global.h"
#include "bug.h"
#include "utils.h"
#include "comm.h" // for proper_exit() and MUD_HALT
#include "sql.h"
#define _RENT_C
#include "rent.h"
struct rent_data rent = {BEGINNING_OF_TIME, 1, 1.0, "SYSTEM"}; // beginning of time...
void setup_rent_table(void)
{
PGresult *res = NULL;
ExecStatusType st = (ExecStatusType) 0;
const char *sql = "CREATE TABLE IF NOT EXISTS rent ( "
" updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "
" enabled BOOLEAN NOT NULL DEFAULT true, "
" factor FLOAT NOT NULL DEFAULT 1.0, "
" set_by TEXT NOT NULL DEFAULT 'SYSTEM'"
"); ";
const char *sql2 = "SELECT count(*) FROM rent;";
const char *sql3 = "INSERT INTO rent (enabled) VALUES (true);";
const char *sql4 = "DELETE FROM rent WHERE updated <> (SELECT max(UPDATED) FROM rent);";
int rows = 0;
int columns = 0;
int count = 0;
sql_connect(&db_wileymud);
res = PQexec(db_wileymud.dbc, sql);
st = PQresultStatus(res);
if (st != PGRES_COMMAND_OK && st != PGRES_TUPLES_OK && st != PGRES_SINGLE_TUPLE)
{
log_fatal("Cannot create rent table: %s", PQerrorMessage(db_wileymud.dbc));
PQclear(res);
proper_exit(MUD_HALT);
}
PQclear(res);
res = PQexec(db_wileymud.dbc, sql2);
st = PQresultStatus(res);
if (st != PGRES_COMMAND_OK && st != PGRES_TUPLES_OK && st != PGRES_SINGLE_TUPLE)
{
log_fatal("Cannot get row count of rent table: %s", PQerrorMessage(db_wileymud.dbc));
PQclear(res);
proper_exit(MUD_HALT);
}
rows = PQntuples(res);
columns = PQnfields(res);
if (rows > 0 && columns > 0)
{
count = atoi(PQgetvalue(res, 0, 0));
}
else
{
log_fatal("Invalid result set from row count!");
PQclear(res);
proper_exit(MUD_HALT);
}
PQclear(res);
if (count < 1)
{
// Insert our default value.
res = PQexec(db_wileymud.dbc, sql3);
st = PQresultStatus(res);
if (st != PGRES_COMMAND_OK && st != PGRES_TUPLES_OK && st != PGRES_SINGLE_TUPLE)
{
log_fatal("Cannot insert placeholder row to rent table: %s", PQerrorMessage(db_wileymud.dbc));
PQclear(res);
proper_exit(MUD_HALT);
}
PQclear(res);
}
else if (count > 1)
{
// That shouldn't happen... fix it!
log_error("There are %d rows in the rent table, instead of just ONE!", count);
res = PQexec(db_wileymud.dbc, sql4);
st = PQresultStatus(res);
if (st != PGRES_COMMAND_OK && st != PGRES_TUPLES_OK && st != PGRES_SINGLE_TUPLE)
{
log_fatal("Cannot remove old rows from rent table: %s", PQerrorMessage(db_wileymud.dbc));
PQclear(res);
proper_exit(MUD_HALT);
}
PQclear(res);
}
}
void load_rent(void)
{
PGresult *res = NULL;
ExecStatusType st = (ExecStatusType) 0;
const char *sql = "SELECT extract('epoch' FROM updated) AS updated, "
"enabled::integer, "
"factor, set_by FROM rent LIMIT 1;";
int rows = 0;
int columns = 0;
struct rent_data the_rent = {BEGINNING_OF_TIME, 1, 1.0, "SYSTEM"}; // beginning of time...
sql_connect(&db_wileymud);
res = PQexec(db_wileymud.dbc, sql);
st = PQresultStatus(res);
if (st != PGRES_COMMAND_OK && st != PGRES_TUPLES_OK && st != PGRES_SINGLE_TUPLE)
{
log_fatal("Cannot get rent from rent table: %s", PQerrorMessage(db_wileymud.dbc));
PQclear(res);
proper_exit(MUD_HALT);
}
rows = PQntuples(res);
columns = PQnfields(res);
if (rows > 0 && columns > 3)
{
log_boot(" Loading rent data from SQL database.");
the_rent.updated = (time_t)atol(PQgetvalue(res, 0, 0));
the_rent.enabled = (int)atoi(PQgetvalue(res, 0, 1));
the_rent.factor = (float)atof(PQgetvalue(res, 0, 2));
strlcpy(the_rent.set_by, PQgetvalue(res, 0, 3), MAX_INPUT_LENGTH);
}
else
{
log_fatal("Invalid result set from rent table!");
PQclear(res);
proper_exit(MUD_HALT);
}
PQclear(res);
rent = the_rent;
}
int toggle_rent(struct char_data *ch)
{
PGresult *res = NULL;
ExecStatusType st = (ExecStatusType) 0;
const char *sql = "UPDATE rent SET enabled = NOT enabled, "
"updated = now(), "
"set_by = $1;";
const char *param_val[1];
int param_len[1];
int param_bin[1] = {0};
char set_by[MAX_INPUT_LENGTH] = "\0\0\0\0\0\0\0";
strlcpy(set_by, GET_NAME(ch), MAX_INPUT_LENGTH);
param_val[0] = (set_by[0]) ? set_by : NULL;
param_len[0] = (set_by[0]) ? strlen(set_by) : 0;
sql_connect(&db_wileymud);
res = PQexecParams(db_wileymud.dbc, sql, 1, NULL, param_val, param_len, param_bin, 0);
st = PQresultStatus(res);
if (st != PGRES_COMMAND_OK && st != PGRES_TUPLES_OK && st != PGRES_SINGLE_TUPLE)
{
log_error("Cannot toggle rent in rent table: %s", PQerrorMessage(db_wileymud.dbc));
PQclear(res);
return FALSE;
// proper_exit(MUD_HALT);
}
PQclear(res);
load_rent();
return TRUE;
}
int set_rent(struct char_data *ch, float factor)
{
PGresult *res = NULL;
ExecStatusType st = (ExecStatusType) 0;
const char *sql = "UPDATE rent SET factor = $1, "
"updated = now(), "
"set_by = $2;";
const char *param_val[2];
int param_len[2];
int param_bin[2] = {0, 0};
char set_by[MAX_INPUT_LENGTH] = "\0\0\0\0\0\0\0";
char factor_buf[MAX_INPUT_LENGTH] = "\0\0\0\0\0\0\0";
snprintf(factor_buf, MAX_INPUT_LENGTH, "%f", factor);
param_val[0] = (factor_buf[0]) ? factor_buf : NULL;
param_len[0] = (factor_buf[0]) ? strlen(factor_buf) : 0;
strlcpy(set_by, GET_NAME(ch), MAX_INPUT_LENGTH);
param_val[1] = (set_by[0]) ? set_by : NULL;
param_len[1] = (set_by[0]) ? strlen(set_by) : 0;
sql_connect(&db_wileymud);
res = PQexecParams(db_wileymud.dbc, sql, 2, NULL, param_val, param_len, param_bin, 0);
st = PQresultStatus(res);
if (st != PGRES_COMMAND_OK && st != PGRES_TUPLES_OK && st != PGRES_SINGLE_TUPLE)
{
log_error("Cannot set rent in rent table: %s", PQerrorMessage(db_wileymud.dbc));
PQclear(res);
return FALSE;
// proper_exit(MUD_HALT);
}
PQclear(res);
load_rent();
return TRUE;
}