Revised repository organization

This commit is contained in:
2017-09-12 02:48:29 -07:00
parent 068c8462d7
commit f18aec4ff5
8 changed files with 204 additions and 18 deletions

180
src/colors.c Normal file
View File

@@ -0,0 +1,180 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include "colors.h"
#define MIN_CONTRAST 0.5
void hsl2rgb(struct Rgb *dest, const struct Hls const *src) {
if ((src -> h < 0 || 360 < src -> h) ||
(src -> s < 0 && 1 < src -> s) ||
(src -> l < 0 && 1 < src -> l)) {
return;
}
float r, g, b;
float hue = (src -> h)/60.0f;
const float chroma = (1 - fabs((2 * (src -> l)) - 1)) * (src -> s);
float x = chroma * (1 - fabs(fmod(hue, 2) - 1));
float m = (src -> l) - (chroma/2);
if (0 <= hue && hue < 1) {
r = chroma;
g = x;
b = 0;
} else if (1 <= hue && hue < 2) {
r = x;
g = chroma;
b = 0;
} else if (2 <= hue && hue < 3) {
r = 0;
g = chroma;
b = x;
} else if (3 <= hue && hue < 4) {
r = 0;
g = x;
b = chroma;
} else if (4 <= hue && hue < 5) {
r = x;
g = 0;
b = chroma;
} else if (5 <= hue && hue < 6) {
r = chroma;
g = 0;
b = x;
} else {
r = 0;
g = 0;
b = 0;
}
dest -> r = ceil((r + m) * 255);
dest -> g = ceil((g + m) * 255);
dest -> b = ceil((b + m) * 255);
}
void rgb2hsl(struct Hls *dest, const struct Rgb const *src) {
if (src == NULL || dest == NULL) {
return;
}
float r = (src -> r) / 255.0f;
float g = (src -> g) / 255.0f;
float b = (src -> b) / 255.0f;
float min = fmin(fmin(r, g), b);
float max = fmax(fmax(r, g), b);
float chroma = max - min;
dest -> l = (max + min)/2; //bi-hexcone model.
if (chroma == 0) { // neutral color.
dest -> h = 0; // undefined.
dest -> s = 0.0f;
} else {
dest -> s = (dest -> l > 0.5) ? (chroma/(2 - max - min)) : (chroma/(max + min));
//piecewise function..
float hue;
if (max == r) {
hue = ((g - b)/chroma) + (g < b ? 6 : 0); // % 6 will break magenta (255, 0, 255)
} else if (max == g) {
hue = ((b - r)/chroma) + 2.0f;
} else {
hue = ((r - g)/chroma) + 4.0f;
}
dest -> h = (int)(hue * 60);
}
}
static void decspan(const int d) {
switch(d) {
case 0:
printf("<span style='color:rgb(128,128,128)'>");
break;
case 1:
printf("<span style='color:rgb(255,0,0)'>");
break;
case 2:
printf("<span style='color:rgb(51,255,0)'>");
break;
case 3:
printf("<span style='color:rgb(255,255,0)'>");
break;
case 4:
printf("<span style='color:rgb(51,102,255)'>");
break;
case 5:
printf("<span style='color:rgb(51,255,255)'>");
break;
case 6:
printf("<span style='color:rgb(255,51,102)'>");
break;
case 7:
printf("<span style='color:rgb(255,255,255)'>");
break;
case 8:
printf("<span style='color:rgb(153,153,153)'>");
break;
case 9:
printf("<span style='color:rgb(128,128,128)'>");
break;
}
}
static void hexspan(const char *str) {
const char h1[2] = {str[0], '\0'};
const char h2[2] = {str[1], '\0'};
const char h3[2] = {str[2], '\0'};
struct Rgb nrgb;
// max value for a color code in darkplaces = 0xF
// 0xF * 0xF = 255, max value for rgb
nrgb.r = strtol(h1, NULL, 16) * 0xF;
nrgb.g = strtol(h2, NULL, 16) * 0xF;
nrgb.b = strtol(h3, NULL, 16) * 0xF;
struct Hls nhls;
rgb2hsl(&nhls, &nrgb);
if (nhls.l < MIN_CONTRAST) {
nhls.l = MIN_CONTRAST;
hsl2rgb(&nrgb, &nhls);
}
printf("<span style=\"color:rgb(%d,%d,%d)\">", nrgb.r, nrgb.g, nrgb.b);
}
static void b(char * const str) {
char *token = strtok(str, "^");
char c;
printf("<TD>");
while (token) {
c = token[0];
if (isdigit(c)) {
decspan(c - '0');
if (strlen(token) > 1) {
printf("%s", token + 1);
}
printf("</span>");
} else if ((c == 'x' && strlen(token) > 3) &&
(isxdigit(token[1]) &&
isxdigit(token[2]) &&
isxdigit(token[3]))) {
hexspan(token + 1); //exclude x
if (strlen(token) > 4){
printf("%s", token + 4);
}
printf("</span>");
} else {
printf("%s", token);
}
token = strtok(NULL, "^");
}
printf("</TD>");
}
void print_plname(const char* str) {
//find instance of ^^
//replace with ^
//concatenate with rest of the string
char *copy;
copy = calloc(strlen(str) + 1, sizeof(char));
strcpy(copy, str);
char *pos = copy;
while (pos = strstr(pos, "^^")) {
strcpy(pos, (pos + 1));
}
b(copy);
free(copy);
}

