Skip to content

Instantly share code, notes, and snippets.

@aaronthorp
Last active March 16, 2016 13:01
Show Gist options
  • Save aaronthorp/8501381 to your computer and use it in GitHub Desktop.
Save aaronthorp/8501381 to your computer and use it in GitHub Desktop.
Meteor.js - Multiplayer Meteor Login Logic. @aaronthorp

Meteor.js - Multiplayer Game Logic

Code implementation for simple multiplayer on login. Requrires accounts-entry or similar login forms and profile-online

Support us via Gittip

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);
}
});
}
GameCollection = new Meteor.Collection("games");
<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>
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});
}
});
}
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});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment