Beispiel #1
0
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            // simple polling of the server
            if (bPollServer)
            {
                await Task.Delay(1000);

                int nGameStatus = turnService.GetGameStatus(gameId);
                if (nGameStatus != 0)
                {
                    // first check if the game is over
                    if ((nGameStatus & 2) == 0)
                    {
                        TurnMessage      = "Game Over";
                        game.NGameStatus = nGameStatus;
                        bPollServer      = false; // if so stop polling
                    }

                    if (nGameStatus != game.NGameStatus)
                    {
                        TurnMessage      = "Your turn";
                        bPollServer      = false;
                        game.NGameStatus = nGameStatus;
                        var history = await gameService.GetLastMove(gameId);

                        if (history != null)
                        {
                            List <Piece> pieces = (List <Piece>) await gameService.GetHistoryPieces(history.HistoryId);

                            if (history.NActionType == 1)
                            {
                                if (pieces.Count() != 0)
                                {
                                    // animate
                                    // this call returns immediately but the javascript method sets up an
                                    // animation that takes 1000ms to complete
                                    await JSRuntime.InvokeVoidAsync("tileAnimate", history.NSourcePos, history.NDestPos + (OtherPlayer.NPlayer - 1) * 25 + 5);

                                    await Task.Delay(100); // wait for javascript to grab the images

                                    // updating components before animation completes
                                    // so the pieces dissappear from the board
                                    playerBoard.Update(game.NGameStatus, null);
                                    truck.Update(game.NGameStatus, pieces[0]);      // remove

                                    await Task.Delay(900);                          // rest of time it takes for animation to complete.

                                    otherBoard.Update(game.NGameStatus, pieces[0]); // add

                                    await RefillTruck(history.NSourcePos);
                                }
                            }
                            else if (history.NActionType == 2)
                            {
                                var harvestIdList = new List <int>();
                                foreach (var piece in pieces)
                                {
                                    harvestIdList.Add(piece.NPosition + (OtherPlayer.NPlayer - 1) * 25 + 5);
                                }
                                await JSRuntime.InvokeVoidAsync("doAnimateArray", (object)harvestIdList);

                                await Task.Delay(100); // wait for javascript to grab the images

                                // updating components before animation completes
                                // so the pieces dissappear from the board
                                otherBoard.ConfirmHarvest(pieces);
                                playerBoard.Update(game.NGameStatus);
                                otherBoard.Update(game.NGameStatus);
                                truck.Update(game.NGameStatus); // remove

                                await Task.Delay(900);          // rest of time it takes for animation to complete.

                                UpdateScores(history.NScore1, history.NScore2);
                            }
                        }
                    }
                }
                StateHasChanged();
            }
        }
Beispiel #2
0
        public async void StatusChangeHandler(Status status)
        {
            Console.WriteLine("StatusChangedHandler");

            if (status.nCaller == 0)
            {
                bFrom     = true;
                iFrom     = status.ButtonId;
                pieceFrom = status.piece;
                StateHasChanged();
            }
            if (status.nCaller != 0 && status.bHarvest)
            {
                var action = new GameAction(gameId, playerBoard.companionBonus, Player.Identity, playerBoard.harvestList);
                var result = await gameService.SubmitAction(action);

                if (result.nResponseCode == 1)
                {
                    await JSRuntime.InvokeVoidAsync("doAnimateArray", (object)playerBoard.harvestIdList);

                    await Task.Delay(100); // wait for javascript to grab the images

                    // updating components before animation completes
                    // so the pieces dissappear from the board
                    game.NGameStatus = result.nGameStatus;
                    playerBoard.ConfirmHarvest();
                    playerBoard.Update(game.NGameStatus);
                    otherBoard.Update(game.NGameStatus);

                    await Task.Delay(900); // rest of time it takes for animation to complete.

                    UpdateScores(result.nScore1, result.nScore2);

                    bPollServer = true;
                    turnService.SetGameStatus(game.GameId, game.NGameStatus);

                    TurnMessage = "Please wait";

                    StateHasChanged();
                }
            }
            if (status.nCaller != 0 && bFrom)
            {
                bFrom = false;
                truck.ClearHighlighting();
                int sourcePosition = pieceFrom.NPosition;   // save source position so we can refill it later
                var action         = new GameAction(1, status.ButtonId - (status.nCaller - 1) * 25 - 5,
                                                    pieceFrom.PieceId, game.GameId, Player.Identity);
                var result = await gameService.SubmitAction(action);

                if (result.nResponseCode == 1)
                {
                    // animate
                    // this call returns immediately but the javascript method sets up an
                    // animation that takes 1000ms to complete
                    await JSRuntime.InvokeVoidAsync("tileAnimate", iFrom, status.ButtonId);

                    await Task.Delay(100); // wait for javascript to grab the image

                    // remove old piece from truck list
                    // so the piece dissappears from the board
                    truck.Update(game.NGameStatus, pieceFrom);

                    await Task.Delay(900); // rest of time it takes for animation to complete.

                    // flip piece if required
                    if (action.nActionArguement % 5 != pieceFrom.NPosition)
                    {
                        pieceFrom.NPieceStatus = (pieceFrom.NPieceStatus | 2); // set bit 2 - star side down
                    }
                    // set pieceFrom new position and owner
                    pieceFrom.NPosition = action.nActionArguement;
                    pieceFrom.NOwner    = Player.NPlayer;

                    // updating components after animation completes
                    // so the piece reappears at the destination
                    game.NGameStatus = result.nGameStatus;
                    playerBoard.Update(game.NGameStatus, pieceFrom);
                    otherBoard.Update(game.NGameStatus);

                    // first check if the game is over
                    if ((game.NGameStatus & 2) == 0)
                    {
                        TurnMessage = "Game Over";
                    }
                    else
                    {
                        bPollServer = true;
                        TurnMessage = "Please wait";
                    }

                    turnService.SetGameStatus(game.GameId, game.NGameStatus);

                    // add new piece
                    await RefillTruck(sourcePosition);

                    StateHasChanged();
                }
            }
        }