public void RejectCurrentWord(NetworkInstanceId clickerId) { Player clickingPlayer = Player.All.FirstOrDefault(p => p.netId == clickerId); if (clickingPlayer == null) { Debug.LogError("Could not find player with id " + clickerId); return; } //If the clicking player is also the drawing player //And as long as nobody has guessed yet if (clickingPlayer == DrawingPlayer && Player.All.All(p => !p.HasGuessed)) { //First we let everybody know the word has been rejected, and what the word was MessageBoard.RpcSubmitMessage(Message.Server(clickingPlayer.GameName + " has rejected the word " + CurrentWord + ".")); //Then we record the rejection in the current word transaction, and complete the transaction _currentWordTransaction.Reject(1.0f - _serverTimeLeft / _timePerTurn.Value); _currentWordTransaction.CompleteTransaction(); _currentWordTransaction = null; _wordBankManager.SaveActiveWordBank(); //Then we (re)start the turn startNextTurn(); } }
/// <summary> /// Given a specific selector, select a word from the word bank to use. Returns /// a WordTransaction that can be used to notify changes to the word that should /// be reflected in the bank. /// /// Once a transaction has begun, you cannot start any new transactions until you /// call EndWord. Modifications to the word will not take effect unless EndWord /// is called. /// </summary> public WordTransaction BeginWord(IWordSelector selector) { if (_currentTransaction != null) { throw new InvalidOperationException("Cannot begin a new word transaction while one is currently in progress."); } string word = selector.SelectWord(_wordData.ToArray(), _entryHistory.ToArray()); _currentTransaction = new WordTransaction(word, endWord); return(_currentTransaction); }
/// <summary> /// Ends the currently active transaction and stores the changes in the database. /// </summary> private void endWord(WordData.TurnData turnData) { _entryHistory.Insert(0, _currentTransaction.word); while (_entryHistory.Count > _wordData.Length) { _entryHistory.RemoveAt(_entryHistory.Count - 1); } WordData wordData = _wordData.Single(d => d.Word == _currentTransaction.word); wordData.Turns.Add(turnData); _currentTransaction = null; }
private void startNextTurn() { Debug.Log("Starting new turn..."); Assert.IsNull(_currentWordTransaction, "The current transaction should be null before we start a new one."); _currentWordTransaction = _wordBankManager.Bank.BeginWord(new BasicSelector()); _currentWordTransaction.SetPlayerCount(Player.InGame.Count()); TargetUpdateCurrentWord(DrawingPlayer.connectionToClient, CurrentWord); foreach (var player in Player.All) { player.HasGuessed = false; player.TimerHasReachedZero = false; } DrawingBoard.ClearAndReset(); _serverTimeLeft = _timePerTurn.Value; RpcUpdateTimeLeft(_serverTimeLeft, forceUpdate: true); }
private void finishTurn() { Debug.Log("Finishing turn..."); Assert.IsNotNull(_currentWordTransaction, "We should always be undergoing a word transaction before finishing a turn."); MessageBoard.RpcSubmitMessage(Message.Server("The word was " + CurrentWord)); _currentWordTransaction.CompleteTransaction(); _currentWordTransaction = null; _wordBankManager.SaveActiveWordBank(); if (Player.All.Any(p => p.HasGuessed)) { //Drawing player gets 9 points as long as someone has guessed //Plus 1 point for every player who guessed (which is at least 1) DrawingPlayer.Score += 9 + Player.All.Count(p => p.HasGuessed); //All other players get points based on the order they guessed //First to guess gets 10, next gets 9, and so on //A player that guesses always gets at least 1 point int points = 10; foreach (var player in Player.All.Where(p => p.HasGuessed).OrderBy(p => p.guessTime)) { player.Score += points; points = Mathf.Max(1, points - 1); } } //Start the lobby if there are not enough players to play a game if (Player.All.Count(p => p.IsInGame) <= 1) { Debug.Log("Starting lobby because there were not enough players..."); StartLobby(); return; } //Decrement the turn counter //And end the game if we have reached zero turns! _turnsLeft--; if (_turnsLeft == 0) { int maxScore = Player.InGame.Select(p => p.Score).Max(); var winners = Player.InGame.Where(p => p.Score == maxScore); if (winners.Count() == 1) { var winner = winners.Single(); MessageBoard.RpcSubmitMessage(Message.Server("Player " + winner.GameName + " wins!")); } else { MessageBoard.RpcSubmitMessage(Message.Server("Game is a tie between " + string.Join(" and ", winners.Select(p => p.GameName).ToArray()) + "!")); } StartLobby(); return; } //Skip to next player who is ingame { int currIndex = Player.All.IndexOf(DrawingPlayer); do { currIndex = (currIndex + 1) % Player.All.Count; } while (!Player.All[currIndex].IsInGame); _drawingPlayerId = Player.All[currIndex].netId.Value; RpcUpdateDrawingPlayer(_drawingPlayerId); } startNextTurn(); }