-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_human.go
91 lines (80 loc) · 1.91 KB
/
player_human.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package game
import (
"github.com/CuteReimu/uno-server/protos"
"github.com/davyxu/cellnet"
)
type HumanPlayer struct {
basePlayer
cellnet.Session
}
func (r *HumanPlayer) Init(game *Game, location int) {
r.basePlayer.Init(game, location)
msg := &protos.InitToc{
PlayerNum: uint32(r.game.TotalPlayerCount),
}
r.Send(msg)
}
func (r *HumanPlayer) NotifyAddHandCard(cards ...ICard) {
msg := &protos.DrawCardToc{}
for _, card := range cards {
msg.Card = append(msg.Card, &protos.UnoCard{
CardId: card.Id(),
Color: uint32(card.Color()),
Num: card.Number(),
})
}
r.Send(msg)
}
func (r *HumanPlayer) NotifyOtherAddHandCard(location int, count int) {
msg := &protos.OtherAddHandCardToc{
PlayerId: r.getAlternativeLocation(location),
Num: uint32(count),
}
r.Send(msg)
}
func (r *HumanPlayer) NotifyDeckNum(count int) {
msg := &protos.SetDeckNumToc{
Num: uint32(count),
}
r.Send(msg)
}
func (r *HumanPlayer) NotifyDiscardCard(location int, card ICard, args ...uint32) {
r.basePlayer.NotifyDiscardCard(location, card, args...)
msg := &protos.DiscardCardToc{
PlayerId: r.getAlternativeLocation(location),
Card: &protos.UnoCard{
CardId: card.Id(),
Color: uint32(card.Color()),
Num: card.Number(),
},
}
if len(args) > 0 {
msg.WantColor = args[0]
}
r.Send(msg)
}
func (r *HumanPlayer) NotifyTurn(location int, dir bool) {
msg := &protos.NotifyTurnToc{
PlayerId: r.getAlternativeLocation(location),
Dir: dir,
}
r.Send(msg)
}
func (r *HumanPlayer) IsWin() bool {
return len(r.cards) == 0
}
func (r *HumanPlayer) NotifyWin(location int) {
r.Send(&protos.NotifyWinToc{
PlayerId: r.getAlternativeLocation(location),
})
}
func (r *HumanPlayer) getAlternativeLocation(location int) uint32 {
if location == 99999 {
return 99999
}
location -= r.Location()
if location < 0 {
location += r.game.TotalPlayerCount
}
return uint32(location % r.game.TotalPlayerCount)
}