public static async Task <GameState> UpdateWinnerAsync(PlayFabAuthenticationContext context, string playerId, string SharedGroupId)
        {
            var sharedGroupData = await SharedGroupDataUtil.GetAsync(context, SharedGroupId);

            sharedGroupData.GameState.Winner = sharedGroupData.Match.PlayerOneId == playerId ?
                                               (int)OccupantType.PLAYER_ONE : (int)OccupantType.PLAYER_TWO;

            await SharedGroupDataUtil.UpdateAsync(context, sharedGroupData);

            return(sharedGroupData.GameState);
        }
示例#2
0
        public static async Task <TicTacToeSharedGroupData> JoinMatchLobby(PlayFabAuthenticationContext authenticationContext, string matchLobbyId, string playerTwo, DocumentClient documentClient)
        {
            var tttShareGroupData = await SharedGroupDataUtil.GetAsync(authenticationContext, matchLobbyId);

            // we need the document reference for the Cosmos DB for using its "etag",
            // which avoids having race-conditions when writing in DDBB.
            var documentLobby = await GetMatchLobbyFromClient(documentClient, matchLobbyId);

            if (tttShareGroupData?.Match == null)
            {
                throw new Exception("Match not exists");
            }

            if (documentLobby == null)
            {
                throw new Exception("Match Lobby does not exist in Database");
            }

            if (!string.IsNullOrWhiteSpace(tttShareGroupData.Match.PlayerOneId) && !string.IsNullOrWhiteSpace(tttShareGroupData.Match.PlayerTwoId))
            {
                throw new Exception("Match is full");
            }

            if (string.Compare(tttShareGroupData.Match.PlayerOneId, playerTwo, true) == 0)
            {
                throw new Exception("This player is already the player one");
            }

            tttShareGroupData.Match.PlayerTwoId = playerTwo;
            tttShareGroupData.MatchLobby.CurrentAvailability--;

            // update the current availability in Cosmos DB
            documentLobby.CurrentAvailability = tttShareGroupData.MatchLobby.CurrentAvailability;

            // This method will update the Cosmos DB document with the newest availability.
            // If there was an outside-modification by other player (race condition),
            // this will throw an error and the player won't be able to join this Lobby.
            await DocumentClientManager.ReplaceDocument(documentClient, documentLobby, Constants.DATABASE_NAME, Constants.MATCH_LOBBY_TABLE_NAME);

            await SharedGroupDataUtil.AddMembersAsync(
                authenticationContext,
                matchLobbyId,
                new List <string> {
                tttShareGroupData.Match.PlayerTwoId
            }
                );

            await SharedGroupDataUtil.UpdateAsync(authenticationContext, tttShareGroupData);

            return(tttShareGroupData);
        }
        public static async Task <GameState> MoveUpdateAsync(PlayFabAuthenticationContext context, string playerId, TicTacToeMove playerMove, string SharedGroupId)
        {
            var sharedGroupData = await SharedGroupDataUtil.GetAsync(context, SharedGroupId);

            var newGameState = UpdateAsync(sharedGroupData.Match, sharedGroupData.GameState, playerId, playerMove);

            if (newGameState != null)
            {
                sharedGroupData.GameState = newGameState;
                await SharedGroupDataUtil.UpdateAsync(context, sharedGroupData);
            }

            return(newGameState);
        }
        public static async Task <GameState> InitializeAsync(PlayFabAuthenticationContext context, string SharedGroupId)
        {
            var sharedGroupData = await SharedGroupDataUtil.GetAsync(context, SharedGroupId);

            sharedGroupData.GameState = new GameState
            {
                BoardState      = new int[9],
                CurrentPlayerId = sharedGroupData.Match.PlayerOneId,
                Winner          = (int)OccupantType.NONE
            };

            await SharedGroupDataUtil.UpdateAsync(context, sharedGroupData);

            return(sharedGroupData.GameState);
        }
示例#5
0
        public static async Task <TicTacToeSharedGroupData> AddMember(PlayFabAuthenticationContext context, string sharedGroupId, string playerId)
        {
            var sharedGroup = await SharedGroupDataUtil.GetAsync(context, sharedGroupId);

            if (!string.IsNullOrWhiteSpace(sharedGroup.Match.PlayerOneId) && !string.IsNullOrWhiteSpace(sharedGroup.Match.PlayerTwoId))
            {
                throw new Exception("Match is full");
            }

            if (string.Compare(sharedGroup.Match.PlayerOneId, playerId, true) == 0)
            {
                throw new Exception("This player is already the player one");
            }

            sharedGroup.Match.PlayerTwoId = playerId;

            return(await SharedGroupDataUtil.UpdateAsync(context, sharedGroup));
        }
示例#6
0
        public static async Task <TicTacToeSharedGroupData> CreateMatchLobby(PlayFabAuthenticationContext authenticationContext, string sharedGroupId, string playerOne, IAsyncCollector <MatchLobby> matchlobbyCollection)
        {
            var sharedGroupData = new TicTacToeSharedGroupData
            {
                SharedGroupId = sharedGroupId,
                Match         = new Match
                {
                    PlayerOneId = playerOne
                },
                MatchLobby = new MatchLobby
                {
                    MatchLobbyId        = sharedGroupId,
                    CurrentAvailability = 1
                }
            };

            var sharedGroupDataUpdated = await SharedGroupDataUtil.UpdateAsync(authenticationContext, sharedGroupData);

            await matchlobbyCollection.AddAsync(sharedGroupDataUpdated.MatchLobby);

            return(sharedGroupDataUpdated);
        }
        public static async Task <GameState> GetAsync(PlayFabAuthenticationContext context, string SharedGroupId)
        {
            var sharedGroupData = await SharedGroupDataUtil.GetAsync(context, SharedGroupId);

            return(sharedGroupData.GameState);
        }