-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
75 lines (66 loc) · 1.16 KB
/
player.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
package main
import (
"log"
"sync/atomic"
"time"
)
type Team struct {
Name string
Score uint64
attackCount uint64
teamBallChan <-chan *Ball
}
type Player struct {
Name string
Team *Team
Assists uint64
Points2Attempt uint64
Points2Count uint64
Points3Attempt uint64
Points3Count uint64
}
type Play interface {
apply(b *Ball)
}
type Basket struct {
player *Player
}
func (p *Basket) apply(b *Ball) {
if Random() {
atomic.AddUint64(&p.player.Points2Attempt, 1)
b.Basket(p.player, 2)
} else {
atomic.AddUint64(&p.player.Points3Attempt, 1)
b.Basket(p.player, 3)
}
}
type Pass struct {
passer *Player
}
func (p *Pass) apply(b *Ball) {
b.Pass(p.passer)
}
func (p *Player) Play(whistle <-chan bool, play chan<- Play) {
for {
select {
case ball := <-p.Team.teamBallChan:
if ball != nil {
log.Println(p.Name, " has the ball")
time.Sleep(time.Second * 2)
if Random() {
play <- &Basket{
player: p,
}
} else {
play <- &Pass{
passer: p,
}
}
log.Println(p.Name, " passed the ball")
}
case <-whistle:
log.Println(p.Name, " quits.")
return
}
}
}