1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-12-23 21:03:14 +00:00
This commit is contained in:
exane 2015-06-13 21:36:02 +02:00
parent a2ca580844
commit e7559df6f7
16 changed files with 1016 additions and 269 deletions

110
assets/data/abilities.js Normal file
View File

@ -0,0 +1,110 @@
module.exports = {
"agile": {
},
"medic": {
},
"morale_boost": {
onAfterPlace: function(card) {
var field = this.getYourside().getField(card.getType());
var cards = field.getCards();
cards.forEach(function(_card) {
_card.boost(1);
})
}
},
"muster": {
onAfterPlace: function(card){
var name = card.getName();
var self = this;
var cards = this.getDeck().find("name", name);
cards.forEach(function(_card) {
self.getDeck().removeFromDeck(_card.getId());
this._placeCard(_card);
})
}
},
"tight_bond": {
onAfterPlace: function(card){
var field = this.getYourside().getField(card.getType());
var cards = field.getCards();
var lastInsert = cards.length;
if(lastInsert < 2) return;
if(cards[lastInsert - 2].getName() == cards[lastInsert - 1].getName()){
cards[lastInsert - 2].boost(+cards[lastInsert - 2].getPower());
cards[lastInsert - 1].boost(+cards[lastInsert - 1].getPower());
}
}
},
"spy": {
changeSide: true,
onAfterPlace: function(card){
this.drawCards(2);
}
},
"weather_fog": {
onEachTurn: function(card) {
var targetRow = card.constructor.TYPE.RANGED;
var forcedPower = 1;
var field1 = this.getYourside().getField(targetRow).getCards();
var field2 = this.getOtherside().getField(targetRow).getCards();
var field = field1.concat(field2);
field.forEach(function(_card) {
if(_card.getRawAbility() == "hero") return;
_card.setForcedPower(forcedPower);
});
}
},
"weather_rain": {
onEachTurn: function(card) {
var targetRow = card.constructor.TYPE.SIEGE;
var forcedPower = 1;
var field1 = this.getYourside().getField(targetRow).getCards();
var field2 = this.getOtherside().getField(targetRow).getCards();
var field = field1.concat(field2);
field.forEach(function(_card) {
if(_card.getRawAbility() == "hero") return;
_card.setForcedPower(forcedPower);
});
}
},
"weather_frost": {
onEachTurn: function(card) {
var targetRow = card.constructor.TYPE.CLOSE_COMBAT;
var forcedPower = 1;
var field1 = this.getYourside().getField(targetRow).getCards();
var field2 = this.getOtherside().getField(targetRow).getCards();
var field = field1.concat(field2);
field.forEach(function(_card) {
if(_card.getRawAbility() == "hero") return;
_card.setForcedPower(forcedPower);
});
}
},
"clear_weather": {
onAfterPlace: function(card) {
var targetRow = card.constructor.TYPE.WEATHER;
var field = this.getYourside().getField(targetRow).getCards();
//todo: remove weather cards
}
},
"decoy": {
replaceWith: true
}
}

206
assets/data/cards.js Normal file
View File

