public object ChangeName(Guid guildId, [FromBody] NameChange change) { if (!FieldChecking.IsValidGuildName(change.Name, Strings.Regex.guildname)) { return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, $@"Invalid guild name." )); } var guild = Guild.LoadGuild(guildId); if (guild != null) { if (guild.Rename(change.Name)) { return(guild); } else { return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, $@"Invalid name, or name already taken." )); } } return(Request.CreateErrorResponse( HttpStatusCode.NotFound, $@"No guild with id '{guildId}'." )); }
/// <summary> /// Create a new Guild instance. /// </summary> /// <param name="creator">The <see cref="Player"/> that created the guild.</param> /// <param name="name">The Name of the guild.</param> public static Guild CreateGuild(Player creator, string name) { name = name.Trim(); if (creator != null && FieldChecking.IsValidGuildName(name, Strings.Regex.guildname)) { using (var context = DbInterface.CreatePlayerContext(readOnly: false)) { var guild = new Guild() { Name = name, FoundingDate = DateTime.UtcNow }; SlotHelper.ValidateSlots(guild.Bank, Options.Instance.Guild.GuildBankSlots); guild.Bank = guild.Bank.OrderBy(bankSlot => bankSlot?.Slot).ToList(); var player = context.Players.FirstOrDefault(p => p.Id == creator.Id); if (player != null) { player.DbGuild = guild; player.GuildRank = 0; player.GuildJoinDate = DateTime.UtcNow; context.ChangeTracker.DetectChanges(); context.SaveChanges(); var member = new GuildMember(player.Id, player.Name, player.GuildRank, player.Level, player.ClassName, player.MapName); guild.Members.AddOrUpdate(player.Id, member, (key, oldValue) => member); creator.Guild = guild; creator.GuildRank = 0; creator.GuildJoinDate = DateTime.UtcNow; // Send our entity data to nearby players. PacketSender.SendEntityDataToProximity(Player.FindOnline(creator.Id)); Guilds.AddOrUpdate(guild.Id, guild, (key, oldValue) => guild); LogActivity(guild.Id, creator, null, GuildActivityType.Created, name); return(guild); } } } return(null); }
/// <summary> /// Renames this guild and then saves the new name to the db /// </summary> /// <param name="name"></param> /// <param name="initiator">The player who initiated this change (null if done by the api or some other method).</param> public bool Rename(string name, Player initiator = null) { if (GuildExists(name) || !FieldChecking.IsValidGuildName(name, Strings.Regex.guildname)) { return(false); } Name = name; foreach (var member in FindOnlineMembers()) { PacketSender.SendEntityDataToProximity(member); } LogActivity(Id, initiator, null, GuildActivityType.Rename, name); Save(); return(true); }