Fixed login system

This commit is contained in:
Michael Chen 2018-01-29 23:15:40 +01:00
parent aec3cb4882
commit 693d2b7dfe
6 changed files with 92 additions and 30 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ sdl2-config
*.psd *.psd
*.exe *.exe
*.json *.json
*.cfg
!bhi.exe !bhi.exe
.tags* .tags*
*.txt *.txt

View File

@ -1,7 +1,7 @@
libs = -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lSDL2_image -lSDL2_ttf -lSDL2_mixer libs = -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
includes = -I".\include" includes = -I".\include"
compiler = gcc compiler = gcc
warningLevel = -Wall -Wno-unused-variable -Wno-unused-but-set-variable warningLevel = -Wall -Wno-parentheses
sources = *.c sources = *.c
linker = -L".\lib" linker = -L".\lib"
dir = bin dir = bin

View File

@ -22,8 +22,8 @@
#define GAMEOVER_HUDScale 16.0f #define GAMEOVER_HUDScale 16.0f
#define GAMEOVER_Scale 4.0f #define GAMEOVER_Scale 4.0f
extern char Username[50]; extern char * Username;
extern char Password[50]; extern char * Password;
extern bool LoggedIn; extern bool LoggedIn;
extern int width, height; extern int width, height;

View File

@ -108,10 +108,9 @@ bool HIGHSCORES_UploadScore(char * username, int score){
char * line = NULL; char * line = NULL;
size_t len = 0; size_t len = 0;
ssize_t read; ssize_t read;
char * name, * scorestring;
sprintf(buffer, "bhi upload %s %s %d", HIGHSCORES_OutputFilePath, username, score); sprintf(buffer, "bhi upload %s %s %d", HIGHSCORES_OutputFilePath, username, score);
// printf("BHI called with \"%s\"\n", buffer); printf("BHI called with \"%s\"\n", buffer);
// printf("Call BHI interface:\n"); // printf("Call BHI interface:\n");
system(buffer); system(buffer);
// printf("BHI interface quit!\nBHI output handling...\n"); // printf("BHI interface quit!\nBHI output handling...\n");
@ -173,7 +172,7 @@ bool HIGHSCORES_Register(char * username, char * password){
FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r"); FILE * fp = fopen(HIGHSCORES_OutputFilePath, "r");
if (fp == NULL) { if (fp == NULL) {
fclose(fp); fclose(fp);
printf("Login failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath); printf("Registration failed: Output file \"%s\" not found!\n", HIGHSCORES_OutputFilePath);
return false; return false;
} }
if ((read = getline(&line, &len, fp)) != -1) { if ((read = getline(&line, &len, fp)) != -1) {

101
main.c
View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <math.h> #include <math.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
@ -18,6 +19,7 @@
#include "background.h" #include "background.h"
#define MAIN_MenuMusicPath "assets/sounds/menu_music.wav" #define MAIN_MenuMusicPath "assets/sounds/menu_music.wav"
#define MAIN_AccountSaveFilePath "account.cfg"
#define MAIN_FadeTime 1000 #define MAIN_FadeTime 1000
#include "main.h" #include "main.h"
@ -37,8 +39,8 @@ bool running = true, fullscreen = false, LoggedIn = false;
GameState gameState = MainMenu; GameState gameState = MainMenu;
Scenery scenery; Scenery scenery;
Mix_Music * MenuLoop; Mix_Music * MenuLoop;
char Username[50]; char * Username;
char Password[50]; char * Password;
int main(int argc, char * args[]){ int main(int argc, char * args[]){
AttemptLogin(); AttemptLogin();
@ -87,36 +89,87 @@ int main(int argc, char * args[]){
return 0; return 0;
} /* main */ } /* main */
bool PushNewCredentialsToSaveFile(const char * filename){
FILE * file;
if (file = fopen(filename, "w")) {
fprintf(file, "%s\n%s", Username, Password);
fclose(file);
return true;
}
return false;
}
bool GrabAccountFromSaveFile(const char * filename){
FILE * file;
if (file = fopen(filename, "r")) {
bool success = true;
success = (fscanf(file, "%s\n%s", Username, Password) > 0);
if (success) printf("Account save file found \"%s\"!\nAttempting automatic login for user \"%s\"...\n", MAIN_AccountSaveFilePath, Username);
else printf("Account save file \"%s\" is invalid!\n", MAIN_AccountSaveFilePath);
fclose(file);
return success;
}
return false;
} /* GrabAccountFromSaveFile */
void GAME_ReadCredentials(){
printf("Input your username: ");
gets(Username);
printf("Input your password: ");
gets(Password);
}
bool GAME_Login(){
printf("Login:\n");
bool loginSuccess = HIGHSCORES_Login(Username, Password);
if (loginSuccess) {
printf("Successfully logged in as %s!\n", Username);
LoggedIn = true;
} else
printf("Login failed!\n");
return loginSuccess;
}
bool GAME_Register(){
printf("Register:\n");
bool loginSuccess = HIGHSCORES_Register(Username, Password);
if (loginSuccess) {
printf("Successfully registered new account: %s!\n", Username);
LoggedIn = true;
} else
printf("Registration failed!\n");
return loginSuccess;
}
void AttemptLogin(){ void AttemptLogin(){
Username = calloc(50, sizeof(char));
Password = calloc(50, sizeof(char));
int state; int state;
bool loginSuccess = false; bool loginSuccess = false;
do { if (GrabAccountFromSaveFile(MAIN_AccountSaveFilePath)) {
if (GAME_Login()) {
printf("Automatic login succeded!\n");
return;
} else {
printf("Automatic login failed! Try manually!\n");
system("pause");
}
}
while (!loginSuccess) {
system("cls"); 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"); 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); state = readIntFromIO("Input>", "Invalid input! Awaited a number from 1 to 3.\n", "%d is not a valid mode!\n", 1, 3);
switch (state) { switch (state) {
case 1: case 1:
printf("Log-In:\nInput your username: "); GAME_ReadCredentials();
gets(Username); loginSuccess = GAME_Login();
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; break;
case 2: case 2:
printf("Register:\nInput a username: "); GAME_ReadCredentials();
gets(Username); loginSuccess = GAME_Register();
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; break;
case 3: case 3:
printf("Skipping login!"); printf("Skipping login!");
@ -130,7 +183,9 @@ void AttemptLogin(){
break; break;
} /* switch */ } /* switch */
if (!loginSuccess) system("pause"); if (!loginSuccess) system("pause");
} while (!loginSuccess); }
if (PushNewCredentialsToSaveFile(MAIN_AccountSaveFilePath)) printf("New login credentials were automatically saved!\n");
else printf("Login credentials could not be autosaved!\n");
} /* AttemptLogin */ } /* AttemptLogin */
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max){ int readIntFromIO(char * m1, char * m2, char * m3, int min, int max){
@ -322,6 +377,8 @@ void INITIALIZE(){
void QUIT(){ void QUIT(){
printf("De-initializing started...\n"); printf("De-initializing started...\n");
free(Username);
free(Password);
Mix_FreeMusic(MenuLoop); Mix_FreeMusic(MenuLoop);
GAMEOVER_Deinitialize(); GAMEOVER_Deinitialize();
BACKGROUND_Deinitialize(); BACKGROUND_Deinitialize();

5
main.h
View File

@ -29,6 +29,11 @@
#define ss "\341" #define ss "\341"
// Prototypes // Prototypes
bool PushNewCredentialsToSaveFile(const char * filename);
bool GrabAccountFromSaveFile(const char * filename);
void GAME_ReadCredentials();
bool GAME_Login();
bool GAME_Register();
void AttemptLogin(); void AttemptLogin();
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max); int readIntFromIO(char * m1, char * m2, char * m3, int min, int max);
void GAME_Escape(); void GAME_Escape();