From b8d123752216c2b490cec0fea35fc36f7b6d429d Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Sun, 25 Aug 2013 06:37:14 +0000 Subject: [PATCH] - [+] move to sqlite for local hiscores ? - [+] replace gethiscores with gethiscores_inet - [+] replace submithiscores with submithiscores_inet - [+] gethiscores_local - just use local files. - [+] submithiscores_local - write code, base it on submithiscores.php - Fix images for lives remaining when players are swapped (ie. player1 is mrs dwarf) --- Makefile | 2 +- data/hiscores.db | Bin 0 -> 2048 bytes data/hiscores.easy.db | Bin 0 -> 2048 bytes data/hiscores.esay.db | 0 rc.c | 149 ++++++++++++++++++++++++++++++++++++++---- rc.h | 5 ++ 6 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 data/hiscores.db create mode 100644 data/hiscores.easy.db create mode 100644 data/hiscores.esay.db diff --git a/Makefile b/Makefile index 3a4d825..1316b9a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ LIBS = libs/libSDL-1.2.0.dylib libs/libSDL_image-1.2.0.dylib libs/libSDL_mixer-1 all: rc edit rc: rc.c shared.c rc.h shared.h globals.h defs.h - gcc -Wall -DREV=`./getsvnrev.sh` -o rc -g rc.c shared.c `sdl-config --cflags --libs` -lSDLmain -lSDL -lSDL_image -lSDL_gfx -lSDL_ttf -lSDL_Mixer + gcc -Wall -DREV=`./getsvnrev.sh` -o rc -g rc.c shared.c `sdl-config --cflags --libs` -lsqlite3 -lSDLmain -lSDL -lSDL_image -lSDL_gfx -lSDL_ttf -lSDL_Mixer edit: edit.c shared.c edit.h shared.h globals.h defs.h gcc -DREV=`./getsvnrev.sh` -D__EDITOR -Wall -o edit -g edit.c shared.c `sdl-config --cflags --libs` -lSDLmain -lSDL -lSDL_image -lSDL_gfx -lSDL_ttf diff --git a/data/hiscores.db b/data/hiscores.db new file mode 100644 index 0000000000000000000000000000000000000000..e2995175976fe48000cf46f978d2d66878d0a705 GIT binary patch literal 2048 zcmeH`J#W)M9LDb@sgu@DCt9wX}x zZ&8n20wDk)m>~oJ0E4%G2=3SV=l#CF=9&oNCm?tRW6*&?2maLoA1bMA7CyS_(bXo~ zwB3foncMrKl4TaECaD&R3nuBQ$>HvswX7rUpwW1^t7O?k)OX(;4R1JjH}0`L5p0z z7#LzHL8%a$F$$DYo;W5rhi30*5zP>*N$~xi zQe@tynh6Jl#@rNtZhblgBh9(g|9t0ZBloSRGI5>Rh1#-(98xQbwm<)zoY~-GAxd1oQoq- zGAdw#zQm?`*S9$n=ZHsS5%94RTCurM5{giAP~Q$0DRy!tza{rKu#m>#qx; z{FeD9^AYBa%yXH$n9G<`nEjcZm`#BaqhK@yMniysn}?NwnNg78+9P(SByMg9=Upd= z!^O(L#3-yTz!a2NBFqWmC?~F9b}cR}%`8e)0166lfRsx!91dU!&MX3n2(g1i!IK1ld4hvWylDY_5q#Ibdb1U}cPg1?)O0&V{3p0a+ s6dBtVa0V1*<|Ts#g_yv4nJ-M>3M@@cg;>D|*2}(7LDD-jFCAnY0BWgVg8%>k literal 0 HcmV?d00001 diff --git a/data/hiscores.esay.db b/data/hiscores.esay.db new file mode 100644 index 0000000..e69de29 diff --git a/rc.c b/rc.c index 62adca9..851d212 100644 --- a/rc.c +++ b/rc.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "shared.h" #include "rc.h" @@ -461,7 +462,8 @@ int main (int argc, char **argv) { SDL_FillRect(endgamebox, NULL, SDL_MapRGB(screen->format,white.r,white.g,white.b)); // try to get hiscores - printf("Attepting to download hiscores from %s:%d...\n",hiscoreserver,hiscoreport); fflush(stdout); + //printf("Attepting to download hiscores from %s:%d...\n",hiscoreserver,hiscoreport); fflush(stdout); + printf("Loading hiscore data...\n"); if (gethiscores(B_FALSE)) { printf("Couldn't download hiscores. Disabling hiscore code.\n"); wanthiscores = B_FALSE; @@ -7732,6 +7734,16 @@ void drawscore(void) { if (player) { + SDL_Surface *myhead,*myhead5; + + if (swapplayers) { + myhead = head2; + myhead5 = head52; + } else { + myhead = head; + myhead5 = head5; + } + if (forcegoodcard) { if ((timer / LUCKYFLASH) % 2 == 0) { scorecol = green; @@ -7780,14 +7792,14 @@ void drawscore(void) { // show "x5" for lots of lives while (numtoshow >= 5) { - SDL_BlitSurface(head5, NULL, screen, &area); - area.x += (head->w + 3); + SDL_BlitSurface(myhead5, NULL, screen, &area); + area.x += (myhead5->w + 3); numtoshow -= 5; } for (i = 0; i < numtoshow; i++) { - SDL_BlitSurface(head, NULL, screen, &area); - area.x += (head->w + 3); + SDL_BlitSurface(myhead, NULL, screen, &area); + area.x += (myhead->w + 3); } } else { area.x = P1LIVESX; @@ -7815,6 +7827,16 @@ void drawscore(void) { } if (player2) { + SDL_Surface *myhead,*myhead5; + + if (swapplayers) { + myhead = head; + myhead5 = head5; + } else { + myhead = head2; + myhead5 = head52; + } + if (forcegoodcard) { if ((timer / LUCKYFLASH) % 2 == 0) { scorecol = green; @@ -7864,11 +7886,11 @@ void drawscore(void) { // go left correct amount numtoshow2 = numtoshow; while (numtoshow2 >= 5) { - area.x -= (head2->w + 3); + area.x -= (myhead->w + 3); numtoshow2 -= 5; } for (i = 0; i < numtoshow2; i++) { - area.x -= (head2->w + 3); + area.x -= (myhead->w + 3); } @@ -7876,17 +7898,17 @@ void drawscore(void) { // show "x5" for lots of lives while (numtoshow >= 5) { - SDL_BlitSurface(head52, NULL, screen, &area); - area.x += (head2->w + 3); + SDL_BlitSurface(myhead5, NULL, screen, &area); + area.x += (myhead5->w + 3); numtoshow -= 5; } for (i = 0; i < numtoshow; i++) { - SDL_BlitSurface(head2, NULL, screen, &area); - area.x += (head2->w + 3); + SDL_BlitSurface(myhead, NULL, screen, &area); + area.x += (myhead->w + 3); } } else { - area.x = 640 - P1LIVESX - (head2->w); + area.x = 640 - P1LIVESX - (myhead->w); area.y = LIVESY; area.w = 0; area.h = 0; @@ -13483,8 +13505,11 @@ void drawcredits(void) { SDL_FreeSurface(credittext); } - int submithiscore(int score,int level, char *name) { + return submithiscore_local(score, level, name); +} + +int submithiscore_inet(int score,int level, char *name) { struct sockaddr_in serveraddress; struct hostent *server; int rv; @@ -13545,7 +13570,44 @@ int submithiscore(int score,int level, char *name) { return B_FALSE; } +int submithiscore_local(int score,int level, char *name) { + int rv; + sqlite3 *db; + char *cmd,*errmsg = NULL; + char filename[BUFLEN]; + + if (easymode()) { + snprintf(filename, BUFLEN, "%s", "data/hiscores.easy.db"); + } else { + snprintf(filename, BUFLEN, "%s", "data/hiscores.db"); + } + rv = sqlite3_open(filename, &db); + if (rv) { + printf("can't open database file %s\n",filename); + return B_TRUE; + } + + + // request hiscores + asprintf(&cmd, "insert into 'hiscores' (name,level,score) VALUES ('%s',%d,%d)",name,level,score); + + rv = sqlite3_exec(db, cmd, processhiscore, NULL, &errmsg); + if (rv != SQLITE_OK) { + printf("* error writing hiscores: '%s'\n", errmsg); + printf("* query was: '%s'\n", cmd); + sqlite3_free(errmsg); + } + free(cmd); + sqlite3_close(db); + + return B_FALSE; +} + int gethiscores(int easyscores) { + return gethiscores_local(easyscores); +} + +int gethiscores_inet(int easyscores) { struct sockaddr_in serveraddress; struct hostent *server; int rv; @@ -13642,6 +13704,67 @@ int gethiscores(int easyscores) { return B_FALSE; } +int gethiscores_local(int easyscores) { + int rv; + sqlite3 *db; + char *cmd,*errmsg = NULL; + char filename[BUFLEN]; + + if (easyscores) { + snprintf(filename, BUFLEN, "%s", "data/hiscores.easy.db"); + } else { + snprintf(filename, BUFLEN, "%s", "data/hiscores.db"); + } + rv = sqlite3_open(filename, &db); + if (rv) { + printf("can't open database file %s\n",filename); + return B_TRUE; + } + + + // request hiscores + asprintf(&cmd, " select (select count(*) from hiscores b where b.score > a.score) + 1 as rank,name,level,score from hiscores a order by score desc limit 10"); + + numhiscores = 0; + + rv = sqlite3_exec(db, cmd, processhiscore, NULL, &errmsg); + if (rv != SQLITE_OK) { + printf("* error reading hiscores: '%s'\n", errmsg); + printf("* query was: '%s'\n", cmd); + sqlite3_free(errmsg); + } + free(cmd); + sqlite3_close(db); + + return B_FALSE; +} + +int processhiscore(void *dataptr, int ncols, char **argv, char **colname) { + int i, rank = -1; + for (i = 0; i < ncols; i++) { + if (!strcmp(colname[i], "rank")) { + rank = atoi(argv[i]) - 1; + break; + } + } + if (rank == -1) { + printf("error processing hiscore data - no rank round.\n"); + exit(1); + } + + for (i = 0; i < ncols; i++) { + if (!strcmp(colname[i], "score")) { + hiscore[rank].score = atoi(argv[i]); + } else if (!strcmp(colname[i], "level")) { + hiscore[rank].level = atoi(argv[i]); + } else if (!strcmp(colname[i], "name")) { + snprintf(hiscore[rank].name, MIDBUFLEN, "%s", argv[i]); + } + } + numhiscores++; + return B_FALSE; +} + void checkhiscores(sprite_t *who){ int finished = B_FALSE; int i; diff --git a/rc.h b/rc.h index f2b80cf..288bf45 100644 --- a/rc.h +++ b/rc.h @@ -102,9 +102,14 @@ void drawoutlinetextcentre(SDL_Surface *where,int y, int size, char *msg, SDL_Co int socket_readline(int sock, char *out); void showhiscores(void); void checkhiscores(sprite_t *s); +int gethiscores_inet(int easyscores); +int gethiscores_local(int easyscores); int gethiscores(int easyscores); +int processhiscore(void *dataptr, int ncols, char **argv, char **colname); void drawhiscores(void); int submithiscore(int score,int level, char *name); +int submithiscore_inet(int score,int level, char *name); +int submithiscore_local(int score,int level, char *name); void getinput(void); int isendoflev(void); void keeponscreen(sprite_t *s);