- commit
- 0e4333d0a391974b646bd62afacb954060529413
- parent
- 570ae983fe02f3bfd0c9a1855ec058016dc9124f
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-29 08:37
close connection on death
Diffstat
| M | player.go | 18 | +++++++++++------- |
| M | server.go | 3 | +++ |
2 files changed, 14 insertions, 7 deletions
diff --git a/player.go b/player.go
@@ -4,6 +4,7 @@ import "github.com/gorilla/websocket" 4 4 5 5 type Player struct { 6 6 Game *Game -1 7 quit chan bool 7 8 send chan []Message 8 9 queue []Message 9 10 conn *websocket.Conn @@ -33,13 +34,16 @@ func (player *Player) Flush() { 33 34 } 34 35 35 36 func (player *Player) TakeDamage(amount uint) {36 -1 // TODO: death if amount >= player.Health37 -1 player.Health -= amount38 -1 player.Enqueue(Message{39 -1 "action": "setHealth",40 -1 "health": player.Health,41 -1 "healthTotal": player.HealthTotal,42 -1 })-1 37 if amount > player.Health { -1 38 player.quit <- true -1 39 } else { -1 40 player.Health -= amount -1 41 player.Enqueue(Message{ -1 42 "action": "setHealth", -1 43 "health": player.Health, -1 44 "healthTotal": player.HealthTotal, -1 45 }) -1 46 } 43 47 } 44 48 45 49 func (player *Player) Heal(amount uint) {
diff --git a/server.go b/server.go
@@ -66,6 +66,8 @@ func (player *Player) writePump() {
66 66 }
67 67 return
68 68 }
-1 69 case <-player.quit:
-1 70 return
69 71 case <-ticker.C:
70 72 if !player.alive {
71 73 return
@@ -95,6 +97,7 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
95 97
96 98 player := &Player{
97 99 Game: game,
-1 100 quit: make(chan bool),
98 101 send: make(chan []Message),
99 102 queue: []Message{},
100 103 conn: conn,