android - How to pass score parameter in play-services-turn-based methods? -
i have started exploring google-play-services-turnbased apis. till have been successful in creating match. documentation haven't been able figure out how player's score after completes turn.
this onclickstartmatch method.
public void onstartmatchclicked() { intent intent = games.turnbasedmultiplayer.getselectopponentsintent(mhelper.getapiclient(), 1, 7, true); startactivityforresult(intent, rc_select_players); }
this onactivityresult method in main activity class.
if (request == rc_select_players) { if (response != result_ok) { // user canceled return; } // invitee list. final arraylist<string> invitees = data.getstringarraylistextra(games.extra_player_ids); // auto-match criteria. bundle automatchcriteria = null; int minautomatchplayers = data.getintextra( multiplayer.extra_min_automatch_players, 0); int maxautomatchplayers = data.getintextra( multiplayer.extra_max_automatch_players, 0); if (minautomatchplayers > 0) { automatchcriteria = roomconfig.createautomatchcriteria( minautomatchplayers, maxautomatchplayers, 0); } else { automatchcriteria = null; } turnbasedmatchconfig tbmc = turnbasedmatchconfig.builder() .addinvitedplayers(invitees) .setautomatchcriteria(automatchcriteria) .build(); // create , start match. games.turnbasedmultiplayer .creatematch(mhelper.getapiclient(), tbmc) .setresultcallback(new matchinitiatedcallback()); }
this matchinitiatedcallback class
public class matchinitiatedcallback implements resultcallback<turnbasedmultiplayer.initiatematchresult>,onturnbasedmatchupdatereceivedlistener { @override public void onresult(turnbasedmultiplayer.initiatematchresult result) { // check if status code not success. status status = result.getstatus(); if (status.issuccess()) { log.d("turnbased","turn based match initiated result: "+status.getstatusmessage()); return; } turnbasedmatch match = result.getmatch(); // if player not first player in match, continue. if (match.getdata() != null) { showturnui(match); return; } // otherwise, first player. initialize game state. initgame(match); // let player take first turn showturnui(match); } public void showturnui(turnbasedmatch match){ if(match.getstatus() == turnbasedmatch.match_status_active){ if(match.getturnstatus() == turnbasedmatch.match_turn_status_my_turn){ turnbasedmatchdata = match.getdata(); games.turnbasedmultiplayer.taketurn(mhelper.getapiclient(),match.getmatchid(), "score:400".getbytes(charset.forname("utf-16")),null).setresultcallback(updatematchresult()); } } } public void initgame(turnbasedmatch match){ games.turnbasedmultiplayer.taketurn(mhelper.getapiclient(),match.getmatchid(),"score:605".getbytes(charset.forname("utf-16")),match.getparticipantid(games.players.getcurrentplayerid(mhelper.getapiclient()))).setresultcallback(updatematchresult()); } public resultcallback<turnbasedmultiplayer.updatematchresult> updatematchresult(){ return null; } @override public void onturnbasedmatchreceived(turnbasedmatch turnbasedmatch) { log.d("turn-based","player played turn"); } @override public void onturnbasedmatchremoved(string s) { } } }
also helpful if can explain how continue game game start , when submit score , how.
figured out. how can it.
public byte[] persist() { jsonobject retval = new jsonobject(); try { retval.put("turncounter", 2); retval.put("score1",100); retval.put("score2",200); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } string st = retval.tostring(); log.d(tag, "==== persisting\n" + st); return st.getbytes(charset.forname("utf-8")); } games.turnbasedmultiplayer.taketurn(mhelper.getapiclient(),match.getmatchid(),persist(),null).setresultcallback(updatematchresult());
Comments
Post a Comment