public void run() {
    this.setName("OrganizeElection (daily)");
    List<Ally> allies = new ArrayList<Ally>(DataAccess.getAllAllies());

    for (Ally ally : allies) {
      if (!ally.getOrganization().equals(Ally.ORGANIZATION_DEMOCRACY)
          && !ally.getOrganization().equals(Ally.ORGANIZATION_WARMONGER)) continue;

      if (ally.getDaysFromCreation() % Election.FREQUENCY == 0
          && ally.getDaysFromCreation() != 0
          && DataAccess.getElectionByAlly(ally.getId()) == null) {
        DataAccess.save(new Election(Utilities.now(), ally.getId()));

        // TODO bmoyet Event : Avertir l'alliance du démarrage du vote
        Event event =
            new Event(Event.EVENT_ELECTION_START, Event.TARGET_ALLY, ally.getId(), 0, -1, -1);
        event.save();

        UpdateTools.queueNewEventUpdate(ally.getMembers(), false);
      }
    }
  }
Esempio n. 2
0
  @Override
  protected String execute(Player player, Map<String, Object> params, Session session)
      throws Exception {

    AllyNews news = DataAccess.getAllyNewsById((Integer) params.get("news"));
    if (news == null) throw new IllegalOperationException("Cette news n'existe pas.");

    Ally ally = player.getAlly();
    if (ally == null) throw new IllegalOperationException("Vous n'appartenez à aucune alliance.");

    if (news.getIdAlly() != player.getIdAlly())
      throw new IllegalOperationException("Vous ne pouvez pas supprimer cette news.");

    if (player.getAllyRank() < ally.getRequiredRank(Ally.RIGHT_MANAGE_NEWS))
      throw new IllegalOperationException(
          "Vous n'avez pas les droits nécessaires pour effectuer cette action.");

    if (news.getIdApplicant() != 0)
      throw new IllegalOperationException(
          "Impossible de supprimer une news concernant la candidature d'un joueur.");

    JSONStringer json = new JSONStringer();

    news.delete();
    Event event =
        new Event(
            Event.EVENT_ALLY_REMOVED_NEWS,
            Event.TARGET_ALLY,
            ally.getId(),
            0,
            -1,
            -1,
            player.getLogin());
    event.save();
    UpdateTools.queueNewEventUpdate(ally.getMembers(), false);

    return json.object().key("id").value(news.getId()).endObject().toString();
  }
  @Override
  protected String execute(Player player, Map<String, Object> params, Session session)
      throws Exception {
    // Flotte qui construit la balise
    Fleet fleet = FleetTools.getFleetByIdWithChecks((Integer) params.get("fleet"), player.getId());
    String password = (String) params.get("password");

    // Vérifie que la flotte a du mouvement
    if (fleet.getMovement() == 0)
      throw new IllegalOperationException("La flotte n'a pas " + "suffisament de mouvement.");

    // Cherche la station spatiale construite sur la case de la flotte
    List<SpaceStation> spaceStations = DataAccess.getSpaceStationsByArea(fleet.getIdCurrentArea());

    int fleetX = fleet.getCurrentX();
    int fleetY = fleet.getCurrentY();

    synchronized (spaceStations) {
      for (SpaceStation spaceStation : spaceStations) {
        int dx = spaceStation.getX() - fleetX;
        int dy = spaceStation.getY() - fleetY;
        double radius = GameConstants.SPACE_STATION_RADIUS;

        if (dx * dx + dy * dy <= radius * radius) {
          String treaty = Treaty.NEUTRAL;
          if (player.getIdAlly() != 0)
            treaty = spaceStation.getAlly().getTreatyWithAlly(player.getIdAlly());

          if (treaty.equals(Treaty.ENEMY)
              || (treaty.equals(Treaty.ALLY)
                  && player.getAllyRank()
                      >= player.getAlly().getRequiredRank(Ally.RIGHT_MANAGE_STATIONS))
              || (treaty.equals(Treaty.NEUTRAL) && fleet.getSkillLevel(Skill.SKILL_PIRATE) >= 0)) {

            boolean newEvent = false;

            List<Update> updates = new ArrayList<Update>();

            // Endommage ou détruit la station
            if (treaty.equals(Treaty.ALLY) || spaceStation.getHull() <= fleet.getPowerLevel()) {
              if (treaty.equals(Treaty.ALLY)
                  && (password == null
                      || !player.getPassword().equals(Utilities.encryptPassword(password))))
                throw new IllegalOperationException("Mot de passe invalide.");

              spaceStation.delete();

              spaceStation.getArea().getSector().updateInfluences();

              newEvent = true;

              if (treaty.equals(Treaty.ALLY)) {
                Event event =
                    new Event(
                        Event.EVENT_STATION_SELF_DESTRUCT,
                        Event.TARGET_ALLY,
                        spaceStation.getIdAlly(),
                        spaceStation.getIdArea(),
                        spaceStation.getX(),
                        spaceStation.getY(),
                        spaceStation.getName(),
                        String.valueOf(player.getLogin()));
                event.save();
              } else {
                Event event =
                    new Event(
                        Event.EVENT_STATION_LOST,
                        Event.TARGET_ALLY,
                        spaceStation.getIdAlly(),
                        spaceStation.getIdArea(),
                        spaceStation.getX(),
                        spaceStation.getY(),
                        spaceStation.getName());
                event.save();
              }

              Effect effect =
                  new Effect(
                      Effect.TYPE_STATION_DESTRUCTION,
                      fleet.getCurrentX(),
                      fleet.getCurrentY(),
                      fleet.getIdCurrentArea());

              UpdateTools.queueEffectUpdate(effect, player.getId(), false);

              updates.add(Update.getEffectUpdate(effect));
            } else {
              double coefBefore =
                  spaceStation.getHull()
                      / (double) SpaceStation.HULL_LEVELS[spaceStation.getLevel()];

              synchronized (spaceStation.getLock()) {
                spaceStation = DataAccess.getEditable(spaceStation);
                spaceStation.setHull(spaceStation.getHull() - fleet.getPowerLevel());
                spaceStation.save();
              }

              double coefAfter =
                  spaceStation.getHull()
                      / (double) SpaceStation.HULL_LEVELS[spaceStation.getLevel()];

              if (coefBefore != 1 && (int) (coefBefore * 5) != (int) (coefAfter * 5)) {
                newEvent = true;

                Event event =
                    new Event(
                        Event.EVENT_STATION_UNDER_ATTACK,
                        Event.TARGET_ALLY,
                        spaceStation.getIdAlly(),
                        spaceStation.getIdArea(),
                        spaceStation.getX(),
                        spaceStation.getY(),
                        spaceStation.getName(),
                        String.valueOf(coefAfter));
                event.save();
              }
            }

            synchronized (fleet.getLock()) {
              fleet = DataAccess.getEditable(fleet);
              fleet.doAction(
                  Fleet.CURRENT_ACTION_ATTACK_STRUCTURE,
                  Utilities.now() + GameConstants.DESTROY_SPACE_STATION_MOVEMENT_RELOAD);
              fleet.save();
            }

            // Mises à jour
            if (newEvent)
              UpdateTools.queueNewEventUpdate(spaceStation.getAlly().getMembers(), false);
            UpdateTools.queueAreaUpdate(fleet.getIdCurrentArea(), player.getId());

            updates.add(Update.getPlayerFleetUpdate(fleet.getId()));
            updates.add(Update.getAreaUpdate());

            return UpdateTools.formatUpdates(player, updates);
          } else {
            throw new IllegalOperationException(
                "Vous ne pouvez pas saboter cette station spatiale.");
          }
        }
      }
    }

    throw new IllegalOperationException("Pas de station spatiale à cet endroit.");
  }