예제 #1
0
        private void RequestPlayerInfoFromServer()
        {
            Debug.Log(TitleDescriptionButtonLinkData.LinkID);
            PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
            {
                FunctionName      = "GetPlayerStats",
                FunctionParameter = new
                {
                    playerId = TitleDescriptionButtonLinkData.LinkID
                },
                GeneratePlayStreamEvent = true,
            },
                                                result =>
            {
                // get Json object representing the Game State out of FunctionResult
                JsonObject jsonResult = (JsonObject)result.FunctionResult;

                // check if data exists
                if (jsonResult == null)
                {
                    Debug.Log("server failed to return data");
                }
                else
                {
                    string statsJSON = RPSCommon.InterpretCloudScriptData(jsonResult, "Stats");
                    PlayerStatsFromServer playerStatsFromServer = PlayerStatsFromServer.CreateFromJSON(statsJSON);
                    PlayerStats playerStats = new PlayerStats(playerStatsFromServer, TitleDescriptionButtonLinkData.Label);
                    UpdatePlayerUI(playerStats);
                }
            },
                                                error => Debug.LogError(error.GenerateErrorReport())
                                                );
        }
예제 #2
0
        public void StartSeason(StartSeasonCallback callback, StartSeasonCallback errorCallback)
        {
            Status = "In Progress";
            PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
            {
                FunctionName      = "StartLeagueSeason",
                FunctionParameter = new
                {
                    status   = Status,
                    leagueId = Key,
                },
                GeneratePlayStreamEvent = true,
            },
                                                result =>
            {
                // get Json object representing the host's schedule out of FunctionResult
                JsonObject jsonResult = (JsonObject)result.FunctionResult;

                // check if data exists
                if (jsonResult == null)
                {
                    Debug.Log("schedule generation failed...");
                    errorCallback();
                    return;
                }

                // data successfully received
                // interpret data
                string error = RPSCommon.InterpretCloudScriptData(jsonResult, "error");
                if (error != null)
                {
                    Debug.Log(error);
                    errorCallback();
                    return;
                }

                string scheduleJSON = RPSCommon.InterpretCloudScriptData(jsonResult, "schedule");

                var matchDataArray = scheduleJSON.Split('"').Where((item, index) => index % 2 != 0);
                foreach (string matchString in matchDataArray)
                {
                    Schedule.Add(new MatchBrief(matchString));
                }

                // send event that league is started, for anyone logged in
                var data = new object[] { Key };
                PhotonNetwork.RaiseEvent(
                    LeagueView.LEAGUE_UPDATE_EVENT, // .Code
                    data,                           // .CustomData
                    RaiseEventOptions.Default,
                    SendOptions.SendReliable
                    );

                callback();
            },
                                                RPSCommon.OnPlayFabError
                                                );
        }
예제 #3
0
        private void GetMatchFromServer()
        {
            PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
            {
                FunctionName      = "GetMatch",
                FunctionParameter = new
                {
                    leagueId   = TitleDescriptionButtonLinkData.LinkID,
                    matchIndex = TitleDescriptionButtonLinkData.DataIndex
                },
                GeneratePlayStreamEvent = true,
            },
                                                result =>
            {
                // update player stats
                PlayerManager.UpdatePlayerStats();

                // get Json object representing the host's schedule out of FunctionResult
                JsonObject jsonResult = (JsonObject)result.FunctionResult;

                // check if data exists
                if (jsonResult == null)
                {
                    Debug.Log("get match failed... missing data");
                    return;
                }

                // data successfully received
                // interpret data
                string matchJSON = RPSCommon.InterpretCloudScriptData(jsonResult, "matchTurn");
                string statsJSON = RPSCommon.InterpretCloudScriptData(jsonResult, "opponentStats");
                matchBrief       = new MatchBrief(RPSCommon.InterpretCloudScriptData(jsonResult, "matchBrief"));
                matchTurn        = MatchTurn.CreateFromJSON(matchJSON);
                opponentStats    = LeaguePlayerStats.CreateFromJSON(statsJSON);
                opponentRating   = Int32.Parse(RPSCommon.InterpretCloudScriptData(jsonResult, "opponentRating"));

                UpdateMatchUI();
            },
                                                RPSCommon.OnPlayFabError
                                                );
        }
