Example #1
0
        private void CreateStrongholdAssignment(Session session, Packet packet)
        {
            uint        cityId;
            uint        strongholdId;
            AttackMode  mode;
            DateTime    time;
            ISimpleStub simpleStub;
            string      description;
            bool        isAttack;

            try
            {
                mode         = (AttackMode)packet.GetByte();
                cityId       = packet.GetUInt32();
                strongholdId = packet.GetUInt32();
                time         = DateTime.UtcNow.AddSeconds(packet.GetInt32());
                isAttack     = packet.GetByte() == 1;
                simpleStub   = PacketHelper.ReadStub(packet, isAttack ? FormationType.Attack : FormationType.Defense);
                description  = packet.GetString();
            }
            catch (Exception)
            {
                ReplyError(session, packet, Error.Unexpected);
                return;
            }

            IStronghold stronghold;

            if (!strongholdManager.TryGetStronghold(strongholdId, out stronghold))
            {
                ReplyError(session, packet, Error.StrongholdNotFound);
                return;
            }

            ICity city;

            if (!cityManager.TryGetCity(cityId, out city))
            {
                ReplyError(session, packet, Error.StrongholdNotFound);
                return;
            }

            // First need to find all the objects that should be locked
            locker.Lock(city, stronghold).Do(() =>
            {
                if (city == null || stronghold == null)
                {
                    ReplyError(session, packet, Error.Unexpected);
                    return;
                }

                // Make sure city belongs to player and he is in a tribe
                if (city.Owner != session.Player || city.Owner.Tribesman == null)
                {
                    ReplyError(session, packet, Error.Unexpected);
                    return;
                }

                // Make sure this player is ranked high enough
                if (city.Owner.Tribesman == null ||
                    !city.Owner.Tribesman.Tribe.HasRight(city.Owner.PlayerId, TribePermission.AssignmentCreate))
                {
                    ReplyError(session, packet, Error.TribesmanNotAuthorized);
                    return;
                }

                int id;
                Error ret = session.Player.Tribesman.Tribe.CreateAssignment(city,
                                                                            simpleStub,
                                                                            stronghold.PrimaryPosition.X,
                                                                            stronghold.PrimaryPosition.Y,
                                                                            stronghold,
                                                                            time,
                                                                            mode,
                                                                            description,
                                                                            isAttack,
                                                                            out id);
                ReplyWithResult(session, packet, ret);
            });
        }