From: Skullheadx Date: Mon, 8 Jun 2026 21:09:42 +0000 (-0400) Subject: rolling dice moves X-Git-Url: http://git.skullheadx.com/nixos/static/simulation.png?a=commitdiff_plain;h=ecaca83c1552fd5150014284abff1f7acf601a79;p=monopoly-web.git rolling dice moves --- diff --git a/game/chance.go b/game/chance.go index 57971a7..b6b0573 100644 --- a/game/chance.go +++ b/game/chance.go @@ -88,7 +88,7 @@ func (ctx *Context) ProcessChance() { ctx.AdjustPlayerMoney(visitorID, -repairCost) case 14: for i := range ctx.Players.Alive { - pID := PlayerID{id: int32(i)} + pID := PlayerID{ID: int32(i)} if pID == visitorID { ctx.AdjustPlayerMoney(pID, -50*int32(len(ctx.Players.Alive)-1)) } else { diff --git a/game/chest.go b/game/chest.go index a00f492..3c141ee 100644 --- a/game/chest.go +++ b/game/chest.go @@ -23,7 +23,7 @@ func (ctx *Context) ProcessChest() { ctx.AdjustPlayerMoney(visitorID, 20) case 7: for i := range ctx.Players.Alive { - pID := PlayerID{id: int32(i)} + pID := PlayerID{ID: int32(i)} if pID == visitorID { ctx.AdjustPlayerMoney(pID, 10*int32(len(ctx.Players.Alive)-1)) } else { diff --git a/game/color.go b/game/color.go index e4b8678..a2eaa5c 100644 --- a/game/color.go +++ b/game/color.go @@ -31,9 +31,9 @@ func (ctx *Context) HasColorMonopoly(playerID PlayerID, targetGroup ColorGroup) func (ctx *Context) ProcessOwnedColors() { for _, oCV := range ctx.Visitors.Color { - visitorID := oCV.visitorID - ownerID := oCV.ownerID - colorID := oCV.colorID + visitorID := oCV.VisitorID + ownerID := oCV.OwnerID + colorID := oCV.ColorID prop := ColorProperties[colorID] prices := ColorPropertyRents[colorID] diff --git a/game/config.go b/game/config.go index b83175b..5f01d9f 100644 --- a/game/config.go +++ b/game/config.go @@ -180,13 +180,13 @@ var TaxSpaces = []TaxSpace{ {Name: "Luxury Tax", Amount: 100}, } -var BankPlayerID PlayerID = PlayerID{id: -1} +var BankPlayerID PlayerID = PlayerID{ID: -1} var SpecialSpaces ChanceSpaceIDs func init() { for i, s := range BoardSpaces { - spaceID := SpaceID{id: int32(i)} + spaceID := SpaceID{ID: int32(i)} propertyType := s.PropertyType diff --git a/game/game.go b/game/game.go index bf6719d..06cad22 100644 --- a/game/game.go +++ b/game/game.go @@ -2,6 +2,7 @@ package game import ( "context" + "encoding/json" "errors" "fmt" "github.com/coder/websocket" @@ -35,6 +36,11 @@ type MonopolyServer struct { randSeed *rand.PCG } +func (ctx *Context) logGameCtx() { + data, _ := json.MarshalIndent(ctx, "", " ") + fmt.Println(string(data)) +} + func NewMonopolyServer() *MonopolyServer { ms := &MonopolyServer{ subscriberMessageBuffer: 16, @@ -204,10 +210,13 @@ func (ms *MonopolyServer) rollHandler(w http.ResponseWriter, r *http.Request) { } func (ms *MonopolyServer) roll() { - fmt.Printf("%#v\n", ms.gameCtx) + ms.gameCtx.logGameCtx() ms.gameCtx.RollDice() - fmt.Printf("%#v\n", ms.gameCtx) + + ms.gameCtx.logGameCtx() ms.gameCtx.ProcessMovement() + + ms.gameCtx.logGameCtx() } func (ms *MonopolyServer) subscribe(w http.ResponseWriter, r *http.Request) error { @@ -358,16 +367,16 @@ func initTurn(pID PlayerID, inJail bool) Turn { } func InitCtx(randSeed rand.Source, players []Player) *Context { - startingPlayerID := PlayerID{id: 0} + startingPlayerID := PlayerID{ID: 0} playerIDs := []PlayerID{} for i := range players { - playerIDs = append(playerIDs, PlayerID{id: int32(i)}) + playerIDs = append(playerIDs, PlayerID{ID: int32(i)}) } ownableProps := []OwnableProperty{} for i, s := range BoardSpaces { - spaceID := SpaceID{id: int32(i)} + spaceID := SpaceID{ID: int32(i)} propertyType := s.PropertyType if propertyType == TypeColor || propertyType == TypeRailroad || propertyType == TypeUtility { @@ -407,7 +416,7 @@ func InitCtx(randSeed rand.Source, players []Player) *Context { func InitPlayer(userUUID string) Player { return Player{ - userUUID: userUUID, + UserUUID: userUUID, Money: StartingMoney, CurrentSpaceID: SpecialSpaces.Go, GetOutOfJailCards: StartingGetOutOfJailFreeCards, @@ -428,21 +437,21 @@ func (ctx *Context) GetCurrentTurnPlayer() *Player { } func (ctx *Context) ValidateIsTurn(userUUID string) bool { - if ctx.GetCurrentTurnPlayer().userUUID == userUUID { + if ctx.GetCurrentTurnPlayer().UserUUID == userUUID { return true } return false } func (ctx *Context) ValidateCanRoll(userUUID string) bool { - if ctx.ValidateIsTurn(userUUID) && ctx.Turn.DiceRollsRemaining > 0 { + if ctx.ValidateIsTurn(userUUID) && ctx.EventPeek() == EventRollDice { return true } return false } func (ctx *Context) ValidateCanEndTurn(userUUID string) bool { - if !(ctx.ValidateIsTurn(userUUID) || ctx.Turn.DiceRollsRemaining > 0 || ctx.GetCurrentTurnPlayer().Money < 0) { + if ctx.ValidateIsTurn(userUUID) || ctx.EventPeek() == EventEndTurn || ctx.GetCurrentTurnPlayer().Money >= 0 { return true } return false @@ -450,7 +459,7 @@ func (ctx *Context) ValidateCanEndTurn(userUUID string) bool { func (ctx *Context) ValidateCanExitJail(userUUD string) bool { for _, iJV := range ctx.Visitors.InJail { - if ctx.Players.Alive[iJV.visitorID.Index()].userUUID == userUUD { + if ctx.Players.Alive[iJV.VisitorID.Index()].UserUUID == userUUD { return true } } @@ -474,6 +483,8 @@ func (ctx *Context) ProcessLanding() { } func (ctx *Context) RollDice() { + ctx.EventPop() + // Roll Dice diceRoll1 := ctx.Random.Int32N(6) + 1 diceRoll2 := ctx.Random.Int32N(6) + 1 @@ -494,10 +505,11 @@ func (ctx *Context) RollDice() { ctx.PutPlayerInJail(ctx.Turn.Current) } + ctx.Turn.MoveQueue = append(ctx.Turn.MoveQueue, diceRoll1+diceRoll2) } func (ctx *Context) EndTurn() { - nextTurnPlayerID := PlayerID{id: (ctx.Turn.Current.id + 1) % int32(len(ctx.Players.Alive))} + nextTurnPlayerID := PlayerID{ID: (ctx.Turn.Current.ID + 1) % int32(len(ctx.Players.Alive))} ctx.Turn = initTurn(nextTurnPlayerID, !ctx.PlayerCanMove(nextTurnPlayerID)) // TODO: send options list to the next player } @@ -527,7 +539,7 @@ func (ctx *Context) IsOwned(spaceID SpaceID) bool { func (ctx *Context) getPropID(spaceID SpaceID) PropertyID { for i, prop := range ctx.Properties.Owners { if spaceID == prop.SpaceID { - return PropertyID{id: int32(i)} + return PropertyID{ID: int32(i)} } } panic("Space is not an ownable property") diff --git a/game/jail.go b/game/jail.go index 8b6ae80..df32d55 100644 --- a/game/jail.go +++ b/game/jail.go @@ -6,7 +6,7 @@ import ( func (ctx *Context) RemovePlayerFromJail(playerID PlayerID) { for i, iJV := range ctx.Visitors.InJail { - if playerID == iJV.visitorID { + if playerID == iJV.VisitorID { ctx.Visitors.InJail[i] = ctx.Visitors.InJail[len(ctx.Visitors.InJail)-1] ctx.Visitors.InJail = ctx.Visitors.InJail[:len(ctx.Visitors.InJail)-1] } @@ -24,7 +24,7 @@ func (ctx *Context) PutPlayerInJail(pID PlayerID) { ctx.Players.CanMove[i] = ctx.Players.CanMove[len(ctx.Players.CanMove)-1] ctx.Players.CanMove = ctx.Players.CanMove[:len(ctx.Players.CanMove)-1] ctx.Turn.EventStack = ctx.Turn.EventStack[0:1] - ctx.Visitors.InJail = append(ctx.Visitors.InJail, InJailVisitor{visitorID: pID, TurnsLeft: JailDefaultTurns}) + ctx.Visitors.InJail = append(ctx.Visitors.InJail, InJailVisitor{VisitorID: pID, TurnsLeft: JailDefaultTurns}) ctx.Turn.DiceRollsRemaining = 0 } } @@ -32,7 +32,7 @@ func (ctx *Context) PutPlayerInJail(pID PlayerID) { func (ctx *Context) ProcessJail() { for _, iJV := range ctx.Visitors.InJail { - visitorID := iJV.visitorID + visitorID := iJV.VisitorID turnsLeft := iJV.TurnsLeft if turnsLeft <= 0 { diff --git a/game/movement.go b/game/movement.go index 62a01ce..1b47ca4 100644 --- a/game/movement.go +++ b/game/movement.go @@ -1,7 +1,7 @@ package game func GetPlayerMoveDistance(start SpaceID, dest SpaceID) int32 { - distance := dest.id - start.id + distance := dest.ID - start.ID if distance < 0 { distance += int32(len(BoardSpaces)) } @@ -10,27 +10,26 @@ func GetPlayerMoveDistance(start SpaceID, dest SpaceID) int32 { func (ctx *Context) ProcessMovement() { cID := ctx.Turn.Current - if ctx.PlayerCanMove(cID) { - dist := ctx.Turn.MoveQueue[0] - ctx.Turn.MoveQueue = ctx.Turn.MoveQueue[1:] - ctx.AdvancePlayer(cID, ctx.Players.Alive[cID.Index()].CurrentSpaceID, dist) + dist := ctx.Turn.MoveQueue[0] + ctx.Turn.MoveQueue = ctx.Turn.MoveQueue[1:] + ctx.AdvancePlayer(cID, ctx.Players.Alive[cID.Index()].CurrentSpaceID, dist) - ctx.ProcessLanding() - } + ctx.ProcessLanding() } func CalculateNextPos(currentPosition SpaceID, distance int32) SpaceID { nextPos := Add(currentPosition, distance) - nextPos.id %= int32(len(BoardSpaces)) + nextPos.ID %= int32(len(BoardSpaces)) return nextPos } func (ctx *Context) AdvancePlayer(playerID PlayerID, currentPosition SpaceID, diceRoll int32) { nextPos := CalculateNextPos(currentPosition, diceRoll) + ctx.Players.Alive[playerID.Index()].CurrentSpaceID = nextPos - numGoPasses := Add(currentPosition, diceRoll).id / int32(len(BoardSpaces)) + numGoPasses := Add(currentPosition, diceRoll).ID / int32(len(BoardSpaces)) if numGoPasses > 0 { if BoardSpaces[nextPos.Index()].PropertyType == TypeGo { @@ -51,7 +50,7 @@ func (ctx *Context) AdvancePlayer(playerID PlayerID, currentPosition SpaceID, di case TypeChance: ctx.Visitors.Chance = append(ctx.Visitors.Chance, playerID) case TypeTax: - ctx.Visitors.Tax = append(ctx.Visitors.Tax, TaxVisitor{visitorID: playerID, taxID: prop.SubIndexID}) + ctx.Visitors.Tax = append(ctx.Visitors.Tax, TaxVisitor{VisitorID: playerID, TaxID: prop.SubIndexID}) case TypePolice: // hardcoding to send straight to jail ctx.PutPlayerInJail(playerID) case TypeColor: @@ -59,30 +58,30 @@ func (ctx *Context) AdvancePlayer(playerID PlayerID, currentPosition SpaceID, di ownerID := ctx.Properties.Owners[propID.Index()].OwnerID if ownerID != BankPlayerID { // property owned? if ownerID != playerID && !ctx.IsMortgaged(propID) { // not by you - ctx.Visitors.Color = append(ctx.Visitors.Color, OwnedColorVisitor{visitorID: playerID, ownerID: ownerID, colorID: prop.SubIndexID}) + ctx.Visitors.Color = append(ctx.Visitors.Color, OwnedColorVisitor{VisitorID: playerID, OwnerID: ownerID, ColorID: prop.SubIndexID}) } } else { - ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{visitorID: playerID, propertyID: propID}) + ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{VisitorID: playerID, PropertyID: propID}) } case TypeRailroad: propID := ctx.getPropID(nextPos) ownerID := ctx.Properties.Owners[propID.Index()].OwnerID if ownerID != BankPlayerID { // property owned? if ownerID != playerID && !ctx.IsMortgaged(propID) { // not by you - ctx.Visitors.Railroad = append(ctx.Visitors.Railroad, OwnedRailroadVisitor{visitorID: playerID, ownerID: ownerID, railroadID: prop.SubIndexID}) + ctx.Visitors.Railroad = append(ctx.Visitors.Railroad, OwnedRailroadVisitor{VisitorID: playerID, OwnerID: ownerID, RailroadID: prop.SubIndexID}) } } else { - ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{visitorID: playerID, propertyID: propID}) + ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{VisitorID: playerID, PropertyID: propID}) } case TypeUtility: propID := ctx.getPropID(nextPos) ownerID := ctx.Properties.Owners[propID.Index()].OwnerID if ownerID != BankPlayerID { // property owned? if ownerID != playerID && !ctx.IsMortgaged(propID) { // not by you - ctx.Visitors.Utility = append(ctx.Visitors.Utility, OwnedUtilityVisitor{visitorID: playerID, ownerID: ownerID, utilityID: prop.SubIndexID, diceRoll: diceRoll}) + ctx.Visitors.Utility = append(ctx.Visitors.Utility, OwnedUtilityVisitor{VisitorID: playerID, OwnerID: ownerID, UtilityID: prop.SubIndexID, DiceRoll: diceRoll}) } } else { - ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{visitorID: playerID, propertyID: propID}) + ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{VisitorID: playerID, PropertyID: propID}) } } } diff --git a/game/railroad.go b/game/railroad.go index 7ecab91..f3ba9f0 100644 --- a/game/railroad.go +++ b/game/railroad.go @@ -26,8 +26,8 @@ func (ctx *Context) numRailroadOwned(playerID PlayerID) int32 { func (ctx *Context) ProcessOwnedRailroad() { for _, oRV := range ctx.Visitors.Railroad { - visitorID := oRV.visitorID - ownerID := oRV.ownerID + visitorID := oRV.VisitorID + ownerID := oRV.OwnerID // railroadID := oRV.railroadID var rent int32 = RailroadRent[ctx.numRailroadOwned(ownerID)] diff --git a/game/tax.go b/game/tax.go index 414d3f7..6ab8b4d 100644 --- a/game/tax.go +++ b/game/tax.go @@ -2,8 +2,8 @@ package game func (ctx *Context) ProcessTax() { for _, tV := range ctx.Visitors.Tax { - playerID := tV.visitorID - taxID := tV.taxID + playerID := tV.VisitorID + taxID := tV.TaxID ctx.AdjustPlayerMoney(playerID, -TaxSpaces[taxID].Amount) } } diff --git a/game/types.go b/game/types.go index dfb38c5..ae30651 100644 --- a/game/types.go +++ b/game/types.go @@ -5,7 +5,7 @@ import ( ) type Player struct { - userUUID string + UserUUID string Money int32 CurrentSpaceID SpaceID GetOutOfJailCards int32 @@ -63,37 +63,37 @@ type ChanceSpaceIDs struct { } type OwnedColorVisitor struct { - visitorID PlayerID - ownerID PlayerID - colorID int32 + VisitorID PlayerID + OwnerID PlayerID + ColorID int32 } type OwnedRailroadVisitor struct { - visitorID PlayerID - ownerID PlayerID - railroadID int32 + VisitorID PlayerID + OwnerID PlayerID + RailroadID int32 } type OwnedUtilityVisitor struct { - visitorID PlayerID - ownerID PlayerID - utilityID int32 - diceRoll int32 + VisitorID PlayerID + OwnerID PlayerID + UtilityID int32 + DiceRoll int32 } type UnownedPropertyVisitor struct { - visitorID PlayerID - propertyID PropertyID + VisitorID PlayerID + PropertyID PropertyID } type InJailVisitor struct { - visitorID PlayerID + VisitorID PlayerID TurnsLeft int32 } type TaxVisitor struct { - visitorID PlayerID - taxID int32 + VisitorID PlayerID + TaxID int32 } type Visitors struct { // SubIndexID is the PK for each resp table @@ -113,31 +113,31 @@ type Visitors struct { // SubIndexID is the PK for each resp table // IDs type PlayerID struct { - id int32 + ID int32 } type PropertyID struct { - id int32 + ID int32 } type SpaceID struct { - id int32 + ID int32 } func (p *PlayerID) Index() int { - return int(p.id) + return int(p.ID) } func (p *PropertyID) Index() int { - return int(p.id) + return int(p.ID) } func (s *SpaceID) Index() int { - return int(s.id) + return int(s.ID) } func Add(a SpaceID, b int32) SpaceID { - return SpaceID{id: a.id + b} + return SpaceID{ID: a.ID + b} } // Game Context (Ctx) diff --git a/game/utility.go b/game/utility.go index 054bd6b..14255c7 100644 --- a/game/utility.go +++ b/game/utility.go @@ -26,10 +26,10 @@ func (ctx *Context) NumUtilities(playerID PlayerID) int32 { func (ctx *Context) ProcessOwnedUtility() { for _, oUV := range ctx.Visitors.Utility { - visitorID := oUV.visitorID - ownerID := oUV.ownerID + visitorID := oUV.VisitorID + ownerID := oUV.OwnerID // utilityID := oUV.utilityID - diceRoll := oUV.diceRoll + diceRoll := oUV.DiceRoll var rent int32 = 0