@@ -58,43 +58,6 @@ void Engine::InitGooglePlayGameServices() {
5858 // Check status code and update UI to signed-in state
5959 OnAuthActionFinished (op, status);
6060 })
61- .SetOnMultiplayerInvitationEvent ([this ](
62- gpg::MultiplayerEvent event, std::string match_id,
63- gpg::MultiplayerInvitation invitation) {
64- // Invoked when invitation has been received
65- // It can be received from the Play Game app, a notification, or
66- // live while the app is running.
67- // (e.g. Though PlayGam app, while the app is running)
68- LOGI (" MultiplayerInvitationEvent callback" );
69-
70- if (event ==
71- gpg::MultiplayerEvent::UPDATED_FROM_APP_LAUNCH ) {
72-
73- // In this case, an invitation has been accepted already
74- // in notification or in Play game app
75- gpg::RealTimeMultiplayerManager::RealTimeRoomResponse result =
76- service_->RealTimeMultiplayer ().AcceptInvitationBlocking (
77- invitation, this );
78- if (gpg::IsSuccess (result.status )) {
79- room_ = result.room ;
80- service_->RealTimeMultiplayer ().ShowWaitingRoomUI (
81- room_, MIN_PLAYERS ,
82- [this ](gpg::RealTimeMultiplayerManager::
83- WaitingRoomUIResponse const &waitResult) {
84- EnableUI (true );
85- if (gpg::IsSuccess (waitResult.status )) {
86- PlayGame ();
87- }
88- });
89- } else {
90- LeaveGame ();
91- }
92- } else {
93- // Otherwise, show default inbox and let players to accept an
94- // invitation
95- ShowRoomInbox ();
96- }
97- })
9861 .Create (platform_configuration);
9962}
10063
@@ -150,193 +113,6 @@ void Engine::OnAuthActionFinished(gpg::AuthOperation op,
150113 });
151114}
152115
153- /*
154- * Show room inbox
155- */
156- void Engine::ShowRoomInbox () {
157- service_->RealTimeMultiplayer ().ShowRoomInboxUI ([this ](
158- gpg::RealTimeMultiplayerManager::RoomInboxUIResponse const &response) {
159- if (gpg::IsSuccess (response.status )) {
160- gpg::RealTimeMultiplayerManager::RealTimeRoomResponse result =
161- service_->RealTimeMultiplayer ().AcceptInvitationBlocking (
162- response.invitation , this );
163- if (gpg::IsSuccess (result.status )) {
164- room_ = result.room ;
165- service_->RealTimeMultiplayer ().ShowWaitingRoomUI (
166- room_, MIN_PLAYERS ,
167- [this ](
168- gpg::RealTimeMultiplayerManager::WaitingRoomUIResponse const &
169- waitResult) {
170- EnableUI (true );
171- if (gpg::IsSuccess (waitResult.status )) {
172- PlayGame ();
173- }
174- });
175- } else
176- EnableUI (true ); // Go back to original state for retry
177- } else {
178- LOGI (" Invalid response status" );
179- EnableUI (true ); // Go back to original state for retry
180- }
181- });
182- EnableUI (false );
183- }
184-
185- /*
186- * Quick match
187- * - Create a match with minimal setting and play the game
188- */
189- void Engine::QuickMatch () {
190- gpg::RealTimeRoomConfig config =
191- gpg::RealTimeRoomConfig::Builder ()
192- .SetMinimumAutomatchingPlayers (MIN_PLAYERS )
193- .SetMaximumAutomatchingPlayers (MAX_PLAYERS )
194- .Create ();
195-
196- service_->RealTimeMultiplayer ().CreateRealTimeRoom (
197- config, this ,
198- [this ](gpg::RealTimeMultiplayerManager::RealTimeRoomResponse const &
199- response) {
200- LOGI (" created a room %d" , response.status );
201- if (gpg::IsSuccess (response.status )) {
202- room_ = response.room ;
203- service_->RealTimeMultiplayer ().ShowWaitingRoomUI (
204- room_, MIN_PLAYERS ,
205- [this ](
206- gpg::RealTimeMultiplayerManager::WaitingRoomUIResponse const &
207- waitResult) {
208- EnableUI (true );
209- if (gpg::IsSuccess (waitResult.status )) {
210- PlayGame ();
211- }
212- });
213- } else
214- EnableUI (true ); // Go back to original state for retry
215- });
216- EnableUI (false );
217- }
218-
219- /*
220- * Invite friends
221- * - Show Player Select UI via ShowPlayerSelectUI,
222- * - When the UI is finished, create match and show game UI
223- */
224- void Engine::InviteFriend () {
225- service_->RealTimeMultiplayer ().ShowPlayerSelectUI (
226- MIN_PLAYERS , MAX_PLAYERS , true ,
227- [this ](gpg::RealTimeMultiplayerManager::PlayerSelectUIResponse const &
228- response) {
229- LOGI (" inviting friends %d" , response.status );
230- if (gpg::IsSuccess (response.status )) {
231- // Create room
232- gpg::RealTimeRoomConfig config =
233- gpg::RealTimeRoomConfig::Builder ()
234- .PopulateFromPlayerSelectUIResponse (response)
235- .Create ();
236-
237- auto roomResponse =
238- service_->RealTimeMultiplayer ().CreateRealTimeRoomBlocking (config,
239- this );
240- if (gpg::IsSuccess (roomResponse.status )) {
241- room_ = roomResponse.room ;
242- service_->RealTimeMultiplayer ().ShowWaitingRoomUI (
243- room_, MIN_PLAYERS ,
244- [this ](gpg::RealTimeMultiplayerManager::
245- WaitingRoomUIResponse const &waitResult) {
246- EnableUI (true );
247- if (gpg::IsSuccess (waitResult.status )) {
248- PlayGame ();
249- }
250- });
251- } else
252- EnableUI (true ); // Go back to original state for retry
253- } else
254- EnableUI (true ); // Go back to original state for retry
255- });
256- EnableUI (false );
257- }
258-
259- /*
260- * Broadcast my score to peers
261- */
262- void Engine::BroadcastScore (bool bFinal) {
263- std::vector<uint8_t > v;
264- if (!bFinal) {
265- v.push_back (' U' );
266- v.push_back (static_cast <uint8_t >(score_counter_));
267- service_->RealTimeMultiplayer ().SendUnreliableMessageToOthers (room_, v);
268- } else {
269- v.push_back (' F' );
270- v.push_back (static_cast <uint8_t >(score_counter_));
271-
272- const std::vector<gpg::MultiplayerParticipant> participants =
273- room_.Participants ();
274- for (gpg::MultiplayerParticipant participant : participants) {
275- service_->RealTimeMultiplayer ().SendReliableMessage (
276- room_, participant, v, [](gpg::MultiplayerStatus const &) {});
277- }
278- }
279- }
280-
281- /*
282- * Got message from peers
283- * room : The room which from_participant is in.
284- * from_participant : The participant who sent the data.
285- * data : The data which was received.
286- * is_reliable : Whether the data was sent using the unreliable or
287- * reliable mechanism.
288- * In this app, packet format is defined as:
289- * 1 byte: indicate score type 'F': final score 'U' updating score
290- * 1 byte: score
291- */
292- void Engine::OnDataReceived (gpg::RealTimeRoom const &room,
293- gpg::MultiplayerParticipant const &from_participant,
294- std::vector<uint8_t > data, bool is_reliable) {
295- if (data[0 ] == ' F' && is_reliable) {
296- // Got final score
297- players_score_[from_participant.Id ()].score = data[1 ];
298- players_score_[from_participant.Id ()].finished = true ;
299- LOGI (" Got final data from name:%s ID:%s" ,
300- from_participant.DisplayName ().c_str (), from_participant.Id ().c_str ());
301- } else if (data[0 ] == ' U' && !is_reliable) {
302- // Got current score
303- uint8_t score = players_score_[from_participant.Id ()].score ;
304- players_score_[from_participant.Id ()].score = std::max (score, data[1 ]);
305- LOGI (" Got data from name:%s ID:%s" ,
306- from_participant.DisplayName ().c_str (), from_participant.Id ().c_str ());
307- }
308- UpdateScore ();
309- }
310-
311- /*
312- * Room status change callback
313- */
314- void Engine::OnRoomStatusChanged (gpg::RealTimeRoom const &room) {
315- room_ = room;
316- }
317-
318- /*
319- * Invoked when participant status changed
320- */
321- void Engine::OnParticipantStatusChanged (
322- gpg::RealTimeRoom const &room,
323- gpg::MultiplayerParticipant const &participant) {
324-
325- // Update participant status
326- LOGI (" Participant %s status changed: %d" , participant.Id ().c_str (),
327- participant.Status ());
328-
329- if (participant.Status () != gpg::ParticipantStatus::JOINED ) {
330- {
331- std::lock_guard<std::mutex> lock (mutex_);
332- if (players_score_.find (participant.Id ()) != players_score_.end ()) {
333- players_score_[participant.Id ()].finished = true ;
334- }
335- }
336- UpdateScore ();
337- }
338- }
339-
340116/*
341117 * Play games UI that is in your turn
342118 */
@@ -380,9 +156,6 @@ void Engine::PlayGame() {
380156 score_counter_++;
381157 UpdateScore ();
382158 UpdateTime ();
383-
384- // Broadcast my score to others via unreliable protocol
385- BroadcastScore (false );
386159 }
387160 }
388161 });
@@ -432,9 +205,6 @@ void Engine::PlayGame() {
432205 std::chrono::milliseconds d (100 );
433206 std::this_thread::sleep_for (d);
434207 }
435- // Broadcast my score to others via reliable protocol
436- BroadcastScore (true );
437-
438208 UpdateScore ();
439209 helper.DetachCurrentThread ();
440210 }).detach ();
@@ -458,32 +228,6 @@ void Engine::UpdateScore() {
458228 playing_ ? " " : " *" );
459229 std::string allstr (str);
460230
461- // Append other player
462- std::vector<gpg::MultiplayerParticipant> participants = room_.Participants ();
463- for (gpg::MultiplayerParticipant participant : participants) {
464- LOGI (" Participant name:%s ID:%s" , participant.DisplayName ().c_str (),
465- participant.Id ().c_str ());
466- if (participant.HasPlayer ())
467- LOGI (" self:%s PlayerID:%s" , self_id_.c_str (),
468- participant.Player ().Id ().c_str ());
469-
470- if (participant.HasPlayer () &&
471- participant.Player ().Id ().compare (self_id_) == 0 )
472- continue ; // Skip local player
473-
474- int32_t score = 0 ;
475- bool finished = false ;
476- if (players_score_.find (participant.Id ()) != players_score_.end ()) {
477- score = players_score_[participant.Id ()].score ;
478- finished = players_score_[participant.Id ()].finished ;
479- }
480-
481- LOGI (" Status %d" , participant.Status ());
482- snprintf (str, SIZE , " %s: %03d %s\n " , participant.DisplayName ().c_str (),
483- score, finished ? " *" : " " );
484- allstr += str;
485- }
486-
487231 // Update game UI, UI update needs to be performed in UI thread
488232 ndk_helper::JNIHelper::GetInstance ()
489233 ->RunOnUiThread ([this , str_myscore, allstr]() {
@@ -543,14 +287,11 @@ void Engine::InitializeGame() {
543287 * Leave game
544288 */
545289void Engine::LeaveGame () {
546- // Lock mutex
547- std::lock_guard<std::mutex> lock (mutex_);
548-
549- service_->RealTimeMultiplayer ().LeaveRoom (
550- room_, [](gpg::ResponseStatus const &status) {});
290+ // Lock mutex
291+ std::lock_guard<std::mutex> lock (mutex_);
551292
552- LOGI (" Game is over" );
553- playing_ = false ;
293+ LOGI (" Game is over" );
294+ playing_ = false ;
554295}
555296
556297/*
@@ -599,36 +340,6 @@ void Engine::InitUI() {
599340
600341 jui_helper::JUIWindow::GetInstance ()->AddView (button_sign_in_);
601342
602- button_quick_match_ = new jui_helper::JUIButton (" Quick match" );
603- SetParameters (button_quick_match_, button_sign_in_);
604- button_quick_match_->SetCallback ([this ](jui_helper::JUIView *view,
605- const int32_t message) {
606- if (message == jui_helper::JUICALLBACK_BUTTON_UP ) {
607- QuickMatch ();
608- }
609- });
610- jui_helper::JUIWindow::GetInstance ()->AddView (button_quick_match_);
611-
612- button_invite_ = new jui_helper::JUIButton (" Invite Friends!" );
613- SetParameters (button_invite_, button_quick_match_);
614- button_invite_->SetCallback ([this ](jui_helper::JUIView *view,
615- const int32_t message) {
616- if (message == jui_helper::JUICALLBACK_BUTTON_UP ) {
617- InviteFriend ();
618- }
619- });
620- jui_helper::JUIWindow::GetInstance ()->AddView (button_invite_);
621-
622- button_matches_ = new jui_helper::JUIButton (" All my invitations" );
623- SetParameters (button_matches_, button_invite_);
624- button_matches_->SetCallback ([this ](jui_helper::JUIView *view,
625- const int32_t message) {
626- if (message == jui_helper::JUICALLBACK_BUTTON_UP ) {
627- ShowRoomInbox ();
628- }
629- });
630- jui_helper::JUIWindow::GetInstance ()->AddView (button_matches_);
631-
632343 status_text_ = new jui_helper::JUITextView (
633344 service_->IsAuthorized () ? " Signed In." : " Signed Out." );
634345 status_text_->AddRule (jui_helper::LAYOUT_PARAMETER_ALIGN_PARENT_BOTTOM ,
@@ -670,12 +381,6 @@ void Engine::SetParameters(jui_helper::JUIButton *button,
670381void Engine::EnableUI (bool enable) {
671382 ndk_helper::JNIHelper::GetInstance ()->RunOnUiThread ([this , enable]() {
672383 button_sign_in_->SetAttribute (" Enabled" , enable);
673-
674- bool b = enable;
675- if (service_->IsAuthorized () == false ) b = false ;
676- button_quick_match_->SetAttribute (" Enabled" , b);
677- button_invite_->SetAttribute (" Enabled" , b);
678- button_matches_->SetAttribute (" Enabled" , b);
679384 });
680385}
681386
0 commit comments