예제 #4
0
        private void StartCountdown(ExecuteCloudScriptResult result)
        {
            // get Json object representing the Game State out of FunctionResult
            JsonObject jsonResult = (JsonObject)result.FunctionResult;

            // check if data exists
            if (jsonResult == null)
            {
                Debug.Log("Game data is missing, disconneting...");
                DisconnectFromGame();
                return;
            }

            // get game state from server
            localGameState = GameState.CreateFromJSON(RPSCommon.InterpretCloudScriptData(jsonResult, "gameState"));

            // start timer
            if (!turnTimerActive) // prevent double action
            {
                turnTimerActive = true;
                SetupCountdownUI();
                StartCoroutine(TurnTimer(localGameState.turnCompletionTime));
            }
        }
예제 #5
0
        private void OnGetGameState(ExecuteCloudScriptResult result)
        {
            // get Json object representing the Game State out of FunctionResult
            JsonObject jsonResult = (JsonObject)result.FunctionResult;

            // check if data exists
            if (jsonResult == null)
            {
                Debug.Log("Game data is missing, disconneting...");
                DisconnectFromGame();
                return;
            }

            // data successfully received
            // interpret data in appropriate classes:
            TurnData   turnData   = TurnData.CreateFromJSON(RPSCommon.InterpretCloudScriptData(jsonResult, "turnData"));
            GameState  gameState  = GameState.CreateFromJSON(RPSCommon.InterpretCloudScriptData(jsonResult, "gameState"));
            PlayerData playerData = PlayerData.CreateFromJSON(RPSCommon.InterpretCloudScriptData(jsonResult, "playerData"));

            gameSettings = GameSettings.CreateFromJSON(RPSCommon.InterpretCloudScriptData(jsonResult, "gameSettings"));


            // turn counting debug
            if (localGameState != null)
            {
                Debug.Log("server count: " + gameState.turnCount + "   local turn count: " + localGameState.turnCount);
            }


            // just started game
            if (localGameState == null)
            {
                // UI
                SetupStartGameUI();

                // in case player data is missing... such as a game client rejoining a running game
                if (PlayerManager.OpponentName == null)
                {
                    ProcessPlayerData(playerData);
                }

                // update player names
                UpdatePlayerUI();

                // update local turn data
                localTurnData = turnData;

                // update game state
                localGameState = gameState;
            }
            else if (gameState.turnCount > localGameState.turnCount) // turn finished
            {
                // set flag so client game loop can continue into next turn
                waitingForGameStateUpdate = false;

                // update game state
                localGameState = gameState;

                // assign weapon to correct player
                isHost = (playerData.hostId == PlayerPrefs.GetString("playFabId"));
                Weapon myWeapon, opponentWeapon;
                if (isHost)
                {
                    myWeapon       = ParseWeapon(gameState.p1Weapon);
                    opponentWeapon = ParseWeapon(gameState.p2Weapon);
                }
                else
                {
                    myWeapon       = ParseWeapon(gameState.p2Weapon);
                    opponentWeapon = ParseWeapon(gameState.p1Weapon);
                }

                if (myWeapon == Weapon.None || opponentWeapon == Weapon.None)
                {
                    Debug.Log("user or opponent failed to submit move, disconneting...");

                    // todo: better handling of this case
                    DisconnectFromGame();
                    return;
                }

                // ui call based on result
                if (localGameState.winner == PlayerPrefs.GetString("playFabId"))
                {   // win
                    wldStats.addWin();
                    SetUpGameOverUI(ShowWinUI, myWeapon, opponentWeapon);
                }
                else if (localGameState.winner == PlayerManager.OpponentId)
                {   // lose
                    wldStats.addLoss();
                    SetUpGameOverUI(ShowLoseUI, myWeapon, opponentWeapon);
                }
                else
                {   // draw
                    wldStats.addDraw();
                    SetUpGameOverUI(ShowDrawUI, myWeapon, opponentWeapon);
                }

                nextRoundPanel.SetActive(true);
            }
        }
