Code implementation for simple multiplayer on login. Requrires accounts-entry
or similar login forms and profile-online
Last active
March 16, 2016 13:01
-
-
Save aaronthorp/8501381 to your computer and use it in GitHub Desktop.
Meteor.js - Multiplayer Meteor Login Logic. @aaronthorp
This file contains 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
if (Meteor.isClient) { | |
Deps.autorun(function() { | |
Subs_Games = Meteor.subscribe("myGames", Meteor.userId()); | |
}); | |
Template.home.game = function() { | |
return GameCollection.findOne({current: true}); | |
}; | |
Template.home.events({ | |
"click #newGame": function() { | |
Meteor.call('newGame'); | |
}, | |
"click #finishGame": function() { | |
var game = GameCollection.findOne({current: true}); | |
Meteor.call('finishGame', game._id); | |
} | |
}); | |
} |
This file contains 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
GameCollection = new Meteor.Collection("games"); |
This file contains 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
<template name="home"> | |
<p>Welcome to my new meteor multiplayer app</p> | |
<p><strong> Current Game Status: </strong> | |
{{#if game.active}} | |
Game is in play! <a href="#" id="finishGame">Finish the Game</a>. | |
{{else}} | |
{{#if game.finished}} | |
Game Completed or other player left. <a href="#" id="newGame">Start a New Game</a>. | |
{{else}} | |
Waiting for new player to login...or <a href="#" id="newGame">Find a New Game</a> by an online user. | |
{{/if}} | |
{{/if}} | |
</p> | |
<p><strong> Current Game ID: </strong>{{game._id}}</p> | |
<p><strong> Current Game Player Count: </strong>{{game.players.length}}</p> | |
<p><strong> Current Game Active: </strong>{{game.active}}</p> | |
<p><a href="{{pathFor 'entrySignOut'}}">Logout</a></p> | |
</template> |
This file contains 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
if (Meteor.isServer) { | |
Meteor.methods({ | |
newGame: function() { | |
allocateGame(this.userId); | |
}, | |
finishGame: function(_id) { | |
GameCollection.update({_id: _id}, {$set: {active: false, finished: true}}); | |
} | |
}) | |
Meteor._onLogin = function (userId) { | |
allocateGame(userId); | |
console.log(userId + "just logged in.") | |
} | |
Meteor._onLogout = function (userId) { | |
leaveGames(userId); | |
console.log(userId + "just logged out.") | |
} | |
allocateGame = function(userId) { | |
var gameWaiting = GameCollection.findOne({players: {$size: 1}}); | |
if (!gameWaiting) { | |
console.log("creating a new game, none available"); | |
GameCollection.update({players: userId}, {$set: {current: false}}, {multi: true}); | |
GameCollection.insert({players: [userId], active: false, finished: false, current: true}); | |
} else { | |
if (_.contains(gameWaiting.players, userId)) { | |
console.log("Cannot play with yourself. But you can stay waiting in the game.") | |
} else { | |
console.log("connecting with an existing waiting player"); | |
GameCollection.update({_id: gameWaiting._id}, {$set: {active: true}, $push: {players: userId}}); | |
} | |
}; | |
leaveGames = function(userId) { | |
console.log("leaving all unfinished games"); | |
GameCollection.remove({$and: [{players: userId, players: {$size: 1}}]}); | |
var games = GameCollection.find({$and: [{players: userId, active: true}]}); | |
games.forEach(function(game) { | |
GameCollection.update({_id: game._id}, {$set: {active: false, finished: true}}); | |
}) | |
}; | |
Meteor.publish('myGames', function() { | |
if (!this.userId) { | |
console.log('MyGames Un-Publish'); | |
//return []; | |
this.ready(); | |
} else { | |
console.log('MyGames Publish: U-'+this.userId); | |
return GameCollection.find({players: this.userId}); | |
} | |
}); | |
} |
This file contains 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
allocateGame = function(userId) { | |
var gameWaiting = GameCollection.findOne({players: {$size: 1}}); | |
if (!gameWaiting) { | |
console.log("creating a new game, none available"); | |
GameCollection.update({players: userId}, {$set: {current: false}}, {multi: true}); | |
GameCollection.insert({players: [userId], active: false, finished: false, current: true}); | |
} else { | |
if (_.contains(gameWaiting.players, userId)) { | |
console.log("Cannot play with yourself. But you can stay waiting in the game.") | |
} else { | |
console.log("connecting with an existing waiting player"); | |
GameCollection.update({_id: gameWaiting._id}, {$set: {active: true}, $push: {players: userId}}); | |
} | |
}; |
This file contains 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
leaveGames = function(userId) { | |
console.log("leaving all unfinished games"); | |
GameCollection.remove({$and: [{players: userId, players: {$size: 1}}]}); | |
var games = GameCollection.find({$and: [{players: userId, active: true}]}); | |
games.forEach(function(game) { | |
GameCollection.update({_id: game._id}, {$set: {active: false, finished: true}}); | |
}) | |
}; |
This file contains 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
Meteor.publish('myGames', function() { | |
if (!this.userId) { | |
console.log('MyGames Un-Publish'); | |
//return []; | |
this.ready(); | |
} else { | |
console.log('MyGames Publish: U-'+this.userId); | |
return GameCollection.find({players: this.userId}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment