Skip to content

Commit

Permalink
Player creation now working with DeckList
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Voswinkel committed Feb 28, 2015
1 parent f228d0a commit 9a1e8f1
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 42 deletions.
14 changes: 0 additions & 14 deletions lib/card_json.js
@@ -1,18 +1,4 @@
var mongoose = require('mongoose')
, db_uri = 'mongodb://localhost/http_runner_'+process.env.NODE_ENV;

mongoose.connect(db_uri, function(err, res){
if(err){ console.log("Connection with "+db_uri+" failed: " + err); }
else{ console.log("Connection to "+db_uri+" established."); }
});

process.on('SIGINT', function(){
mongoose.connection.close(function(){
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});


var cardSchema = mongoose.Schema({ card_id: String, card: {} })
, CardJSON = mongoose.model('Card', cardSchema);
Expand Down
4 changes: 2 additions & 2 deletions lib/deck_list.js
Expand Up @@ -68,7 +68,7 @@ function DeckList(deck_list, finalized){

async.parallel(card_retrieval, function(err){
if(err){ return finalized(err); }
return finalized(checkDeckSize.call(context));
return finalized.call(context, checkDeckSize.call(context));
});
}

Expand Down Expand Up @@ -110,7 +110,7 @@ function DeckList(deck_list, finalized){

this.cards = [];
this.influence = 0;
this.deck_list = deck_list;
this.deck_list = deck_list || {};
if(finalized == undefined) finalized = function(){};
discernIdentity.call(this);
}
Expand Down
9 changes: 8 additions & 1 deletion lib/identities/corp.js
Expand Up @@ -16,7 +16,14 @@ function Corp(json){
setFaction.call(this, json.faction_code || json.faction);
setCorpAttributes.call(this, json);
}
Corp.prototype.parent = Object.create(Identity.prototype);

Corp.prototype = Object.create(Identity.prototype);
Corp.prototype.parent = Identity.prototype;

Corp.prototype.prepare = function(player){
player.bad_reputation = 0;
this.parent.prepare(player);
}

Corp.identityIDs = function(){
var id_list = ["02031"];
Expand Down
3 changes: 3 additions & 0 deletions lib/identities/identity.js
Expand Up @@ -16,4 +16,7 @@ function Identity(json){
setMaximumInfluence.call(this, json.influencelimit || json.influence_max);
}

Identity.prototype.prepare = function(player){
}

module.exports = Identity;
1 change: 1 addition & 0 deletions lib/identities/runner.js
Expand Up @@ -19,6 +19,7 @@ Runner = function(json){
setRunnerAttributes.call(this, json);
}
Runner.prototype = Object.create(Identity.prototype);
Runner.prototype.parent = Identity.prototype;

Runner.identityIDs = function(){
var id_list = ["05030"];
Expand Down
24 changes: 16 additions & 8 deletions lib/player.js
@@ -1,14 +1,22 @@
function Player(options){
var DeckList = require('./deck_list');

function Player(options, finalize){
var player = this;
options = options || {};
this.deck = options.deck;
if(this.deck == undefined) throw Error('Player has no Deck');
finalize = finalize || function(){};

new DeckList(options.deck, function(err){
if(err != undefined){ return finalize(err) }

this.identity = options.deck.identity;
if(this.identity == undefined) throw Error('Player has no Identity');
player.identity = this.identity;
player.deck = this.cards;
player.scored_agendas = [];
player.credits = 5;
player.hand = [];

this.scored_agendas = [];
this.credits = 0;
this.hand = [];
player.identity.prepare(player);
finalize();
})
}

module.exports = Player;
9 changes: 9 additions & 0 deletions test/fixtures/base.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions test/lib/deck_list_test.js
Expand Up @@ -2,16 +2,15 @@ require('../support');

var netrunner = require('../../')
, DeckList = netrunner.DeckList
, fixtures = require('../fixtures/')
, Base = new fixtures.Base()
, expect = require('expect.js');

describe('DeckList', function(){
describe('#new', function(){
var legit_deck_list;
beforeEach(function(){
legit_deck_list = {"00001":3,"00002":3,"00003":3,"00004":3,"00005":3
,"00011":3,"00012":3,"00013":3,"00014":3,"00015":3
,"00021":3,"00022":3,"00023":3,"00024":3,"00025":3
,"02031":1};
legit_deck_list = Base.corp_legit_deck_list();
});


Expand Down
2 changes: 1 addition & 1 deletion test/lib/factories/card_factory_test.js
Expand Up @@ -2,7 +2,7 @@ require('../../support');

var netrunner = require('../../../')
, CardFactory = netrunner.CardFactory
, expect = require('expect.js')
, expect = require('expect.js');

var example_corp_identity_json = {"code":"02031","title":"Jinteki: Replicating Perfection","type":"Identity","type_code":"identity","subtype":"Megacorp","subtype_code":"megacorp","text":"The Runner cannot run on remote servers. Ignore this ability until the end of the turn whenever the Runner runs on a central server.","faction":"Jinteki","faction_code":"jinteki","faction_letter":"j","influencelimit":15,"minimumdecksize":45,"number":31,"quantity":3,"setname":"Trace Amount","set_code":"ta","side":"Corp","side_code":"corp","uniqueness":false,"limited":false,"cyclenumber":2,"url":"http:\/\/netrunnerdb.com\/en\/card\/02031"}

Expand Down
18 changes: 18 additions & 0 deletions test/lib/factories/game_factory_test.js
@@ -0,0 +1,18 @@
require('../../support');
var netrunner = require('../../../')
, Player = netrunner.Player
, fixtures = require('../../fixtures/')
, Base = new fixtures.Base()
, expect = require('expect.js');

describe('GameFactory', function(){
describe('::create', function(){
xit('requires two players', function(done){
p1 = new Player({deck: Base.corp_legit_deck_list()});
new GameFactory(p1, function(err){
expect(err).to.be.an(Error);
done();
});
});
});
});
2 changes: 1 addition & 1 deletion test/lib/factories/identity_factory_test.js
Expand Up @@ -12,7 +12,7 @@ var fixtures = new card_fixtures.Base()
, test_runner_id;

describe('IdentityFactory', function(){
describe('#create', function(){
describe('::create', function(){
beforeEach(function(done){
CardFactory.create({json: example_corp_identity_json}, function(card){
test_corp_id = card;
Expand Down
24 changes: 14 additions & 10 deletions test/lib/player_test.js
@@ -1,24 +1,28 @@
var netrunner = require('../../')
, Player = netrunner.Player
, fixtures = require('../fixtures/')
, Base = new fixtures.Base()
, expect = require('expect.js');


describe('Player', function(){
describe('#new', function(){
var test_function = function(opts){
new Player(opts);
var test_function = function(opts, finalize){
new Player(opts, finalize);
}
it('throws an error if no deck is present', function(){
expect(test_function).to.throwException(/player has no deck/i);
})

it('throws an error if no identity is present', function(){
expect(test_function).withArgs({deck: {}}).to.throwException(/player has no identity/i);
it('throws an error if DeckList encounters an error', function(done){
test_function(undefined, function(err){
expect(err.message).to.match(/deck list has no identity/i);
done();
});
})

it('returns a Player if identity is given', function(){
player = new Player({deck: {identity: 'Derp'}});
expect(player).to.be.a(Player);
it('returns a Player if DeckList builds successfully', function(done){
var player = new Player({deck: Base.corp_legit_deck_list()}, function(){
expect(player).to.be.a(Player);
done();
});
})
})
})
22 changes: 21 additions & 1 deletion test/support.js
@@ -1,6 +1,26 @@
require('./fixtures/');

var mongoose = require('mongoose');
var mongoose = require('mongoose')
, db_uri = 'mongodb://localhost/http_runner_'+process.env.NODE_ENV;

before(function(done){
mongoose.connect(db_uri, function(err, res){
if(err){
console.log("Connection with "+db_uri+" failed: " + err);
}else{
console.log("Connection to "+db_uri+" established.");
done();
}
});

process.on('SIGINT', function(){
mongoose.connection.close(function(){
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});

});

after(function(done){
mongoose.connection.db.dropDatabase(function(){
Expand Down

0 comments on commit 9a1e8f1

Please sign in to comment.