ConnectionManager.java :  » Game » magicwars » mw » client » managers » Java Open Source

Java Open Source » Game » magicwars 
magicwars » mw » client » managers » ConnectionManager.java
package mw.client.managers;

import gnu.cajo.utils.extra.TransparentItemProxy;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;

import mw.client.utils.cache.SaveObjectUtil;
import mw.mtgforge.Deck;
import mw.mtgforge.Constant.DeckType;
import mw.server.MWProfile;
import mw.server.list.CardBeanList;
import mw.server.model.DeckInfo;
import mw.server.model.MagicWarsModel.GameZone;
import mw.server.model.MagicWarsModel.PhaseName;
import mw.server.model.bean.CardBean;
import mw.server.model.bean.CombatBean;
import mw.server.model.bean.SpellBean;
import mw.server.model.cost.ManaCost;
import mw.server.rmi.MessagingInterface;
import mw.server.socket.ZippedObject;
import mw.server.socket.ZippedObjectImpl;

import org.apache.log4j.Logger;

/**
 * Singleton class for connection with server
 */
public class ConnectionManager {

  private static MessagingInterface connection = null;

  public final static String SERVER_NAME = "mtg";

  public final static String defaultPort = "1198";

  private static String hostName = "";
  
  private static final Logger log = Logger.getLogger(ConnectionManager.class);

  public static MessagingInterface getRMIConnection() {
    return getRMIConnection(SettingsManager.getManager().getAddress());
  }

