From: Skullheadx Date: Mon, 18 May 2026 03:26:44 +0000 (-0400) Subject: escape jail X-Git-Url: http://git.skullheadx.com/nixos/blog/openbsd_html_css.html?a=commitdiff_plain;h=f71ff024a3ca722d8f2052c654da333daaacc439;p=monopoly-web.git escape jail --- diff --git a/game/chance.go b/game/chance.go index 756fb01..5874a10 100644 --- a/game/chance.go +++ b/game/chance.go @@ -70,7 +70,7 @@ func ProcessChance() { case 6: MoveQueue = append(MoveQueue, GetPlayerMoveDistance(currentPos, GoSpaceID)) case 7: - // TODO: GET OUT OF JAIL FREE + Users[visitorID].GetOutOfJailCards++ case 8: MoveQueue = append(MoveQueue, GetPlayerMoveDistance(currentPos, BoardwalkSpaceID)) case 9: diff --git a/game/chest.go b/game/chest.go index 7f0b348..1664dc6 100644 --- a/game/chest.go +++ b/game/chest.go @@ -76,7 +76,7 @@ func ProcessChest() { case 13: InJailVisitors = append(InJailVisitors, InJailVisitor{visitorID: visitorID, turns: DEFAULT_JAIL_TURNS}) case 14: - // TODO: GET OUT OF JAIL FREE + Users[visitorID].GetOutOfJailCards++ case 15: AdjustPlayerMoney(visitorID, 10) } diff --git a/game/game.go b/game/game.go index ef774ef..e147eb4 100644 --- a/game/game.go +++ b/game/game.go @@ -36,6 +36,16 @@ func ValidateCanEndTurn(UUID string) bool { return true } +func ValidateCanExitJail(UUID string) bool { + for _, iJV := range InJailVisitors { + player := Users[iJV.visitorID] + if Users[TurnPlayerID].UUID == UUID && player.UUID == UUID { + return true + } + } + return false +} + func ProcessLanding() { ProcessGo() ProcessTax() diff --git a/game/jail.go b/game/jail.go index a1387d4..b8211a9 100644 --- a/game/jail.go +++ b/game/jail.go @@ -1,6 +1,11 @@ package game +import ( + "errors" +) + const DEFAULT_JAIL_TURNS int32 = 3 +const JAIL_BUYOUT_COST int32 = 50 func RemovePlayerFromJail(playerID int32) { for i, iJV := range InJailVisitors { @@ -11,6 +16,7 @@ func RemovePlayerFromJail(playerID int32) { } } + MoveablePlayers = append(MoveablePlayers, playerID) } func RemovePlayerFromMoveable(pID int32) { @@ -29,10 +35,33 @@ func ProcessJail() { if turns <= 0 { RemovePlayerFromJail(visitorID) - MoveablePlayers = append(MoveablePlayers, visitorID) } else { RemovePlayerFromMoveable(visitorID) } } } + +var ErrNotEnoughJailCards = errors.New("Cannot use jail card: player does not have enough get out of jail free cards") +var ErrNotEnoughMoney = errors.New("Cannot execute action: player does not have enough money") + +func JailUseCard() error { + if Users[TurnPlayerID].GetOutOfJailCards > 0 { + RemovePlayerFromJail(TurnPlayerID) + Users[TurnPlayerID].GetOutOfJailCards -= 1 + return nil + + } else { + return ErrNotEnoughJailCards + } +} + +func JailBuyout() error { + if Users[TurnPlayerID].Money >= JAIL_BUYOUT_COST { + RemovePlayerFromJail(TurnPlayerID) + AdjustPlayerMoney(TurnPlayerID, -JAIL_BUYOUT_COST) + return nil + } else { + return ErrNotEnoughMoney + } +} diff --git a/game/movement.go b/game/movement.go index 284e91b..8c64ec1 100644 --- a/game/movement.go +++ b/game/movement.go @@ -304,7 +304,6 @@ func AllowedToMove(playerID int32) bool { func ProcessMovement() { for i, playerID := range MoveablePlayers { if playerID == TurnPlayerID { - // Movement for { // condition to stop moving diff --git a/game/types.go b/game/types.go index 8d104e6..a4fc9e9 100644 --- a/game/types.go +++ b/game/types.go @@ -1,9 +1,10 @@ package game type User struct { - UUID string - Money int32 - CurrentSpaceID int32 + UUID string + Money int32 + CurrentSpaceID int32 + GetOutOfJailCards int32 } type PropertyType int diff --git a/main.go b/main.go index 3303452..01ad29d 100644 --- a/main.go +++ b/main.go @@ -11,13 +11,14 @@ import ( func main() { fmt.Println("monopoly-web backend") - game.Users = append(game.Users, game.User{UUID: "abc", Money: 100, CurrentSpaceID: 0}) + game.Users = append(game.Users, game.User{UUID: "abc", Money: 100, CurrentSpaceID: 0, GetOutOfJailCards: 0}) fmt.Println(game.Users) // register routes http.HandleFunc("/health", healthHandler) http.HandleFunc("/api/v1/roll", rollDiceHandler) http.HandleFunc("POST /api/v1/turn", endTurnHandler) + http.HandleFunc("POST /api/v1/exit-jail", exitJailHandler) // listen and serve log.Fatal(http.ListenAndServe(":8080", nil)) @@ -41,3 +42,39 @@ func endTurnHandler(w http.ResponseWriter, req *http.Request) { game.EndTurn() } } + +func exitJailHandler(w http.ResponseWriter, req *http.Request) { + err := req.ParseForm() + if err != nil { + http.Error(w, "Bad Request: Failed to parse form data", http.StatusBadRequest) + return + } + + if !game.ValidateCanExitJail(UUID) { + w.WriteHeader(http.StatusForbidden) + w.Write([]byte(`{"status": "forbidden", "message": "Not your turn or not in jail"}`)) + } + + method := req.PostForm.Get("method") + + switch method { + case "buyout": + err = game.JailBuyout() + if err == game.ErrNotEnoughMoney { + http.Error(w, "error: Insufficient funds", http.StatusUnprocessableEntity) + } + + case "jail_free_card": + err = game.JailUseCard() + if err == game.ErrNotEnoughJailCards { + http.Error(w, "error: Insufficient jail cards", http.StatusUnprocessableEntity) + } + default: + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(`{"status": "bad req", "message": "escape jail method does not exist"}`)) + return + } + + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"status": "success", "message": "Action processed"}`)) +} diff --git a/types/types.go b/types/types.go deleted file mode 100644 index 7e75639..0000000 --- a/types/types.go +++ /dev/null @@ -1,21 +0,0 @@ -package game - -type User struct { - UUID string - Money int32 -} - -type PropertyType int - -const ( - TypeGo PropertyType = iota - TypeColor - TypeChest - TypeTax - TypeRailroad - TypeChance - TypeJail - TypeUtility - TypeParking - TypePolice -)