Пример #1
0
        private void Join(Session session, Packet packet)
        {
            uint        cityId;
            int         assignmentId;
            ISimpleStub stub;

            try
            {
                cityId       = packet.GetUInt32();
                assignmentId = packet.GetInt32();
            }
            catch (Exception)
            {
                ReplyError(session, packet, Error.Unexpected);
                return;
            }

            if (!session.Player.IsInTribe)
            {
                ReplyError(session, packet, Error.TribesmanNotPartOfTribe);
                return;
            }

            ITribe tribe = session.Player.Tribesman.Tribe;

            locker.Lock(session.Player, tribe).Do(() =>
            {
                ICity city = session.Player.GetCity(cityId);
                if (city == null)
                {
                    ReplyError(session, packet, Error.CityNotFound);
                    return;
                }

                // TODO: Clean this up
                Assignment assignment = tribe.Assignments.FirstOrDefault(x => x.Id == assignmentId);
                if (assignment == null)
                {
                    ReplyError(session, packet, Error.AssignmentDone);
                    return;
                }

                try
                {
                    stub = PacketHelper.ReadStub(packet, assignment.IsAttack ? FormationType.Attack : FormationType.Defense);
                }
                catch (Exception)
                {
                    ReplyError(session, packet, Error.Unexpected);
                    return;
                }

                Error result = tribe.JoinAssignment(assignmentId, city, stub);

                ReplyWithResult(session, packet, result);
            });
        }
Пример #2
0
        private string AssignmentJoin(Session session, string[] parms)
        {
            bool   help     = false;
            string cityName = string.Empty;
            int    id       = int.MaxValue;

            try
            {
                var p = new OptionSet
                {
                    { "?|help|h", v => help = true },
                    { "city=", v => cityName = v.TrimMatchingQuotes() },
                    { "id=", v => id = int.Parse(v) },
                };
                p.Parse(parms);
            }
            catch (Exception)
            {
                help = true;
            }

            if (help || string.IsNullOrEmpty(cityName) || id == int.MaxValue)
            {
                return("AssignementCreate --city=city_name --id=id");
            }

            uint cityId;

            if (!world.Cities.FindCityId(cityName, out cityId))
            {
                return("City not found");
            }

            ICity city;

            if (!world.TryGetObjects(cityId, out city))
            {
                return("City not found!");
            }

            if (city.Owner.Tribesman == null)
            {
                return("Not in tribe");
            }

            ITribe tribe = city.Owner.Tribesman.Tribe;

            return(locker.Lock(city, tribe).Do(() =>
            {
                if (city.DefaultTroop.Upkeep == 0)
                {
                    return "No troops in the city!";
                }

                Assignment assignment = tribe.Assignments.FirstOrDefault(x => x.Id == id);

                if (assignment == null)
                {
                    return "Assignment not found";
                }

                // TODO: Clean this up.. shouldnt really need to do this here
                ITroopStub stub = city.CreateTroopStub();
                FormationType formation = assignment.IsAttack ? FormationType.Attack : FormationType.Defense;
                stub.AddFormation(formation);
                foreach (var unit in city.DefaultTroop[FormationType.Normal])
                {
                    stub.AddUnit(formation, unit.Key, unit.Value);
                }

                Error error = tribe.JoinAssignment(id, city, stub);
                if (error != Error.Ok)
                {
                    return Enum.GetName(typeof(Error), error);
                }

                return string.Format("OK");
            }));
        }