162
src/dbquery.c Normal file
View File

@@ -0,0 +1,162 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sqlite3.h>
#include "colors.h"
#define QOVERVIEW 'o'
#define QRPLAYER 'p'
#define QMLEADERBOARD 'm'
static inline char *get_filename(char * const c) {
char *qout = "queries/mranks.sql";
if (c != NULL) {
switch(*c) {
default:
*c = QOVERVIEW;
break;
case QMLEADERBOARD:
qout = "queries/mleaderboard-ojoin.sql";
break;
case QRPLAYER:
qout = "queries/rplayers.sql";
break;
}
}
return qout;
}
static inline void print_tblheader(const char *c) {
char *labels;
switch (*c) {
default:
labels = "<TABLE class='center'>\
<TH class='tablename' COLSPAN='4'> <H3><BR>Map List</H3> </TH>\
<TR>\
<TH class='columnname'>Name</TH>\
<TH class='columnname'>Records</TH>\
<TH class='columnname'>Best Time</TH>\
<TH class='columnname'>Held By</TH>\
</TR>";
break;
case QMLEADERBOARD:
labels = "<TABLE class='center'>\
<TH class='tablename' COLSPAN='3'><H3><BR>Leaderboard</H3></TH>\
<TR>\
<TH class='columnname'>Rank</TH>\
<TH class='columnname'>Name</TH>\
<TH class='columnname'>Time</TH>\
</TR>";
break;
case QRPLAYER:
labels = "<TABLE class='center'>\
<TH class='tablename' COLSPAN='3'> <H3><BR>Ranks</H3> </TH>\
<TR>\
<TH class='columnname'>Name</TH>\
<TH class='columnname'>Map</TH>\
<TH class='columnname'>Rank</TH>\
</TR>";
break;
}
printf("%s", labels);
}
// input is an integer as a string.
// the time is in centiseconds.
// this means the longest XDF run this can support is
// 21,474,836.47 seconds
// someone would need to idle in-game for 248 days
static void print_time(const unsigned char *strcs) {
const unsigned long num = strtoul((const char*)strcs, NULL, 10);
unsigned long s = num/100;
const unsigned long cs = num % 100;
const unsigned long min = s/60;
s = s % 60;
if (min > 0) {
printf("<TD>%u:%.2u.%.2u</TD>", min, s, cs);
} else {
printf("<TD>%u.%.2u</TD>", s, cs);
}
}
// display and format the results of the query.
static void qresult(sqlite3_stmt * const sp, const char *c) {
#define ISPLAYERNAME(x, y) (y == 1 && *x == QMLEADERBOARD) || \
(y == 3 && *x == QOVERVIEW)|| \
(y == 0 && *x == QRPLAYER)
#define ISMAPNAME(x, y) (y == 0 && *x == QOVERVIEW) ||\
(y == 1 && *x == QRPLAYER)
int e;
unsigned int i;
const unsigned int cc = sqlite3_column_count(sp);
print_tblheader(c);
while ((e = sqlite3_step(sp)) == SQLITE_ROW) {
printf("<TR>");
for (i = 0; i < cc; ++i) {
unsigned const char * const field = sqlite3_column_text(sp, i);
if (ISPLAYERNAME(c, i)) {
print_plname(field);
} else if (ISMAPNAME(c, i)) {
printf("<TD><a href='/cgi/cts?map=%s'>%s</a></TD>", field, field);
} else if (i == 2 && (*c == QMLEADERBOARD || *c == QOVERVIEW)) {
print_time(field);
} else {
printf("<TD>%s</TD>", field);
}
}
printf("</TR>");
}
printf("</TABLE>");
}
// const char 'sql' is never NULL.
// const char 'str' may be NULL, however it is only when
// the contents of mranks.sql is in const char 'sql'.
static bool executequery(const char *sql, const char *str) {
const char *p = (str != NULL) ? strchr(str, '=') + 1 : NULL;
const char qc = (str != NULL) ? str[0] : QOVERVIEW;
sqlite3 *db;
int con = sqlite3_open("db/cts.db", &db);
if (con != SQLITE_OK) {
fprintf(stderr, "Can not open database: %s\n", sqlite3_errmsg(db));
return false;
}
sqlite3_stmt *s;
// prepare sql to s, reading until null character.
con = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
if (con != SQLITE_OK) {
fprintf(stderr, "Could not prepare SQL statement: %s\n\n%s", sqlite3_errmsg(db), sql);
return false;
}
// bind parameter with a string length that is the # of bytes to null char.
con = sqlite3_bind_text(s, 1, p, -1, NULL);
qresult(s, &qc);
sqlite3_finalize(s);
sqlite3_close(db);
return true;
}
// both allocates and frees memory used up by the string..
// containing the data from query files.
void getquery(char * const qs) {
char *qf = NULL;
char *fname = get_filename(qs);
if (fname != NULL) {
FILE *f = fopen(fname, "r");
if (f != NULL) {
// change from fseek/SEEK_END when sql queries are >2 GB in size.
fseek(f, 0, SEEK_END); // go to the end of file
unsigned int size = ftell(f);
qf = calloc(size, sizeof(char));
if (qf != NULL) {
fseek(f, 0, SEEK_SET); // go to the start of file
fread(qf, sizeof(char), size, f);
}
}
fclose(f);
}
executequery(qf, qs);
free(qf);
}

