/* * Copyright (C) 2012 Joe AmRhein * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.arconus.dicecommander.interactors.managers; import android.text.TextUtils; import android.util.Log; import com.arconus.dicecommander.Application; import com.arconus.dicecommander.events.CharacterSelectedEvent; import com.arconus.dicecommander.events.DeletedPowerEvent; import com.arconus.dicecommander.events.NewCharacterEvent; import com.arconus.dicecommander.interactors.CharPowerBuilder; import com.arconus.dicecommander.model.entities.character.GameCharacter; import com.arconus.dicecommander.model.entities.character.power.CharPower; import com.arconus.dicecommander.model.entities.character.power.CharPowerRollResult; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class GameCharacterManager { private volatile static GameCharacterManager instance; private List<GameCharacter> characters; private String selectedCharacterUUID; private GameCharacterManager() { characters = new ArrayList<GameCharacter>(); } public static GameCharacterManager getInstance() { if (instance == null) { synchronized (GameCharacterManager.class) { if (instance == null) { instance = new GameCharacterManager(); } } } return instance; } public static void addGameCharacter(GameCharacter character) { GameCharacterManager instance = getInstance(); instance.characters.add(character); Collections.sort(instance.characters); //Now set selected character to new character int position = instance.characters.indexOf(character); if (position > -1) { instance.setSelectedCharacter(position); } Application.bus.post(new NewCharacterEvent(character.getName())); } public List<GameCharacter> getGameCharacters() { return characters; } public void setSelectedCharacter(int position) { GameCharacter gameCharacter = characters.get(position); if (gameCharacter != null) { setSelectedCharacter(gameCharacter.getId()); Application.bus.post(new CharacterSelectedEvent()); } else { Log.w(GameCharacterManager.class.getName(), "Could not find any characters at position " + position + ". Character size is: " + characters.size()); } } public void setSelectedCharacterFromPreferences() { String uuid = Application.prefs.getSelectedCharacterUUID(); if (!TextUtils.isEmpty(uuid)) { if (!uuid.equalsIgnoreCase(selectedCharacterUUID)) { GameCharacter newSelectedChar = getCharacterFromUUID(uuid); if (newSelectedChar != null) { setSelectedCharacter(newSelectedChar.getId()); Application.bus.post(new CharacterSelectedEvent()); } } } } private void setSelectedCharacter(String uuid) { selectedCharacterUUID = uuid; Application.prefs.setSelectedCharacterUUID(uuid); } public static GameCharacter getSelectedCharacter() { return getInstance().getCharacterFromUUID(getInstance().selectedCharacterUUID); } private GameCharacter getCharacterFromUUID(String uuid) { for (GameCharacter character : characters) { if (character.getId().equalsIgnoreCase(uuid)) { return character; } } Log.w(this.getClass().getCanonicalName(), "getCharacterFromUUID couldn't find a character in the list for id: " + selectedCharacterUUID); return null; } public boolean isCharacterSelected() { return selectedCharacterUUID != null; } public CharPowerRollResult rollSelectedCharacter(int position) { GameCharacter character = getSelectedCharacter(); if (character != null) { CharPower power = character.getCharPowers().get(position); if (power != null) { return power.roll(); } } Log.w(this.getClass().getCanonicalName(), "rollSelectedCharacter couldn't roll for position: " + position); return null; } public static CharPowerBuilder getPowerBuilderFromSelectedChar() { GameCharacter character = getInstance().getSelectedCharacter(); return new CharPowerBuilder(character); } public CharPower getPowerFromSelectedChar(int position) { GameCharacter character = getSelectedCharacter(); return character.getCharPowers().get(position); } public void deleteCharPowerFromSelectedCharacter(int position) { GameCharacter character = getSelectedCharacter(); character.removeCharPower(position); Application.bus.post(new DeletedPowerEvent()); } public static void setCharactersFromStore(List<GameCharacter> characters) { GameCharacterManager instance = getInstance(); if (characters != null) { instance.characters.clear(); instance.characters.addAll(characters); instance.selectedCharacterUUID = null; } } public static boolean isEmpty() { return getInstance().characters.isEmpty(); } }