Exemplo n.º 1
0
  @Override
  public void awake() {

    if (find(npcGuild) == null) {
      Guild guild = new Guild();
      guild.id = npcGuild;
      guild.name = npcGuildName;
      guild.ownerId = npcGuild;

      save(guild);
    }

    for (Guild guild : Guild.db().findAll()) {
      guilds.put(guild.id, guild);
    }
  }
Exemplo n.º 2
0
  @Override
  public void onGameMessage(GameMessage gameMessage) {

    if (gameMessage.guildAction != null) {
      GuildAction guildAction = gameMessage.guildAction;
      Guild guild = null;
      if (guildAction.action.equals("guilds")) {
        Guilds guilds = new Guilds();
        guilds.guild = Guild.db().findAll();
        gameMessage.guilds = guilds;
        PlayerMessage.tell(gameMessage, playerId);

      } else if (guildAction.action.equals("members")) {
        guild = playerGuild(playerId);
        if (guild != null) {
          Guilds guilds = new Guilds();
          guilds.addGuild(guild);
          gameMessage.guilds = guilds;
          gameMessage.guildMemberList = members(guild.id);
          PlayerMessage.tell(gameMessage, playerId);
        } else {
          logger.warning(playerId + " is not in a guild ");
          return;
        }

      } else if (guildAction.action.equals("create")) {
        if (exists(guildAction.guildId)) {
          return;
        }

        guild = new Guild();
        guild.id = guildAction.guildId;
        guild.name = guildAction.guildName;
        guild.ownerId = playerId;

        save(guild);
        addMember(guild.id, playerId);
        gameMessage.guildAction.response = "success";
        PlayerMessage.tell(gameMessage, playerId);
      } else {

        if (!Strings.isNullOrEmpty(guildAction.guildId)) {
          guild = find(guildAction.guildId);
        }

        if (guildAction.action.equals("accept_invite")) {
          if (invites.containsKey(playerId)) {
            String invite = invites.get(playerId);
            String[] parts = invite.split(Pattern.quote("|"));
            String guildId = parts[0];
            String inviteId = parts[1];
            guild = find(guildId);

            if (invite.equals(guildAction.inviteId)) {
              if (guild == null) {
                logger.warning("Guild not found " + guildId);
                return;
              }

              if (memberOf(guild.id, playerId)) {
                logger.warning(playerId + " is already in guild " + guild.id);
                return;
              }

              addMember(guild.id, playerId);
              gameMessage.guildAction.response = "success";
              PlayerMessage.tell(gameMessage, playerId);
              logger.warning(playerId + " joined guild " + guild.id);
            } else {
              logger.warning(inviteId + " does not match  " + guildAction.inviteId);
            }
          } else {
            logger.warning("Invite not found for " + playerId);
          }

        } else if (guildAction.action.equals("invite")) {
          if (playerId.equals(guild.ownerId)) {
            Random rand = new Random();
            String inviteId = guild.id + "|" + String.valueOf(rand.nextInt(10000) + 10);
            gameMessage.guildAction.inviteId = inviteId;

            // fixme, player id will be null here most likely
            String to = CharacterService.instance().find(guildAction.to).playerId;
            invites.put(to, inviteId);
            if (!Strings.isNullOrEmpty(to)) {
              PlayerMessage.tell(gameMessage, to);
              logger.warning("Invite sent from " + playerId + " to " + guildAction.to);
            } else {
              logger.warning("Player id not found for " + guildAction.to);
            }

          } else {
            gameMessage.guildAction.response = "Not owner";
            PlayerMessage.tell(gameMessage, playerId);
          }

        } else if (guildAction.action.equals("leave")) {
          removeMember(guild.id, playerId);
          if (playerId.equals(guild.ownerId)) {
            destroy(guild);
          }

          gameMessage.guildAction.response = "success";
          PlayerMessage.tell(gameMessage, playerId);

        } else if (guildAction.action.equals("destroy")) {
          if (playerId.equals(guild.ownerId)) {
            destroy(guild);
            gameMessage.guildAction.response = "success";
          } else {
            gameMessage.guildAction.response = "Not owner";
          }
          PlayerMessage.tell(gameMessage, playerId);
        }
      }
    }
  }