@ -0,0 +1,206 @@
/**
* types
* 0 close combat
* 1 ranged
* 2 siege
* 3 leader
* 4 special (decoy)
* 5 weather
*/
module.exports = {
"redanian_foot_soldier": {
name: "Redanian Foot Soldier",
power: 1,
ability: null,
img: "foot_soldier1",
faction: "Northern Realm",
type: 0
},
"poor_fucking_infantry": {
name: "Poor Fucking Infantry",
power: 1,
ability: "tight_bond",
img: "infantry",
faction: "Northern Realm",
type: 0
},
"yarpen_zigrin": {
name: "Yarpen Zigrin",
power: 1,
ability: null,
img: "yarpen",
faction: "Northern Realm",
type: 0
},
"blue_stripes_commando": {
name: "Blue Stripes Commando",
power: 4,
ability: "tight_bond",
img: "commando",
faction: "Northern Realm",
type: 0
},
"sigismunt_dijkstra": {
name: "Sigismunt Dijkstra",
power: 4,
ability: "spy",
img: "dijkstra",
faction: "Northern Realm",
type: 0
},
"prince_stennis": {
name: "Prince Stennis",
power: 5,
ability: "spy",
img: "stennis",
faction: "Northern Realm",
type: 0
},
"siegfried_of_denesle": {
name: "Siegfried of Denesle",
power: 5,
ability: null,
img: "siegfried",
faction: "Northern Realm",
type: 0
},
"ves": {
name: "Ves",
power: 5,
ability: null,
img: "ves",
faction: "Northern Realm",
type: 0
},
"vernon_roche": {
name: "Vernon Roche",
power: 10,
ability: "hero",
img: "roche",
faction: "Northern Realm",
type: 0
},
"john_natalis": {
name: "John Natalis",
power: 10,
ability: "hero",
img: "natalis",
faction: "Northern Realm",
type: 0
},
"sheldon_skaggs": {
name: "Sheldon Skaggs",
power: 4,
ability: null,
img: "skaggs",
faction: "Northern Realm",
type: 1
},
"sabrina_glevissig": {
name: "Sabrina Glevissig",
power: 4,
ability: null,
img: "sabrina",
faction: "Northern Realm",
type: 1
},
"crinfrid_reavers_dragon_hunter": {
name: "Crinfrid Reaver's Dragon Hunter",
power: 5,
ability: "tight_bond",
img: "crinfrid",
faction: "Northern Realm",
type: 1
},
"sile_de_tansarville": {
name: "Síle de Tansarville",
power: 5,
ability: null,
img: "sile",
faction: "Northern Realm",
type: 1
},
"keira_metz": {
name: "Keira Metz",
power: 5,
ability: null,
img: "keira",
faction: "Northern Realm",
type: 1
},
"dethmold": {
name: "Dethmold",
power: 6,
ability: null,
img: "dethmold",
faction: "Northern Realm",
type: 1
},
"kaedweni_siege_expert": {
name: "Kaedweni Siege Expert",
power: 1,
ability: "morale_boost",
img: "siege_expert1",
faction: "Northern Realm",
type: 2
},
"dun_banner_medic": {
name: "Dun Banner Medic",
power: 5,
ability: "medic",
img: "medic",
faction: "Northern Realm",
type: 2
},
"ballista": {
name: "Ballista",
power: 6,
ability: null,
img: "ballista1",
faction: "Northern Realm",
type: 2
},
"trebuchet": {
name: "Trebuchet",
power: 6,
ability: null,
img: "trebuchet1",
faction: "Northern Realm",
type: 2
},
"thaler": {
name: "Thaler",
power: 1,
ability: "spy",
img: "thaler",
faction: "Northern Realm",
type: 2
},
"foltest_king_of_temeria": {
name: "Foltest: King of Temeria",
power: 0,
ability: "foltest_leader1",
img: "foltest_king",
faction: "Northern Realm",
type: 3
},
"decoy": {
name: "Decoy",
power: 0,
ability: "decoy",
img: "decoy",
faction: null,
type: 4
},
"impenetrable_fog": {
name: "Impenetrable Fog",
power: 0,
ability: "weather_fog",
img: "fog",
faction: null,
type: 5
}
}

30
assets/data/deck.js Normal file
View File

@ -0,0 +1,30 @@
module.exports = {
"test": [
"redanian_foot_soldier",
"poor_fucking_infantry",
"redanian_foot_soldier",
"poor_fucking_infantry",
"yarpen_zigrin",
"blue_stripes_commando",
"sigismunt_dijkstra",
"prince_stennis",
"siegfried_of_denesle",
"ves",
"vernon_roche",
"john_natalis",
"sheldon_skaggs",
"sabrina_glevissig",
"crinfrid_reavers_dragon_hunter",
"sile_de_tansarville",
"keira_metz",
"dethmold",
"kaedweni_siege_expert",
"dun_banner_medic",
"ballista",
"trebuchet",
"thaler",
"foltest_king_of_temeria",
"decoy",
"impenetrable_fog"]
}

View File

@ -15,10 +15,16 @@
"socket.io-client": "^1.3.5"
},
"devDependencies": {
"babelify": "^6.1.2",
"browserify": "^10.2.4",
"gulp": "^3.9.0",
"gulp-livereload": "^3.8.0",
"gulp-sass": "^2.0.1",
"handlebars": "^3.0.3",
"jquery": "^2.1.4",
"promise": "^7.0.1",
"shortid": "^2.2.2"
"shortid": "^2.2.2",
"vinyl-source-stream": "^1.1.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"

View File

@ -8,7 +8,7 @@
<link rel="stylesheet" href="../build/main.css">
</head>
<body>
<div class="container board">
<script id="battle-template" type="text/x-handlebars-template">
<div class="col-xs-3 left-side">
<div class="col-xs-12 game-info game-info-foe foe">
<div class="col-xs-12 info-name"></div>
@ -73,6 +73,11 @@
</div>
<div class="row">
<div class="col-xs-12 field field-hand">
{{#each cards}}
<div class="card">
<img src='../assets/cards/{{_data.img}}.png'>
</div>
{{/each}}
</div>
</div>
</div>
@ -95,7 +100,7 @@
</div>
</div>
</div>
</div>
</script>
<script id="matchmaker-template" type="text/x-handlebars-template">
<div class="col-xs-12">
<div class="panel panel-default">

View File

@ -1,200 +0,0 @@
var io = require("socket.io-client")("http://localhost:16918");
var Backbone = require("backbone");
var Handlebars = require("handlebars");
var $ = require("jquery");
Handlebars.registerHelper("health", function(lives, options){
var out = "";
for(var i = 0; i < 2; i++) {
out += "<i";
if(i < lives){
out += " class='ruby'";
}
out += "></i>";
}
return out;
});
var App = Backbone.Router.extend({
routes: {
"*other": "defaultRoute"
},
initialize: function(){
},
defaultRoute: function(){
},
search: function(){
}
});
var Player = Backbone.Model.extend({
defaults: {
name: "",
lives: 2,
MAX_LIVES: 2,
hand: 0,
score: 0
},
initialize: function(){
var self = this;
window.self = self;
this.send("request:name", this.get("name") == "unnamed" ? null : {name: this.get("name")});
this.receive("response:name", function(data){
self.set("name", data.name);
});
this.receive("init:battle", function(){
console.log("opponent found!");
})
this.receive("response:createRoom", function(roomID){
self.get("room").set("id", roomID);
console.log("room created", roomID);
});
this.receive("response:joinRoom", function(roomID){
var room = new Room();
room.set("id", roomID);
self.set("room", room);
})
},
receive: function(event, cb){
this.get("socket").on(event, cb);
},
send: function(event, data, room){
data = data || null;
room = room || null;
var socket = this.get("socket");
if(!data && !room){
socket.emit(event);
}
else if(data && !room){
socket.emit(event, data);
}
else if(!data && room){
socket.to(room).emit(event);
}
else {
socket.to(room).emit(event, data);
}
},
setName: function(name){
this.send("request:name", {name: name});
}
});
var Battleside = Backbone.Model.extend({
});
var Room = Backbone.Model.extend({
defaults: {
id: ""
},
initialize: function(){
}
});
var RoomView = Backbone.View.extend({
el: ".container",
template: Handlebars.compile($("#matchmaker-template").html()),
initialize: function(){
this.listenTo(this.model, "change", this.render);
this.render();
},
events: {
"click .create-room": "createRoom",
"click .join-room": "joinRoom",
"blur .name-input": "changeName"
},
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
},
createRoom: function(){
var room = new Room();
this.model.set("room", room);
this.model.send("request:createRoom");
},
joinRoom: function(){
this.model.send("request:joinRoom");
},
changeName: function(e){
var name = $(e.target).val();
this.model.setName(name);
}
})
;
var InfoView = Backbone.View.extend({
template: Handlebars.compile($("#game-info-template").html()),
initialize: function(){
this.listenTo(this.model, "change", this.render);
this.render();
},
render: function(){
this.$el.html(this.template(this.model.attributes));
return this;
}
});
var Gwent = (function(){
var Gwent = function(){
if(!(this instanceof Gwent)){
return (new Gwent());
}
/**
* constructor here
*/
};
var r = Gwent.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r.view = null;
r.foe = null;
r.player = null;
r.playerView = null;
r.foeView = null;
r.init = function(){
var app = new App();
Backbone.history.start();
window.player = this.player = new Player({
battleside: new Battleside(),
socket: io
});
/* this.playerView = new InfoView({
el: ".game-info-player",
model: this.player
});*/
this.roomView = new RoomView({
model: this.player
});
}
return Gwent;
})();
module.exports = Gwent;

40
public/js/client-lobby.js Normal file
View File

@ -0,0 +1,40 @@
var Backbone = require("backbone");
var Handlebars = require("handlebars");
var $ = require("jquery");
var Lobby = Backbone.View.extend({
defaults: {
id: ""
},
className: "container",
template: Handlebars.compile($("#matchmaker-template").html()),
initialize: function(){
this.app = app;
this.listenTo(app.user, "change", this.render);
$(this.el).prependTo('body');
this.render();
},
events: {
"click .create-room": "createRoom",
"click .join-room": "joinRoom",
"blur .name-input": "changeName"
},
render: function(){
this.$el.html(this.template(this.app.user.attributes));
return this;
},
createRoom: function(){
this.app.send("request:createRoom");
},
joinRoom: function(){
this.app.send("request:joinRoom");
},
changeName: function(e){
var name = $(e.target).val();
this.app.user.setName(name);
}
});
module.exports = Lobby;

212
public/js/client.js Normal file
View File

@ -0,0 +1,212 @@
var io = require("socket.io-client")/*("http://localhost:16918")*/;
var Backbone = require("backbone");
var Handlebars = require("handlebars");
var $ = require("jquery");
//var Lobby = require("./client-lobby");
Handlebars.registerHelper("health", function(lives, options){
var out = "";
for(var i = 0; i < 2; i++) {
out += "<i";
if(i < lives){
out += " class='ruby'";
}
out += "></i>";
}
return out;
});
var Config = {};
Config.Server = {
"URL": "http://localhost",
"PORT": 16918
}
var App = Backbone.Router.extend({
routes: {
"lobby": "lobbyRoute",
"battle": "battleRoute",
"*path": "defaultRoute"
},
initialize: function(){
var self = this;
this.connect();
this.user = new User({app: this});
Backbone.history.start();
},
connect: function(){
this.socket = io(Config.Server.URL + ":" + Config.Server.PORT);
},
receive: function(event, cb){
this.socket.on(event, cb);
},
receiveOnce: function(event, cb){
this.socket.once(event, cb);
},
send: function(event, data){
data = data || null;
var socket = this.socket;
if(!data){
socket.emit(event);
}
if(data){
socket.emit(event, data);
}
},
lobbyRoute: function(){
if(this.currentView){
this.currentView.remove();
}
this.currentView = new Lobby({
app: this,
user: this.user
});
},
battleRoute: function(){
if(this.currentView){
this.currentView.remove();
}
this.currentView = new BattleView({
app: this,
user: this.user
});
},
defaultRoute: function(path){
this.navigate("lobby", {trigger: true});
},
parseEvent: function(event){
var regex = /(\w+):?(\w*)\|?/g;
var res = {};
var r;
while(r = regex.exec(event)) {
res[r[1]] = r[2];
}
return res;
}
});
var BattleView = Backbone.View.extend({
className: "container",
template: Handlebars.compile($("#battle-template").html()),
initialize: function(options){
var self = this;
var user = this.user = options.user;
var app = this.app = options.app;
$(this.el).prependTo('body');
this.$hand = this.$el.find(".field-hand");
this.app.receive("update:hand", function(data){
console.log("update:hand", user.get("roomSide"), data._roomSide);
if(user.get("roomSide") == data._roomSide){
self.handCards = JSON.parse(data.cards);
self.render();
}
});
var interval = setInterval(function(){
if(!user.get("room")) return;
this.app.send("request:gameLoaded", {_roomID: user.get("room")});
clearInterval(interval);
}.bind(this), 100);
this.render();
},
render: function(){
var self = this;
this.$el.html(this.template({
cards: self.handCards
}));
return this;
}
});
var User = Backbone.Model.extend({
defaults: {
name: ""
},
initialize: function(){
var self = this;
var app = this.get("app");
app.receive("response:name", function(data){
self.set("name", data.name);
});
app.receive("init:battle", function(data){
console.log("opponent found!");
self.set("roomSide", data.side);
app.navigate("battle", {trigger: true});
})
app.receive("response:createRoom", function(roomID){
self.set("room", roomID);
console.log("room created", roomID);
});
app.receive("response:joinRoom", function(roomID){
self.set("room", roomID);
console.log("room id", self.get("room"));
})
app.on("createRoom", this.createRoom, this);
app.on("joinRoom", this.joinRoom, this);
app.on("setName", this.setName, this);
app.send("request:name", this.get("name") == "unnamed" ? null : {name: this.get("name")});
},
createRoom: function(){
this.get("app").send("request:createRoom");
},
joinRoom: function(){
this.get("app").send("request:joinRoom");
},
setName: function(name){
this.get("app").send("request:name", {name: name});
}
});
var Lobby = Backbone.View.extend({
defaults: {
id: ""
},
className: "container",
template: Handlebars.compile($("#matchmaker-template").html()),
initialize: function(options){
this.user = options.user;
this.app = options.app;
this.listenTo(this.app.user, "change", this.render);
$(this.el).prependTo('body');
this.render();
},
events: {
"click .create-room": "createRoom",
"click .join-room": "joinRoom",
"blur .name-input": "changeName"
},
render: function(){
this.$el.html(this.template(this.user.attributes));
return this;
},
createRoom: function(){
this.app.trigger("createRoom");
},
joinRoom: function(){
this.app.trigger("joinRoom");
},
changeName: function(e){
var name = $(e.target).val();
this.app.trigger("setName", name);
}
});
module.exports = App;

View File

@ -1,9 +1,7 @@
var Gwent = require("./Gwent");
var App = require("./client");
(function main(){
var gwent = Gwent();
gwent.init();
var app = new App();
})();

View File

@ -1,14 +1,16 @@
var Battleside = require("./Battleside");
var io = global.io;
var Battle = (function(){
var Battle = function(){
var Battle = function(id){
if(!(this instanceof Battle)){
return (new Battle());
return (new Battle(id));
}
/**
* constructor here
*/
this._id = id;
};
var r = Battle.prototype;
/**
@ -17,33 +19,31 @@ var Battle = (function(){
* r.getProperty = function() {...}
*/
r._player = null;
r.p1 = null;
r.p2 = null;
r.turn = 0;
r.init = function(p1, p2){
this.setPlayer(p1, p2);
this.initBattleside();
this.both(function(p) {
p.send("init:battle");
})
r._id = null;
r.init = function(){
this.p1 = Battleside("Player 1", 0, this);
this.p2 = Battleside("Player 2", 1, this);
this.p1.foe = this.p2;
this.p2.foe = this.p1;
this.start();
}
r.setPlayer = function(p1, p2){
this._player = [];
this._player.push(p1);
this._player.push(p2);
r.start = function() {
this.p1.draw(10);
this.p2.draw(10);
}
r.initBattleside = function() {
this._player.forEach(function(p) {
p.setBattleside(Battleside(p));
});
r.send = function(event, data) {
io.to(this._id).emit(event, data);
}
r.both = function() {
this._player.forEach(cb);
}
return Battle;
})();

View File

@ -1,13 +1,24 @@
var io = global.io;
var DeckData = require("../assets/data/deck");
var Deck = require("./Deck");
var Hand = require("./Hand");
var Battleside = (function(){
var Battleside = function(player){
var Battleside = function(name, n, battle){
if(!(this instanceof Battleside)){
return (new Battleside(player));
return (new Battleside(name, n, battle));
}
/**
* constructor here
*/
this._player = player;
this.n = n ? "p2" : "p1";
this._name = name;
this.battle = battle;
this.hand = Hand();
this.deck = Deck(DeckData["test"]);
};
var r = Battleside.prototype;
/**
@ -15,16 +26,38 @@ var Battleside = (function(){
* r.property = null;
* r.getProperty = function() {...}
*/
r._player = null;
r._deck = null;
r._name = null;
r._discard = null;
r._hand = null;
r._leader = null;
r._close = null;
r._range = null;
r._siege = null;
r._field = null;
r.foe = null;
r.hand = null;
r.battle = null;
r.deck = null;
r.draw = function(times) {
while(times--) {
var card = this.deck.draw();
this.hand.add(card);
}
console.log("update:hand fired");
this.send("update:hand", {cards: JSON.stringify(this.hand.getCards())});
}
r.send = function(event, msg) {
msg = msg || {};
msg._roomSide = this.n;
this.battle.send(event, msg);
}
return Battleside;
})();

91
server/Card.js Normal file
View File

@ -0,0 +1,91 @@
var CardData = require("../assets/data/cards");
var AbilityData = require("../assets/data/abilities");
var Card = (function(){
var Card = function(key){
if(!(this instanceof Card)){
return (new Card(key));
}
/**
* constructor here
*/
this._key = key;
this._data = CardData[key];
this._boost = 0;
this._forcedPower = -1;
this._init();
};
var r = Card.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._key = null;
r._data = null;
r._id = null;
r._owner = null;
r._boost = null;
r._forcedPower = null;
Card.__id = 0;
Card.TYPE = {
CLOSE_COMBAT: 0,
RANGED: 1,
SIEGE: 2,
LEADER: 3,
SPECIAL: 4,
WEATHER: 5
};
r._init = function(){
this._id = ++Card.__id;
}
r.getName = function(){
return this._data.name;
}
r.getPower = function(){
if(this._forcedPower > -1) {
return this._forcedPower + this._boost;
}
return this._data.power + this._boost;
}
r.setForcedPower = function(nr) {
this._forcedPower = nr;
}
r.getRawAbility = function() {
return this._data.ability;
}
r.getAbility = function(){
return AbilityData[this._data.ability];
}
r.getImage = function(){
return "../assets/cards/" + this._data.img + ".png";
}
r.getFaction = function(){
return this._data.faction;
}
r.getType = function(){
return this._data.type;
}
r.getKey = function(){
return this._key;
}
r.getId = function(){
return this._id;
}
r.boost = function(nr) {
this._boost += nr;
}
r.getProperty = function(prop){
return this._data[prop];
}
return Card;
})();
module.exports = Card;

114
server/Deck.js Normal file
View File

@ -0,0 +1,114 @@
var Card = require("./Card");
/*var CardManager = require("./CardManager");*/
var Deck = (function(){
var Deck = function(deck){
if(!(this instanceof Deck)){
return (new Deck(deck));
}
/**
* constructor here
*/
this._deck = [];
this._originalDeck = [];
this.setDeck(deck);
};
var r = Deck.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._deck = null;
r._owner = null;
r._originalDeck = null;
r.setDeck = function(deckData){
this._originalDeck = deckData.slice();
this._deck = deckData.slice();
this._loadCards();
this.shuffle();
}
r.getLength = function(){
return this._deck.length;
}
r.length = function() {
return this.getLength();
}
r.getDeck = function(){
return this._deck;
}
r.draw = function(times){
if(!this._deck.length) return 0;
var card = this.pop();
return card;
}
/*
r._loadCards = function(){
var n = this._originalDeck.length;
for(var i = 0; i < n; i++) {
this._deck.push(CardManager().add(this._originalDeck[i], this._owner));
}
}*/
r._loadCards = function() {
this._deck = this.getDeck().map(function(cardkey) {
return Card(cardkey);
});
}
r.pop = function(){
var id = this._deck.pop();/*
var card = CardManager().getCardById(id);*/
return id;
}
/*r.find = function(key, val){
var res = [];
this.getDeck().forEach(function(id){
var card = CardManager().getCardById(id);
if(card.getProperty(key) == val){
res.push(card);
}
});
return res;
}*/
r.removeFromDeck = function(id){
var n = this.length();
for(var i = 0; i < n; i++) {
var cardID = this.getDeck()[i];
if(id == cardID){
this.getDeck().splice(i, 1);
return id;
}
}
return -1;
}
r.shuffle = function(){
var deck = this.getDeck();
var n = this.length();
for(var i = n - 1; i > 0; i--) {
var j = (Math.random() * i) | 0;
var tmp;
tmp = deck[j];
deck[j] = deck[i];
deck[i] = tmp;
}
}
return Deck;
})();
module.exports = Deck;

60
server/Hand.js Normal file
View File

@ -0,0 +1,60 @@
/*var $ = require("jquery");*//*
var CardManager = require("./CardManager");*//*
var PubSub = require("./pubsub");*/
var Hand = (function(){
var Hand = function(){
if(!(this instanceof Hand)){
return (new Hand());
}
/**
* constructor here
*/
this._hand = [];
};
var r = Hand.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._hand = null;
r.add = function(card){
this._hand.push(card);
}
r.getCards = function(){
return this._hand;
}
r.remove = function(id){
var n = this.length();
for(var i = 0; i < n; i++) {
if(this._hand[i].getId() != id) continue;
return this._hand.splice(i, 1);
}
return -1;
}
r.getRandomCard = function(){
var rnd = (Math.random() * this._hand.length) | 0;
return this._hand[rnd];
}
r.getLength = function(){
return this._hand.length;
}
r.length = function(){
return this._hand.length;
}
return Hand;
})();
module.exports = Hand;

View File

@ -1,4 +1,5 @@
var shortid = require("shortid");
var Battle = require("./Battle");
var Room = (function(){
var Room = function(){
@ -11,6 +12,7 @@ var Room = (function(){
this._id = shortid.generate();
this._room = [];
this._ready = {};
};
var r = Room.prototype;
/**
@ -21,37 +23,72 @@ var Room = (function(){
r.MAX_USER = 2;
r._room = null;
r._id = null;
r._battle = null;
r._ready = null;
r.getID = function() {
r.getID = function(){
return this._id;
}
r.join = function(user) {
if(this._room.lenght >= 2) return;
r.join = function(user){
if(this._room.length >= 2) return;
this._room.push(user);
user.setRoom(this);/*
user.socket.join(this._id);*/
user.setRoom(this);
user.joinRoom(this.getID());
if(!this.isOpen()) {
this._room.forEach(function(user) {
user.send("init:battle");
})
if(!this.isOpen()){
this.initBattle();
}
}
r.isOpen = function() {
r.isOpen = function(){
return !(this._room.length >= 2);
}
r.send = function(event, data) {
r.send = function(event, data){
io.to(this._id).emit(event, data);
}
r.getPlayers = function() {
r.getPlayers = function(){
return this._room;
}
r.initBattle = function(){
var self = this;
var side = 0;
this._battle = Battle(this._id);/*
this.setReady(this._room[0], false);
this.setReady(this._room[1], false);*/
this._room[0].send("init:battle", {side: "p1"});
this._room[1].send("init:battle", {side: "p2"});
}
r.setReady = function(user, b){
b = typeof b == "undefined" ? true : b;
this._ready[user.getID()] = b;
if(this.bothReady()) {
this._battle.init();
}
/*
if(!this.checkIfReady()) return;
this._room[0].send("init:battle", {side: "p1"});
this._room[1].send("init:battle", {side: "p2"});
if(!this.checkIfReady()) return;
this._battle.init();*/
}
r.bothReady = function() {
return !!this._ready[this._room[0].getID()] && !!this._ready[this._room[1].getID()];
}
/*
r.checkIfReady = function(){
for(var i = 0; i < this._room.length; i++) {
if(!this._ready[this._room[i].getID()]) return false;
}
return true;
}*/
return Room;
})();

View File

@ -19,10 +19,10 @@ var Socket = (function(){
* constructor here
*/
this.connections = Connections();
/*
this.matchmaker = Matchmaker(this.connections);
*/
this._roomCollection = [];
/*
this.matchmaker = Matchmaker(this.connections);
*/
this.roomCollection = {};
app.listen(this.port);
this.io = io;
this._events();
@ -36,10 +36,10 @@ var Socket = (function(){
r.io = null;
r.port = 16918;
r.connections = null;
r._roomCollection = null;
/*
r.matchmaker = null;
*/
r.roomCollection = null;
/*
r.matchmaker = null;
*/
r._events = function(){
var self = this;
@ -48,13 +48,6 @@ var Socket = (function(){
self.connections.add(user);
console.log("new user ", user.getName());
/* self.matchmaker.findOpponent(user)
.then(function(p1, p2, roomID) {
console.log("yo");
var battle = Battle();
battle.init(p1, p2);
})*/
socket.on("request:name", function(data){
if(data && data.name){
user.setName(data.name);
@ -62,29 +55,37 @@ var Socket = (function(){
socket.emit("response:name", {name: user.getName()});
})
socket.on("request:createRoom", function() {
socket.on("request:createRoom", function(){
var room = Room();
self._roomCollection.push(room);
self.roomCollection[room.getID()] = room;
room.join(user);
console.log("room %s created by %s", room.getID(), user.getName());
user.send("response:createRoom", room.getID());
})
socket.on("request:joinRoom", function() {
socket.on("request:joinRoom", function(){
console.log("joinroom");
var interval = setInterval(function(){
self._roomCollection.forEach(function(room) {
/*self.roomCollection.forEach(function(room) {
if(room.isOpen()) {
room.join(user);
clearInterval(interval);
console.log("user %s joined room %s", user.getName(), room.getID());
user.send("response:joinRoom", room.getID());
}
});
});*/
for(var key in self.roomCollection) {
var room = self.roomCollection[key];
if(!room.isOpen()) continue;
room.join(user);
clearInterval(interval);
console.log("user %s joined room %s", user.getName(), room.getID());
user.send("response:joinRoom", room.getID());
}
}, 1000);
})
socket.on("request:roomData", function() {
socket.on("request:roomData", function(){
var room = user.getRoom();
var players = room.getPlayers();
user.send("response:roomData", {players: players});
@ -94,6 +95,10 @@ var Socket = (function(){
self.connections.remove(user);
user.disconnect();
})
socket.on("request:gameLoaded", function(data){
self.roomCollection[data._roomID].setReady(user);
})
});
}
return Socket;