So, the other day we released an RNG service to the wild. It is a Marsenne Twister implementation with background cycling and discarded draws. It’s fast, secure and possible to drop right into you existing Firebase projects. Here’s how:
First, you need to add the service JAR into your projects dependencies in order to use it. Here’s how it looks in Maven:
<dependency> <groupId>com.cubeia.firebase.service</groupId> <artifactId>mt-random</artifactId> <version>1.0</version> <type>jar</type> <scope>provided</scope> </dependency>
Note that we add it as “provided” as it will be included in the service when we’re deployed, so we don’t need to include it into our other dependencies. Generally it’s a good pattern to try not to duplicate classes that are included in services as you open up a can of class loading problems if you do.
Having done this, what you need in order to get hold of an actual Random object to use, is the Firebase service registry. This is available in, for example, the Game context which you get on the “init” method. If we have the game context we can do a method like this:
private Random getRandom() { Class<RandomService> key = RandomService.class; ServiceRegistry registry = gameContext.getServices(); // needs context RandomService randomService = registry.getServiceInstance(key); // get service return randomService.getSystemDefaultRandom(); // get random }
Now you’re ready to roll! You could assign the random to an instance field, but make sure you don’t do it in the game state as the Random is not serialize at the moment, and it will result in an ugly exception if you try it.
Here’s one more trick for you: if you want to use “mvn firebase:run” in you project (have a look here for more details on that), you need to add another dependency. The Firebase Maven plugin needs to know about the actual service archive in order to get your dependencies right, so also add the following to your dependencies:
<dependency> <groupId>com.cubeia.firebase.service</groupId> <artifactId>mt-random</artifactId> <version>1.0</version> <type>firebase-sar</type> <scope>provided</scope> </dependency>
By default, all Firebase artifact such as the one above, which are in the scope “provided” will be included in the deploy folder when you run Firebase via Maven. If on the other hand, you’re running Firebase from a standard installation, just copy the “mt-random.sar” archive into “game/deploy” and you’re done!