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 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; }
private void detach_DraftMoves(DraftMove entity) { this.SendPropertyChanging(); entity.DraftOrder = null; }
partial void DeleteDraftMove(DraftMove instance);
partial void UpdateDraftMove(DraftMove instance);
partial void InsertDraftMove(DraftMove instance);
private void attach_DraftMoves(DraftMove entity) { this.SendPropertyChanging(); entity.User = this; }