Example #1
0
        void StartQuickGame()
        {
            // quick-start a game with 1 randomly selected opponent
            int    MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
            Bundle autoMatchCriteria = RoomConfig.CreateAutoMatchCriteria(MIN_OPPONENTS,
                                                                          MAX_OPPONENTS, 0);

            RoomConfig.Builder rtmConfigBuilder = RoomConfig.InvokeBuilder(this);
            rtmConfigBuilder.SetMessageReceivedListener(this);
            rtmConfigBuilder.SetRoomStatusUpdateListener(this);
            rtmConfigBuilder.SetAutoMatchCriteria(autoMatchCriteria);
            SwitchToScreen(Resource.Id.screen_wait);
            KeepScreenOn();
            ResetGameVars();
            GamesClass.RealTimeMultiplayer.Create(mGoogleApiClient, rtmConfigBuilder.Build());
        }
Example #2
0
        // Handle the result of the "Select players UI" we launched when the user clicked the
        // "Invite friends" button. We react by creating a room with those players.
        private void HandleSelectPlayersResult(Result response, Intent data)
        {
            if (response != Result.Ok)
            {
                Log.Warn(TAG, "*** select players UI cancelled, " + response);
                SwitchToMainScreen();
                return;
            }

            Log.Debug(TAG, "Select players UI succeeded.");

            // get the invitee list
            var invitees = data.GetStringArrayListExtra(GamesClass.ExtraPlayerIds);

            Log.Debug(TAG, "Invitee count: " + invitees.Count);

            // get the automatch criteria
            Bundle autoMatchCriteria   = null;
            int    minAutoMatchPlayers = data.GetIntExtra(Multiplayer.ExtraMinAutomatchPlayers, 0);
            int    maxAutoMatchPlayers = data.GetIntExtra(Multiplayer.ExtraMaxAutomatchPlayers, 0);

            if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0)
            {
                autoMatchCriteria = RoomConfig.CreateAutoMatchCriteria(
                    minAutoMatchPlayers, maxAutoMatchPlayers, 0);
                Log.Debug(TAG, "Automatch criteria: " + autoMatchCriteria);
            }

            // create the room
            Log.Debug(TAG, "Creating room...");
            RoomConfig.Builder rtmConfigBuilder = RoomConfig.InvokeBuilder(this);
            rtmConfigBuilder.AddPlayersToInvite(invitees);
            rtmConfigBuilder.SetMessageReceivedListener(this);
            rtmConfigBuilder.SetRoomStatusUpdateListener(this);
            if (autoMatchCriteria != null)
            {
                rtmConfigBuilder.SetAutoMatchCriteria(autoMatchCriteria);
            }
            SwitchToScreen(Resource.Id.screen_wait);
            KeepScreenOn();
            ResetGameVars();
            GamesClass.RealTimeMultiplayer.Create(mGoogleApiClient, rtmConfigBuilder.Build());
            Log.Debug(TAG, "Room created, waiting for it to be ready...");
        }
Example #3
0
        public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
        {
            var view = p0.Inflate(Resource.Layout.Lobby, p1, false);

            _builder = RoomConfig.InvokeBuilder((GameActivity)Activity);
            _builder.SetMessageReceivedListener((GameActivity)Activity);

            var autoMatch = view.FindViewById <Button>(Resource.Id.AutoMatch);

            autoMatch.Click += delegate
            {
                var am = RoomConfig.CreateAutoMatchCriteria(1, 1, 0);
                _builder.SetAutoMatchCriteria(am);
                _config = _builder.Build();
                ((GameActivity)Activity).GamesClient.CreateRoom(_config);
                Activity.Window.AddFlags(WindowManagerFlags.KeepScreenOn);
                autoMatch.Enabled = false;
            };

            var hostGame = view.FindViewById <Button>(Resource.Id.HostGame);

            hostGame.Click += delegate
            {
                var intent = ((GameActivity)Activity).GamesClient.GetSelectPlayersIntent(1, 1);
                StartActivityForResult(intent, FriendsIntent);
            };

            var joinGame = view.FindViewById <Button>(Resource.Id.JoinGame);

            joinGame.Click += delegate
            {
                var intent = ((GameActivity)Activity).GamesClient.InvitationInboxIntent;
                StartActivityForResult(intent, InboxIntent);
            };

            return(view);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            // Check if this was the error code we supplied. That is the only one we care about.
            if (requestCode == REQUEST_CODE_RESOLVE_ERR && resultCode == Result.Ok)
            {
                // TODO: Is google API the only one who sends REQUEST_CODE_RESOLVE_ERR?
                mGoogleApiClient.Connect();
                return;
            }
            else if (requestCode == RC_SELECT_PLAYERS)
            {
                if (resultCode != Result.Ok)
                {
                    Toast.MakeText(this, "Player Did Not Accept Invite", ToastLength.Long).Show();

                    // user canceled
                    return;
                }

                // get the invitee list
                IList <string> invitees = data.GetStringArrayListExtra(Android.Gms.Games.GamesClass.ExtraPlayerIds);

                // get auto-match criteria
                Bundle autoMatchCriteria   = null;
                int    minAutoMatchPlayers = data.GetIntExtra(Multiplayer.ExtraMinAutomatchPlayers, 0);
                int    maxAutoMatchPlayers = data.GetIntExtra(Multiplayer.ExtraMaxAutomatchPlayers, 0);

                if (minAutoMatchPlayers > 0)
                {
                    autoMatchCriteria = RoomConfig.CreateAutoMatchCriteria(minAutoMatchPlayers, maxAutoMatchPlayers, 0);
                }
                else
                {
                    autoMatchCriteria = null;
                }

                TurnBasedMatchConfig tbmc = TurnBasedMatchConfig.InvokeBuilder().AddInvitedPlayers(invitees).SetAutoMatchCriteria(autoMatchCriteria).Build();

                // kick the match off
                GamesClass.TurnBasedMultiplayer.CreateMatch(mGoogleApiClient, tbmc).SetResultCallback(this);
            }
            else if (requestCode == RC_OPEN_INBOX)
            {
                if (resultCode != Result.Ok)
                {
                    Toast.MakeText(this, "Inbox not ok", ToastLength.Long).Show();

                    // user canceled
                    return;
                }
                try
                {
                    ITurnBasedMatch match = Java.Lang.Object.GetObject <ITurnBasedMatch>(data.GetParcelableExtra(Multiplayer.ExtraTurnBasedMatch).Handle, JniHandleOwnership.DoNotTransfer);
                    mMatch.UpdateMatchInfo(match);
                }
                catch
                {
                    Toast.MakeText(this, "Failed to cast Match", ToastLength.Long).Show();
                }
            }
        }