Generate unique room code to share with friends
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Generate unique room code to share with friends
Iggy
2021-02-25 21:13:25
Hello, I was curious is there a way for PUN to generate a unique secret code out of 4-5 numbers that you can share with your friends?
If anyone has played phasmophobia you'd know what I mean. When you create a room you don't have to go through the bureaucracy of giving your room a name and a password. You get an automatically generated secret code that you can share with your friends, which they use to join you.
I could randomize the number from 0-99999 but there's no guarantee that it will be unique. Do I have to write server code or is there another, better way?
Thanks :)
Comments
Hi @Iggy,
Thank you for choosing Photon!
You could do this in multiple options:
of course preferably a server generates the codes (via custom plugins) but it's not an option for everyone (self-hosted or Enterprise Cloud)
here is an example using webhooks + web based backend
otherwise to minimize clash and make this based on client only:
make the random number generation takes input timestamp and maybe room creator's UserID (add region, AppVersion, etc.)
try CreateRoom if it fails with GameAlreadyExists retry.
Thanks for the help @JohnTube , I've managed to write a little server in GO and host it on Heroku.
I followed Oddreams instructions and it worked really nicely. But I also found that you can optimize the number of requests if you make the host player get the code before creating the room, and assign it to the name of the room. That way everyone who joins doesn't need to send a request for conversion of code => room name.
Of course, that creates an issue where you can allocate a ton of codes on the custom API without actually creating a photon room (Photon error, Postman spamming). My solution here is to timeout codes that didn't get a matching PathCreate webhook call.
Current implementation:
- User connects to the Photon Server.
- User presses a button to create a new room.
- We send a request for a unique code from the custom API.
- Custom API generates a code, stores it in the map, starts a timeout for it, and sends it to the user.
- We make a call to create a Photon room with the name set to the received code.
- Custom API catches the PathCreate webhook, and cancels the timeout of this code.
- Another user joins the game by specifying the code as the room name and is successful in joining.
- Both players leave and the room is closed.
- Custom API catches the PathClose webhook, and removes the code from the map.
Kaiserludi
2021-03-02 18:14:12
@Iggy, @JohnTube.
If the 4-5 numbers are not a hard requirement, then the easiest actually is, to not let the room creator specify a room name. The server will then generate a GUID for the room name.
This GUID is 32 characters long, so considerably longer than the 4-5 characters that were mentioned in the question, but it is guaranteed to be unique and you neither have to write custom server side code, nor to host the server yourself or use the Enterprise Cloud, nor do you have to generate a random number on the client side.
The room creator can specify at room creation time that the room should be invisible. That way the room name is secret and only known by the creator and by anyone to whom the creator sends that room name
@Kaiserludi Oh interesting, that could definitely work well if you utilize the clipboard. I guess you have to pick your battles.
For anyone that needs help, I uploaded my code to GitHub.
Back to top