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); }
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); }
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); }
public DraftMoveObj Put(OnClockUpdateObj update) { DraftUser user = DraftAuthentication.AuthenticateRequest(Request, 1); DraftMoveObj toRet = dataSource.UpdateLastOnTheClock(update); if (toRet == null) { throw new HttpStatusException(HttpStatusCode.NotFound, "Unable to find pick to update"); } return(toRet); }
private List <DraftPickStatusObj> GetDraftPicks() { DraftUser user = DraftAuthentication.AuthenticateRequest(Request); List <DraftPickStatusObj> toRet = new List <DraftPickStatusObj>(); List <DraftMoveObj> onTheClock = new List <DraftMoveObj>(); Dictionary <QuickPick, DraftMoveObj> draftData = dataSource.QueryDraftData(out onTheClock); DraftPickStatusObj onClockPick = null; DraftMoveObj lastOnClockMoveObj = null; for (QuickPick index = new QuickPick(); index.Round <= BallersDraftObj.Settings.RoundsPerDraft; index.Round++) { for (index.Pick = 1; index.Pick <= BallersDraftObj.Settings.TeamsPerDraft; index.Pick++) { DraftMoveObj nextMove = draftData[index]; toRet.Add(new DraftPickStatusObj(BallersDraftObj.Settings.TeamsPerDraft) { Round = nextMove.Round, Pick = nextMove.Pick, Team = nextMove.UserID, Player = nextMove.PlayerID, Type = nextMove.TypeInt }); } } foreach (DraftMoveObj currMove in onTheClock) { lastOnClockMoveObj = currMove; onClockPick = new DraftPickStatusObj(BallersDraftObj.Settings.TeamsPerDraft) { Round = currMove.Round, Pick = currMove.Pick, Team = currMove.UserID, Type = currMove.TypeInt }; toRet[onClockPick.TotalPick - 1] = onClockPick; } if (lastOnClockMoveObj != null) { TimeSpan timeLeft = TimeSpan.FromSeconds(BallersDraftObj.Settings.SecondsPerPick) - (DateTime.Now - lastOnClockMoveObj.Time); if (timeLeft < TimeSpan.FromTicks(0)) { timeLeft = TimeSpan.FromTicks(0); } onClockPick.TimeLeft = (int)timeLeft.TotalSeconds; } return(toRet); }
DraftMoveObj FindUserOnClock(int userID) { DraftMoveObj match = null; List <DraftMoveObj> onClock = FindOnTheClock(); foreach (DraftMoveObj allowed in onClock) { if (allowed.UserID == userID) { match = allowed; break; } } return(match); }
/// <summary> /// NOTE: You MUST Lock before calling this function if you are going to use the date to write /// </summary> /// <returns></returns> private DraftMoveObj FindNextEmptySlot() { DraftMoveObj toRet = null; Dictionary <QuickPick, DraftMoveObj> draft = GetDraftStatus(); for (QuickPick index = new QuickPick() { Round = 1, Pick = 1 }; index.Round <= Settings.RoundsPerDraft && toRet == null; index.Round++) { for (index.Pick = 1; index.Pick <= Settings.TeamsPerDraft; index.Pick++) { if (draft[index].IsEmpty) { toRet = draft[index]; break; } } } return(toRet); }
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; }
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; }
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; }
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); }