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
}
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)
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
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))}
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}`)
}
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')
}
})
+ 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}`)
+ }
+ })
+
})()