Skip to content
Snippets Groups Projects
Select Git revision
  • 73ec3fe3034844ce07700630d9b0cee0a68c2d1a
  • master default protected
  • new-features
  • 3-header-navigation-responsive
  • 3-header-navigation-responsive-2
5 results

persist.js

Blame
  • Forked from an inaccessible project.
    persist.js 1.85 KiB
    /*
    Page: persist.js
    Author: Dylan Wacker
    Description: persistant data -> if refresh don't loose previous data
     */
    
    // json database for user
    let database_favorite_games = {
        dylan: {
            games: [3498, 3328],
            token: undefined
        },
        root: {
            games: [28, 3070],
            token: undefined
        }
    }
    exports.database = database_favorite_games;
    let database_user = new Map();
    database_user.set("dylan", "test");
    database_user.set("alexandre", "123");
    database_user.set("root", "super");
    
    
    function idExist(gameId, username) {
        return database_favorite_games[username].games.indexOf(parseInt(gameId)) !== -1;
    }
    
    function userExist(username, password) {
        for (let [u, p] of database_user) {
            if ((username == u) && (password == p)) {
                return true;
            }
        }
        return false;
    }
    
    // Favorite games of a specified user
    exports.getIdsGamesForUser = function(username) {
        return database_favorite_games[username].games.sort();
    }
    
    // Know if a game is in favorite game of a specified user
    exports.isIdExistsForUser = function(gameId, username) {
        return idExist(gameId, username);
    }
    
    // Add a game in favorite game of a specified user
    exports.addGameIdToUser = function(gameId, username) {
        if (!idExist(gameId, username)) {
            database_favorite_games[username].games.push(parseInt(gameId));
            console.log(database_favorite_games[username].games);
            return true;
        }
        return false;
    }
    
    // Remove a game in favorite game of a specified user
    exports.removeGameIdToUser = function(gameId, username) {
        if (idExist(gameId, username)) {
            database_favorite_games[username].games.splice(database_favorite_games[username].games.indexOf(parseInt(gameId)), 1);
            return true;
        }
        return false;
    }
    
    // Check user exist
    exports.checkUserExist = function(username, password) {
        return userExist(username, password);
    }