]> Skullheadx's Git Forge - monopoly-web.git/commitdiff
rolling dice moves
authorSkullheadx <admonty1@protonmail.com>
Mon, 8 Jun 2026 21:09:42 +0000 (17:09 -0400)
committerSkullheadx <admonty1@protonmail.com>
Mon, 8 Jun 2026 21:09:42 +0000 (17:09 -0400)
game/chance.go
game/chest.go
game/color.go
game/config.go
game/game.go
game/jail.go
game/movement.go
game/railroad.go
game/tax.go
game/types.go
game/utility.go

index 57971a70f13787933db8daebe84302fc3ad8a71d..b6b05734f55d3cb566c61b0dc8579fa0fd1676ad 100644 (file)
@@ -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 {
index a00f4925c9cfba427649d5a407623608d49adf6d..3c141ee8fab69dd78b0052855b6e0795ea3dec95 100644 (file)
@@ -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 {
index e4b8678b522055d3aa09a5d1c3a2b689b069b597..a2eaa5cd8add552a10f0da7bf13f62ced5bcc883 100644 (file)
@@ -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]
index b83175b8e306469f9fcecbddb5ff2bd48b184eb5..5f01d9f8b2cc7779636c676ba4ad9238f2d03732 100644 (file)
@@ -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
 
index bf6719dea7321a2006d4e62ed406e52fbc111242..06cad22a1c5d439110be9e7d6b64097d012cf292 100644 (file)
@@ -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")
index 8b6ae803e46b37acfb11ad6f84113dc6e7a42365..df32d55866e188fca983c4282d27755d44712fe1 100644 (file)
@@ -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 {
index 62a01ce59d45bd39059e1a6c27e930d8d3a8138f..1b47ca455f9373a0f09fe7b812da3e79558a9e6a 100644 (file)
@@ -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})
                }
        }
 }
index 7ecab91fc5de99fa8caac0c488f7b352e41b0e6f..f3ba9f0952930df06a086d84230c60aefcfa6c74 100644 (file)
@@ -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)]
index 414d3f75baa835e63f8cc5f6b10e17b6094394d8..6ab8b4d4c8d900805abeb0c0fbf03dc397c5ec7b 100644 (file)
@@ -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)
        }
 }
index dfb38c5f71f0e0c91b32721bf02b5fdceaf4dc16..ae30651cf846a11c787f3837e27c2f16dfaf472e 100644 (file)
@@ -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)
index 054bd6b5210da4b4968a4b648b1609ddba8bc1f2..14255c72b21b2d602a982e8efe364c3394927972 100644 (file)
@@ -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