laneya2

cave exploration game
git clone https://git.ce9e.org/laneya2.git

commit
44acba5a43349ff62debf94ef79cd1c3c78851fb
parent
0cf62dc5b422174ae8df261f07346e1826fbc83a
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-10-06 06:16
fix concurrency: ignore messages from players/monsters that no longer exist

Diffstat

M game.go 6 ++++++

1 files changed, 6 insertions, 0 deletions


diff --git a/game.go b/game.go

@@ -305,6 +305,9 @@ func (game *Game) run() {
  305   305 				mux.Unlock()
  306   306 			}
  307   307 		case pmsg := <-game.Msg:
   -1   308 			if _, ok := game.Players[pmsg.Player]; !ok {
   -1   309 				return
   -1   310 			}
  308   311 			if pmsg.Msg["action"] == "move" {
  309   312 				dir, ok := pmsg.Msg["dir"].(string)
  310   313 				if ok {
@@ -326,6 +329,9 @@ func (game *Game) run() {
  326   329 				log.Println("unknown action", pmsg.Msg)
  327   330 			}
  328   331 		case monster := <-game.MMsg:
   -1   332 			if _, ok := game.Monsters[monster]; !ok {
   -1   333 				return
   -1   334 			}
  329   335 			monster.Move()
  330   336 		}
  331   337 		game.Flush()