Skip to content

Commit

Permalink
整理
Browse files Browse the repository at this point in the history
  • Loading branch information
bieberg0n committed Jun 13, 2018
1 parent 3bf1f41 commit 53d4120
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
59 changes: 41 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class Player {
this.status = status
this.cards = deckList
}

cardInCards(card: string): boolean {
return this.cards.indexOf(card) !== -1
}

cardsInCards(cards: string[]): boolean {
return this.cards.every(card => this.cardInCards(card))
}
}

class Message {
Expand Down Expand Up @@ -67,27 +75,36 @@ class UnoServer {
return this.players[-1]
}

lobby(msg: Message, conn: net.Socket) {
join(conn: net.Socket, msg: Message){
log(`${conn.remoteAddress}:${conn.remotePort} join`)
const player = new Player(conn, uuid.v4(), 'join', [])
this.players.push(player)
const m = new Message('', player.id, '', [])
conn.write(JSON.stringify(m))
this.broadcast(player.id.slice(0, 6) + ' join.' )
}

ready(conn: net.Socket, msg: Message) {
log(`${conn.remoteAddress}:${conn.remotePort} ready`)
const player = this.playerFromArr(msg.id)
player.status = msg.action
for (let p of this.players) {
if (p.status != 'ready') {
return
}
}
this.status = 'playing'
log('playing')
this.dealCards()
this.pushCards()
}

lobby(conn: net.Socket, msg: Message) {
if ((msg.action === 'join') && !this.idInPlayers(msg.id)) {
log(`${conn.remoteAddress}:${conn.remotePort} join`)
const player = new Player(conn, uuid.v4(), 'join', [])
this.players.push(player)
const m = new Message('', player.id, '', [])
conn.write(JSON.stringify(m))
this.join(conn, msg)

} else if ((msg.action === 'ready') && this.idInPlayers(msg.id)) {
log(`${conn.remoteAddress}:${conn.remotePort} ready`)
const player = this.playerFromArr(msg.id)
player.status = msg.action
for (let p of this.players) {
if (p.status != 'ready') {
return
}
}
this.status = 'playing'
log('playing')
this.dealCards()
this.pushCards()
this.ready(conn, msg)
}
}

Expand All @@ -106,7 +123,13 @@ class UnoServer {

playing(msg: Message) {
if (this.idInPlayers(msg.id)) {
if ((msg.action === 'push') &&
(msg.cards.length === 1) &&
(this.playerFromArr(msg.id).cardsInCards(msg.cards))
) {


}
} else {
return
}
Expand Down
1 change: 1 addition & 0 deletions test/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def main():
data_str = s.recv(512).decode()
data = json.loads(data_str)

print(s.recv(512))
# data2_str = s2.recv(512).decode()
# data2 = json.loads(data2_str)

Expand Down

0 comments on commit 53d4120

Please sign in to comment.