예제 #1
0
파일: Game.cs 프로젝트: SinaC/TetriNET2
        private void PlacePieceAction(IClient client, int pieceIndex, int highestIndex, Pieces piece, int orientation, int posX, int posY, byte[] grid)
        {
            Log.Default.WriteLine(LogLevels.Debug, "Game {0}: PlacePieceAction[{1}]{2}:{3} {4} {5} at {6},{7} {8}", Name, client.Name, pieceIndex, highestIndex, piece, orientation, posX, posY, grid == null ? -1 : grid.Count(x => x > 0));

            //if (index != player.PieceIndex)
            //    Log.Default.WriteLine(LogLevels.Error, "!!!! piece index different for player {0} local {1} remote {2}", player.Name, player.PieceIndex, index);

            bool sendNextPieces = false;
            // Set grid
            client.Grid = grid;
            // Get next piece
            client.PieceIndex = pieceIndex;
            List<Pieces> nextPiecesToSend = new List<Pieces>();
            Log.Default.WriteLine(LogLevels.Debug, "{0} {1} indexes: {2} {3}", client.Id, client.Name, highestIndex, pieceIndex);
            if (highestIndex < pieceIndex)
                Log.Default.WriteLine(LogLevels.Error, "PROBLEM WITH INDEXES!!!!!");
            if (highestIndex < pieceIndex + PiecesSendOnPlacePiece) // send many pieces when needed
            {
                for (int i = 0; i < 2 * PiecesSendOnPlacePiece; i++)
                    nextPiecesToSend.Add(_pieceProvider[highestIndex + i]);
                sendNextPieces = true;
            }
            else if (highestIndex < pieceIndex + 2 * PiecesSendOnPlacePiece) // send next pieces only if needed
            {
                for (int i = 0; i < PiecesSendOnPlacePiece; i++)
                    nextPiecesToSend.Add(_pieceProvider[highestIndex + i]);
                sendNextPieces = true;
            }

            // Send grid to other playing players and spectators
            foreach (IClient target in Clients.Where(x => x != client))
                target.OnGridModified(client.Id, grid);

            if (sendNextPieces)
            {
                Log.Default.WriteLine(LogLevels.Debug, "Send next piece {0} {1} {2}", highestIndex, pieceIndex, nextPiecesToSend.Any() ? nextPiecesToSend.Select(x => x.ToString()).Aggregate((n, i) => n + "," + i) : String.Empty);
                // Send next pieces
                client.OnPiecePlaced(highestIndex, nextPiecesToSend);
            }
        }