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
*.exe
*.json
*.cfg
!bhi.exe
.tags*
*.txt

View File

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

View File

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

View File

@ -108,10 +108,9 @@ bool HIGHSCORES_UploadScore(char * username, int score){
char * line = NULL;
size_t len = 0;
ssize_t read;
char * name, * scorestring;
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");
system(buffer);
// 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");
if (fp == NULL) {
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;
}
if ((read = getline(&line, &len, fp)) != -1) {

105
main.c
View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
@ -17,8 +18,9 @@
#include "settings.h"
#include "background.h"
#define MAIN_MenuMusicPath "assets/sounds/menu_music.wav"
#define MAIN_FadeTime 1000
#define MAIN_MenuMusicPath "assets/sounds/menu_music.wav"
#define MAIN_AccountSaveFilePath "account.cfg"
#define MAIN_FadeTime 1000
#include "main.h"
@ -37,8 +39,8 @@ bool running = true, fullscreen = false, LoggedIn = false;
GameState gameState = MainMenu;
Scenery scenery;
Mix_Music * MenuLoop;
char Username[50];
char Password[50];
char * Username;
char * Password;
int main(int argc, char * args[]){
AttemptLogin();
@ -87,36 +89,87 @@ int main(int argc, char * args[]){
return 0;
} /* 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(){
Username = calloc(50, sizeof(char));
Password = calloc(50, sizeof(char));
int state;
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");
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");
GAME_ReadCredentials();
loginSuccess = GAME_Login();
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");
GAME_ReadCredentials();
loginSuccess = GAME_Register();
break;
case 3:
printf("Skipping login!");
@ -130,7 +183,9 @@ void AttemptLogin(){
break;
} /* switch */
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 */
int readIntFromIO(char * m1, char * m2, char * m3, int min, int max){
@ -322,6 +377,8 @@ void INITIALIZE(){
void QUIT(){
printf("De-initializing started...\n");
free(Username);
free(Password);
Mix_FreeMusic(MenuLoop);
GAMEOVER_Deinitialize();
BACKGROUND_Deinitialize();

5
main.h
View File

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