  public static MessagingInterface getRMIConnection(String host_name) {
    if (connection == null) {
      if (host_name == null)
        hostName = SettingsManager.getManager().getAddress();
      else
        hostName = host_name;
      String cajoPort = System.getProperty("cajo", defaultPort);
      String url = "//" + hostName + ":" + cajoPort + "/" + SERVER_NAME + cajoPort;
      log.info("rmi connection url: " + url);
      try {
        connection = (MessagingInterface) TransparentItemProxy.getItem(url, new Class[] { MessagingInterface.class });
        //connection = (MessagingInterface)Naming.lookup(url);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    return connection;
  }

  public static void sendNoMulligan(int playerID) throws RemoteException {
    getRMIConnection().noMulligan(playerID);
  }

  public static int sendPlayCard(CardBean card, GameZone zone, int playerId) throws RemoteException {
    return getRMIConnection().playCard(playerId, card, zone);
  }

  public static ManaCost sendTapPermanentForMana(int playerID, Integer permanentID, ManaCost manaCost) throws RemoteException {
    ManaCost manacost = getRMIConnection().tapPermanentForMana(playerID, permanentID, manaCost);
    SaveObjectUtil.saveObject(manacost, ProfileManager.getMyId(), "manacost");
    return manacost;
  }

  public static boolean sendAddAttacker(Integer permanentID, GameZone zone, String player) throws RemoteException {
    return getRMIConnection().addAttacker(ProfileManager.getMyId(), permanentID, zone, player);
  }

  public static void sendCombat(CombatBean combat) throws RemoteException {
    ZippedObjectImpl<CombatBean> zipped = new ZippedObjectImpl<CombatBean>(combat);
    SaveObjectUtil.saveObject(zipped, ProfileManager.getMyId(), "send_combat");
    getRMIConnection().setCombat(ProfileManager.getMyId(), zipped);
  }

  public static void sendPWCombat(HashMap<Integer, CombatBean> pwCombat) throws RemoteException {
    SaveObjectUtil.saveObject(pwCombat, ProfileManager.getMyId(), "send_pwcombat");
    getRMIConnection().setPWCombat(ProfileManager.getMyId(), pwCombat);
  }

  public static void sendSetAssignedDamage() throws RemoteException {
    getRMIConnection().setAssignedDamage(ProfileManager.getMyId());
  }

  public static void sendAcceptStack() throws RemoteException {
    getRMIConnection().acceptStack(ProfileManager.getMyId());
  }

  public static void sendChosenPermanents(CardBeanList cardlist) throws RemoteException {
    SaveObjectUtil.saveObject(cardlist, ProfileManager.getMyId(), "chosen_perm_list");
    getRMIConnection().setChosenPermanents(ProfileManager.getMyId(), cardlist);
  }

  public static void sendChosenTargetsForPay(CardBeanList cardlist) throws RemoteException {
    getRMIConnection().addChosenTargetsForPay(ProfileManager.getMyId(), cardlist);
  }

  public static void sendChosenPlayer(int targetPlayerID) throws RemoteException {
    getRMIConnection().addTargetPlayer(ProfileManager.getMyId(), targetPlayerID);
  }
  
  public static void sendTargets(CardBeanList targets) throws RemoteException {
    getRMIConnection().setTargets(ProfileManager.getMyId(), targets);
  }

  public static void sendDiscardCard(CardBean card) throws RemoteException {
    SaveObjectUtil.saveObject(card, ProfileManager.getMyId(), "card_d");
    getRMIConnection().discardCard(ProfileManager.getMyId(), card);
  }

  public static void sendPayDiscardCard(CardBean card) throws RemoteException {
    SaveObjectUtil.saveObject(card, ProfileManager.getMyId(), "card_dpay");
    getRMIConnection().payDiscardCard(ProfileManager.getMyId(), card);
  }

  public static void sendPlayAtEndOfTurn(boolean isToplay) throws RemoteException {
    getRMIConnection().setPlayAtEndOfTurn(ProfileManager.getMyId(), isToplay);
  }

  public static void sendPlayBeforeCombat(boolean isToPlay) throws RemoteException {
    getRMIConnection().setPlayBeforeCombat(ProfileManager.getMyId(), isToPlay);
  }

  public static void sendCancelPlayingCard() throws RemoteException {
    getRMIConnection().cancelPlayingCard(ProfileManager.getMyId());
  }

  public static void sendCancelChoosingSpecific() throws RemoteException {
    getRMIConnection().cancelChoosingSpecific(ProfileManager.getMyId());
  }
  
  public static void sendCancelChoosingTargets() throws RemoteException {
    getRMIConnection().cancelChoosingTargets(ProfileManager.getMyId());
  }

  public static void sendSetAssignedDamage(CardBeanList list) throws RemoteException {
    getRMIConnection().setAssignedDamage(ProfileManager.getMyId(), list);
  }

  public static int sendAddChosenCard(CardBean card) throws RemoteException {
    return getRMIConnection().addChosenCard(ProfileManager.getMyId(), card);
  }

  public static int sendAddChosenAbility(SpellBean spellBean) throws RemoteException {
    SaveObjectUtil.saveObject(spellBean, ProfileManager.getMyId(), "chosen_sa");
    return getRMIConnection().addChosenAbility(ProfileManager.getMyId(), spellBean);
  }

  public static void sendAddChosenAbilityToPlay(SpellBean sa) throws RemoteException {
    SaveObjectUtil.saveObject(sa, ProfileManager.getMyId(), "chosen_sa_to_play");
    getRMIConnection().addChosenAbilityToPlay(ProfileManager.getMyId(), sa);
  }
  
  public static void sendResult(Serializable value) throws RemoteException {
    SaveObjectUtil.saveObject(value, ProfileManager.getMyId(), "chosen_text_value");
    getRMIConnection().setResult(ProfileManager.getMyId(), value);
  }

  public static ManaCost sendChosenManaSymbol(String manaSymbol) throws RemoteException {
    ManaCost manacost = getRMIConnection().setChosenManaSymbol(ProfileManager.getMyId(), manaSymbol);
    SaveObjectUtil.saveObject(manacost, ProfileManager.getMyId(), "chosen_manacost");
    return manacost;
  }

  public static void sendCombatMarks(EnumSet<PhaseName> marks) throws RemoteException {
    SaveObjectUtil.saveObject(marks, ProfileManager.getMyId(), "send_marks");
    getRMIConnection().setCombatPhaseMarks(ProfileManager.getMyId(), marks);
  }

  public static void sendChatMessage(String text) throws RemoteException {
    getRMIConnection().addChatMessage(ProfileManager.getMyId(), text);
  }

  public static int sendGetOpponentHandSize() throws RemoteException {
    return getRMIConnection().getOpponentHandSize(ProfileManager.getMyId());
  }

  public static void sendXValue(int value) throws RemoteException {
    getRMIConnection().setXValue(ProfileManager.getMyId(), value);
  }
  
  public static void sendXValue2(int value) throws RemoteException {
    getRMIConnection().setXValue2(ProfileManager.getMyId(), value);
  }

  public synchronized static void sendChosenDeckAndStartGame(String deckName, MWProfile profile) throws RemoteException {
    getRMIConnection().setChosenDeckAndStartGame(profile.getPlayerID(), deckName);
  }

  public static ArrayList<DeckInfo> getListOfDecks(DeckType type) throws RemoteException {
    ArrayList<DeckInfo> deckList = getRMIConnection().getListOfDecks(ProfileManager.getMyId(), type);
    SaveObjectUtil.saveObject(deckList, ProfileManager.getMyId(), "decklist");
    return deckList;
  }
  
  public static void sendNextPhase(int playerID, PhaseName phase) throws RemoteException {
    getRMIConnection().nextPhase(playerID, phase);
  }
  
  public static Deck sendGetDeck(String deckName) throws RemoteException {
    ZippedObject<Deck> deck = getRMIConnection().getDeck(ProfileManager.getMyId(), deckName);
    SaveObjectUtil.saveObject(deck, ProfileManager.getMyId(), "deck");
    return deck.unzip();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.