]> Skullheadx's Git Forge - monopoly-web.git/commitdiff
roll Dice and end Turn handlers
authorSkullheadx <admonty1@protonmail.com>
Sun, 17 May 2026 06:45:10 +0000 (02:45 -0400)
committerSkullheadx <admonty1@protonmail.com>
Sun, 17 May 2026 06:45:10 +0000 (02:45 -0400)
game/chance.go
game/color.go
game/game.go
game/helpers.go [new file with mode: 0644]
game/movement.go
game/railroad.go
game/utility.go
main.go

index 08e6e67a432c8178898ad3634ed100772084b50d..0d89f174da287b8f8b50ca53fffab4a25c582187 100644 (file)
@@ -27,7 +27,7 @@ func getPlayerMoveDistance(start int32, dest int32) int32 {
        return distance
 }
 
-func processChance() {
+func ProcessChance() {
        for _, visitorID := range ChanceVisitors {
                card := RandSrc.IntN(len(ChanceCards))
 
@@ -64,13 +64,13 @@ func processChance() {
                        }
 
                case 4:
-                       // TODO
+               // TODO: GO TO JAIL
                case 5:
                        MoveQueue = append(MoveQueue, getPlayerMoveDistance(currentPos, ReadingRailroadSpaceID))
                case 6:
                        MoveQueue = append(MoveQueue, getPlayerMoveDistance(currentPos, GoSpaceID))
                case 7:
-                       // TODO
+               // TODO: GET OUT OF JAIL FREE
                case 8:
                        MoveQueue = append(MoveQueue, getPlayerMoveDistance(currentPos, BoardwalkSpaceID))
                case 9:
index 9cad60ba7b13157bcfcf186a34bf7f3abde30139..c49c55ad228811700f9be105437e889c6032ec26 100644 (file)
@@ -13,7 +13,7 @@ func HasColorMonopoly(playerID int32, targetGroup ColorGroup) bool {
        return ownedCount == ColorGroupSizes[targetGroup]
 }
 
-func processOwnedColors() {
+func ProcessOwnedColors() {
        for _, oCV := range OwnedColorVisitors {
                visitorID := oCV.visitorID
                ownerID := oCV.ownerID
index ff03d7a650e7fd37e9a4071ddad098e292054d84..a40b22396aa2b7efacc99e7f61a89cef8869a224 100644 (file)
@@ -14,29 +14,74 @@ var MoveQueue []int32
 var ModifierRailroadRentMultiplier int32 = 1
 var ModifierUtilityForceRentMultiplier bool = false
 
-func IsInDebt(playerID int32) (bool, int32) {
-       for i, pID := range DebtEvents {
-               if pID == playerID {
-                       return true, int32(i)
-               }
+var TurnPlayerID int32 = 0
+var TurnEndedSignal bool = false
+var DiceRollsRemaining int32 = 1
+var numDiceRolled int32 = 0
+var Doubles bool = false
+
+func ValidateCanRoll(UUID string) bool {
+       if Users[TurnPlayerID].UUID == UUID && DiceRollsRemaining > 0 {
+               return true
        }
-       return false, -1
+
+       return false
 }
 
-func AdjustPlayerMoney(playerID int32, amount int32) {
-       Users[playerID].Money += amount
+func ValidateCanEndTurn(UUID string) bool {
+       if Users[TurnPlayerID].UUID != UUID || DiceRollsRemaining > 0 || Users[TurnPlayerID].Money < 0 {
+               return false
+       }
+       return true
+}
 
-       inDebt, i := IsInDebt(playerID)
+func RollDice() {
+       // Roll Dice
+       diceRoll1 := RandSrc.Int32N(6) + 1
+       diceRoll2 := RandSrc.Int32N(6) + 1
 
-       if Users[playerID].Money < 0 {
-               if !inDebt {
-                       DebtEvents = append(DebtEvents, playerID)
-               }
-       } else { // Money >= 0
-               if inDebt { // remove player from DebtEvents table
-                       DebtEvents[i] = DebtEvents[len(DebtEvents)-1]
-                       DebtEvents = DebtEvents[:len(DebtEvents)-1]
+       numDiceRolled++
+
+       if diceRoll1 == diceRoll2 {
+               Doubles = true
+               DiceRollsRemaining++
+       }
+
+       if numDiceRolled >= 3 {
+               // TODO: GO TO JAIL
+       }
+
+       // Movement
+       for {
+               // condition to stop moving
+               if len(MoveQueue) == 0 {
+                       break
                }
+
+               // does one movement
+               // TODO: prevent movement while in JAIL
+               ProcessMovement()
+
+               ProcessGo()
+               // ProcessTax()
+
+               ProcessOwnedColors()
+               ProcessOwnedUtility()
+               ProcessOwnedRailroad()
+
+               ProcessChance()
+               // ProcessChest()
+
+               // ProcessPolice()
+               // ProcessJail()
        }
 
 }
+
+func EndTurn() {
+       TurnPlayerID = (TurnPlayerID + 1) % int32(len(Users))
+       TurnEndedSignal = false
+       DiceRollsRemaining = 1
+       numDiceRolled = 0
+       Doubles = false
+}
diff --git a/game/helpers.go b/game/helpers.go
new file mode 100644 (file)
index 0000000..262d844
--- /dev/null
@@ -0,0 +1,28 @@
+package game
+
+func IsInDebt(playerID int32) (bool, int32) {
+       for i, pID := range DebtEvents {
+               if pID == playerID {
+                       return true, int32(i)
+               }
+       }
+       return false, -1
+}
+
+func AdjustPlayerMoney(playerID int32, amount int32) {
+       Users[playerID].Money += amount
+
+       inDebt, i := IsInDebt(playerID)
+
+       if Users[playerID].Money < 0 {
+               if !inDebt {
+                       DebtEvents = append(DebtEvents, playerID)
+               }
+       } else { // Money >= 0
+               if inDebt { // remove player from DebtEvents table
+                       DebtEvents[i] = DebtEvents[len(DebtEvents)-1]
+                       DebtEvents = DebtEvents[:len(DebtEvents)-1]
+               }
+       }
+
+}
index c60d8712bdb76d1a518604fb1f2ad2b0f74bf2e7..40a9f5d786aba3a4df596404b3adaa9f793ac51c 100644 (file)
@@ -211,7 +211,6 @@ func init() {
                        SpaceToRespProperty[spaceID] = colorIndex
                        OwnableToRespProperty[ownableIndex] = colorIndex
                        RespPropertyToOwnable[colorIndex] = ownableIndex
-                       colorIndex++
 
                        if ColorProperties[colorIndex].Name == "St. Charles Place" {
                                StCharlesPlaceSpaceID = spaceID
@@ -219,14 +218,15 @@ func init() {
                                BoardwalkSpaceID = spaceID
                        }
 
+                       colorIndex++
                case TypeRailroad:
                        SpaceToRespProperty[spaceID] = railroadIndex
                        OwnableToRespProperty[ownableIndex] = railroadIndex
                        RespPropertyToOwnable[railroadIndex] = ownableIndex
-                       railroadIndex++
                        if ColorProperties[colorIndex].Name == "Reading Railroad" {
                                ReadingRailroadSpaceID = spaceID
                        }
+                       railroadIndex++
                case TypeUtility:
                        SpaceToRespProperty[spaceID] = utilityIndex
                        OwnableToRespProperty[ownableIndex] = utilityIndex
@@ -283,6 +283,13 @@ var (
        PoliceVisitors          []int32
 )
 
+func ProcessMovement() {
+       dist := MoveQueue[0]
+       MoveQueue = MoveQueue[1:]
+       player := Users[TurnPlayerID]
+       AdvancePlayer(TurnPlayerID, player.CurrentSpaceID, dist)
+}
+
 func CalculateNextPos(currentPosition int32, distance int32) int32 {
        nextPos := (currentPosition + distance)
        nextPos %= int32(len(BoardSpaces))
index ad7b13a7ff1f6033eb26a6b68bb7088341306cac..ba79a570809ae7e10bfbdd547b4db84569bd5414 100644 (file)
@@ -11,7 +11,7 @@ func numRailroadOwned(playerID int32) int32 {
        return ownedCount
 }
 
-func processOwnedRailroad() {
+func ProcessOwnedRailroad() {
        for _, oRV := range OwnedRailroadVisitors {
                visitorID := oRV.visitorID
                ownerID := oRV.ownerID
index 4a7a3a5ef3adb35afd9ed457012cdb7eed861abf..924fcd9ad46c13c101e85c3100949276c7853184 100644 (file)
@@ -11,7 +11,7 @@ func numUtilities(playerID int32) int32 {
        return ownedCount
 }
 
-func processOwnedUtility() {
+func ProcessOwnedUtility() {
        for _, oUV := range OwnedUtilityVisitors {
                visitorID := oUV.visitorID
                ownerID := oUV.ownerID
diff --git a/main.go b/main.go
index 295b129708408f425c318f152c36af6aa49c3f4e..e50478b98eb6ecee3523f31c1f0141ce291aa1fe 100644 (file)
--- a/main.go
+++ b/main.go
@@ -11,11 +11,13 @@ import (
 func main() {
        fmt.Println("monopoly-web backend")
 
-       game.Users[0] = game.User{UUID: "abc", Money: 100}
+       game.Users = append(game.Users, game.User{UUID: "abc", Money: 100, CurrentSpaceID: 0})
        fmt.Println(game.Users)
 
        // register routes
        http.HandleFunc("/health", healthHandler)
+       http.HandleFunc("/api/v1/roll", rollDiceHandler)
+       http.HandleFunc("POST /api/v1/turn", endTurnHandler)
 
        // listen and serve
        log.Fatal(http.ListenAndServe(":8080", nil))
@@ -24,3 +26,17 @@ func main() {
 func healthHandler(w http.ResponseWriter, req *http.Request) {
        io.WriteString(w, "Status: healthy\n")
 }
+
+const UUID = "abc" // TODO: UUID in cookie
+
+func rollDiceHandler(w http.ResponseWriter, req *http.Request) {
+       if game.ValidateCanRoll(UUID) {
+               game.RollDice()
+       }
+}
+
+func endTurnHandler(w http.ResponseWriter, req *http.Request) {
+       if game.ValidateCanEndTurn(UUID) {
+               game.EndTurn()
+       }
+}