Skip to content

Instantly share code, notes, and snippets.

@ShukantPal
Last active February 19, 2021 10:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShukantPal/2c1f5daedfaee784bfeb622d4e26736e to your computer and use it in GitHub Desktop.
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;
}
@palfonso
Copy link

palfonso commented Oct 4, 2019

Why is this "truncated" ?
Is there any complete version for this code ?

@ShukantPal
Copy link
Author

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).

@palfonso
Copy link

palfonso commented Oct 4, 2019

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.

@ShukantPal
Copy link
Author

ShukantPal commented Oct 4, 2019 via email

@palfonso
Copy link

palfonso commented Oct 4, 2019

Thanks!

@palfonso
Copy link

palfonso commented Oct 4, 2019

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 ?

@ShukantPal
Copy link
Author

ShukantPal commented Oct 5, 2019 via email

@braidsapp
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment