From aec3cb4882ea2f1477706e97fe66dc019c29b39f Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Mon, 29 Jan 2018 21:26:48 +0100 Subject: [PATCH] Login and Registration system implemented --- bin/bhi.exe | Bin 15360 -> 15360 bytes gameover.c | 26 +++++++++++------- gameover.h | 2 +- highscores.c | 73 ++++++++++++++++++++++++++++++++++++++++++++----- highscores.h | 2 ++ main.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++--- main.h | 4 ++- 7 files changed, 160 insertions(+), 22 deletions(-) diff --git a/bin/bhi.exe b/bin/bhi.exe index baeb20790d8b03e032d7ac12d5c61b512637cc85..c53fca8e531c0623370295fda6528e2c6fad0511 100644 GIT binary patch delta 3132 zcmai$e{fXQ701tcyYI)oZ1zVs* z4z#pxf{0ighRm&wVEHW$ooNd*%NU$N44AQwKgLeQPBRm5!9i*3AEg~_L2-KSc{jST zbo!Y4x%YeSx%YhE&3lg>T0gXY`xe(t8#5zYYtR_^^-4S{5EJ^(ZUv|o*S9o^J1~J> zF^swR#We7>gs6cI;@b1kLo|y3L2cVWw8fjf0botC62KM{nh0x= zhHyi3_O+>CLshE|qhg1vs$mzcYo~>p25_9(+%+!RdfPx{OIh{-x7s8Ua-SI#AG+GC z?G#Gs*=E^RZlILN(q`7FPUy^@?^)w z3+~j6O;j{wP21$O+{EHdR*5g%bzfJl*BfzM^y-VskCEq)ciJ*d9M+@PZKDbHANksU z(nBTXO{^6c^tRkLJ$ZtqnO4f+d&th!sk#$L(ox%IY+1@TtrX|@GI5&D)DxUVPJkE5 zVyEe7ujz!XjEap$(RsdVRCGJL_%k!f3-5BaClr`g_j}S?fa@k}WN~@w&N3C1j;Z+B zs_fkY&6-^2Z^#8*OR3Sr1b=xYU3Ze+ zL*hzNzMQ*-7X5(wL)6cuUhSU)ogR`^6u5`_`D#KIu&M2)*dc0iEU!?j8?w|>C>Zvp z{4UF7?MyaM!hS1qyVs)Aob@-&l}u42Np9^&n&~#`Dl<(qrjC?dmuw-*zo(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lB97atJ>BnKITTweE@a<`{lZsSq*ggVcboNC2bV4sJ=lhR`mLw4x&?0b zfcs@{xw{Z)`lYB;QlIkfqxJwcV}op%*;^dxBEu&7wUNUPv|wj;@60cFih-{T|M1lV zd(JHT#=u+m**o69@@)MDd8yv@8X{Pp^(J$MJk$7c16}dgs618f<+RXIx+IO#ePmV7 zf}R&^8v3uAUEQ+#-N9cK96y;o(d3BYYxgVvsi#<7+bW0MsNsDeJKkJkWP`K+Ws4cv M@I8MO!!7oI0JH2&L;wH) diff --git a/gameover.c b/gameover.c index eb22ede..08a87e4 100644 --- a/gameover.c +++ b/gameover.c @@ -22,6 +22,10 @@ #define GAMEOVER_HUDScale 16.0f #define GAMEOVER_Scale 4.0f +extern char Username[50]; +extern char Password[50]; +extern bool LoggedIn; + extern int width, height; int GAMEOVER_HUDMargin = 5; @@ -94,18 +98,19 @@ void GAMEOVER_Initialize(SDL_Renderer * renderer){ printf("Gameover already initialized!\n"); } /* GAMEOVER_Initialize */ -void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b){ +void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery){ if (b.button == SDL_BUTTON_LEFT) { - if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) { - if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) { - GAMEOVER_UploadState = Uploading; - if (HIGHSCORES_UploadScore("TestUser", 244)) { - GAMEOVER_UploadState = Finished; - } else { - GAMEOVER_UploadState = Failed; + if (LoggedIn) + if (GAMEOVER_UploadState == Initial || GAMEOVER_UploadState == Failed) { + if (clickInRect(b, (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState))) { + GAMEOVER_UploadState = Uploading; + if (HIGHSCORES_UploadScore(Username, (scenery->Score))) { + GAMEOVER_UploadState = Finished; + } else { + GAMEOVER_UploadState = Failed; + } } } - } } } @@ -130,7 +135,8 @@ void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery){ SDL_RenderCopy(renderer, GAMEOVER_Numbers, (GAMEOVER_NumberRects + GAMEOVER_Digits[i]), &target); xOffset += target.w - 1; } - GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState)); + if (LoggedIn) + GAMEOVER_DrawHorizontalCenter(renderer, GAMEOVER_UploadTexture, (GAMEOVER_UploadRects + GAMEOVER_UploadState), (GAMEOVER_UploadTargetRects + GAMEOVER_UploadState)); } /* GAMEOVER_Draw */ void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect){ diff --git a/gameover.h b/gameover.h index 2f348a9..ac50e2f 100644 --- a/gameover.h +++ b/gameover.h @@ -20,7 +20,7 @@ typedef enum uploadStateEnum { Initial = 0, Uploading = 1, Finished = 2, Failed // Prototypes void GAMEOVER_Initialize(SDL_Renderer * renderer); -void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b); +void GAMEOVER_MouseClicked(SDL_MouseButtonEvent b, Scenery * scenery); void GAMEOVER_Draw(SDL_Renderer * renderer, Scenery * scenery); void GAMEOVER_DrawHorizontalCenter(SDL_Renderer * renderer, SDL_Texture * texture, SDL_Rect * srcRect, SDL_Rect * dstRect); void GAMEOVER_GetDigits(int input, int * digitCount); diff --git a/highscores.c b/highscores.c index 56da867..c5d3dca 100644 --- a/highscores.c +++ b/highscores.c @@ -58,7 +58,6 @@ void HIGHSCORES_Draw(SDL_Renderer * renderer){ void HIGHSCORES_Deinitialize(){ printf("De-initializing Highscores...\n"); TTF_CloseFont(HIGHSCORES_FontFamily); - HIGHSCORES_FontFamily = NULL; // to be safe... SDL_DestroyTexture(HIGHSCORES_TableTexture); SDL_FreeSurface(tempSurface); free(HIGHSCORES_UserList); @@ -112,10 +111,10 @@ bool HIGHSCORES_UploadScore(char * username, int score){ char * name, * scorestring; sprintf(buffer, "bhi upload %s %s %d", HIGHSCORES_OutputFilePath, username, score); - printf("BHI called with \"%s\"\n", buffer); - printf("Call BHI interface:\n"); + // printf("BHI called with \"%s\"\n", buffer); + // printf("Call BHI interface:\n"); system(buffer); - printf("BHI interface quit!\nBHI output handling...\n"); + // printf("BHI interface quit!\nBHI output handling...\n"); FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r"); if (fp == NULL) { fclose(fp); @@ -130,10 +129,70 @@ bool HIGHSCORES_UploadScore(char * username, int score){ return true; } /* HIGHSCORES_UploadScore */ +bool HIGHSCORES_Login(char * username, char * password){ + char buffer[200]; + char * line = NULL; + size_t len = 0; + ssize_t read; + + sprintf(buffer, "bhi login %s %s %s", HIGHSCORES_OutputFilePath, username, password); + // printf("BHI called with \"%s\"\n", buffer); + printf("Logging in...\n"); + system(buffer); + // printf("BHI interface quit!\nBHI output handling...\n"); + FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r"); + if (fp == NULL) { + fclose(fp); + printf("Login failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath); + return false; + } + if ((read = getline(&line, &len, fp)) != -1) { + if (line[0] == '0') { + // if ((read = getline(&line, &len, fp)) != -1) { + // printf("Error: %s\n", line); + // } + fclose(fp); + return false; + } + } + fclose(fp); + return true; +} /* HIGHSCORES_Login */ + +bool HIGHSCORES_Register(char * username, char * password){ + char buffer[200]; + char * line = NULL; + size_t len = 0; + ssize_t read; + + sprintf(buffer, "bhi register %s %s %s", HIGHSCORES_OutputFilePath, username, password); + // printf("BHI called with \"%s\"\n", buffer); + printf("Registering...\n"); + system(buffer); + // printf("BHI interface quit!\nBHI output handling...\n"); + FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r"); + if (fp == NULL) { + fclose(fp); + printf("Login failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath); + return false; + } + if ((read = getline(&line, &len, fp)) != -1) { + if (line[0] == '0') { + if ((read = getline(&line, &len, fp)) != -1) { + printf("Error: %s\n", line); + } + fclose(fp); + return false; + } + } + fclose(fp); + return true; +} /* HIGHSCORES_Login */ + void HIGHSCORES_ReloadList(){ printf("Call BHI interface:\n"); system("bhi top output.txt"); - printf("BHI interface quit!\nBHI output handling...\n"); + // printf("BHI interface quit!\nBHI output handling...\n"); HIGHSCORES_EntriesGot = 0; FILE * fp; @@ -145,8 +204,10 @@ void HIGHSCORES_ReloadList(){ bool switchread = false; HIGHSCORES_UserList = malloc(10 * sizeof(User)); fp = fopen("output.txt", "r"); - if (fp == NULL) + if (fp == NULL) { + printf("Reload failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath); return; + } if ((read = getline(&line, &len, fp)) != -1) if (line[0] == '0') return; diff --git a/highscores.h b/highscores.h index 75ae4e9..eea45dd 100644 --- a/highscores.h +++ b/highscores.h @@ -15,6 +15,8 @@ void HIGHSCORES_Deinitialize(); void HIGHSCORES_GenerateTexture(SDL_Renderer * renderer); void HIGHSCORES_DrawText(char * text, SDL_Rect * Message_rect); bool HIGHSCORES_UploadScore(char * username, int score); +bool HIGHSCORES_Login(char * username, char * password); +bool HIGHSCORES_Register(char * username, char * password); void HIGHSCORES_ReloadList(); // End Prototypes diff --git a/main.c b/main.c index 7488ed7..42525ae 100644 --- a/main.c +++ b/main.c @@ -41,7 +41,7 @@ char Username[50]; char Password[50]; int main(int argc, char * args[]){ - // AttemptLogin(); + AttemptLogin(); INITIALIZE(); Uint32 fps_lasttime = SDL_GetTicks(); // the last recorded time. Uint32 fps_current; // the current FPS. @@ -88,8 +88,75 @@ int main(int argc, char * args[]){ } /* main */ void AttemptLogin(){ + int state; + bool loginSuccess = false; -} + do { + system("cls"); + printf("If you want to upload your score to the scoreboard you need to login! Enter\n\t- 1 for logging in with an existing account\n\t- 2 for creating a new account\n\t- 3 for playing unranked\n"); + state = readIntFromIO("Input>", "Invalid input! Awaited a number from 1 to 3.\n", "%d is not a valid mode!\n", 1, 3); + switch (state) { + case 1: + printf("Log-In:\nInput your username: "); + gets(Username); + printf("Input your password: "); + gets(Password); + loginSuccess = HIGHSCORES_Login(Username, Password); + if (loginSuccess) + printf("Successfully logged in as %s!\n", Username); + else + printf("Login failed!\n"); + break; + case 2: + printf("Register:\nInput a username: "); + gets(Username); + printf("Input a password: "); + gets(Password); + loginSuccess = HIGHSCORES_Register(Username, Password); + if (loginSuccess) + printf("Successfully registered new account: %s!\n", Username); + else + printf("Registration failed!\n"); + break; + case 3: + printf("Skipping login!"); + LoggedIn = false; + loginSuccess = true; + break; + default: + printf("This should not happen! State is %d...\n", state); + LoggedIn = false; + loginSuccess = false; + break; + } /* switch */ + if (!loginSuccess) system("pause"); + } while (!loginSuccess); +} /* AttemptLogin */ + +int readIntFromIO(char * m1, char * m2, char * m3, int min, int max){ + int nitems, num; + + while (1) { + while (1) { + printf(m1); + nitems = scanf("%d", &num); + if (nitems == 0) { + printf(m2); + fflush(stdin); + continue; + } else { + break; + } + } + if ((num < min) || (num > max)) { + printf(m3, num); + } else { + break; + } + } + fflush(stdin); + return(num); +} /* readIntFromIO */ void GAME_Escape(){ running = false; @@ -167,7 +234,7 @@ void HandleSDLEvents(){ void mousePress(SDL_MouseButtonEvent b){ // Debug prop switch (gameState) { case GameOver: - GAMEOVER_MouseClicked(b); + GAMEOVER_MouseClicked(b, &scenery); break; default: printf("Gamestate currently ignores Mouse press event: %d!\n", gameState); @@ -223,7 +290,7 @@ void DrawBackground(SDL_Renderer * renderer){ SDL_RenderClear(renderer); } /* DrawFrame */ -void INITIALIZE() { +void INITIALIZE(){ printf("Initializing started...\n"); srand(time(NULL)); if (SDL_Init(SDL_INIT_EVERYTHING) != 0) printf("SDL could not initialize! Error: %s\n", SDL_GetError()); diff --git a/main.h b/main.h index a66139c..3eacf8f 100644 --- a/main.h +++ b/main.h @@ -29,6 +29,8 @@ #define ss "\341" // Prototypes +void AttemptLogin(); +int readIntFromIO(char * m1, char * m2, char * m3, int min, int max); void GAME_Escape(); void MENU_StartMusic(); void MENU_PauseMusic(); @@ -39,7 +41,7 @@ void keyPress(SDL_KeyboardEvent b); void toggleFullscreen(); void windowChanged(SDL_WindowEvent b); void DrawBackground(SDL_Renderer * renderer); -void INITIALIZE() ; +void INITIALIZE(); void QUIT(); // End Prototypes