public int getBalance() throws LogonException {
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    GetBalanceRequest getBalanceRequest = new GetBalanceRequest();
    getBalanceRequest.setSessionId(session.getSessionId());

    return ((GetBalanceResponse) wsTemplate.marshalSendAndReceive(getBalanceRequest)).getBalance();
  }
  public DamagedEarthServer(String serverAddress, String userName) {

    session = new Session();

    session.setServerAddress(serverAddress);
    session.setUserName(userName);

    wsTemplate = initializeConnection();
  }
  public void submitQuest(int questID) throws LogonException {
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    SubmitQuestRequest submitQuestRequest = new SubmitQuestRequest();
    submitQuestRequest.setSessionId(session.getSessionId());
    submitQuestRequest.setQuestID(questID);

    wsTemplate.marshalSendAndReceive(submitQuestRequest);
  }
  public List<BuildingInfo> getBuildings() throws LogonException {
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    GetBuildingsRequest getBuildingsRequest = new GetBuildingsRequest();
    getBuildingsRequest.setSessionId(session.getSessionId());

    return ((GetBuildingsResponse) wsTemplate.marshalSendAndReceive(getBuildingsRequest))
        .getPayload()
        .getGetBuildingsResponseMessage()
        .getBuildings();
  }
  public List<QuestNPCInfo> getQuests(int npcID) throws LogonException {
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    GetQuestsNPCRequest questsNPCRequest = new GetQuestsNPCRequest();
    questsNPCRequest.setSessionId(session.getSessionId());
    questsNPCRequest.setNPCID(npcID);

    GetQuestsNPCResponse getQuestsNPCResponse =
        (GetQuestsNPCResponse) wsTemplate.marshalSendAndReceive(questsNPCRequest);

    return getQuestsNPCResponse.getPayload().getQuestsNPCResponseMessage().getQuests();
  }
  public String respawn() throws LogonException {

    // Make sure user is already logged on before letting them logout
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    RespawnRequest respawnRequest = new RespawnRequest();
    respawnRequest.setSessionId(session.getSessionId());

    RespawnResponse logoutResponse =
        (RespawnResponse) wsTemplate.marshalSendAndReceive(respawnRequest);

    return logoutResponse.getStatus();
  }
  public void logout() throws LogonException {

    // Make sure user is already logged on before letting them logout
    if (!session.alreadyLoggedOn())
      throw new LogonException("You must first be logged on before calling Logout");

    LogoutRequest logoutRequest = new LogoutRequest();
    logoutRequest.setSessionId(session.getSessionId());

    LogoutResponse logoutResponse =
        (LogoutResponse) wsTemplate.marshalSendAndReceive(logoutRequest);
    System.out.println("logout response = " + logoutResponse.getStatus());
    session.wipe();
  }
  public String runAdminCommand(String adminCommand) throws LogonException {

    // Make sure user is  already logged on
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    AdminCommandRequest adminCommandRequest = new AdminCommandRequest();
    adminCommandRequest.setSessionId(session.getSessionId());
    adminCommandRequest.setCommand(adminCommand);

    AdminCommandResponse adminCommandResponse =
        (AdminCommandResponse) wsTemplate.marshalSendAndReceive(adminCommandRequest);

    return adminCommandResponse.getStatus();
  }
  public List<String> playerAttack() throws LogonException {
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    AttackEntityRequest attackEntityRequest = new AttackEntityRequest();
    attackEntityRequest.setSessionId(session.getSessionId());

    AttackEntityResponse attackEntityResponse =
        (AttackEntityResponse) wsTemplate.marshalSendAndReceive(attackEntityRequest);

    try {
      return attackEntityResponse.getPayload().getEntity();
    } catch (Exception e) {
      return null;
    }
  }
  public void login() throws LogonException {

    // Make sure user is not already logged on
    if (session.alreadyLoggedOn()) throw new LogonException("Already Logged On; Session Exists");

    LoginRequest loginRequest = new LoginRequest();
    loginRequest.setUserName(session.getUserName());

    LoginResponse loginResponse = (LoginResponse) wsTemplate.marshalSendAndReceive(loginRequest);
    System.out.println("login response = " + loginResponse.getSessionId());

    // See if the server returned an error.  If so, throw it to caller as an exception
    if (loginResponse.getSessionId().equalsIgnoreCase("Already Logged On"))
      throw new LogonException(loginResponse.getSessionId());

    session.setSessionId(loginResponse.getSessionId());
  }
  public double[] updateLocation(double x, double y) throws LogonException {

    // Make sure user is  already logged on
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    SetMyLocationRequest setMyLocationRequest = new SetMyLocationRequest();
    setMyLocationRequest.setSessionId(session.getSessionId());
    setMyLocationRequest.setXCooridinate(x);
    setMyLocationRequest.setYCooridinate(y);

    SetMyLocationResponse setMyLocationResponse =
        (SetMyLocationResponse) wsTemplate.marshalSendAndReceive(setMyLocationRequest);

    return new double[] {
      setMyLocationResponse.getModifiedLocationX(), setMyLocationResponse.getModifiedLocationY()
    };
  }
  public List<NPCInfo> getNPCInfo() throws LogonException {

    // Make sure user is  already logged on
    if (!session.alreadyLoggedOn())
      throw new LogonException("Not Logged On; Session does not exist");

    // Finally try to get a list of user locations
    GetNPCInfoRequest getNPCInfoRequest = new GetNPCInfoRequest();
    getNPCInfoRequest.setSessionId(session.getSessionId());

    GetNPCInfoResponse getNPCInfoResponse =
        (GetNPCInfoResponse) wsTemplate.marshalSendAndReceive(getNPCInfoRequest);
    List<NPCInfo> npcList;

    try {
      npcList = getNPCInfoResponse.getPayload().getNPCInfoResponseMessage().getNPCs();
    } catch (NullPointerException e) {
      npcList = null;
    }

    return npcList;
  }
  private WebServiceTemplate initializeConnection() {
    // Create message factory
    SOAPMessageFactory1_1Impl impl = new SOAPMessageFactory1_1Impl();
    SaajSoapMessageFactory msgFactory = new SaajSoapMessageFactory(impl);

    msgFactory.setSoapVersion(SoapVersion.SOAP_11);

    // Create an instance of jaxbmarshaller
    HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, false);
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

    marshaller.setContextPaths("com.andrewcraft.schema");
    marshaller.setMarshallerProperties(properties);

    CommonsHttpMessageSender msgSender = new CommonsHttpMessageSender();

    WebServiceTemplate wsTemplate = new WebServiceTemplate(msgFactory);
    wsTemplate.setMarshaller(marshaller);
    wsTemplate.setUnmarshaller(marshaller);
    wsTemplate.setMessageSender(msgSender);
    wsTemplate.setDefaultUri(session.getServerAddress());
    return wsTemplate;
  }