From: Skullheadx Date: Mon, 8 Jun 2026 23:20:10 +0000 (-0400) Subject: buy properties X-Git-Url: http://git.skullheadx.com/index.css?a=commitdiff_plain;h=3b7345847ba6c40007b84aa926415f70d90e1af6;p=monopoly-web.git buy properties --- diff --git a/game/game.go b/game/game.go index 3fbf01b..862d00b 100644 --- a/game/game.go +++ b/game/game.go @@ -70,6 +70,8 @@ func NewMonopolyServer() *MonopolyServer { ms.serveMux.HandleFunc("/subscribe", ms.subscribeHandler) ms.serveMux.HandleFunc("/start", ms.startHandler) ms.serveMux.HandleFunc("/roll", ms.rollHandler) + ms.serveMux.HandleFunc("/buy", ms.buyHandler) + // ms.serveMux.HandleFunc("/auction", ms.auctionHandler) return ms } @@ -180,11 +182,59 @@ func (ms *MonopolyServer) startHandler(w http.ResponseWriter, r *http.Request) { return } + if ms.gameCtx != nil { + http.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict) + return + } + ms.start() w.WriteHeader(http.StatusAccepted) } +func (ms *MonopolyServer) buyHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return + } + + cookie, err := r.Cookie("user") + if err != nil { + if err == http.ErrNoCookie { + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + userUUID := cookie.Value + + _, ok := ms.users[userUUID] + if !ok { + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + + if ms.gameCtx == nil { + http.Error(w, "Game has not started yet", http.StatusConflict) + return + } + if !ms.gameCtx.ValidateCanBuy(userUUID) { + http.Error(w, "Not your turn", http.StatusForbidden) + return + } + ms.buy() + w.WriteHeader(http.StatusOK) +} + +func (ms *MonopolyServer) buy() { + ms.gameCtx.Buy() + + ms.gameCtx.logGameCtx() +} + func (ms *MonopolyServer) rollHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) @@ -454,6 +504,16 @@ func (ctx *Context) ValidateIsTurn(userUUID string) bool { return false } +func (ctx *Context) ValidateCanBuy(userUUID string) bool { + player := ctx.Players.Alive[ctx.Turn.Current.Index()] + prop := ColorProperties[BoardSpaces[player.CurrentSpaceID.Index()].SubIndexID] + + if ctx.ValidateIsTurn(userUUID) && ctx.EventPeek() == EventLandUnowned && player.Money >= prop.Price { + return true + } + return false +} + func (ctx *Context) ValidateCanRoll(userUUID string) bool { if ctx.ValidateIsTurn(userUUID) && (ctx.EventPeek() == EventRollDice || ctx.EventPeek() == EventJail) { return true @@ -523,6 +583,22 @@ func (ctx *Context) RollDice() { ctx.Turn.MoveQueue = append(ctx.Turn.MoveQueue, diceRoll1+diceRoll2) } +func (ctx *Context) Buy() { + ctx.EventPop() + + playerID := ctx.Turn.Current + player := ctx.Players.Alive[playerID.Index()] + prop := ColorProperties[BoardSpaces[player.CurrentSpaceID.Index()].SubIndexID] + + for i, p := range ctx.Properties.Owners { + if p.SpaceID == player.CurrentSpaceID { + ctx.Properties.Owners[i].OwnerID = playerID + } + } + + ctx.AdjustPlayerMoney(playerID, -prop.Price) +} + func (ctx *Context) EndTurn() { ctx.EventPop() nextTurnPlayerID := PlayerID{ID: (ctx.Turn.Current.ID + 1) % int32(len(ctx.Players.Alive))} diff --git a/public/index.html b/public/index.html index 3dc8e76..b7d2579 100644 --- a/public/index.html +++ b/public/index.html @@ -20,6 +20,7 @@ + diff --git a/public/index.js b/public/index.js index e965ea7..83c1844 100644 --- a/public/index.js +++ b/public/index.js @@ -29,6 +29,11 @@ const resp = await fetch('/loggedin', { method: 'GET' }) + if (resp.status === 404) { + console.warn("User not logged in."); + return; + } + if (resp.status !== 200) { throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`) } @@ -44,6 +49,7 @@ const loginInput = document.getElementById('login-input') const startButton = document.getElementById('start') const rollButton = document.getElementById('roll') + const buyButton = document.getElementById('buy') function appendGameLog(text) { const p = document.createElement('p') @@ -104,4 +110,17 @@ } }) + buyButton.addEventListener('click', async () => { + try { + const resp = await fetch('/buy', { + method: 'POST', + }) + if (resp.status !== 200) { + throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText} ${resp.message}`) + } + } catch (err) { + console.error(`Start failed: ${err.message}`) + } + }) + })()