K-NOTE

ポイ活とかスマホアプリ開発とか気の向くままに

当ブログの内容等の正確性は保証しません。ご利用は自己責任で!

【Android】Google Play Game Servicesを試してみたい #5 - リーダーボードの実装

はじめに

リーダーボードの閲覧と、スコア送信を実装します。
プロジェクトは前回のものを使用します。

ゲームサービスでリーダーボードを設定する

Developer Consoleにアクセスして、「ゲームサービス」に入ります。
リーダーボードを選択して「リーダーボードを追加」を押します。


名前を入力します。
今回は他の設定値はデフォルトのまま「保存」を押します。


リーダーボードのIDが発行されます。
クライアントアプリは、このIDを使ってスコアの登録を行います。


リーダーボードの設定は以上です。

リーダーボードIDの定義

リソースファイル「ids.xml」にリーダーボードIDを定義します。

ids.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_id">YOUR_APP_ID</string>
    <string name="lb_id">YOUR_LB_ID</string>
</resources>

YOUR_LB_IDに先ほど取得したリーダーボードIDを指定します。

レイアウトファイル

前回のレイアウトファイルに、リーダーボード見るボタンとスコア送信ボタンを追加します。

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- サインイン -->
    <com.google.android.gms.common.SignInButton
        android:id="@+id/button_sign_in"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- サインアウト -->
    <Button
        android:id="@+id/button_sign_out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign Out" />

    <!-- リーダーボード見る -->
    <Button
        android:id="@+id/button_show_lb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="リーダーボード見る" />

    <!-- スコア送信 -->
    <Button
        android:id="@+id/button_send_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="スコア送信" />

</LinearLayout>

Activityの実装

MainActivity.java
package jp.kforce.samplegame;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gms.common.SignInButton;
import com.google.example.games.basegameutils.BaseGameActivity;

public class MainActivity extends BaseGameActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // サインインボタンのクリックリスナー
        final SignInButton buttonSignIn = (SignInButton) findViewById(R.id.button_sign_in);
        buttonSignIn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.v("tag", "buttonSignIn click");
                beginUserInitiatedSignIn();
            }
        });

        // サインアウトボタンのクリックリスナー
        final Button buttonSignOut = (Button) findViewById(R.id.button_sign_out);
        buttonSignOut.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.v("tag", "buttonSignOut click");
                signOut();
            }
        });

        // リーダーボード見るボタンのクリックリスナー
		final Button buttonShowLB = (Button) findViewById(R.id.button_show_lb);
		buttonShowLB.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Log.v("tag", "buttonShowLB click");
            	if (isSignedIn()) {
                    startActivityForResult(getGamesClient().getAllLeaderboardsIntent(), 5001);
                } else {
                    showAlert("Please sign in to view leaderboards.");
                }
            }
        });

        // スコア送信ボタンのクリックリスナー
		final Button buttonSendScore = (Button) findViewById(R.id.button_send_score);
		buttonSendScore.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Log.v("tag", "buttonSendScore click");
            	if (isSignedIn()) {
            		int score = (int)(Math.random() * 100);
            		getGamesClient().submitScore(getString(R.string.lb_id), score);
                    showAlert("send : "+score);
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onSignInSucceeded() {
        Log.v("tag", "onSignInSucceeded");
        // ここにサインイン成功時の処理を書く
    }

    @Override
    public void onSignInFailed() {
        Log.v("tag", "onSignInFailed");
        // ここにサインイン失敗時の処理を書く
    }
}

今回も非常に簡単です。
前回からの追加点は「リーダーボード見るボタンのクリックリスナー」と「スコア送信ボタンのクリックリスナー」のみです。
リーダーボードを見るには

startActivityForResult(getGamesClient().getAllLeaderboardsIntent(), 5001);

とするだけです。

また、スコア送信は

getGamesClient().submitScore(getString(R.string.lb_id), score);

とするだけです。

おわりに

「Google Play Game Servicesを試してみたい」の連載はこれで終了です。
あとは気になったことがあれば単発で載せると思います。
ありがとうございました。