return distance
}
-func processChance() {
+func ProcessChance() {
for _, visitorID := range ChanceVisitors {
card := RandSrc.IntN(len(ChanceCards))
}
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:
return ownedCount == ColorGroupSizes[targetGroup]
}
-func processOwnedColors() {
+func ProcessOwnedColors() {
for _, oCV := range OwnedColorVisitors {
visitorID := oCV.visitorID
ownerID := oCV.ownerID
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
+}
--- /dev/null
+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]
+ }
+ }
+
+}
SpaceToRespProperty[spaceID] = colorIndex
OwnableToRespProperty[ownableIndex] = colorIndex
RespPropertyToOwnable[colorIndex] = ownableIndex
- colorIndex++
if ColorProperties[colorIndex].Name == "St. Charles Place" {
StCharlesPlaceSpaceID = spaceID
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
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))
return ownedCount
}
-func processOwnedRailroad() {
+func ProcessOwnedRailroad() {
for _, oRV := range OwnedRailroadVisitors {
visitorID := oRV.visitorID
ownerID := oRV.ownerID
return ownedCount
}
-func processOwnedUtility() {
+func ProcessOwnedUtility() {
for _, oUV := range OwnedUtilityVisitors {
visitorID := oUV.visitorID
ownerID := oUV.ownerID
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))
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()
+ }
+}