32
loading...
This website collects cookies to deliver better user experience
npm install
npm start
http://localhost:3000
and – boom! – there’s good ole pac-man running in your browser.n
to start a new game. Press s
to toggle the sound on and off. Use the arrows keys to move pac-man around.ctrl+c
. Next, restart it with the client-side api key you copied from Split:SPLIT_AUTH_KEY=<your client-side key> npm start
n
key. The ghosts are moving around randomly as before. Now, it’s time for some split magic. Hit the p
key to pause the game. Next, change the default treatment from off to on. Click Save and then Confirm.p
again to continue the game. But – you better run! You’ll notice that the ghosts are now hyper focused on your location and their closing in fast.index.html
file – the entry point for the app.<div id="pacman"></div>
<script src="https://cdn.split.io/sdk/split-10.15.4.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
<script type="module" src="main.js"></script>
div
is where the pac-man game will be rendered. The first script
tag gets the Split JavaScript SDK. The second script
tag gets a library called modernizr, which helps to identify features of different browsers. The last script
tag loads the main JavaScript file. Note that is uses module
type so that the pac-man app can use a modern modular approach to JavaScript using ECMAScript 2015 (short name ES6 as it’s the sixth edition of ECMAScript).main.js
.import { SplitConfig } from './split_config.js';
authorizationKey
as well as a key
. The authorizationKey
is the client-side API key you copied earlier and was written to this file when you started the app above.key
identifier can be a little confusing at first. This is a free form value that can represent a unique identifier of your choice. It is often an email address or other user id. Split leaves it up to you on purpose so that it can be very flexible. In this demo, we’re not using any sort of authentication or user identifier, so it’s just hard-coded to ANONYMOUS
.SplitConfig
available now, we set up the Split client:var factory = splitio(SplitConfig);
var splitClient = factory.client();
splitClient.on(splitClient.Event.SDK_READY, function () {
handleTreatments();
console.log('Split is ready!');
});
splitClient.on(splitClient.Event.SDK_UPDATE, function () {
handleTreatments();
console.log('The SDK has been updated!');
});
SDK_READY
(when the sdk is ready to interact with Split) and SDK_UPDATE
(when any change is made to treatments defined in Split). In both cases, the function handleTreatments()
is called:function handleTreatments() {
var treatments = splitClient.getTreatments(['PacMan_RadarGhost', 'PacMan_SuperPac']);
el.dispatchEvent(new CustomEvent('splitChange', { detail: treatments }));
}
handleTreatments()
function obtains the values of the named treatments from Split. For this post, the one we care about is: PacMan_RadarGhost
. Notice that it takes the collection of treatments returned from Split and fires a custom event called splitChange
. This simple but powerful line enables us to decouple the rest of the application from Split. The Pac-Man game has a listener for this custom event and will react to the treatments
object passed into it.game.js
:wrapper.addEventListener('splitChange', function(e) {
console.log(e.detail);
console.log('detected split change');
if (e.detail['PacMan_RadarGhost']) {
let ghostMode = Ghost.CHILL;
let userForGhost = undefined;
if (e.detail['PacMan_RadarGhost'] === 'on') {
ghostMode = Ghost.RADAR;
userForGhost = user;
}
for (let i = 0; i < ghosts.length; i++) {
ghosts[i].setMode(ghostMode);
ghosts[i].setUser(userForGhost);
}
}
});
PacMan_RadarGhost
is in the details passed in to the event handler. If so, we check to see if the value of PacMan_RadarGhost
is set to on
. If so, the ghostMode
is set to Ghost.RADAR
. This is what changes the status to RADAR
in the game. Finally, we iterate over the list of ghosts and set the mode accordingly. The user object is also passed into each of the ghosts so that they can know pac-man’s position on the screen when in RADAR
mode.move
function in ghost.js
:function move(ctx) {
if (mode === Ghost.CHILL) {
return moveChill(ctx);
} else if (mode === Ghost.RADAR) {
return moveRadar(ctx);
}
};
Deploy to Heroku
button. At Split, we <3
Heroku for rapid deployment, testing and hosting. They have a generous free tier that you can sign up for here. If you click the purple button (and, you’re logged in to your Heroku account), you’re taken to a page that asks two simple questions in an input form: what would you like to call the app? And, what is your client-side API key for Split?app.json
file in the project that drives this easy deployment.32