예제 #6
0
        public void UpdateMatchList()
        {
            ClearButtonList(matchButtonList);
            CultureInfo culture = new CultureInfo("en-US");

            // generate match list
            if (LeagueManager.league.Schedule != null)
            {
                int matchIndex = 0;
                foreach (MatchBrief match in LeagueManager.league.Schedule)
                {
                    if (match == null)
                    { // skip bye round
                        matchIndex++;
                        continue;
                    }

                    GameObject obj = Instantiate(PlayerButtonPrefab, MatchListContent.transform);
                    matchButtonList.Add(obj);
                    var tdButton = obj.GetComponent <TitleDescriptionButton>();

                    string description = "";
                    if (match.Result == WLD.None)
                    {
                        string weaponText = "No Selection";
                        if (match.MyWeapon != Weapon.None)
                        {
                            weaponText = "Playing " + match.MyWeapon.ToString();
                        }
                        description =
                            weaponText + "\n" +
                            RPSCommon.UnixTimeToDateTime(match.DateTime).ToString("m", culture)
                            + ",  " +
                            RPSCommon.UnixTimeToDateTime(match.DateTime).ToString("t", culture);
                    }
                    else
                    {
                        string weaponText = "No Selection";
                        if (match.MyWeapon != Weapon.None)
                        {
                            weaponText = match.MyWeapon.ToString() +
                                         " VS " +
                                         match.OpponentWeapon.ToString();
                        }
                        if (match.Result == WLD.Draw)
                        {
                            description = weaponText + "\n" +
                                          match.Result.ToString();
                        }
                        else
                        {
                            description = weaponText + "\n" +
                                          match.Result.ToString();
                        }
                    }

                    var buttonData = new TitleDescriptionButtonData(
                        LeagueManager.league.Key,
                        match.Opponent,
                        description
                        );
                    tdButton.SetupButton(buttonData, "MatchOverview", "", matchIndex++);

                    if (match.Result != WLD.None)
                    {
                        int points = 0;
                        if (match.Result == WLD.Draw)
                        {
                            points = 1;
                        }
                        if (match.Result == WLD.Win)
                        {
                            points = 3;
                        }
                        tdButton.SetPointText(points);
                    }
                }
            }
        }
예제 #7
0
        private void GetLeagueDataFromServer()
        {
            var leagueId = TitleDescriptionButtonLinkData.LinkID;

            // keys that must exist for valid league
            List <string> validLeagueKeys = new List <string>()
            {
                "Status",
                "Name",
                "HostName",
                "Settings",
            };

            PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
            {
                FunctionName      = "GetLeague",
                FunctionParameter = new
                {
                    leagueKey = leagueId
                },
                GeneratePlayStreamEvent = true,
            },
                                                result =>
            {
                // update player stats
                PlayerManager.UpdatePlayerStats();

                // get Json object representing the Game State out of FunctionResult
                JsonObject jsonResult = (JsonObject)result.FunctionResult;

                // check if data exists
                if (jsonResult == null)
                {
                    ShowLeagueCancelledAlert();
                    Debug.Log("server failed to return data");
                }
                else
                {
                    Debug.Log("League data received from server");

                    // get list of players
                    string playerListJSON = RPSCommon.InterpretCloudScriptData(jsonResult, "Players");
                    var playerArray       = JsonHelper.getJsonArray <LeaguePlayerStats>(playerListJSON);
                    var playerList        = new List <LeaguePlayerStats>();
                    foreach (LeaguePlayerStats player in playerArray)
                    {
                        playerList.Add(player);
                    }
                    // order them WLD
                    playerList.Sort(
                        delegate(LeaguePlayerStats c1, LeaguePlayerStats c2)
                    {
                        return(c1.WLDScore.CompareTo(c2.WLDScore));
                    }
                        );
                    playerList.Reverse();

                    // create instance of league
                    LeagueManager.league = new League(
                        RPSCommon.InterpretCloudScriptData(jsonResult, "Status"),
                        RPSCommon.InterpretCloudScriptData(jsonResult, "Name"),
                        RPSCommon.InterpretCloudScriptData(jsonResult, "HostName"),
                        LeagueSettings.CreateFromJSON(RPSCommon.InterpretCloudScriptData(jsonResult, "Settings")),
                        leagueId,
                        playerList
                        );

                    // interpret schedule as object and save in league object
                    if (LeagueManager.league.Status != "Open")
                    {
                        string scheduleJSON = RPSCommon.InterpretCloudScriptData(jsonResult, "Schedule");
                        Debug.Log(scheduleJSON);
                        if (scheduleJSON != "null")
                        {
                            scheduleJSON       = scheduleJSON.Trim(']');
                            scheduleJSON       = scheduleJSON.Trim('[');
                            scheduleJSON       = scheduleJSON.Replace("\"", string.Empty);
                            var matchDataArray = scheduleJSON.Split(',');

                            foreach (string matchString in matchDataArray)
                            {
                                if (matchString == "null")
                                {
                                    LeagueManager.league.Schedule.Add(null);
                                }
                                else
                                {
                                    LeagueManager.league.Schedule.Add(new MatchBrief(matchString));
                                }
                            }
                        }
                    }
                }

                // determine type of UI we need to set up, OPEN league or CLOSED
                // Open means it is still recruiting, otherwise it has started or completed
                if (LeagueManager.league.Status == "Open")
                {
                    LeagueViewOpenUI();
                }
                else
                {
                    LeagueViewClosedUI();
                }
            },
                                                RPSCommon.OnPlayFabError
                                                );
        }
