-
-
Save ShukantPal/2c1f5daedfaee784bfeb622d4e26736e to your computer and use it in GitHub Desktop.
public class FirebasePlayerMatchMaker { | |
public static interface OnMatchMadeCallback { | |
public void run(FirebasePlayerMatchMaker c); | |
} | |
public static final String RANDOM_ROOM_ID = "/Globl"; | |
public static final String ROOM_ID = "/GameRooms"; | |
public static final String GAMES_RECORD = "/OpenGameMoves"; | |
private DatabaseReference mUserRoomRef; | |
private DatabaseReference mOwnChallengeRef; | |
public String mOpener; | |
public String mGamePath; | |
public int mLocalPlayerIndex; | |
private boolean mClosed = false; | |
private OnMatchMadeCallback mOnComplete; | |
public boolean isClosed() { | |
return mClosed; | |
} | |
private FirebasePlayerMatchMaker(DatabaseReference userRoomRef, OnMatchMadeCallback onComplete) { | |
mUserRoomRef = userRoomRef; | |
mOnComplete = onComplete; | |
} | |
protected Matcher mMatcher; | |
protected SelfChallengeManager mSelfChallengeManager; | |
protected SelfChallengeCanceller mSelfChallengeCanceller; | |
public void findMatch() { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
OnFailCallback onMatchNotFoundFallback = new OnFailCallback() { | |
@Override | |
public void onFail() { | |
mMatcher = null; | |
mSelfChallengeManager = new SelfChallengeManager(); | |
mUserRoomRef.runTransaction(mSelfChallengeManager); | |
} | |
}; | |
mMatcher = new Matcher(onMatchNotFoundFallback); | |
mUserRoomRef.runTransaction(mMatcher); | |
} | |
}).start(); | |
} | |
public void stop() { | |
if (mSelfChallengeManager == null || | |
mSelfChallengeCanceller != null) { | |
return; | |
} | |
mSelfChallengeCanceller = new SelfChallengeCanceller(mSelfChallengeManager); | |
mUserRoomRef.runTransaction(mSelfChallengeCanceller); | |
} | |
public static FirebasePlayerMatchMaker newInstance(String userRoom, OnMatchMadeCallback onComplete) { | |
return new FirebasePlayerMatchMaker( | |
FirebaseDatabase.getInstance().getReference(ROOM_ID + "/" + userRoom), onComplete); | |
} | |
private boolean isChallengeCompat(Challenge oppoChallenge) { | |
return true; | |
} | |
protected boolean mIsThisOpener; | |
protected void onMatchFound(boolean isOpener) { | |
mIsThisOpener = isOpener; | |
mLocalPlayerIndex = (isOpener) ? 1 : 0; | |
mOnComplete.run(this); | |
} | |
public boolean isThisOpener() { | |
return mIsThisOpener; | |
} | |
public interface OnFailCallback { | |
public void onFail(); | |
} | |
protected class Matcher implements Transaction.Handler; | |
protected class SelfChallengeManager implements Transaction.Handler, ValueEventListener; | |
protected class SelfChallengeCanceller implements Transaction.Handler; | |
} |
The three inner classes in the match maker are not implemented in this Gist. I believe I have the Gist where the implementation is shown. I'll add it when I find it (or you could look at my profile and search, if possible).
Hi Sukant,
Thank for your response. This link is the one mentioned in your notes ( https://hashnode.com/post/match-making-with-firebase-cjrzgoi6k0010ads2xqojc7a1#ck1c4f9m300772is1kw7zxdg3 and https://www.freecodecamp.org/news/match-making-with-firebase-hashnode-de9161e2b6a7/ ).
I'm looking in "https://gist.github.com/SukantPal" now, but it is still showing the "truncated" file.
Regards,
-Pablo.
Thanks!
One more question. As per your document, last statement: "Now read the article on game communication to finish up."
Can you please provide the link that the document ?
Hi Sukant,
Thanks for posting, I have been scourging the internet for help with Firebase matchmaking. I am very new to the world of android development. I am trying to implement this code in an OnClickListener for my user to find a chat buddy. How can I call it in the Listener?
At the moment I am trying the following but I'm struggling to figure out what the second argument should be:
FirebasePlayerMatchMaker.newInstance(dbRef, ).findMatch();
Thanks in advance
Why is this "truncated" ?
Is there any complete version for this code ?