From: Skullheadx Date: Sun, 17 May 2026 06:45:10 +0000 (-0400) Subject: roll Dice and end Turn handlers X-Git-Url: http://git.skullheadx.com/index.css?a=commitdiff_plain;h=bb132f840c989ea214376b142706c927d8eaf3ba;p=monopoly-web.git roll Dice and end Turn handlers --- diff --git a/game/chance.go b/game/chance.go index 08e6e67..0d89f17 100644 --- a/game/chance.go +++ b/game/chance.go @@ -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: diff --git a/game/color.go b/game/color.go index 9cad60b..c49c55a 100644 --- a/game/color.go +++ b/game/color.go @@ -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 diff --git a/game/game.go b/game/game.go index ff03d7a..a40b223 100644 --- a/game/game.go +++ b/game/game.go @@ -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 index 0000000..262d844 --- /dev/null +++ b/game/helpers.go @@ -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] + } + } + +} diff --git a/game/movement.go b/game/movement.go index c60d871..40a9f5d 100644 --- a/game/movement.go +++ b/game/movement.go @@ -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)) diff --git a/game/railroad.go b/game/railroad.go index ad7b13a..ba79a57 100644 --- a/game/railroad.go +++ b/game/railroad.go @@ -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 diff --git a/game/utility.go b/game/utility.go index 4a7a3a5..924fcd9 100644 --- a/game/utility.go +++ b/game/utility.go @@ -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 295b129..e50478b 100644 --- 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() + } +}