@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); } }
public static Guild playerGuild(String playerId) { for (Guild guild : Guild.db().findAll()) { if (memberOf(guild.id, playerId)) { return guild; } } return null; }
public static Guild find(String id) { if (!guilds.containsKey(id)) { Guild guild = Guild.db().findFirst("guild_id = ?", id); if (guild == null) { return null; } else { guilds.put(id, guild); } } return guilds.get(id); }
@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); } } } }
private void destroy(Guild guild) { // TerritoryHandler.removeOwner(guild.id); Guild.db().deleteWhere("guild_id = ?", guild.id); guilds.remove(guild.id); GuildMembers.db().deleteWhere("guild_members_guild_id = ?", guild.id); }
private void save(Guild guild) { Guild.db().save(guild); guilds.put(guild.id, guild); }