// Add score to a game history // 0 : no error // 1 : game or score is just initialized // 2 : game not in db // 42 : check error public int addScore(BTGame game, BTGameHistory score) { int status = 0; // Check if both Game and GameHistory objects aren't just initialized if (game.Equals(default(BTGame)) || score.Equals(default(BTGameHistory))) { status = 1; } else { bool temp_status = false; // Check if game exists in database foreach (BTGame game_in_db in database.game) { if (game_in_db.game_id == game.game_id) { temp_status = true; } } // If it exists if (temp_status == true) { try { // Adding score to database for (int i = 0; i < database.game.Count; i++) { if (database.game[i].game_id == game.game_id) { database.game[i].scores.history.Add(score); } } // Saving changes saveDatabase(); } // If something else happend catch (Exception ex) { error = ex.Message; status = 42; } } else { status = 2; } } return(status); }
// Create new game // 0 : no error // 1 : userID is not in database // 2 : game_title or created is Empty // 3 : Playlist is just bearly initialized // 4 : invalid scores // 5 : name already given // 42: Get error string to know what happend public int createGame(int userID, string game_title, string created, BTPlaylist playlist, int title_score, int artist_score, int album_score, int duration_score) { int status = 0; bool temp_status = false; try { // Check if userID is correct foreach (BTUser user in database.user) { if (user.user_id == userID) { temp_status = true; } } if (temp_status == false) { status = 1; } else { // Check if game_title and created are empty if (game_title == "" || created == "") { status = 2; } else { // Check if given Playlist Object isn't just initialized if (playlist.Equals(default(BTPlaylist))) { status = 3; } else { // Check if scores are initialized if (title_score >= 0 && artist_score >= 0 && album_score >= 0 && duration_score >= 0) { // Let's create that game! // Default gameID is 0 int gameID = 0; // If games already exists if (database.game.Count > 0) { // Let's generate that game // To generate random int between 0 and 10000 Random random_int = new Random(); temp_status = true; while (temp_status == true) { // Generating gameID gameID = random_int.Next(0, 10000); // Checking if it exists foreach (BTGame game in database.game) { // If gameID not attributed, exit the loop if (game.game_id != gameID) { temp_status = false; } else { temp_status = true; } } } } // Now that gameID is generated, let's create that game! BTGame game_to_create = new BTGame(); game_to_create.game_id = gameID; game_to_create.user_id = userID; game_to_create.game_title = game_title; DateTime created_date = DateTime.Parse(created); game_to_create.created = created_date.ToString(); game_to_create.tracklist = playlist.tracklist; BTGameScores scores_to_assign = new BTGameScores(); scores_to_assign.title = title_score; scores_to_assign.artist = artist_score; scores_to_assign.album = album_score; scores_to_assign.duration = duration_score; scores_to_assign.history = new List <BTGameHistory>(); game_to_create.scores = scores_to_assign; // Check if game title is not already given bool is_game_title_already_given = false; foreach (BTGame game in database.game) { if (game.user_id == userID) { if (game.game_title == game_title) { is_game_title_already_given = true; } } } if (is_game_title_already_given == true) { status = 5; } else { // Add game to database database.game.Add(game_to_create); // Save changes saveDatabase(); } } else { status = 4; } } } } } // If something else happend catch (Exception ex) { status = 42; error = ex.Message; } return(status); }
// Set selected game (stored here) public void setSelectedGame(BTGame game) { selected_game = game; }
// Delete game // 0 : no error // 1 : user doesn't exist in database // 2 : game doesn't exist in database // 3 : game isn't created by given user! // 4 : invalid password // 42: Get error string to know what happend public int deleteGame(BTUser user_src, BTGame game_src, string password) { int status = 0; bool user_exists = false, game_exists = false; BTUser user_selected = new BTUser(); BTGame game_selected = new BTGame(); try { // First, check if user exists in db (using userID) foreach (BTUser user in database.user) { if (user.user_id == user_src.user_id) { user_exists = true; user_selected = user; } } // If user doesn't exists if (user_exists == false) { status = 1; } else { // Check if game exists in database foreach (BTGame game in database.game) { if (game.game_id == game_src.game_id) { game_selected = game; game_exists = true; } } // if game exists if (game_exists == true) { // Check if game is made by given user if (user_src.user_id == game_selected.user_id) { // Check if password is incorrect if (hashPassword(password) != user_selected.password) { status = 4; } else { // Delete game in database database.game.Remove(game_selected); // Save changes saveDatabase(); } } else { status = 3; } } else { status = 2; } } } // In case of other error catch (Exception ex) { status = 42; error = ex.Message; } return(status); }
/* =================================================================================================================== */ /* Game part */ /* =================================================================================================================== */ // Rename game // 0 : no error // 1 : name or user index empty // 2 : Game ID not in DB // 3 : Game is not created by given user_id // 42: Get error string to know what happend public int renameGame(int game_id, int user_id, string src_name) { int status = 0; BTGame game_edited = new BTGame(); try { // checks if source string is empty if (src_name == "") { status = 1; } // If they aren't empty else { // Check if game is in db and if it's by good user bool is_game_in_db = false; bool is_game_by_user = false; foreach (BTGame game in database.game) { if (game.game_id == game_id) { is_game_in_db = true; if (game.user_id == user_id) { is_game_by_user = true; game_edited = game; } } } // if user in is database if (is_game_in_db == true) { // If game is created by this user if (is_game_by_user == true) { // Rename it game_edited.game_title = src_name; // Saving game for (int i = 0; i < database.game.Count; i++) { if (database.game[i].game_id == game_edited.game_id) { database.game[i] = game_edited; } } // Saving changes to database.json file saveDatabase(); } else { status = 3; } } else { status = 2; } } } // If something else happend catch (Exception ex) { status = 42; error = ex.Message; } return(status); }