Exemplo n.º 1
0
        public SortedDictionary <Coordinate, Segment> GetRandomisedShipCoordinates(IList <IShip> ships)
        {
            if (ships != null)
            {
                int totalShipLength = ships.Sum(q => q.ShipLength);

                // Create a temporary segment list and pass it along by reference
                // Once done, we can clear it out and make sure the GC does its job
                SortedDictionary <Coordinate, Segment> temporarySegments = new SortedDictionary <Coordinate, Segment>(new CoordinateComparer());

                if (totalShipLength != segments.Count)
                {
                    foreach (IShip ship in ships)
                    {
                        ShipDirection direction = Randomise.Next(this.GridDimension) % 2 == 0
                                                      ? ShipDirection.Horizontal
                                                      : ShipDirection.Vertical;

                        while (temporarySegments != null && temporarySegments.Count != ship.ShipLength)
                        {
                            if (direction == ShipDirection.Horizontal)
                            {
                                this.MapXAxis(ship, ref temporarySegments);
                                if (temporarySegments.Count == ship.ShipLength)
                                {
                                    segments.AddRange(temporarySegments);
                                    temporarySegments.Clear();
                                    break;
                                }
                            }

                            if (direction == ShipDirection.Vertical)
                            {
                                this.MapYAxis(ship, ref temporarySegments);

                                if (temporarySegments != null && temporarySegments.Count == ship.ShipLength)
                                {
                                    segments.AddRange(temporarySegments);
                                    temporarySegments.Clear();
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(segments);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 请求API的通用方法,需要手动构建参数
        /// </summary>
        /// <param name="action">接口名</param>
        /// <param name="patameters">参数,无参数请传null</param>
        /// <param name="url">api地址</param>
        /// <param name="region">区域</param>
        /// <returns></returns>
        public async Task <string> RequestAPiManuallyAsync(string action, SortedDictionary <string, object> patameters, string url, string region)
        {
            var basePatameters = new SortedDictionary <string, object>(StringComparer.Ordinal)
            {
                { "Action", action },
                { "Nonce", RandomHelper.RNGRandom(100) },
                { "Region", region },
                { "SecretId", _SecretId },
                { "Timestamp", (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds }
            };

            basePatameters.AddRange(patameters);

            return(await SendAsync(url, basePatameters));
        }
Exemplo n.º 3
0
        private IDictionary <DateTime, int> GetEditStatistics(GrowDbContext context)
        {
            var editData   = new SortedDictionary <DateTime, int>();
            var cutoffDate = DateTime.Now.AddDays(-30).Date;

            // fill with data
            var data = GetEditStatisticsForAllTables(context, cutoffDate)
                       .SelectMany(enm => enm)
                       .GroupBy(dt => dt)
                       .Select(group => new KeyValuePair <DateTime, int>(group.Key, group.Count()));

            editData.AddRange(data);

            // fill holes in data
            for (var date = cutoffDate; date <= DateTime.Now; date += new TimeSpan(1, 0, 0, 0))
            {
                if (!editData.ContainsKey(date))
                {
                    editData.Add(date, 0);
                }
            }

            return(editData);
        }