35
src/main.c Normal file
View File

@@ -0,0 +1,35 @@
#include "dbquery.h"
#include <stdlib.h>
#include <stdio.h>
void html(void) {
const char *html_top = "Content-Type: text/html\n\n\
<html><head><meta content=\"text/html; charset=UTF-8\" />\
<link rel=\"stylesheet\" type=\"text/css\" href=\"page.css\">\
<title>/v/ - Xonotic</title>\
<p class='hidden'>:-) / nice one<br></p>";
const char *html_bot = "<br><br><p classname='footer'>Pages under construction.<br>\
Service may sporadically become unavailable.<br>\
In-game database is not directly synced with this web server.\
</p>\
</body></html>";
const char *html_mid = "<br>\
<H2>hi / good luck and have fun.<br><br>Available Pages</H2>\
<H3>Map Leaderboards</H3>\
<p>cts?map=[map name]</p>\
<H3>Player Ranks</H3>\
<p>cts?player=[client id fingerprint]<br>\
<hr>";
const char *qstr = getenv("QUERY_STRING");
printf("%s", html_top);
if (qstr == NULL) {
printf("%s", html_mid);
}
getquery(qstr);
printf("%s", html_bot);
}
int main(void) {
html();
return 0;
}

182
src/tcolor.c Normal file
View File

