You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

190 lines
5.1 KiB

class Game{
constructor(playercount){
this.players = [];
this.online = false;
for (let i = 0; i < playercount; i++){
this.players.push(new Player(i, null, "Player"));
}
playerDomSetup(this.players.length, this.players);
this.cards = [];
this.cardSize = $($(".card_on_table")[0]).width();
$("#interface, #showInterface_btn").show();
}
static startShuffle(){
while (!Game.allPaired()){
let c1 = random(cards);
let c2 = random(cards);
if (!c1.equals(c2) && !c2.hasSwitchPartner){
Game.switchPartner(c1, c2);
}
}
for (let c of cards){
c.moveToPosition();
c.hasSwitchPartner = false;
}
}
static switchPartner(c1, c2){
c1.hasSwitchPartner = true;
c2.hasSwitchPartner = true;
let temp = c1.pos;
c1.pos = c2.pos;
c2.pos = temp;
let index = {c1: cards.indexOf(c1),
c2: cards.indexOf(c2)};
temp = cards[index.c1];
cards[index.c1] = cards[index.c2];
cards[index.c2] = temp;
}
static allPaired(){
for (let c of cards){
if (!c.hasSwitchPartner){
return false;
}
}
return true;
}
start(){
if (this.running || !this.shuffled) return;
this.running = true;
random(game.players).hasTurn(true);
}
currentPlayer(){
for (let p of this.players){
if (p.hasTurn()) return p;
}
}
revealCard(calledByServer, cardId){
if (this.nextTurn){
if (!this.pairFound()) for (let c of this.cards) c.hide();
this.cards = [];
this.nextTurn = false;
}
let card = Card.getById(cardId);
card.reveal();
this.cards.push(card);
if (this.cards.length == 2) this.next();
}
pairFound(){
if (this.cards.length < 2) return false;
return this.cards[0].equals(this.cards[1]);
}
next(){
this.nextTurn = true;
if (this.pairFound()) this.thisPlayer();
else this.nextPlayer();
}
nextPlayer(){
let p = this.currentPlayer();
p.hasTurn(false);
let i = this.players.indexOf(p) + 1;
if (i == this.players.length) i = 0;
this.players[i].hasTurn(true);
}
thisPlayer(){
this.collectPair();
}
getLeader(){
return this.players[0];
}
collectPair(){
let p = this.currentPlayer();
let stapleOffset = 2;
let staple = p.dom.find(".staple");
p.cards.push(this.cards[0], this.cards[1]);
p.addPairs(1);
let last = staple.find("img").last();
let count = 1;
for (let c of this.cards) {
let x = (p.pairs - 1) ? last.offset().left + stapleOffset * count : staple.offset().left;
let y = (p.pairs - 1) ? last.offset().top : staple.offset().top;
count++;
c.savePosition();
c.pos = {left: x, top: y};
c.moveToPosition("collect", p.dom.find(".staple"));
c.dom.css("z-index", p.cards.length - 2 + this.cards.indexOf(c));
c.dom.after(createEmpty());
cards.splice(cards.indexOf(c), 1);
}
}
shuffle(){
for (let c of cards){
c.savePosition();
c.hide();
}
setTimeout(Game.startShuffle, Card.hideTime());
}
}
class OnlineGame extends Game{
constructor(players, seed){
super(0);
this.online = true;
randomSeed(seed);
for (let p of players){
this.players.push(new Player(players.indexOf(p), p.id, p.name));
}
playerDomSetup(this.players.length, this.players);
if (socket.id === this.players[0].id){
$("#interface, #showInterface_btn").show();
} else {
$("#interface, #showInterface_btn").hide();
}
}
start(calledByServer){
if (calledByServer){
super.start();
} else
socket.emit("game-action", "start");
}
revealCard(calledByServer, cardId){
if (calledByServer){
super.revealCard(false, cardId);
} else {
if (socket.id !== this.currentPlayer().id) return;
socket.emit("game-action", "revealCard", cardId);
}
}
shuffle(calledByServer){
if (calledByServer){
super.shuffle();
} else
socket.emit("game-action", "shuffle");
}
setPlayers(players){
/*let newPlayers = [];
for (let p of this.players){
if (p.hasTurn() && !p.isIn(players)){
this.nextPlayer();
}
if (p.isIn(players)) newPlayers.push(p);
else if (p.hasTurn())
}
playerDomSetup(this.players.length, this.players);
if (socket.id === this.players[0].id){
$("#interface, #showInterface_btn").show();
}*/
//TODO
}
}