게임 플랫폼 응용프로그래밍

06/24 GPGS 4 리더보드

박준희 2021. 6. 24. 11:50

리더보드.

 

 

 

 

 

 

using GooglePlayGames;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using UnityEngine.UI;

public class GameMain : MonoBehaviour
{
    public Text txtUserName;
    public Button btnAchivement;
    public Button btnAttack;
    public GameObject doing;
    public GameObject done;
    public Button btnLeaderBoardUI;
    public Button btnScore;
    public Button btnPostScore;

    public Text txtStep;
    public Text txtCount;
    public Text txtScore;
    public Text txtHighScore;

    private int goalCount = 10;
    private int totalSteps = 2;
    private int currentCount = 0;
    private int currentStep = 1;
    private string userName;
    private int score;

    public void Init(string userName)
    {
        Debug.Log("userName: " + userName);
        this.userName = userName;
        this.InitAchievement();
        this.InitLeaderBoard();


    }

    private void InitLeaderBoard()
    {
        this.btnLeaderBoardUI.onClick.AddListener(() =>
        {
            PlayGamesPlatform.Instance.ShowLeaderboardUI();
        });

        this.btnScore.onClick.AddListener(() =>
        {
            this.score += 10;
            this.txtScore.text = this.score.ToString();
        });

        this.btnPostScore.onClick.AddListener(() =>
        {
            Social.ReportScore(this.score, GPGSIds.leaderboard, (success) =>
            {
                Debug.Log(success);
                this.score = 0;
                this.txtScore.text = this.score.ToString();
            });
        });

        Social.LoadScores(GPGSIds.leaderboard, scores => {
            if (scores.Length > 0)
            {
                Debug.Log("Got " + scores.Length + " scores");
                string myScores = "Leaderboard:\n";
                foreach (IScore score in scores)
                    myScores += "\t" + score.userID + " " + score.formattedValue + " " + score.date + "\n";
                Debug.Log(myScores);

                Debug.Log("----------------------------------------");
                //내꺼 찾기
                var fountMyScore = scores.Where<IScore>(x => x.userID == Social.localUser.id);
                foreach(var score in fountMyScore)
                {
                    Debug.LogFormat("score : {0}", score);
                }
            }
            else
                Debug.Log("No scores loaded");
        });
    }

    private void InitAchievement()
    {

        string stepKey = string.Format("{0}_step", GPGSIds.achievement__10);

        string currentCountKey = string.Format("{0}_currentCount", GPGSIds.achievement__10);

        PlayerPrefs.GetInt(stepKey);
        PlayerPrefs.GetInt(currentCountKey);

        if (PlayerPrefs.GetInt(stepKey) == 0)
        {
            PlayerPrefs.SetInt(stepKey, currentStep);
        }
        else
        {
            this.currentStep = PlayerPrefs.GetInt(stepKey);
        }

        if (PlayerPrefs.GetInt(currentCountKey) == 0)
        {

        }
        else
        {
            this.currentCount = PlayerPrefs.GetInt(currentCountKey);
        }


        this.txtCount.text = string.Format("kill count: {0}/{1}", this.currentCount, this.goalCount);
        this.txtStep.text = string.Format("step: {0}/{1}", this.currentStep, this.totalSteps);

        this.btnAttack.onClick.AddListener(() =>
        {
            this.currentCount++;

            //저장
            PlayerPrefs.SetInt(GPGSIds.achievement__10, currentCount);

            //UI업데이트
            this.txtCount.text = string.Format("kill count: {0}/{1}", this.currentCount, this.goalCount);

            if (this.currentCount >= this.goalCount)
            {
                if (this.currentStep >= this.totalSteps)
                {
                    PlayGamesPlatform.Instance.ReportProgress(GPGSIds.achievement__10, 100, (success) =>
                    {
                        if (success)
                        {
                            this.doing.SetActive(false);

                            this.done.SetActive(true);
                        }
                    });
                }
                else
                {
                    //로딩바를 보여준다

                    PlayGamesPlatform.Instance.ReportProgress(GPGSIds.achievement__10, 100, (success) =>
                    //로딩바를 안보여준다
                    {
                        if (success)
                        {
                            //스탭을 올려준다
                            this.currentStep++;

                            PlayerPrefs.SetInt(stepKey, this.currentCount);

                            this.txtStep.text = string.Format("step: {0}/{1}", this.currentStep, this.totalSteps);

                            //로딩바를 보여준다
                            PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement__10, currentStep, (success) =>
                            {
                                //로딩바를 안보여준다
                                Debug.LogFormat("{0} {1} {2}", GPGSIds.achievement__10, this.currentStep, success);
                            });

                            this.currentCount = 0;

                            PlayerPrefs.SetInt(currentCountKey, this.currentCount);

                            this.txtCount.text = string.Format("kill count: {0}/{1}", this.currentCount, this.goalCount);
                        }
                    });
                }
            }
        });

        //PlayGamesPlatform.Instance.LoadAchievements((ach) =>
        //{
        //    Debug.LogFormat("{0}, {1}", ach[1].id, ach[1].percentCompleted);
        //});

        this.txtUserName.text = userName;

        this.btnAchivement.onClick.AddListener(() =>
        {
            PlayGamesPlatform.Instance.ShowAchievementsUI();
        });
        PlayGamesPlatform.Instance.IncrementAchievement(
        GPGSIds.achievement, 1, (bool success) => {
            // handle success or failure
            Debug.LogFormat("IncrementAchivement {0}, {1}", GPGSIds.achievement, success);
        });
    }
}