예제 #1
0
    public static TraitValueQueryResult QueryTraitValue(GeneticTrait trait, float floatValue)
    {
        var allTraitTypes      = ReflectionUtils.GetTypesWithHelpAttribute(Assembly.GetCallingAssembly(), typeof(SingleTraitAttribute)).ToList();
        var searchedTraitTypes = allTraitTypes.Where(c => c.GetCustomAttribute <SingleTraitAttribute>().Trait == trait).ToList();

        Assert.IsTrue(searchedTraitTypes.Count == 1, $"Failure when looking for enums with GeneticTrait {trait}. Wanted exacly one, found {searchedTraitTypes.Count}");

        var searchedType = searchedTraitTypes.First();
        var enumValues   = Enum.GetValues(searchedType);

        var enumValuesWithAttribute = new List <object>(enumValues.Cast <object>()).Select(c =>
        {
            var name      = Enum.GetName(searchedType, c);
            var attribute = searchedType.GetField(name).GetCustomAttributes(false).OfType <TraitSingleValueCharacteristic>().SingleOrDefault();
            Assert.IsNotNull(attribute, $"Cannot find attribute SingleTraitAttribute in enum {searchedType} value {c}");

            return(new
            {
                enumValue = c,
                attribute = attribute
            });
        }).ToList();
        var closestValue = enumValuesWithAttribute.OrderBy(c => Mathf.Abs(floatValue - c.attribute.ContinuousValue)).First();

        return(new TraitValueQueryResult()
        {
            Strength = closestValue.attribute.Strength,
            QuantisizedEnumValue = closestValue.enumValue
        });
    }
예제 #2
0
 public SingleTraitAttribute(GeneticTrait trait)
 {
     _trait = trait;
 }
예제 #3
0
 //Returns the trait value of the genetic trait.
 public int GetTrait(GeneticTrait trait)
 {
     return(geneticTraits[(int)trait]);
 }
예제 #4
0
        public IHttpActionResult Post(GeneticTrait value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (value.humanId == null)
            {
                return(BadRequest());
            }

            using (var dbContextTransaction = db.Database.BeginTransaction())
            {
                try
                {
                    tUserSourceService userSourceServiceObj = null;

                    //Get credentials
                    tCredential credentialObj =
                        db.tCredentials.SingleOrDefault(x => x.SourceID == 5 && x.SourceUserID == value.humanId &&
                                                        x.SystemStatusID == 1);
                    if (credentialObj == null)
                    {
                        throw new NoUserCredentialsException("Unable to find any matching HAPI user credentials");
                    }
                    else
                    {
                        userSourceServiceObj = db.tUserSourceServices
                                               .SingleOrDefault(x => x.SourceServiceID == 1 && x.CredentialID == credentialObj.ID &&
                                                                x.SystemStatusID == 1);

                        if (userSourceServiceObj == null)
                        {
                            userSourceServiceObj = new tUserSourceService();
                            userSourceServiceObj.SourceServiceID     = 1;
                            userSourceServiceObj.UserID              = credentialObj.UserID;
                            userSourceServiceObj.CredentialID        = credentialObj.ID;
                            userSourceServiceObj.ConnectedOnDateTime = DateTime.Now;
                            userSourceServiceObj.LastSyncDateTime    = DateTime.Now;
                            userSourceServiceObj.LatestDateTime      = DateTime.Now;
                            userSourceServiceObj.StatusID            = 3; //connected
                            userSourceServiceObj.SystemStatusID      = 1; //valid
                            userSourceServiceObj.tCredential         = credentialObj;

                            db.tUserSourceServices.Add(userSourceServiceObj);
                        }
                        else
                        {
                            //update LatestDateTime to the most recent datetime
                            userSourceServiceObj.LatestDateTime = DateTime.Now;
                        }
                    }

                    tGeneticTrait geneticTrait = db.tGeneticTraits
                                                 .SingleOrDefault(x => x.Description == value.description && x.Trait == value.trait);
                    if (geneticTrait == null)
                    {
                        geneticTrait             = new tGeneticTrait();
                        geneticTrait.Description = value.description;
                        geneticTrait.Trait       = value.trait;

                        db.tGeneticTraits.Add(geneticTrait);
                        db.SaveChanges();
                    }

                    tUserGeneticTrait userGeneticTrait = db.tUserGeneticTraits
                                                         .SingleOrDefault(x => x.UserID == credentialObj.UserID && x.GeneticTraitID == geneticTrait.ID);

                    if (userGeneticTrait == null)
                    {
                        //insert
                        userGeneticTrait                     = new tUserGeneticTrait();
                        userGeneticTrait.UserID              = credentialObj.UserID;
                        userGeneticTrait.tUserSourceService  = userSourceServiceObj;
                        userGeneticTrait.UserSourceServiceID = userSourceServiceObj.ID;
                        userGeneticTrait.tGeneticTrait       = geneticTrait;

                        userGeneticTrait.SystemStatusID = 1;

                        userGeneticTrait.GeneticTraitID = geneticTrait.ID;

                        db.tUserGeneticTraits.Add(userGeneticTrait);
                    }

                    db.SaveChanges();

                    dbContextTransaction.Commit();

                    return(Ok(userGeneticTrait));
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();

                    //Insert Error Log
                    tUserDataErrLog userErrorLog = new tUserDataErrLog();

                    userErrorLog.ErrTypeID   = (int)ErrorLogging.enumErrorType.Application;
                    userErrorLog.ErrSourceID = (int)AuditLogging.enumApplication.SFCBAL;
                    userErrorLog.Code        = ex.HResult.ToString();
                    userErrorLog.Description = ex.Message;
                    userErrorLog.Trace       = ex.StackTrace;

                    dbErr.tUserDataErrLogs.Add(userErrorLog);
                    dbErr.SaveChanges();

                    string ErrMsg = "An error occured and we have logged the error. Please try again later.";

                    Exception Err = new Exception(ErrMsg, ex);

                    return(InternalServerError(Err));
                }
            }
        }
 public float GetTrait(GeneticTrait traitIndex)
 {
     return((float)((Godot.Collections.Array)TotalGenome[(int)DominanceMask[(int)traitIndex]])[(int)traitIndex]);
 }