Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Gestion du modèle

  • Loading branch information...
commit 33423ed5d318eede24d3f2322c20134d63f7a447 1 parent 1c75b59
@nicompte authored
View
7 app.js
@@ -0,0 +1,7 @@
+var app = express.createServer();
+
+app.get('/', function(req, res){
+ res.send('Hello World');
+});
+
+app.listen('process.env.C9_PORT');
View
15 models/Belote.js
@@ -1,5 +1,14 @@
-function Belote(){
-
+var CardGame = require('./CardGame').CardGame;
+var Deck = require('./Deck').Deck;
+
+function Belote(players){
+ this.deck = new Deck(32).shuffle();
+ this.players = players;
+ //CardGame.apply(this.deck, this.players);
+ this.distribute([3,2], 1);
}
-Belote.prototype = new CardGame();
+Belote.prototype = new CardGame;
+Belote.prototype.constructor = Belote;
+
+exports.Belote = Belote;
View
67 models/Belote2.js
@@ -0,0 +1,67 @@
+var CardGame = require('./CardGame2').CardGame;
+var Deck = require('./Deck2').Deck;
+
+var Belote = CardGame.extend({
+ init: function(players){
+ var deck = new Deck(32);
+ this._super(deck, players);
+ this.teams = new Array();
+ this.trick = new Array();
+ this.trump = null;
+ this.trickTaker = null;
+ this.cardValue = [5, 6, 7, 9, 10, 11, 8, 12];
+ this.trickValue = [5, 6, 10, 11, 8, 12, 7, 9];
+ },
+ setTeams: function(t1,t2){
+ this.teams[0]=t1;
+ this.teams[1]=t2;
+ },
+ distribute: function(schema, numberOfTimes, startsWith){
+ this._super(schema, numberOfTimes, startsWith);
+ },
+ startGame: function(){
+ this.distribute([3,2], 1, this.round%4);
+ },
+ play: function(playerId, card){
+ var player = this.getPlayerById(playerId);
+ this.trump.push([player, card]);
+ if(this.trump.length==4){
+ return this.getTrumpWinner();
+ }else{
+ return WHO_HAS_TO_PLAY;
+ }
+ },
+ getTrumpWinner: function(){
+ var winningCard = this.trump[0];
+ for(var i=1; i<this.trump.length; i++){
+ if(!this.winsAgainst(this.trump[i-1], this.trump[i])){
+ winningCard = this.trump[i];
+ }
+ }
+ },
+ isTrick: function(card){
+ return Card.families.getPosition(card.family) == this.trick;
+ },
+ getPlayerById: function(playerId){
+ this.players.forEach(function(player){
+ if(player.id == playerId){
+ return player;
+ }
+ });
+ },
+ winsAgainst: function(card1, card2){
+ if(!this.isTrick(card1) && !this.isTrick(card2)){
+ if(card1.family == card2.family){
+ return this.cardValue.indexOf(Card.families.indexOf(card1.value))<this.cardValue.indexOf(Card.families.indexOf(card2.value));
+ }else if(this.isTrick(card1) && !this.isTrick(card2)){
+ return false
+ }else if(!this.isTrick(card1) && this.isTrick(card2)){
+ return true;
+ }else if(this.isTrick(card1) && this.isTrick(card2)){
+ return this.trickValue.indexOf(Card.families.indexOf(card1.value))<this.trickValue.indexOf(Card.families.indexOf(card2.value));
+ }
+ }
+ }
+});
+
+exports.Belote = Belote;
View
14 models/Card.js
@@ -2,4 +2,16 @@ function Card(id, family, value){
this.id = id;
this.family = family;
this.value = value;
-}
+}
+
+Card.families = {
+ en: ["Hearts", "Diamonds", "Spades", "Clubs"],
+ fr: ["Cœur", "Carreau", "Pique", "Trèfle"]
+};
+
+Card.values = {
+ en: ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"],
+ fr: ["2", "3", "4", "5", "6", "7", "8", "9", "D", "V", "D", "R", "A"]
+};
+
+exports.Card = Card;
View
20 models/CardGame.js
@@ -1,8 +1,18 @@
-function CardGame(deck){
+var Deck = require('./Deck.js').Deck;
+
+function CardGame(deck, players){
this.deck = deck;
-
+ this.players = players;
+ //console.log(this.deck);
}
-CardGame.prototype.distribute = function(numberOfPlayers, schema, numberOfCards){
-
-};
+CardGame.prototype.distribute = function(schema, numberOfTimes){
+ var deck = this.deck;
+ for(var i=0; i<numberOfTimes; i++){
+ this.players.forEach(function(player){
+ player.hand.push(deck.takeCards(schema[i]));
+ });
+ }
+};
+
+exports.CardGame = CardGame;
View
22 models/CardGame2.js
@@ -0,0 +1,22 @@
+var Deck = require('./Deck2.js').Deck;
+
+var CardGame = Class.extend({
+ init: function(deck, players){
+ this.deck = deck;
+ this.players = players;
+ this.round = 0;
+ },
+ distribute: function(schema, numberOfTimes){
+ var deck = this.deck;
+ for(var i=0; i<numberOfTimes; i++){
+ for(var j = startsWith; j<this.players.length; j++){
+ this.players[j].hand.push(deck.takeCards(schema[i]));
+ if(j==players.length){
+ j=0;
+ }
+ }
+ }
+ }
+});
+
+exports.CardGame = CardGame;
View
71 models/Deck.js
@@ -1,32 +1,47 @@
-exports.Deck = function(){
+var Card = require("./Card.js").Card;
- function Deck(numberOfCards) {
- var families = ["Hearts", "Diamonds", "Spades", "Clubs"];
- var values = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"];
- switch (numberOfCards) {
- case 32:
- for(var i =0; i<32; i++){
- this.deck.push(new Card(i, Math.floor(i/8) , (32%8)+5));
- }
- break;
+
+function Deck(numberOfCards) {
+ this.deck = new Array();
+ switch (numberOfCards) {
+ case 32:
+ for(var i =0; i<32; i++){
+ this.deck.push(new Card(i, Card.families['fr'][Math.floor(i/8)] , Card.values['fr'][(i%8)+5]));
}
+ break;
}
+}
- /**
- * Shuffle a deck.
- */
- Deck.prototype.shuffle = function(){
- var j = 0;
- var valI = '';
- var valJ = valI;
- var l = this.deck.length - 1;
- while(l > -1) {
- j = Math.floor(Math.random() * l);
- valI = a[l];
- valJ = a[j];
- this.deck[l] = valJ;
- this.deck[j] = valI;
- l = l - 1;
- }
- };
-};
+/**
+ * Shuffle the deck.
+ */
+Deck.prototype.shuffle = function(){
+ var j = 0;
+ var valI = '';
+ var valJ = valI;
+ var l = this.deck.length - 1;
+ while(l > -1) {
+ j = Math.floor(Math.random() * l);
+ valI = this.deck[l];
+ valJ = this.deck[j];
+ this.deck[l] = valJ;
+ this.deck[j] = valI;
+ l = l - 1;
+ }
+};
+
+/**
+ * Take a number of cards from the deck.
+ */
+Deck.prototype.takeCards = function(numberOfCards){
+ return this.deck.splice(0, numberOfCards);
+};
+
+/**
+ * Add cards to deck.
+ */
+Deck.prototype.addCards = function(cards){
+ return this.deck.push(cards);
+};
+
+exports.Deck = Deck;
View
38 models/Deck2.js
@@ -0,0 +1,38 @@
+var Card = require("./Card.js").Card;
+
+
+var Deck = Class.extend({
+ init: function(numberOfCards) {
+ this.deck = new Array();
+ switch (numberOfCards) {
+ case 32:
+ for(var i =0; i<32; i++){
+ this.deck.push(new Card(i, Card.families['fr'][Math.floor(i/8)] , Card.values['fr'][(i%8)+5]));
+ }
+ break;
+ }
+ },
+ shuffle: function(){
+ var j = 0;
+ var valI = '';
+ var valJ = valI;
+ var l = this.deck.length - 1;
+ while(l > -1) {
+ j = Math.floor(Math.random() * l);
+ valI = this.deck[l];
+ valJ = this.deck[j];
+ this.deck[l] = valJ;
+ this.deck[j] = valI;
+ l = l - 1;
+ }
+ },
+ takeCards: function(numberOfCards){
+ return this.deck.splice(0, numberOfCards);
+ },
+ addCards: function(cards){
+ return this.deck.push(cards);
+ }
+
+});
+
+exports.Deck = Deck;
View
7 models/Player.js
@@ -0,0 +1,7 @@
+function Player(id, name){
+ this.id = id;
+ this.name = name;
+ this.hand = new Array();
+};
+
+exports.Player = Player;
View
63 models/inheritance.js
@@ -0,0 +1,63 @@
+/* Simple JavaScript Inheritance
+ * By John Resig http://ejohn.org/
+ * MIT Licensed.
+ */
+// Inspired by base2 and Prototype
+(function(){
+ var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
+ // The base Class implementation (does nothing)
+ this.Class = function(){};
+
+ // Create a new Class that inherits from this class
+ Class.extend = function(prop) {
+ var _super = this.prototype;
+
+ // Instantiate a base class (but only create the instance,
+ // don't run the init constructor)
+ initializing = true;
+ var prototype = new this();
+ initializing = false;
+
+ // Copy the properties over onto the new prototype
+ for (var name in prop) {
+ // Check if we're overwriting an existing function
+ prototype[name] = typeof prop[name] == "function" &&
+ typeof _super[name] == "function" && fnTest.test(prop[name]) ?
+ (function(name, fn){
+ return function() {
+ var tmp = this._super;
+
+ // Add a new ._super() method that is the same method
+ // but on the super-class
+ this._super = _super[name];
+
+ // The method only need to be bound temporarily, so we
+ // remove it when we're done executing
+ var ret = fn.apply(this, arguments);
+ this._super = tmp;
+
+ return ret;
+ };
+ })(name, prop[name]) :
+ prop[name];
+ }
+
+ // The dummy class constructor
+ function Class() {
+ // All construction is actually done in the init method
+ if ( !initializing && this.init )
+ this.init.apply(this, arguments);
+ }
+
+ // Populate our constructed prototype object
+ Class.prototype = prototype;
+
+ // Enforce the constructor to be what we expect
+ Class.prototype.constructor = Class;
+
+ // And make this class extendable
+ Class.extend = arguments.callee;
+
+ return Class;
+ };
+})();
View
14 models/prototype/Belote.js
@@ -0,0 +1,14 @@
+var CardGame = require('./CardGame').CardGame;
+var Deck = require('./Deck').Deck;
+
+function Belote(players){
+ this.deck = new Deck(32).shuffle();
+ this.players = players;
+ //CardGame.apply(this.deck, this.players);
+ this.distribute([3,2], 1);
+}
+
+Belote.prototype = new CardGame;
+Belote.prototype.constructor = Belote;
+
+exports.Belote = Belote;
View
18 models/prototype/CardGame.js
@@ -0,0 +1,18 @@
+var Deck = require('./Deck.js').Deck;
+
+function CardGame(deck, players){
+ this.deck = deck;
+ this.players = players;
+ //console.log(this.deck);
+}
+
+CardGame.prototype.distribute = function(schema, numberOfTimes){
+ var deck = this.deck;
+ for(var i=0; i<numberOfTimes; i++){
+ this.players.forEach(function(player){
+ player.hand.push(deck.takeCards(schema[i]));
+ });
+ }
+};
+
+exports.CardGame = CardGame;
View
47 models/prototype/Deck.js
@@ -0,0 +1,47 @@
+var Card = require("./Card.js").Card;
+
+
+function Deck(numberOfCards) {
+ this.deck = new Array();
+ switch (numberOfCards) {
+ case 32:
+ for(var i =0; i<32; i++){
+ this.deck.push(new Card(i, Card.families['fr'][Math.floor(i/8)] , Card.values['fr'][(i%8)+5]));
+ }
+ break;
+ }
+}
+
+/**
+ * Shuffle the deck.
+ */
+Deck.prototype.shuffle = function(){
+ var j = 0;
+ var valI = '';
+ var valJ = valI;
+ var l = this.deck.length - 1;
+ while(l > -1) {
+ j = Math.floor(Math.random() * l);
+ valI = this.deck[l];
+ valJ = this.deck[j];
+ this.deck[l] = valJ;
+ this.deck[j] = valI;
+ l = l - 1;
+ }
+};
+
+/**
+ * Take a number of cards from the deck.
+ */
+Deck.prototype.takeCards = function(numberOfCards){
+ return this.deck.splice(0, numberOfCards);
+};
+
+/**
+ * Add cards to deck.
+ */
+Deck.prototype.addCards = function(cards){
+ return this.deck.push(cards);
+};
+
+exports.Deck = Deck;
View
100 web.js
@@ -1,16 +1,31 @@
-var port = process.env.C9_PORT,
+var port = process.env.C9_PORT || 1337,
express = require("express"),
stylus = require("stylus"),
io = require("socket.io"),
connections = {};
-parseCookie = require('connect').utils.parseCookie;
+parseCookie = require('connect').utils.parseCookie,
+MemoryStore = express.session.MemoryStore,
+sessionStore = new MemoryStore();
+require('./models/inheritance.js');
-//var deckModel = require("./models/Deck.js").Deck();
-//var deck = new deckModel(32);
+
+var Player = require("./models/Player.js").Player;
+var Belote = require("./models/Belote2.js").Belote;
+var players = new Array();
+for(var i = 0; i<3; i++){
+ players.push(new Player(i, "player"+i));
+}
+this.belote = new Belote(players);
+console.log(this.belote);
+/*
+var Deck = require("./models/Deck2.js").Deck;
+var deck = new Deck(32);
+*/
+//console.log(deck);
var app = express.createServer(), io = io.listen(app);
-app.configure(function() {
+app.configure(function(){
this.set ('view engine', 'jade');
this.use( stylus.middleware({
src: __dirname + "/views",
@@ -19,11 +34,11 @@ app.configure(function() {
}));
this.use(express.bodyParser());
this.use(express.cookieParser());
- this.sessionStore = new express.session.MemoryStore({ reapInterval: 60000 * 10 });
+ //this.sessionStore = new express.session.MemoryStore({ reapInterval: 60000 * 10 });
this.use(express.session({
secret: "Cosson forever",
key: "express.sid",
- sessionStore: this.sessionStore
+ store: sessionStore
}));
this.use(express.static(__dirname + '/public'));
});
@@ -71,65 +86,35 @@ io.of('/belote').on('connection', function (socket) {
});
});
*/
-/*
-io.configure(function (){
- io.set('authorization', function (handshakeData, callback) {
- var cookies = parseCookie(handshakeData.headers.cookie);
- console.log(cookies);
- var sessionID = cookies['connect.sid'];
- if (!sessionID) {
- callback('No session', false);
- } else {
- handshakeData.sessionID = sessionID;
- app.sessionStore.get(sessionID, function (err, session) {
- if (!err && session && session.username) {
- handshakeData.username = session.username;
- callback(null, true);
- } else {
- callback(err || 'User not authenticated', false);
- }
- });
- }
- });
-});
-*/
-io.set('authorization', function (data, accept) {
+var Session = require('connect').middleware.session.Session;
+io.of('/belote').authorization(function(data, accept) {
if (data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
- console.log(data.cookie);
- data.sessionID = data.cookie['express.sid'];
- if(!data.sessionID) return accept('Not logged in.', false);
- app.sessionStore.get(data.sessionID, function (err, session) {
- if (!err && session && session.username) {
- data.username = session.username;
- callback(null, true);
- } else {
- return accept('Not logged in.', false);
- }
- });
+ if (!data.cookie['express.sid']) {
+ return accept('Not logged in 1.', false);
+ } else {
+ data.sessionID = data.cookie['express.sid'];
+ data.sessionStore = sessionStore;
+ sessionStore.get(data.sessionID, function(err, session) {
+ if (!err && session && session.username) {
+ data.session = new Session(data, session);
+ return accept(null, true);
+ } else {
+ return accept('Not logged in 2.', false);
+ }
+ });
+ }
} else {
- return accept('No cookie transmitted.', false);
+ return accept('No cookie transmitted.', false);
}
- accept(null, true);
+ //accept(null, true);
});
io.sockets.on('connection', function (socket) {
- console.log('A socket with sessionID ' + socket.handshake.sessionID
- + ' connected!');
+ console.log('A socket with sessionID ' + socket.handshake.sessionID + ' and name ' + socket.handshake.session.username+' connected!');
});
/*
-io.on('connection', function (socket) {
- var sessionID = socket.handshake.sessionID;
- var username = socket.handshake.username;
- if ('undefined' == typeof connections[sessionID]) {
- connections[sessionID] = { "length": 0 };
- io.emit('join', username, Date.now());
- }
- connections[sessionID][socket.id] = socket;
- connections[sessionID].length ++;
-});
-
io.on('disconnect', function () {
var userConnections = connections[sessionID];
if (userConnections.length && userConnections[socket.id]) {
@@ -142,5 +127,4 @@ io.on('disconnect', function () {
});
*/
-app.listen(port);
-//http://naholyr.fr/2011/07/authentification-et-websocket-avec-node-js-express-et-socket-io/
+app.listen(port);
Please sign in to comment.
Something went wrong with that request. Please try again.