Newer
Older
CactusServer / src / main / java / cactusServer / models / Accounts.java
y-ota on 28 Jun 2018 3 KB fuckin json!!!
package cactusServer.models;

import javax.inject.Singleton;

import cactusServer.entities.*;
import cactusServer.utils.RandomStringGenerator;

import java.util.*;

@Singleton
public class Accounts {
	public static final int ACCOUNT_NUM_MAX = 10000;
	private static Accounts theInstance = null;
	private ArrayList<Account> accounts = new ArrayList<>(ACCOUNT_NUM_MAX);
	private HashSet<String> userIDSet = new HashSet<>(ACCOUNT_NUM_MAX);
	private HashSet<String> uniqueIDSet = new HashSet<>(ACCOUNT_NUM_MAX);
	
	private Accounts() {

	}

	public static Accounts getInstance() {
		if (theInstance == null) {
			theInstance = new Accounts();
		}
		return theInstance;
	}

	public URIAddressedAccount createAcount(String userID, String userName, String userPass) {
		if (!userIDSet.add(userID))
			return null;
		String uniqueID = RandomStringGenerator.generateUniqueString(12, RandomStringGenerator.ALPHA_NUMERIC,
				uniqueIDSet);
		uniqueIDSet.add(uniqueID);
		Account newAccount = new Account(userID, userName, userPass, uniqueID);
		accounts.add(newAccount);
		return new URIAddressedAccount("/CactusServer/rest/accounts/" + uniqueID, newAccount);
	}

	public Account getAccountToken(String token) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getToken().equals(token)) {
				return editAccount;
			}
		}
		return null;
	}

	public Account getAccountByID(String userID) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getId().equals(userID)) {
				return editAccount;
			}
		}
		return null;
	}

	public Account getAccountByName(String userName) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getName().equals(userName)) {
				return editAccount;
			}
		}
		return null;
	}

	public Account getAccountByuniqueID(String uniqueID) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getUniqueID().equals(uniqueID)) {
				return editAccount;
			}
		}
		return null;
	}

	public URIAddressedAccount loginAccount(String userID, String userPass) {
		if (getAccountByID(userID) != null && getAccountByID(userID).getPass().equals(userPass)) {
			Accounts.getInstance().getAccountByID(userID).setLogin(true);
			Accounts.getInstance().getAccountByID(userID).formToken();
			return new URIAddressedAccount(
					"/CactusServer/rest/accounts/" + Accounts.getInstance().getAccountByID(userID).getUniqueID(),
					Accounts.getInstance().getAccountByID(userID));
		} else {
			return null;
		}
	}

	public Account logoutAccount(String token) {
		Accounts.getInstance().getAccountToken(token).setLogin(false);
		return Accounts.getInstance().getAccountToken(token);
	}

	public Account deleteAccount(String uniqueID) {
		Account edit = Accounts.getInstance().getAccountByuniqueID(uniqueID);
		if (edit == null) {
			return edit;
		}
		userIDSet.remove(edit.getId());
		uniqueIDSet.remove(edit.getUniqueID());
		accounts.remove(edit);
		return edit;
	}

	public ArrayList<Account> getAccounts() {
		return accounts;
	}
}