Exemplo n.º 1
0
        internal void ChangeRelation(Guild guild, GuildUnionRequestType requestType, GuildRelationShipType relationShipType)
        {
            switch (requestType)
            {
            case GuildUnionRequestType.Join:
                switch (relationShipType)
                {
                case GuildRelationShipType.Union:
                    if (Union.Count == 0)
                    {
                        Union.Add(this);
                    }

                    Union.Add(guild);
                    guild.Union = Union;
                    break;

                case GuildRelationShipType.Rivals:
                    Rival.Add(guild);
                    guild.Rival.Add(this);
                    break;
                }
                break;

            case GuildUnionRequestType.BreakOff:
                switch (relationShipType)
                {
                case GuildRelationShipType.Union:
                    if (this == guild)
                    {
                        Union[0].ChangeRelation(this, requestType, relationShipType);
                        return;
                    }
                    Union.Remove(guild);
                    if (Union.Count == 1)
                    {
                        Union.Remove(this);
                    }

                    guild.Union = new List <Guild>();
                    break;

                case GuildRelationShipType.Rivals:
                    Rival.Remove(guild);
                    guild.Rival.Remove(this);
                    break;
                }
                break;
            }

            using (var game = new GameContext())
            {
                var guildDtoA = (from g in game.Guilds
                                 where g.GuildId == Index
                                 select g).Single();

                var guildDtoB = (from g in game.Guilds
                                 where g.GuildId == guild.Index
                                 select g).Single();

                guildDtoA.AllianceId = Union.FirstOrDefault()?.Index ?? null;
                guildDtoB.AllianceId = Union.FirstOrDefault()?.Index ?? null;
                for (var i = 0; i < 5; i++)
                {
                    var rivA = Rival.Count > i ? (int?)Rival[i].Index : null;
                    var rivB = guild.Rival.Count > i ? (int?)guild.Rival[i].Index : null;
                    guildDtoA.Set("Rival" + (i + 1), rivA);
                    guildDtoB.Set("Rival" + (i + 1), rivB);
                }

                game.Update(guildDtoA);
                game.Update(guildDtoB);
                game.SaveChanges();
            }
        }