private void AssignPrompts() { IReadOnlyList <User> users = this.Lobby.GetAllUsers(); List <Prompt> randomizedOrderChallenges = this.PromptsToPopulate.OrderBy(_ => Rand.Next()).ToList(); List <Prompt> excessChallenges = randomizedOrderChallenges.Skip(this.NumRounds).ToList(); randomizedOrderChallenges = randomizedOrderChallenges.Take(this.NumRounds).ToList(); foreach (Prompt extra in excessChallenges) { this.PromptsToPopulate.Remove(extra); } if (randomizedOrderChallenges.Count == 0) { throw new Exception("Can't play the game if there are no prompts"); } List <IGroup <User> > groups = MemberHelpers <User> .Assign( randomizedOrderChallenges.Cast <IConstraints <User> >().ToList(), users, this.NumDrawingsPerUser); var assignments = groups.Zip(randomizedOrderChallenges); foreach ((IGroup <User> groupedUsers, Prompt tracker) in assignments) { tracker.UsersToDrawings = new ConcurrentDictionary <User, UserDrawing>( groupedUsers.Members.ToDictionary <User, User, UserDrawing>( keySelector: (user) => user, elementSelector: (user) => null)); tracker.Imposter = tracker.UsersToDrawings.Keys.ElementAt(Rand.Next(tracker.UsersToDrawings.Keys.Count)); } }
public void Assign(int[] inputIds, int duplicateCount, int groups, int maxPerGroup, bool allowDuplicateIds) { const int rerunCount = 100; Exception exc = null; int failCount = 0; for (int i = 0; i < rerunCount; i++) { try { TestUserManager.ResetTestUsers(); List <IConstraints <UserCreatedObject> > constraints = Enumerable.Range(0, groups) .Select(_ => new Constraints <UserCreatedObject> { AllowDuplicateIds = allowDuplicateIds, MaxMemberCount = maxPerGroup, }).Cast <IConstraints <UserCreatedObject> >().ToList(); List <UserCreatedObject> inputObjects = inputIds.Select(id => new UserCreatedObject() { Owner = TestUserManager.GetTestUser(id), Id = TestUserManager.GetTestUser(id).Id }).ToList(); List <IGroup <UserCreatedObject> > returnedObjects = MemberHelpers <UserCreatedObject> .Assign(constraints, inputObjects, duplicateCount).ToList(); Assert.AreEqual(groups, returnedObjects.Count()); if (inputIds.Length * duplicateCount > returnedObjects.Sum(group => group.Members.Count())) { Assert.IsTrue(returnedObjects.All(group => group.Members.Count() == maxPerGroup)); } else { Assert.AreEqual(inputIds.Length * duplicateCount, returnedObjects.Sum(group => group.Members.Count())); Assert.IsTrue(returnedObjects.All(group => group.Members.Count() <= maxPerGroup)); } } catch (Exception e) { exc = e; failCount++; } } Assert.AreEqual(0, failCount, message: $"Success rate ({rerunCount - failCount}/{rerunCount}) not 100%. Example Exception: {exc}"); }
private void AssignPrompts() { Stopwatch stopwatch = Stopwatch.StartNew(); IReadOnlyList <User> users = this.Lobby.GetAllUsers(); List <ChallengeTracker> randomizedOrderChallenges = this.SubChallenges.Keys.OrderBy(_ => Rand.Next()).ToList(); List <ChallengeTracker> excessChallenges = randomizedOrderChallenges.Skip(this.NumRounds).ToList(); randomizedOrderChallenges = randomizedOrderChallenges.Take(this.NumRounds).ToList(); foreach (ChallengeTracker extra in excessChallenges) { this.SubChallenges.Remove(extra, out object _); } if (randomizedOrderChallenges.Count == 0) { throw new Exception("Can't play the game if there are no prompts"); } List <IGroup <User> > groups = MemberHelpers <User> .Assign( randomizedOrderChallenges.Cast <IConstraints <User> >().ToList(), users, this.LayersPerTeam *this.TeamsPerPrompt); var assignments = groups.Zip(randomizedOrderChallenges); foreach ((IGroup <User> groupedUsers, ChallengeTracker tracker) in assignments) { int badCodingPractice = 0; tracker.UserSubmittedDrawings = groupedUsers.Members.ToDictionary((user) => user, (user) => new ChallengeTracker.TeamUserDrawing { TeamId = Invariant($"{(badCodingPractice / LayersPerTeam)+1}"), Color = tracker.Colors[badCodingPractice++ % LayersPerTeam], Owner = user }); } Debug.WriteLine(Invariant($"Assigned user prompts in ({stopwatch.ElapsedMilliseconds} ms)"), "Timing"); }
public FriendQuizGameMode(Lobby lobby, List <ConfigureLobbyRequest.GameModeOptionRequest> gameModeOptions, StandardGameModeOptions standardOptions) { GameDuration duration = standardOptions.GameDuration; double questionPoolMultiplier = 2.5; // Question pool is X times bigger than number of questions per person. int numQuestionsToAnswer = (int)gameModeOptions[(int)GameModeOptionsEnum.NumAnswerQuestions].ValueParsed; int numRounds = Math.Min(lobby.GetAllUsers().Count(), FriendQuizConstants.MaxUserRounds[duration]); int numQuestionSetup = (int)(numQuestionsToAnswer * questionPoolMultiplier / numRounds) + 1; // How many questions each user should contribute. TimeSpan?setupTimer = null; TimeSpan?answeringTimer = null; TimeSpan?votingTimer = null; if (standardOptions.TimerEnabled) { setupTimer = FriendQuizConstants.SetupTimer[duration]; answeringTimer = FriendQuizConstants.AnsweringTimer[duration]; votingTimer = FriendQuizConstants.VotingTimer[duration]; } Dictionary <User, List <Question> > usersToAssignedQuestions = new Dictionary <User, List <Question> >(); int maxNumAssigned = 1; // Variable for tracking user with most questions. StateChain gameStateChain = new StateChain(stateGenerator: (int counter) => { if (counter == 0) { setupTimer = setupTimer?.Multiply(numQuestionSetup); return(new Setup_GS( lobby: lobby, roundTracker: RoundTracker, numExpectedPerUser: numQuestionSetup, setupDuration: setupTimer)); } else if (counter == 1) { List <UserQuestionsHolder> userQuestionsHolders = lobby.GetAllUsers().Select(user => new UserQuestionsHolder(user, numQuestionsToAnswer)).ToList(); List <Question> randomizedQuestions = RoundTracker.Questions.OrderBy(_ => Rand.Next()).ToList(); List <IGroup <Question> > assignments = MemberHelpers <Question> .Assign(userQuestionsHolders.Cast <IConstraints <Question> >().ToList(), randomizedQuestions, lobby.GetAllUsers().Count); var pairings = userQuestionsHolders.Zip(assignments); foreach ((UserQuestionsHolder holder, IGroup <Question> questions) in pairings) { if (questions.Members.Count() > maxNumAssigned) { maxNumAssigned = questions.Members.Count(); } // Makes a copy of the questions so that it can handle multiple people answering the same question without them both overriding the same object usersToAssignedQuestions.Add(holder.QuestionedUser, questions.Members.Select(question => new Question(question) { MainUser = holder.QuestionedUser }).ToList()); } answeringTimer = answeringTimer?.Multiply(maxNumAssigned); return(new AnswerQuestion_GS( lobby: lobby, usersToAssignedQuestions: usersToAssignedQuestions, answerTimeDuration: answeringTimer)); } else if (counter == 2) { votingTimer = votingTimer?.Multiply(maxNumAssigned); List <User> randomizedUsers = usersToAssignedQuestions.Keys .OrderBy(_ => Rand.Next()) .ToList() // Probably not needed, but just in case. .Take(numRounds) // Number of rounds is limited based on game duration. .ToList(); return(GetUserQueryStateChain(randomizedUsers)); } else { return(null); } }); this.Entrance.Transition(gameStateChain); gameStateChain.Transition(this.Exit); StateChain GetUserQueryStateChain(List <User> users) { List <State> chain = new List <State>(); foreach (User user in users) { List <Question> nonAbstainedQuestions = usersToAssignedQuestions[user].Where(question => !question.Abstained).ToList(); if (nonAbstainedQuestions.Count > 0) { chain.Add(new StateChain(stateGenerator: (int counter) => { if (counter == 0) { return(new SliderQueryAndReveal( lobby: lobby, objectsToQuery: nonAbstainedQuestions, usersToQuery: lobby.GetAllUsers().Where(lobbyUser => lobbyUser != user).ToList(), queryTime: votingTimer) { QueryPromptTitle = $"How do you think {user.DisplayName} answered these questions?", QueryPromptDescription = "The tighter the range of your guess, the more points if you're correct", QueryViewOverrides = new UnityViewOverrides() { Title = $"How do you think {user.DisplayName} answered these questions?", }, RevealViewOverrides = new UnityViewOverrides() { Title = $"This is how {user.DisplayName} answered those questions.", }, QueryExitListener = CountQueries, }); } else if (counter == 1) { if (user == users.Last()) { return(new ScoreBoardGameState(lobby, "Final Scores")); } else { return(new ScoreBoardGameState(lobby)); } } else { return(null); } })); } } return(new StateChain(chain)); } }
public BattleReadyGameMode(Lobby lobby, List <ConfigureLobbyRequest.GameModeOptionRequest> gameModeOptions, StandardGameModeOptions standardOptions) { GameDuration duration = standardOptions.GameDuration; this.Lobby = lobby; int numRounds = BattleReadyConstants.NumRounds[duration]; int numPlayers = lobby.GetAllUsers().Count(); TimeSpan?setupDrawingTimer = null; TimeSpan?setupPromptTimer = null; TimeSpan?creationTimer = null; TimeSpan?votingTimer = null; int numOfEachPartInHand = (int)gameModeOptions[(int)GameModeOptionsEnum.NumEachPartInHand].ValueParsed; int numPromptsPerRound = Math.Min(numPlayers, BattleReadyConstants.MaxNumSubRounds[duration]); int minDrawingsRequired = numOfEachPartInHand * 3; // the amount to make one playerHand to give everyone int expectedPromptsPerUser = (int)Math.Ceiling(1.0 * numPromptsPerRound * numRounds / lobby.GetAllUsers().Count); int expectedDrawingsPerUser = Math.Max((minDrawingsRequired / numPlayers + 1) * 2, BattleReadyConstants.NumDrawingsPerPlayer[duration]); if (standardOptions.TimerEnabled) { setupDrawingTimer = BattleReadyConstants.SetupPerDrawingTimer[duration]; setupPromptTimer = BattleReadyConstants.SetupPerPromptTimer[duration]; creationTimer = BattleReadyConstants.PerCreationTimer[duration]; votingTimer = BattleReadyConstants.VotingTimer[duration]; } SetupDrawings_GS setupDrawing = new SetupDrawings_GS( lobby: lobby, drawings: this.Drawings, numExpectedPerUser: expectedDrawingsPerUser, setupDurration: setupDrawingTimer * expectedDrawingsPerUser); SetupPrompts_GS setupPrompt = new SetupPrompts_GS( lobby: lobby, prompts: Prompts, numExpectedPerUser: expectedPromptsPerUser, setupDuration: setupPromptTimer); List <Prompt> battlePrompts = new List <Prompt>(); IReadOnlyList <PeopleUserDrawing> headDrawings = new List <PeopleUserDrawing>(); IReadOnlyList <PeopleUserDrawing> bodyDrawings = new List <PeopleUserDrawing>(); IReadOnlyList <PeopleUserDrawing> legsDrawings = new List <PeopleUserDrawing>(); setupDrawing.AddExitListener(() => { // Trim extra prompts/drawings. headDrawings = Drawings.ToList().FindAll((drawing) => drawing.Type == BodyPartType.Head); bodyDrawings = Drawings.ToList().FindAll((drawing) => drawing.Type == BodyPartType.Body); legsDrawings = Drawings.ToList().FindAll((drawing) => drawing.Type == BodyPartType.Legs); }); int numPromptsPerUserPerRound = 0; // Set during below exit listener. setupPrompt.AddExitListener(() => { battlePrompts = MemberHelpers <Prompt> .Select_Ordered(Prompts.OrderBy(prompt => prompt.CreationTime).ToList(), numPromptsPerRound * numRounds); numRounds = (battlePrompts.Count - 1) / numPromptsPerRound + 1; numPromptsPerRound = (int)Math.Ceiling(1.0 * battlePrompts.Count / numRounds); numPromptsPerUserPerRound = Math.Max(1, numPromptsPerRound / 2); int maxNumUsersPerPrompt = Math.Min(12, (int)Math.Ceiling(1.0 * numPlayers * numPromptsPerUserPerRound / numPromptsPerRound)); foreach (Prompt prompt in battlePrompts) { prompt.MaxMemberCount = maxNumUsersPerPrompt; } }); List <GameState> creationGameStates = new List <GameState>(); List <GameState> votingGameStates = new List <GameState>(); List <GameState> voteRevealedGameStates = new List <GameState>(); List <GameState> scoreboardGameStates = new List <GameState>(); int countRounds = 0; #region GameState Generators GameState CreateContestantCreationGamestate() { RoundTracker.ResetRoundVariables(); List <Prompt> prompts = battlePrompts.Take(numPromptsPerRound).ToList(); battlePrompts.RemoveRange(0, prompts.Count); List <IGroup <User> > assignments = MemberHelpers <User> .Assign( prompts.Cast <IConstraints <User> >().ToList(), lobby.GetAllUsers().ToList(), duplicateMembers : (int)Math.Ceiling((1.0 * prompts.Count / numPromptsPerRound) * numPromptsPerUserPerRound)); var pairings = prompts.Zip(assignments); foreach ((Prompt prompt, IGroup <User> users) in pairings) { foreach (User user in users.Members) { prompt.UsersToUserHands.TryAdd(user, new Prompt.UserHand { // Users have even probabilities regardless of how many drawings they submitted. HeadChoices = MemberHelpers <PeopleUserDrawing> .Select_DynamicWeightedRandom(headDrawings, numOfEachPartInHand), BodyChoices = MemberHelpers <PeopleUserDrawing> .Select_DynamicWeightedRandom(bodyDrawings, numOfEachPartInHand), LegChoices = MemberHelpers <PeopleUserDrawing> .Select_DynamicWeightedRandom(legsDrawings, numOfEachPartInHand), Owner = user }); if (!RoundTracker.UsersToAssignedPrompts.ContainsKey(user)) { RoundTracker.UsersToAssignedPrompts.Add(user, new List <Prompt>()); } RoundTracker.UsersToAssignedPrompts[user].Add(prompt); } } GameState toReturn = new ContestantCreation_GS( lobby: lobby, roundTracker: RoundTracker, creationDuration: creationTimer); toReturn.Transition(CreateVotingGameStates(prompts)); return(toReturn); } Func <StateChain> CreateVotingGameStates(List <Prompt> roundPrompts) { return(() => { StateChain voting = new StateChain( stateGenerator: (int counter) => { if (counter < roundPrompts.Count) { Prompt roundPrompt = roundPrompts[counter]; return GetVotingAndRevealState(roundPrompt, votingTimer); } else { // Stops the chain. return null; } }); voting.Transition(CreateScoreGameState(roundPrompts)); return voting; }); } Func <GameState> CreateScoreGameState(List <Prompt> roundPrompts) { return(() => { List <Person> winnersPeople = roundPrompts.Select((prompt) => (Person)prompt.UsersToUserHands[prompt.Winner]).ToList(); countRounds++; GameState displayPeople = new DisplayPeople_GS( lobby: lobby, title: "Here are your winners", peopleList: winnersPeople, imageTitle: (person) => roundPrompts[winnersPeople.IndexOf(person)].Text, imageHeader: (person) => person.Name ); if (battlePrompts.Count <= 0) { GameState finalScoreBoard = new ScoreBoardGameState( lobby: lobby, title: "Final Scores"); displayPeople.Transition(finalScoreBoard); finalScoreBoard.Transition(this.Exit); } else { GameState scoreBoard = new ScoreBoardGameState( lobby: lobby); displayPeople.Transition(scoreBoard); scoreBoard.Transition(CreateContestantCreationGamestate); } return displayPeople; }); } #endregion this.Entrance.Transition(setupDrawing); setupDrawing.Transition(setupPrompt); setupPrompt.Transition(CreateContestantCreationGamestate); }