From: Skullheadx Date: Mon, 8 Jun 2026 22:31:00 +0000 (-0400) Subject: switch to pop queue X-Git-Url: http://git.skullheadx.com/tech/index.html?a=commitdiff_plain;h=8732b3fb799c93d8f84725f7e630132722fe3191;p=monopoly-web.git switch to pop queue --- diff --git a/game/chance.go b/game/chance.go index b6b0573..1303475 100644 --- a/game/chance.go +++ b/game/chance.go @@ -10,7 +10,12 @@ func (ctx *Context) getOwnerID(sID SpaceID) PlayerID { } func (ctx *Context) ProcessChance() { - for _, visitorID := range ctx.Visitors.Chance { + for { + visitorID, done := QueuePop(&ctx.Visitors.Chance) + if done { + break + } + card := ctx.Random.IntN(len(ChanceCards)) currentPos := ctx.Players.Alive[visitorID.Index()].CurrentSpaceID diff --git a/game/chest.go b/game/chest.go index 3c141ee..9a31622 100644 --- a/game/chest.go +++ b/game/chest.go @@ -1,7 +1,11 @@ package game func (ctx *Context) ProcessChest() { - for _, visitorID := range ctx.Visitors.Chest { + for { + visitorID, done := QueuePop(&ctx.Visitors.Chest) + if done { + break + } card := ctx.Random.IntN(len(ChanceCards)) currentPos := ctx.Players.Alive[visitorID.Index()].CurrentSpaceID diff --git a/game/color.go b/game/color.go index a2eaa5c..e1826c9 100644 --- a/game/color.go +++ b/game/color.go @@ -30,7 +30,12 @@ func (ctx *Context) HasColorMonopoly(playerID PlayerID, targetGroup ColorGroup) } func (ctx *Context) ProcessOwnedColors() { - for _, oCV := range ctx.Visitors.Color { + for { + oCV, done := QueuePop(&ctx.Visitors.Color) + if done { + break + } + visitorID := oCV.VisitorID ownerID := oCV.OwnerID colorID := oCV.ColorID diff --git a/game/movement.go b/game/movement.go index 1b47ca4..9819009 100644 --- a/game/movement.go +++ b/game/movement.go @@ -10,11 +10,16 @@ func GetPlayerMoveDistance(start SpaceID, dest SpaceID) int32 { func (ctx *Context) ProcessMovement() { cID := ctx.Turn.Current - dist := ctx.Turn.MoveQueue[0] - ctx.Turn.MoveQueue = ctx.Turn.MoveQueue[1:] - ctx.AdvancePlayer(cID, ctx.Players.Alive[cID.Index()].CurrentSpaceID, dist) + for { + dist := ctx.Turn.MoveQueue[0] + ctx.Turn.MoveQueue = ctx.Turn.MoveQueue[1:] + ctx.AdvancePlayer(cID, ctx.Players.Alive[cID.Index()].CurrentSpaceID, dist) + ctx.ProcessLanding() + if len(ctx.Turn.MoveQueue) == 0 { + break + } - ctx.ProcessLanding() + } } diff --git a/game/railroad.go b/game/railroad.go index f3ba9f0..fc1c99b 100644 --- a/game/railroad.go +++ b/game/railroad.go @@ -25,7 +25,11 @@ func (ctx *Context) numRailroadOwned(playerID PlayerID) int32 { } func (ctx *Context) ProcessOwnedRailroad() { - for _, oRV := range ctx.Visitors.Railroad { + for { + oRV, done := QueuePop(&ctx.Visitors.Railroad) + if done { + break + } visitorID := oRV.VisitorID ownerID := oRV.OwnerID // railroadID := oRV.railroadID diff --git a/game/tax.go b/game/tax.go index 6ab8b4d..a6ad530 100644 --- a/game/tax.go +++ b/game/tax.go @@ -1,7 +1,11 @@ package game func (ctx *Context) ProcessTax() { - for _, tV := range ctx.Visitors.Tax { + for { + tV, done := QueuePop(&ctx.Visitors.Tax) + if done { + break + } playerID := tV.VisitorID taxID := tV.TaxID ctx.AdjustPlayerMoney(playerID, -TaxSpaces[taxID].Amount) diff --git a/game/types.go b/game/types.go index 1e589dd..e0c5b6d 100644 --- a/game/types.go +++ b/game/types.go @@ -25,6 +25,20 @@ func (ctx *Context) EventPop() EventType { return last } +// first, done +func QueuePop[T any](q *[]T) (T, bool) { + if len(*q) == 0 { + var zero T + return zero, true + } + + lastIdx := len(*q) - 1 + + first := (*q)[lastIdx] + *q = (*q)[:lastIdx] + return first, false +} + type Space struct { PropertyType PropertyType SubIndexID int32 // FK to resp. OwnableProperty types diff --git a/game/unowned.go b/game/unowned.go index c42ca86..3c0b43a 100644 --- a/game/unowned.go +++ b/game/unowned.go @@ -1,29 +1,11 @@ package game func (ctx *Context) ProcessUnowned() { - // for _, uV := range ctx.Visitors.Unowned { - // visitorID := uV.visitorID - // propID := uV.propertyID - - // prop := ctx.Properties.Owners[propID.Index()] - - // TODO: trigger buy or auction - - // spaceID := prop.SpaceID - // space := BoardSpaces[spaceID.Index()] - // - // var price int32 = 0 - // - // switch space.PropertyType { - // case TypeColor: - // price = ColorProperties[space.SubIndexID].Price - // case TypeUtility: - // price = UtilityPrice - // case TypeRailroad: - // price = RailroadPrice - // } - // - // ctx.AdjustPlayerMoney(visitorID, -price) - - // } + for { + _, done := QueuePop(&ctx.Visitors.Unowned) + if done { + break + } + ctx.Turn.EventStack = append(ctx.Turn.EventStack, EventLandUnowned) + } } diff --git a/game/utility.go b/game/utility.go index 14255c7..301e793 100644 --- a/game/utility.go +++ b/game/utility.go @@ -25,7 +25,11 @@ func (ctx *Context) NumUtilities(playerID PlayerID) int32 { } func (ctx *Context) ProcessOwnedUtility() { - for _, oUV := range ctx.Visitors.Utility { + for { + oUV, done := QueuePop(&ctx.Visitors.Utility) + if done { + break + } visitorID := oUV.VisitorID ownerID := oUV.OwnerID // utilityID := oUV.utilityID