Exemple #1
0
        //public async Task<IEnumerable<UnitModel>> GetDeffenceUnits(Tribe tribe)
        //{
        //    var units = await collection
        //        .AsQueryable()
        //        .Where(x => x.Tribe == tribe && (x.UnitType == UnitType.FOOT_TROOPS || x.UnitType == UnitType.CAVALRY))
        //        .ToListAsync();

        //    return units.Where(x => x.Attack < x.DeffenceAgainstCavalry || x.Attack < x.DeffenceAgainstInfantry);
        //}

        public async Task <IEnumerable <UnitModel> > GetDeffenceUnits(Tribe tribe)
        {
            PipelineDefinition <UnitModel, UnitModel> pipeline = new BsonDocument[]
            {
                new BsonDocument {
                    { "$addFields", new BsonDocument
                      {
                          { "iDiff", new BsonDocument {
                                { "$cmp", new BsonArray {
                                        "$attack", "$deffenceAgainstInfantry"
                                    } }
                            } },
                          { "cDiff", new BsonDocument {
                                { "$cmp", new BsonArray {
                                        "$attack", "$deffenceAgainstCavalry"
                                    } }
                            } }
                      } }
                },
                new BsonDocument
                {
                    {
                        "$match", new BsonDocument
                        {
                            {
                                "$and", new BsonArray
                                {
                                    new BsonDocument
                                    {
                                        { "tribe", tribe.GetEnumDisplayName() }
                                    },
                                    new BsonDocument
                                    {
                                        {
                                            "$or", new BsonArray
                                            {
                                                new BsonDocument
                                                {
                                                    { "iDiff", new BsonDocument {
                                                          { "$eq", -1 }
                                                      } }
                                                },
                                                new BsonDocument
                                                {
                                                    { "cDiff", new BsonDocument {
                                                          { "$eq", -1 }
                                                      } }
                                                }
                                            }
                                        }
                                    },
                                    new BsonDocument
                                    {
                                        {
                                            "$or", new BsonArray
                                            {
                                                new BsonDocument
                                                {
                                                    { "unitType", UnitType.FOOT_TROOPS.GetEnumDisplayName() }
                                                },
                                                new BsonDocument
                                                {
                                                    { "unitType", UnitType.CAVALRY.GetEnumDisplayName() }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var units = _collection.Aggregate(pipeline);

            return(await units.ToListAsync());
        }