예제 #8
0
        private void UpdateMatchUI()
        {
            // initially set both panel states to false
            showWeaponPanel.SetActive(false);
            chooseWeaponPanel.SetActive(false);

            // set up title panel
            titlePanel.SetActive(true);
            titleTextSelf.text     = PlayerManager.PlayerName + " " + PlayerManager.PlayerStats.Rating;
            titleTextOpponent.text = matchTurn.OpponentName + " " + opponentRating.ToString();

            if (matchBrief.Result == WLD.None)
            { // set up choose weapon panel
                chooseWeaponPanel.SetActive(true);
                CultureInfo culture = new CultureInfo("en-US");
                dateText.text =
                    RPSCommon.UnixTimeToDateTime(matchTurn.DateTime).ToString("m", culture) +
                    "\n" +
                    RPSCommon.UnixTimeToDateTime(matchTurn.DateTime).ToString("t", culture);

                opponentStatsText.text =
                    matchTurn.OpponentName + " League Stats" + "\n" +
                    "Points\t  " + opponentStats.WLDScore + "\n" +
                    "Wins\t  " + opponentStats.Wins.ToString() + "\n" +
                    "Losses\t  " + opponentStats.Losses.ToString() + "\n" +
                    "Draws\t  " + opponentStats.Draws.ToString();

                SetWeaponToggleUI(RPSCommon.ParseWeapon(matchTurn.MyWeapon));
            }
            else
            { // set up show results panel
                showWeaponPanel.SetActive(true);
                foreach (GameObject weap in opponentWeaponChoice)
                {
                    weap.SetActive(false);
                }
                opponentWeaponChoice[(int)matchBrief.OpponentWeapon].SetActive(true);
                foreach (GameObject weap in myWeaponChoice)
                {
                    weap.SetActive(false);
                }
                myWeaponChoice[(int)matchBrief.MyWeapon].SetActive(true);

                winPanel.SetActive(false);
                losePanel.SetActive(false);
                drawPanel.SetActive(false);

                if (matchBrief.Result == WLD.Win)
                {
                    winPanel.SetActive(true);
                }
                else if (matchBrief.Result == WLD.Lose)
                {
                    losePanel.SetActive(true);
                }
                else
                {
                    drawPanel.SetActive(true);
                }
            }
        }
예제 #9
0
        public void OnCreateLeagueButtonPress()
        {
            // Manage UI
            SetAllButtonsInteractable(false);

            // Save input from league settings input fields
            LeagueManager.SetMatchCount(Int32.Parse(matchCountInputField.text));
            // * 3600 is hours to seconds conversion
            LeagueManager.SetRoundDuration(Int32.Parse(roundDurationInputField.text) * 3600);
            LeaguePlayerStats leaguePlayerData = new LeaguePlayerStats(
                PlayerManager.PlayerName,
                PlayerPrefs.GetString("playFabId")
                );

            // call to Playfab cloud script function called "CreateNewLeague"
            PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest()
            {
                FunctionName      = "CreateNewLeague",
                FunctionParameter = new
                {
                    status         = "Open",
                    leagueName     = leagueNameInput.text,
                    hostName       = PlayerManager.PlayerName,
                    playerData     = leaguePlayerData.ToJSON(),
                    leagueSettings = LeagueManager.leagueSettings.ToJSON()
                },
                GeneratePlayStreamEvent = true,
            },
                                                result =>
            {
                // get Json object representing the Game State out of FunctionResult
                JsonObject jsonResult = (JsonObject)result.FunctionResult;

                // check if data exists
                if (jsonResult == null)
                {
                    Debug.Log("server failed to return data");
                }
                else
                {
                    Debug.Log("New League created");

                    // save key for accessing the league from server
                    TitleDescriptionButtonLinkData.LinkID =
                        RPSCommon.InterpretCloudScriptData(jsonResult, "leagueKey");

                    // reselect UI element from league dashboard
                    EventSystem.current.SetSelectedGameObject(prevUISelection);

                    // Load league view for displaying new league
                    SceneManager.UnloadSceneAsync("NewLeague");
                    SceneManager.LoadScene("LeagueView", LoadSceneMode.Additive);
                }
            },
                                                errorCallback =>
            {
                Debug.Log(errorCallback.ErrorMessage + "error creating new League.");

                // TODO: more specific error messages to help with league creation.
                alertPanelText.text =
                    "Oops! According to the server, The league could not be created.";
                alertPanel.SetActive(true);

                // let player interact again
                SetAllButtonsInteractable(true);
            }
                                                );
        }