Ejemplo n.º 1
0
        internal long GetWeight(HttpRequestBase httpRequest, long kgWeight, StatisticsDbModel userStatistics)
        {
            var  dbContext      = new ApplicationDbContext();
            long convertedValue = kgWeight;

            if (string.IsNullOrEmpty(userStatistics.WeightUnit))
            {
                if (httpRequest != null && httpRequest.Cookies[DataConversion.WeightUnitCookie] != null &&
                    httpRequest.Cookies[DataConversion.WeightUnitCookie].Value != null)
                {
                    if (httpRequest.Cookies[DataConversion.WeightUnitCookie].Value == DataConversion.WeightPounds)
                    {
                        userStatistics.WeightUnit = DataConversion.WeightPounds;
                        convertedValue            = DataConversion.ConvertKilogramsToPounds(kgWeight);
                    }
                    else
                    {
                        userStatistics.WeightUnit = DataConversion.WeightKilograms;
                    }
                    dbContext.SaveChanges();
                }
            }
            else
            {
                convertedValue = userStatistics.WeightUnit == DataConversion.WeightPounds ?
                                 DataConversion.ConvertKilogramsToPounds(kgWeight) : kgWeight;
            }

            return(convertedValue);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage FinishJob(string icaoStr, string payloadStr, string usarIdStr, string fuelWeightStr, string tailNumberStr, string planeDescriptionStr)
        {
            var response = Request.CreateResponse(HttpStatusCode.BadRequest, "Server error");

            try
            {
                var        dbContext = new ApplicationDbContext();
                JobDbModel job       = null;
                if (icaoStr.Length == 3)
                {
                    job = dbContext.JobDbModels.FirstOrDefault(j => j.User.Id == usarIdStr &&
                                                               j.IsActivated && j.InProgress &&
                                                               (j.ArrivalICAO.Substring(1).ToLower() == icaoStr.ToLower() ||
                                                                j.AlternativeICAO.Substring(1).ToLower() == icaoStr.ToLower()));
                }
                else
                {
                    job = dbContext.JobDbModels.FirstOrDefault(j => j.User.Id == usarIdStr &&
                                                               j.IsActivated && j.InProgress &&
                                                               (j.ArrivalICAO.ToLower() == icaoStr.ToLower() ||
                                                                j.AlternativeICAO.ToLower() == icaoStr.ToLower()));
                }

                if (job == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, "Wrong destination to finish this job."));
                }

                string name = job.IsChallenge ? "Challenge" : "Job";

                if (job.IsChallenge && job.ChallengeExpirationDate <= DateTime.Now)
                {
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, CHALLENGE_EXPIRED));
                }

                // Check GUEST
                if (job.User != null && job.User.Email == AccountController.GuestEmail)
                {
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, AccountController.GuestMessage));
                }

                long payload = Convert.ToInt64(Math.Round(Convert.ToDouble(payloadStr, new CultureInfo("en-US"))));
                // Check payload
                if (payload >= (job.Payload + 150) || payload <= (job.Payload - 150))
                {
                    var payloadInPounds = DataConversion.ConvertKilogramsToPounds(job.Payload);
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, $"Wrong. Active {name} payload is: {job.Payload}kg / {payloadInPounds}lbs"));
                }

                var diffTime = DateTime.Now - job.StartTime;

                var minTime = (job.Dist * 11) / 100;

                if (diffTime.TotalMinutes < minTime)
                {
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, "Impossible to arrive at destination in this short time."));
                }

                job.InProgress       = false;
                job.EndTime          = DateTime.Now;
                job.IsDone           = true;
                job.IsActivated      = false;
                job.ModelName        = tailNumberStr;
                job.ModelDescription = planeDescriptionStr;

                long fuelWeight = Convert.ToInt64(Math.Round(Convert.ToDouble(fuelWeightStr, new CultureInfo("en-US"))));
                job.FinishFuelWeight = fuelWeight;

                var expectedFuelBurned = (job.Dist * job.Payload * 0.12) / 1000;
                //// Check Fuel
                if ((job.UsedFuelWeight) < expectedFuelBurned)
                {
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, $"Impossible to finish this {name} with {job.UsedFuelWeight}Kg burned fuel."));
                }

                var licenseExpired = UpdateStatistics(job, dbContext);
                UpdateAirline(job, dbContext);

                dbContext.SaveChanges();

                string resultMsg = $"{name} finish successfully at: " + job.EndTime.ToShortTimeString() + "  (UTC)";
                if (licenseExpired)
                {
                    resultMsg = $"{name} finish. Your license is expired. Check Profile page.";
                }

                return(Request.CreateResponse(HttpStatusCode.OK, resultMsg));
            }
            catch (Exception e)
            {
                ErrorLog.GetDefault(null).Log(new Error(e));
                response = Request.CreateResponse(HttpStatusCode.BadRequest, "Process error.");
            }

            return(response);
        }
Ejemplo n.º 3
0
        private HttpResponseMessage StartJob(string icaoStr, string payloadStr, string usarIdStr, string fuelWeightStr)
        {
            var        dbContext = new ApplicationDbContext();
            JobDbModel job       = null;

            if (icaoStr.Length == 3)
            {
                job = dbContext.JobDbModels.FirstOrDefault(j => j.User.Id == usarIdStr &&
                                                           j.IsActivated &&
                                                           j.DepartureICAO.Substring(1).ToLower() == icaoStr.ToLower());
            }
            else
            {
                job = dbContext.JobDbModels.FirstOrDefault(j => j.User.Id == usarIdStr &&
                                                           j.IsActivated &&
                                                           j.DepartureICAO.ToLower() == icaoStr.ToLower());
            }

            if (job == null)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden, "You don't have any job activated for this location."));
            }

            if (job.IsChallenge && job.ChallengeExpirationDate <= DateTime.Now)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden, CHALLENGE_EXPIRED));
            }

            // Check GUEST
            if (job.User != null && job.User.Email == AccountController.GuestEmail)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden, AccountController.GuestMessage));
            }

            long payload = Convert.ToInt64(Math.Round(Convert.ToDouble(payloadStr, new CultureInfo("en-US"))));

            // Check payload
            if (payload >= (job.Payload + 150) || payload <= (job.Payload - 150))
            {
                var payloadInPounds = DataConversion.ConvertKilogramsToPounds(job.Payload);
                return(Request.CreateResponse(HttpStatusCode.Forbidden, $"Wrong. Active job payload is: {job.Payload}kg / {payloadInPounds}lbs"));
            }

            long fuelWeight = Convert.ToInt64(Math.Round(Convert.ToDouble(fuelWeightStr, new CultureInfo("en-US"))));

            job.StartFuelWeight = fuelWeight;

            job.InProgress = true;
            job.StartTime  = DateTime.Now;
            dbContext.SaveChanges();

            string name = job.IsChallenge ? "Challenge" : "Job";

            var licenseOverdue = IsLicenseOverdue(dbContext, job.User.Id);
            var resultMsg      = $"{name} to " + job.ArrivalICAO + " started at: " + job.StartTime.ToShortTimeString() + " (UTC)";

            if (licenseOverdue)
            {
                resultMsg = $"{name} started. Warn: Your pilot license is expired. Check profile page.";
            }

            var response = Request.CreateResponse(HttpStatusCode.OK, resultMsg);

            response.Headers.Add("arrival-icao", job.ArrivalICAO);

            return(response);
        }