-
-
Save stephenlb/ff24833411f8acce93349dd7a48c57f5 to your computer and use it in GitHub Desktop.
Random Matchmaking (MemeWarz 5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pubnub.subscribe({ | |
| channels: [channel], | |
| state: { | |
| skill: 2 | |
| } | |
| }); | |
| pubnub.addListener({ | |
| status: function(statusEvent) { | |
| }, | |
| message: function(message) { | |
| console.log("New Message!!", message); | |
| }, | |
| presence: function(presenceEvent) { | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pubnub.hereNow( | |
| { | |
| channels: [channel], | |
| channelGroups : ["cg1"], | |
| includeUUIDs: true, | |
| includeState: true | |
| }, | |
| function (status, response) { | |
| // handle status, response | |
| } | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "uuids": [ | |
| { | |
| "state": { | |
| "skill": 2 | |
| }, | |
| "uuid": "fuchsia_whale" | |
| }, | |
| { | |
| "state": { | |
| "skill": 3 | |
| }, | |
| "uuid": "lime_impala" | |
| } | |
| ], | |
| "occupancy": 2 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var my_skill = Math.floor(Math.random() * 3) + 1; | |
| $('#my_skill').text(my_skill); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <a href="#"> | |
| You are <strong id="whoami"></strong> | |
| <span id="my_skill" class="badge"></span> | |
| </a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pubnub.subscribe({ | |
| channels: [channel], | |
| state: { | |
| skill: my_skill | |
| } | |
| }); | |
| pubnub.addListener({ | |
| status: function(statusEvent) { | |
| }, | |
| message: function(message) { | |
| console.log("New Message!!", message); | |
| }, | |
| presence: function(presenceEvent) { | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pubnub.hereNow( | |
| { | |
| channels: [channel], | |
| channelGroups : ["cg1"], | |
| includeUUIDs: true, | |
| includeState: true | |
| }, | |
| function (status, response) { | |
| // handle status, response | |
| } | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $('#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); | |
| } | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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