Пример #1
0
        public PickResult SubmitPick(int userID, int playerID)
        {
            PickResult result = PickResult.Success;
            PlayerObj  player = null;

            lock (_DraftLock)
            {
                player = FindPlayer(playerID);
                if (player == null)
                {
                    result = PickResult.InvalidPlayer;
                }
                else if (!Settings.PositionMaxes.ContainsKey(player.Position))
                {
                    result = PickResult.InvalidPosition;
                }
                else
                {
                    List <PlayerObj> draftedPlayers = GetUsersDraftedPlayers(userID);
                    int posCount = draftedPlayers.Sum(x => x.Position == player.Position ? 1 : 0);
                    if (posCount >= Settings.PositionMaxes[player.Position])
                    {
                        result = PickResult.PositionMax;
                    }
                    else
                    {
                        PlayerObj pickedPlayer = FindPickedPlayer(playerID);
                        if (pickedPlayer != null)
                        {
                            result = PickResult.AlreadyPicked;
                        }
                        else
                        {
                            DraftMoveObj match = FindUserOnClock(userID);
                            if (match == null)
                            {
                                result = PickResult.NotTurn;
                            }
                            else
                            {
                                DraftMove move = new DraftMove()
                                {
                                    Pick     = match.Pick,
                                    Round    = match.Round,
                                    SeasonID = Settings.DraftSeasonID,
                                    PlayerID = playerID,
                                    Time     = DateTime.Now,
                                    MoveType = (int)DraftMoveType.Pick,
                                    UserID   = userID
                                };
                                db.DraftMoves.InsertOnSubmit(move);
                                db.SubmitChanges();
                            }
                        }
                    }
                }
            }

            return(result);
        }
Пример #2
0
        public DraftMoveObj RemoveLastOnTheClock()
        {
            DraftMoveObj toRet = null;

            lock (_DraftLock)
            {
                bool isPaused = this.GetCurrentDraftStatus().Status == (int)DraftStatusType.Paused;
                if (isPaused)
                {
                    List <DraftMove> onClock = this.GetCurrentOnClock_Internal();

                    if (onClock.Count >= 1)
                    {
                        onClock.Sort(new DraftMoveComparer());
                        DraftMove last = onClock[onClock.Count - 1];
                        toRet = new DraftMoveObj(last);
                        db.DraftMoves.DeleteOnSubmit(last);

                        if (onClock.Count >= 2)
                        {
                            DraftMove nextToLast = onClock[onClock.Count - 2];
                            nextToLast.Time = DateTime.Now - TimeSpan.FromSeconds(Settings.SecondsPerPick - 30);
                        }

                        db.SubmitChanges();
                    }
                }
            }

            return(toRet);
        }
Пример #3
0
        public DraftMoveObj UpdateLastOnTheClock(OnClockUpdateObj update)
        {
            DraftMoveObj toRet = null;

            lock (_DraftLock)
            {
                bool isPaused = this.GetCurrentDraftStatus().Status == (int)DraftStatusType.Paused;
                if (isPaused)
                {
                    List <DraftMove> onClock = this.GetCurrentOnClock_Internal();

                    if (onClock.Count >= 1)
                    {
                        onClock.Sort(new DraftMoveComparer());
                        DraftMove last        = onClock[onClock.Count - 1];
                        int       secondsLeft = update.SecondsLeft > Settings.SecondsPerPick ? Settings.SecondsPerPick : update.SecondsLeft;
                        last.Time = DateTime.Now - TimeSpan.FromSeconds(Settings.SecondsPerPick - secondsLeft);
                        toRet     = new DraftMoveObj(last);

                        db.SubmitChanges();
                    }
                }
            }

            return(toRet);
        }
Пример #4
0
 public DraftMoveObj(DraftMove toCopy)
 {
     SeasonID = toCopy.SeasonID;
     Time     = toCopy.Time;
     Round    = toCopy.Round;
     Pick     = toCopy.Pick;
     UserID   = toCopy.UserID;
     PlayerID = toCopy.PlayerID;
     TypeInt  = toCopy.MoveType;
 }
Пример #5
0
        public BallersDraftObj()
        {
            // Make sure the Current Draft starts with a 'Keep' or 'Empty' in every slot
            lock (_DraftLock)
            {
                int intKeep  = (int)DraftMoveType.Keep;
                int intEmpty = (int)DraftMoveType.Empty;
                Dictionary <QuickPick, int> overrides = GetDraftOverrides();
                var query = from t in db.DraftMoves
                            where t.SeasonID == Settings.DraftSeasonID && (t.MoveType == intKeep || t.MoveType == intEmpty)
                            select new DraftMoveObj(t);

                if (query.Count() < (Settings.RoundsPerDraft * Settings.TeamsPerDraft))
                {
                    int      intMoveType             = (int)DraftMoveType.Empty;
                    DateTime runTime                 = DateTime.Now;
                    Dictionary <int, int> draftOrder = GetDraftOrder();
                    if (draftOrder.Count == 0)
                    {
                        throw new InvalidOperationException("There is no draft order for the current season, please ensure there is valid draft order data in the database");
                    }
                    Dictionary <QuickPick, DraftMoveObj> existing = new Dictionary <QuickPick, DraftMoveObj>();
                    foreach (DraftMoveObj nextMove in query)
                    {
                        existing[nextMove.Index] = nextMove;
                    }
                    // for (int round = 1; round <= Settings.RoundsPerDraft; round++)
                    for (QuickPick index = new QuickPick()
                    {
                        Round = 1, Pick = 1
                    }; index.Round <= Settings.RoundsPerDraft; index.Round++)
                    {
                        for (index.Pick = 1; index.Pick <= Settings.TeamsPerDraft; index.Pick++)
                        {
                            if (!existing.ContainsKey(index))
                            {
                                DraftMove temp = new DraftMove()
                                {
                                    Round    = index.Round,
                                    Pick     = index.Pick,
                                    MoveType = intMoveType,
                                    PlayerID = null,
                                    SeasonID = Settings.DraftSeasonID,
                                    Time     = runTime,
                                    UserID   = overrides.ContainsKey(index) ? overrides[index] : draftOrder[index.Pick]
                                };
                                db.DraftMoves.InsertOnSubmit(temp);
                            }
                        }
                    }
                    db.SubmitChanges();
                }
            }
        }
Пример #6
0
        public List <DraftMoveObj> FindOnTheClock()
        {
            List <DraftMoveObj> onclock      = new List <DraftMoveObj>();
            List <DraftMove>    toPause      = new List <DraftMove>();
            DraftMoveObj        addedOnClock = null;

            // This entire operation MUST be atomic, so a Lock is required
            lock (_DraftLock)
            {
                DraftStatus     oStatus    = GetCurrentDraftStatus();
                DraftStatusType currStatus = Converter.ToDraftStatusType(oStatus.Status);
                bool            isPaused   = (currStatus == DraftStatusType.Paused);

                var allNextStatus = from t in db.DraftStatus
                                    where t.ID == 2
                                    select t;
                DraftActionType nextStatus = Converter.ToDraftActionType(allNextStatus.First().Status);

                List <DraftMove> currentOnClock = this.GetCurrentOnClock_Internal();
                if (isPaused)
                {
                    toPause = currentOnClock;
                }
                else
                {
                    foreach (DraftMove mOnClock in currentOnClock)
                    {
                        onclock.Add(new DraftMoveObj(mOnClock));
                    }

                    if (onclock.Count > 0)
                    {
                        onclock.Sort(new DraftMoveObjComparer());
                    }
                }

                // Determine if a new OnClock Move is needed
                bool addOnClock = (onclock.Count == 0 && toPause.Count == 0);

                // We attempt to minimize the time delay by waiting to calculate the current time
                // as far as possible. So if we don't need to calculate how much time is left
                // on the last OnClock Move, we'll wait longer. Otherwise, we have to re-use this one
                // to keep everything synchronized
                DateTime?currentTime = null;

                if (onclock.Count > 0)
                {
                    currentTime = DateTime.Now;
                    DraftMoveObj lastOnClock = onclock[onclock.Count - 1];
                    TimeSpan     timeLeft    = TimeSpan.FromSeconds(Settings.SecondsPerPick) - (currentTime.Value - lastOnClock.Time);

                    // Check if the last OnClock Move is over the time limit
                    if (timeLeft < TimeSpan.FromTicks(0))
                    {
                        addOnClock = true;
                    }
                }

                if (addOnClock && !isPaused)
                {
                    // Find the next empty slot
                    DraftMoveObj nextEmpty = FindNextEmptySlot();
                    // If no Empty Slots, the Draft is over, so make sure it gets paused
                    if (nextEmpty == null)
                    {
                        nextStatus = DraftActionType.Pause;
                    }
                    else
                    {
                        if (!currentTime.HasValue)
                        {
                            currentTime = DateTime.Now;
                        }
                        DraftMove newOnClock = new DraftMove()
                        {
                            SeasonID = nextEmpty.SeasonID,
                            Round    = nextEmpty.Round,
                            Pick     = nextEmpty.Pick,
                            PlayerID = null,
                            MoveType = (int)DraftMoveType.OnClock,
                            UserID   = nextEmpty.UserID,
                            Time     = currentTime.Value
                        };
                        db.DraftMoves.InsertOnSubmit(newOnClock);
                        addedOnClock = new DraftMoveObj(newOnClock);
                    }
                }

                // If the draft is paused, we need to update all the onClock Moves
                if (toPause.Count > 0)
                {
                    if (!currentTime.HasValue)
                    {
                        currentTime = DateTime.Now;
                    }
                    foreach (DraftMove nextMove in toPause)
                    {
                        TimeSpan origGap = oStatus.Time - nextMove.Time;
                        nextMove.Time = currentTime.Value - origGap;
                        onclock.Add(new DraftMoveObj(nextMove));
                    }
                    onclock.Sort(new DraftMoveObjComparer());
                }

                // If we added a new OnClock Move, make sure it is at the end of the onclock list
                if (addedOnClock != null)
                {
                    onclock.Add(addedOnClock);
                    onclock.Sort(new DraftMoveObjComparer());
                }

                // If the Draft is Paused or needs to change actions, update it
                int iCurr = (int)currStatus;
                int iNext = (int)nextStatus;
                if (isPaused || iCurr != iNext)
                {
                    if (!currentTime.HasValue)
                    {
                        currentTime = DateTime.Now;
                    }

                    oStatus.Time   = currentTime.Value;
                    oStatus.Status = iNext;
                }

                // Commit all changes to the DB
                db.SubmitChanges();
            }

            return(onclock);
        }