Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from ToeJamson/1.js
Last active November 15, 2017 21:06
Show Gist options
  • Save domadev812/fa35fa5cf911b5519d25e066f141b846 to your computer and use it in GitHub Desktop.
Save domadev812/fa35fa5cf911b5519d25e066f141b846 to your computer and use it in GitHub Desktop.
Random Matchmaking (MemeWarz 5)
pubnub.subscribe({
channels: [channel],
state: {
skill: 2
}
});
pubnub.addListener({
status: function(statusEvent) {
},
message: function(message) {
console.log("New Message!!", message);
},
presence: function(presenceEvent) {
}
})
pubnub.hereNow(
{
channels: [channel],
channelGroups : ["cg1"],
includeUUIDs: true,
includeState: true
},
function (status, response) {
// handle status, response
}
);
{
"uuids": [
{
"state": {
"skill": 2
},
"uuid": "fuchsia_whale"
},
{
"state": {
"skill": 3
},
"uuid": "lime_impala"
}
],
"occupancy": 2
}
var my_skill = Math.floor(Math.random() * 3) + 1;
$('#my_skill').text(my_skill);
<a href="#">
You are <strong id="whoami"></strong>
<span id="my_skill" class="badge"></span>
</a>
pubnub.subscribe({
channels: [channel],
state: {
skill: my_skill
}
});
pubnub.addListener({
status: function(statusEvent) {
},
message: function(message) {
console.log("New Message!!", message);
},
presence: function(presenceEvent) {
}
})
pubnub.hereNow(
{
channels: [channel],
channelGroups : ["cg1"],
includeUUIDs: true,
includeState: true
},
function (status, response) {
// handle status, response
}
);
$('#find-match').click(function(){
pubnub.hereNow(
{
channels: ["ch1"],
channelGroups : ["cg1"],
includeUUIDs: true,
includeState: true
},
function (status, data) {
// handle status, response
var users = data.uuids;
var matches = [];
for(var i = users.length - 1; i >= 0; i--) {
if(users[i].uuid !== me && users[i].state.skill == my_skill) {
matches.push(users[i])
}
}
if(!matches.length) {
alert('Nobody is online with the same skill level.');
} else {
var opponent = matches[Math.floor(Math.random() * matches.length)];
alert('Opponent Found: ' + opponent.uuid + ' with skill level ' + opponent.state.skill);
}
});
});
pubnub.subscribe({
channel: [channel],
withPresence: true,
state: {
skill: my_skill
}});
pubnub.addListener({
status: function(statusEvent) {
},
message: function(message) {
console.log("New Message!!", message);
},
presence: function(presenceEvent) {
if(data.action == "join") {
var $new_user = $('<li id="' + data.uuid + '" class="list-group-item"><span class="badge">' + data.data.skill + '</span>' + data.uuid + '</li>')
$('#online-users').append($new_user);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment