Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store: Improved the lockers #54

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions store/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ func (ps *Players) Reduce(state, a interface{}) interface{} {
return state
}

ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

switch act.Type {
case action.AddPlayer:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pstate.Players[act.AddPlayer.ID] = &Player{
ID: act.AddPlayer.ID,
Name: act.AddPlayer.Name,
Expand All @@ -114,8 +114,14 @@ func (ps *Players) Reduce(state, a interface{}) interface{} {
Gold: 40,
}
case action.RemovePlayer:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

delete(pstate.Players, act.RemovePlayer.ID)
case action.StealLive:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

fp := pstate.Players[act.StealLive.FromPlayerID]
fp.Lives -= 1
if fp.Lives < 0 {
Expand All @@ -139,9 +145,15 @@ func (ps *Players) Reduce(state, a interface{}) interface{} {
tp.Winner = true
}
case action.SummonUnit:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pstate.Players[act.SummonUnit.PlayerID].Income += unit.Units[act.SummonUnit.Type].Income
pstate.Players[act.SummonUnit.PlayerID].Gold -= unit.Units[act.SummonUnit.Type].Gold
case action.IncomeTick:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pstate.IncomeTimer -= 1
if pstate.IncomeTimer == 0 {
pstate.IncomeTimer = incomeTimer
Expand All @@ -150,14 +162,29 @@ func (ps *Players) Reduce(state, a interface{}) interface{} {
}
}
case action.PlaceTower:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pstate.Players[act.PlaceTower.PlayerID].Gold -= tower.Towers[act.PlaceTower.Type].Gold
case action.RemoveTower:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pstate.Players[act.RemoveTower.PlayerID].Gold += tower.Towers[act.RemoveTower.TowerType].Gold / 2
case action.PlayerReady:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pstate.Players[act.PlayerReady.ID].Ready = true
case action.UnitKilled:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pstate.Players[act.UnitKilled.PlayerID].Gold += unit.Units[act.UnitKilled.UnitType].Income
case action.UpdateState:
ps.mxPlayers.Lock()
defer ps.mxPlayers.Unlock()

pids := make(map[string]struct{})
for id := range pstate.Players {
pids[id] = struct{}{}
Expand Down
16 changes: 12 additions & 4 deletions store/towers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func (ts *Towers) Reduce(state, a interface{}) interface{} {
return state
}

ts.mxTowers.Lock()
defer ts.mxTowers.Unlock()

switch act.Type {
case action.PlaceTower:
ts.mxTowers.Lock()
defer ts.mxTowers.Unlock()

p := ts.store.Players.GetPlayerByID(act.PlaceTower.PlayerID)

var w, h float64 = 16 * 2, 16 * 2
Expand All @@ -92,6 +92,9 @@ func (ts *Towers) Reduce(state, a interface{}) interface{} {
PlayerID: p.ID,
}
case action.UpdateState:
ts.mxTowers.Lock()
defer ts.mxTowers.Unlock()

tids := make(map[string]struct{})
for id := range tstate.Towers {
tids[id] = struct{}{}
Expand All @@ -105,14 +108,19 @@ func (ts *Towers) Reduce(state, a interface{}) interface{} {
delete(tstate.Towers, id)
}
case action.RemovePlayer:
ts.mxTowers.Lock()
defer ts.mxTowers.Unlock()

for id, t := range tstate.Towers {
if t.PlayerID == act.RemovePlayer.ID {
delete(tstate.Towers, id)
}
}
case action.RemoveTower:
ts.mxTowers.Lock()
defer ts.mxTowers.Unlock()

delete(tstate.Towers, act.RemoveTower.TowerID)
default:
}
return tstate
}
28 changes: 24 additions & 4 deletions store/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
return state
}

us.mxUnits.Lock()
defer us.mxUnits.Unlock()

switch act.Type {
case action.SummonUnit:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

// We wait for the towers store as we need to interact with it
us.GetDispatcher().WaitFor(us.store.Towers.GetDispatcherToken())
var w, h float64 = 16, 16
Expand Down Expand Up @@ -114,6 +114,9 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
u.Path = us.Astar(us.store.Map, u.CurrentLineID, u.MovingObject, tws)
ustate.Units[uid.String()] = u
case action.MoveUnit:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

for _, u := range ustate.Units {
if len(u.Path) > 0 {
nextStep := u.Path[0]
Expand All @@ -125,6 +128,9 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
}
}
case action.PlaceTower:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

// We wait for the towers store as we need to interact with it
us.GetDispatcher().WaitFor(us.store.Towers.GetDispatcherToken())
ts := us.store.Towers.GetState().(TowersState)
Expand All @@ -144,6 +150,9 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
}
}
case action.RemoveTower:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

// We wait for the towers store as we need to interact with it
us.GetDispatcher().WaitFor(us.store.Towers.GetDispatcherToken())
ts := us.store.Towers.GetTowers()
Expand All @@ -163,8 +172,14 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
}
}
case action.RemoveUnit:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

delete(ustate.Units, act.RemoveUnit.UnitID)
case action.TowerAttack:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

u, ok := ustate.Units[act.TowerAttack.UnitID]
if !ok {
break
Expand All @@ -174,6 +189,9 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
u.Health = 0
}
case action.UpdateState:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

uids := make(map[string]struct{})
for id := range ustate.Units {
uids[id] = struct{}{}
Expand All @@ -187,12 +205,14 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
delete(ustate.Units, id)
}
case action.RemovePlayer:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

for id, u := range ustate.Units {
if u.PlayerID == act.RemovePlayer.ID {
delete(ustate.Units, id)
}
}
default:
}
return ustate
}