- [+] 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)
This commit is contained in:
Rob Pearce 2013-08-25 06:37:14 +00:00
parent 9146ee5ab1
commit b8d1237522
6 changed files with 142 additions and 14 deletions

View File

@ -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

BIN
data/hiscores.db Normal file

Binary file not shown.

BIN
data/hiscores.easy.db Normal file

Binary file not shown.

0
data/hiscores.esay.db Normal file
View File

149
rc.c
View File

@ -24,6 +24,7 @@
#include <SDL_ttf.h>
#include <SDL_framerate.h>
#include <SDL_mixer.h>
#include <sqlite3.h>
#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;

5
rc.h
View File

@ -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);