Skip to content

Commit

Permalink
lock commanda
Browse files Browse the repository at this point in the history
  • Loading branch information
tivvit committed Oct 10, 2023
1 parent 36c1beb commit a908724
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions server/dungeonsandtrolls/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Game struct {
MaxLevelReached int32 `json:"max_reached_level"`
Game api.GameState `json:"-"`
GameLock sync.RWMutex `json:"-"`
CommandsLock sync.RWMutex `json:"-"`
TickCond *sync.Cond `json:"-"`

generatorLock sync.RWMutex
Expand Down Expand Up @@ -309,7 +310,9 @@ func (g *Game) MarkVisitedLevel(level int32) {
}

func (g *Game) Respawn(player *gameobject.Player, markDeath bool) {
g.CommandsLock.RLock()
g.Commands[player.GetId()] = &api.CommandsBatch{}
g.CommandsLock.RUnlock()
player.SetMovingTo(nil)

if markDeath {
Expand Down Expand Up @@ -951,10 +954,17 @@ func (g *Game) GetCurrentPlayer(token string) (*gameobject.Player, error) {
}

func (g *Game) GetCommands(pId string) *api.CommandsBatch {
g.CommandsLock.RLock()
if pc, ok := g.Commands[pId]; ok {
g.CommandsLock.RUnlock()
return pc
}
g.CommandsLock.RUnlock()

g.CommandsLock.Lock()
g.Commands[pId] = &api.CommandsBatch{}
defer g.CommandsLock.Unlock()

return g.Commands[pId]
}

Expand Down
2 changes: 2 additions & 0 deletions server/dungeonsandtrolls/handlers/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ func Commands(game *dungeonsandtrolls.Game, c *api.CommandsBatch, token string)
}

pc := game.GetCommands(p.Character.Id)
game.CommandsLock.Lock()
pc.Yell = c.Yell
pc.Buy = c.Buy
pc.PickUp = c.PickUp
pc.Skill = c.Skill
pc.AssignSkillPoints = c.AssignSkillPoints
game.CommandsLock.Unlock()

return nil
}
2 changes: 2 additions & 0 deletions server/dungeonsandtrolls/handlers/monster_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ func validateMonsterCommands(game *dungeonsandtrolls.Game, mc *api.CommandsForMo
}

pc := game.GetCommands(mId)
game.CommandsLock.Lock()
pc.Yell = c.Yell
pc.Skill = c.Skill
game.CommandsLock.Unlock()
}
return nil
}
Expand Down

0 comments on commit a908724

Please sign in to comment.