@@ -0,0 +1,182 @@
#include "colors.h"
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#define RGB_BLACK 0,0,0
#define RGB_WHITE 255,255,255
#define RGB_RED 255,0,0
#define RGB_LIME 0,255,0
#define RGB_BLUE 0,0,255
#define RGB_YELLOW 255,255,0
#define RGB_CYAN 0,255,255
#define RGB_MAGENTA 255,0,255
#define RGB_SILVER 192,192,192
#define RGB_GREY 128,128,128
#define RGB_MAROON 128,0,0
#define RGB_OLIVE 128,128,0
#define RGB_GREEN 0,128,0
#define RGB_PURPLE 128,0,128
#define RGB_TEAL 0,128,128
#define RGB_NAVY 0,0,128
#define HSL_BLACK 0,0.0f,0.0f
#define HSL_WHITE 0,0.0f,1.0f
#define HSL_RED 0,1.0f,0.5f
#define HSL_LIME 120,1.0f,0.5f
#define HSL_BLUE 240,1.0f,0.5f
#define HSL_YELLOW 60,1.0f,0.5f
#define HSL_CYAN 180,1.0f,0.5f
#define HSL_MAGENTA 300,1.0f,0.5f
#define HSL_SILVER 0,0.0f,0.75f
#define HSL_GREY 0,0.0f,0.50f
#define HSL_MAROON 0,1.0f,0.25f
#define HSL_OLIVE 60,1.0f,0.25f
#define HSL_GREEN 120,1.0f,0.25f
#define HSL_PURPLE 300,1.0f,0.25f
#define HSL_TEAL 180,1.0f,0.25f
#define HSL_NAVY 240,1.0f,0.25f
#define ERROR_TOLERANCE 0.01
#define CLR4BIT 16
bool trgb2hsl(const int r, const int g, const int b,
const int h, const float s, const float l) {
bool result = true;
struct Rgb x;
x.r = r;
x.g = g;
x.b = b;
struct Hls y;
rgb2hsl(&y, &x);
if (y.h == h && fabs(y.s - s) <= ERROR_TOLERANCE && fabs(y.l - l) <= ERROR_TOLERANCE) {
// printf("COLOR CONVERSION OK!\n");
;
} else {
printf("COLOR CONVERSION UNACCEPTABLE\n\texpected: %d %0.2f %0.2f\n", h, s, l);
result = false;
}
printf("\t%d %0.2f %0.2f\n", y.h, y.s, y.l);
return result;
}
void test_rgb2hsl(bool * const r) {
r[0] = trgb2hsl(RGB_BLACK, HSL_BLACK);
r[1] = trgb2hsl(RGB_WHITE, HSL_WHITE);
r[2] = trgb2hsl(RGB_RED, HSL_RED);
r[3] = trgb2hsl(RGB_LIME, HSL_LIME);
r[4] = trgb2hsl(RGB_BLUE, HSL_BLUE);
r[5] = trgb2hsl(RGB_YELLOW, HSL_YELLOW);
r[6] = trgb2hsl(RGB_CYAN, HSL_CYAN);
r[7] = trgb2hsl(RGB_MAGENTA, HSL_MAGENTA);
r[8] = trgb2hsl(RGB_SILVER, HSL_SILVER);
r[9] = trgb2hsl(RGB_GREY, HSL_GREY);
r[10] = trgb2hsl(RGB_MAROON, HSL_MAROON);
r[11] = trgb2hsl(RGB_OLIVE, HSL_OLIVE);
r[12] = trgb2hsl(RGB_GREEN, HSL_GREEN);
r[13] = trgb2hsl(RGB_PURPLE, HSL_PURPLE);
r[14] = trgb2hsl(RGB_TEAL, HSL_TEAL);
r[15] = trgb2hsl(RGB_NAVY, HSL_NAVY);
}
bool thls2rgb(int h, float s, float l, int r, int g, int b) {
bool result = true;
struct Hls x;
x.h = h;
x.s = s;
x.l = l;
struct Rgb y;
hsl2rgb(&y, &x);
if (y.r == r && y.g == g && y.b == b) {
;
} else {
printf("COLOR CONVERSION INEXACT\n\texpected: %d %d %d\n", r, g, b);
result = false;
}
printf("%d %d %d\n", y.r, y.g, y.b);
return result;
}
void test_hsl2rgb(bool * const r) {
r[0] = thls2rgb(HSL_BLACK, RGB_BLACK);
r[1] = thls2rgb(HSL_WHITE, RGB_WHITE);
r[2] = thls2rgb(HSL_RED, RGB_RED);
r[3] = thls2rgb(HSL_LIME, RGB_LIME);
r[4] = thls2rgb(HSL_BLUE, RGB_BLUE);
r[5] = thls2rgb(HSL_YELLOW, RGB_YELLOW);
r[6] = thls2rgb(HSL_CYAN, RGB_CYAN);
r[7] = thls2rgb(HSL_MAGENTA, RGB_MAGENTA);
r[8] = thls2rgb(HSL_SILVER, RGB_SILVER);
r[9] = thls2rgb(HSL_GREY, RGB_GREY);
r[10] = thls2rgb(HSL_MAROON, RGB_MAROON);
r[11] = thls2rgb(HSL_OLIVE, RGB_OLIVE);
r[12] = thls2rgb(HSL_GREEN, RGB_GREEN);
r[13] = thls2rgb(HSL_PURPLE, RGB_PURPLE);
r[14] = thls2rgb(HSL_TEAL, RGB_TEAL);
r[15] = thls2rgb(HSL_NAVY, RGB_NAVY);
}
void test4bitcolors(void (*f)(bool * const)) {
const char *colors[CLR4BIT] = {
"BLACK",
"WHITE",
"RED",
"LIME",
"BLUE",
"YELLOW",
"CYAN",
"MAGENTA",
"SILVER",
"GREY",
"MAROON",
"OLIVE",
"GREEN",
"PURPLE",
"TEAL",
"NAVY"
};
bool results[CLR4BIT];
f(results);
unsigned int i;
for (i = 0; i < CLR4BIT; ++i) {
if (results[i]) {
printf("%s's OK\n", colors[i]);
} else {
printf("%s's INCORRECT\n", colors[i]);
}
}
}
void dpconvert(int r, int g, int b) {
struct Rgb t;
t.r = r * 0xf;
t.g = g * 0xf;
t.b = b * 0xf;
struct Hls z;
rgb2hsl(&z, &t);
printf("%d %f %f\n", z.h, z.s, z.l);
if (z.l < 0.5) {
z.l = 0.5;
hsl2rgb(&t, &z);
printf("%d %d %d\n", t.r, t.g, t.b);
}
}
int main() {
/* print_plname("^xf00Unregistered Player^7");
print_plname("^9[^1S^9]^x469Kom^0ier^7");
print_plname("^9sjn^5|^9F^5e^9tus^7");
print_plname("^0[^7>>^0]^xaefdizzy^7");
print_plname("^x46^xasdf"); */
print_plname("^xB90Beemann^7");
printf("Testing hsl 2 rgb\n");
test4bitcolors(&test_hsl2rgb);
printf("Testing rgb 2 hsl\n");
test4bitcolors(&test_rgb2hsl);
dpconvert(4, 6, 9);
trgb2hsl(60, 90, 135, 216, 0.38f, 0.38f);
trgb2hsl(79, 118, 176, 216, 0.38f, 0.50f);
thls2rgb(216, 0.38f, 0.50f, 79, 118, 176);
}