From: Skullheadx Date: Tue, 9 Jun 2026 02:09:55 +0000 (-0400) Subject: some logging X-Git-Url: http://git.skullheadx.com/projects/blog/openbsd_html_css/static/gitweb.css?a=commitdiff_plain;p=monopoly-web.git some logging --- diff --git a/game/actions.go b/game/actions.go index bd7c2f1..58e2232 100644 --- a/game/actions.go +++ b/game/actions.go @@ -1,11 +1,16 @@ package game +import "fmt" + func (ctx *Context) RollDice() { ctx.EventPop() // Roll Dice diceRoll1 := ctx.Random.Int32N(6) + 1 diceRoll2 := ctx.Random.Int32N(6) + 1 + total := diceRoll1 + diceRoll2 + + ctx.Turn.Log = append(ctx.Turn.Log, fmt.Sprintf(LogRolled, total)) ctx.Turn.NumDiceRolled++ ctx.Turn.DiceRollsRemaining-- @@ -23,7 +28,7 @@ func (ctx *Context) RollDice() { ctx.PutPlayerInJail(ctx.Turn.Current) } - ctx.Turn.MoveQueue = append(ctx.Turn.MoveQueue, diceRoll1+diceRoll2) + ctx.Turn.MoveQueue = append(ctx.Turn.MoveQueue, total) } func (ctx *Context) Buy() { diff --git a/game/chance.go b/game/chance.go index 3baa945..d155329 100644 --- a/game/chance.go +++ b/game/chance.go @@ -18,6 +18,8 @@ func (ctx *Context) ProcessChance() { card := ctx.Random.IntN(len(ChanceCards)) + ctx.Turn.Log = append(ctx.Turn.Log, LogLandChance) + currentPos := ctx.Players.Alive[visitorID.Index()].CurrentSpaceID switch card { diff --git a/game/chest.go b/game/chest.go index 9dbfb89..58f16ff 100644 --- a/game/chest.go +++ b/game/chest.go @@ -8,6 +8,8 @@ func (ctx *Context) ProcessChest() { } card := ctx.Random.IntN(len(ChanceCards)) + ctx.Turn.Log = append(ctx.Turn.Log, LogLandChest) + currentPos := ctx.Players.Alive[visitorID.Index()].CurrentSpaceID switch card { diff --git a/game/color.go b/game/color.go index e1826c9..7cd7bf7 100644 --- a/game/color.go +++ b/game/color.go @@ -1,5 +1,7 @@ package game +import "fmt" + func (ctx *Context) HasColorMonopoly(playerID PlayerID, targetGroup ColorGroup) bool { var ownedCount int32 for _, prop := range ctx.Properties.Owners { @@ -43,6 +45,8 @@ func (ctx *Context) ProcessOwnedColors() { prop := ColorProperties[colorID] prices := ColorPropertyRents[colorID] + ctx.Turn.Log = append(ctx.Turn.Log, fmt.Sprintf(LogLandColor, prop.Name)) + var rent int32 = prices[prop.Houses] if prop.Houses == 0 && ctx.HasColorMonopoly(ownerID, prop.GroupID) { diff --git a/game/config.go b/game/config.go index 9e309ce..8a43d54 100644 --- a/game/config.go +++ b/game/config.go @@ -1,8 +1,30 @@ package game const ( - MSG_START uint8 = iota - MSG_END + MsgStart = "START" + MsgEnd = "END" // for turns + MsgFinish = "FINISH" +) + +const ( + LogRolled = " rolled %v." + LogRemoveJail = " escaped Jail." + LogPutJail = " was put in Jail." + + LogLandGo = " landed on GO." + LogLandColor = " landed on %v." + LogLandUtility = " landed on %v." + LogLandRailroad = " landed on %v." + LogLandChance = " landed on Chance." + LogLandChest = " landed on Community Chest." + LogLandParking = " landed on Free Parking." + LogLandJail = " is just visiting Jail." + LogLandPolice = " was told by the police to go to Jail." + LogLandTax = " landed on %v." + + LogMoneyGain = " earned %v." + LogMoneyLose = " lost %v." + LogMoneyBankrupt = " is bankrupt." ) const ( diff --git a/game/game.go b/game/game.go index d800363..4a20f61 100644 --- a/game/game.go +++ b/game/game.go @@ -18,6 +18,26 @@ func (ms *MServer) deleteSubscriber(s *MSub) { ms.subscribersMu.Unlock() } +func (ms *MServer) broadcastMsg(msg string) { + for s := range ms.Subscribers { + select { + case s.Msgs <- []byte(msg): + default: + go s.CloseSlow() + } + } +} + +func (ms *MServer) broadcastLog() { + for { + l, done := QueuePop(&ms.gameCtx.Turn.Log) + if done { + break + } + ms.broadcastMsg(l) + } +} + func (ms *MServer) start() { ms.subscribersMu.Lock() defer ms.subscribersMu.Unlock() @@ -34,15 +54,7 @@ func (ms *MServer) start() { ms.gameCtx.logGameCtx() ms.gameCtxMu.Unlock() - msg := []byte{MSG_START} - for s := range ms.Subscribers { - select { - case s.Msgs <- msg: - default: - go s.CloseSlow() - } - } - + ms.broadcastMsg(MsgStart) } func (ms *MServer) roll() { @@ -50,10 +62,12 @@ func (ms *MServer) roll() { ms.gameCtx.ProcessMovement() ms.gameCtx.logGameCtx() + ms.broadcastLog() } func (ms *MServer) buy() { ms.gameCtx.Buy() ms.gameCtx.logGameCtx() + ms.broadcastLog() } diff --git a/game/go.go b/game/go.go index 21d8339..656dbdb 100644 --- a/game/go.go +++ b/game/go.go @@ -2,6 +2,8 @@ package game func (ctx *Context) ProcessGo() { for _, playerID := range ctx.Visitors.Go { + ctx.Turn.Log = append(ctx.Turn.Log, LogLandGo) + ctx.AdjustPlayerMoney(playerID, GoSalary) } } diff --git a/game/helpers.go b/game/helpers.go index 9381bba..f3ea4c9 100644 --- a/game/helpers.go +++ b/game/helpers.go @@ -9,6 +9,12 @@ import ( func (ctx *Context) AdjustPlayerMoney(playerID PlayerID, amount int32) { ctx.Players.Alive[playerID.Index()].Money += amount + if amount >= 0 { + ctx.Turn.Log = append(ctx.Turn.Log, fmt.Sprintf(LogMoneyGain, amount)) + } else { + ctx.Turn.Log = append(ctx.Turn.Log, fmt.Sprintf(LogMoneyLose, -amount)) + } + if ctx.Players.Alive[playerID.Index()].Money < 0 { ctx.Turn.InDebt = true } else { // Money >= 0 diff --git a/game/jail.go b/game/jail.go index a88674f..1163df9 100644 --- a/game/jail.go +++ b/game/jail.go @@ -5,6 +5,8 @@ func (ctx *Context) RemovePlayerFromJail(playerID PlayerID) { if playerID == iJV.VisitorID { ctx.Visitors.InJail[i] = ctx.Visitors.InJail[len(ctx.Visitors.InJail)-1] ctx.Visitors.InJail = ctx.Visitors.InJail[:len(ctx.Visitors.InJail)-1] + + ctx.Turn.Log = append(ctx.Turn.Log, LogRemoveJail) } } @@ -22,6 +24,7 @@ func (ctx *Context) PutPlayerInJail(pID PlayerID) { ctx.Turn.EventStack = ctx.Turn.EventStack[0:1] ctx.Visitors.InJail = append(ctx.Visitors.InJail, InJailVisitor{VisitorID: pID, TurnsLeft: JailDefaultTurns}) ctx.Turn.DiceRollsRemaining = 0 + ctx.Turn.Log = append(ctx.Turn.Log, LogPutJail) } } } diff --git a/game/movement.go b/game/movement.go index 0b165aa..8adb4e5 100644 --- a/game/movement.go +++ b/game/movement.go @@ -73,6 +73,7 @@ func (ctx *Context) AdvancePlayer(playerID PlayerID, currentPosition SpaceID, di case TypeTax: ctx.Visitors.Tax = append(ctx.Visitors.Tax, TaxVisitor{VisitorID: playerID, TaxID: prop.SubIndexID}) case TypePolice: // hardcoding to send straight to jail + ctx.Turn.Log = append(ctx.Turn.Log, LogLandPolice) ctx.PutPlayerInJail(playerID) case TypeColor: propID := ctx.getOwnablePropID(nextPos) diff --git a/game/railroad.go b/game/railroad.go index fc1c99b..1da7070 100644 --- a/game/railroad.go +++ b/game/railroad.go @@ -1,5 +1,7 @@ package game +import "fmt" + func (ctx *Context) numRailroadOwned(playerID PlayerID) int32 { var ownedCount int32 = 0 for _, prop := range ctx.Properties.Owners { @@ -32,7 +34,9 @@ func (ctx *Context) ProcessOwnedRailroad() { } visitorID := oRV.VisitorID ownerID := oRV.OwnerID - // railroadID := oRV.railroadID + railroadID := oRV.RailroadID + + ctx.Turn.Log = append(ctx.Turn.Log, fmt.Sprintf(LogLandRailroad, RailroadProperties[railroadID].Name)) var rent int32 = RailroadRent[ctx.numRailroadOwned(ownerID)] diff --git a/game/tax.go b/game/tax.go index a6ad530..9e89d11 100644 --- a/game/tax.go +++ b/game/tax.go @@ -1,5 +1,7 @@ package game +import "fmt" + func (ctx *Context) ProcessTax() { for { tV, done := QueuePop(&ctx.Visitors.Tax) @@ -8,6 +10,9 @@ func (ctx *Context) ProcessTax() { } playerID := tV.VisitorID taxID := tV.TaxID + + ctx.Turn.Log = append(ctx.Turn.Log, fmt.Sprintf(LogLandTax, TaxSpaces[taxID].Name)) + ctx.AdjustPlayerMoney(playerID, -TaxSpaces[taxID].Amount) } } diff --git a/game/todo b/game/todo index f679787..489e557 100644 --- a/game/todo +++ b/game/todo @@ -14,7 +14,7 @@ ** DONE buy prop ** TODO auction -* login multiple times makes new websocket +* DONE login multiple times makes new websocket * DONE Better errors for validators * TODO Response for frontend game log ** TODO start diff --git a/game/types.go b/game/types.go index 980ce3c..c3da541 100644 --- a/game/types.go +++ b/game/types.go @@ -178,6 +178,7 @@ type Turn struct { EventStack []EventType InDebt bool Modifier Modifiers + Log []string } type Properties struct { diff --git a/game/utility.go b/game/utility.go index 301e793..1b50553 100644 --- a/game/utility.go +++ b/game/utility.go @@ -1,5 +1,7 @@ package game +import "fmt" + func (ctx *Context) NumUtilities(playerID PlayerID) int32 { var ownedCount int32 = 0 for _, prop := range ctx.Properties.Owners { @@ -32,9 +34,11 @@ func (ctx *Context) ProcessOwnedUtility() { } visitorID := oUV.VisitorID ownerID := oUV.OwnerID - // utilityID := oUV.utilityID + utilityID := oUV.UtilityID diceRoll := oUV.DiceRoll + ctx.Turn.Log = append(ctx.Turn.Log, fmt.Sprintf(LogLandUtility, UtilityProperties[utilityID].Name)) + var rent int32 = 0 if !ctx.Turn.Modifier.UtilityForceRentMultiplier { diff --git a/public/index.js b/public/index.js index 22280f4..8732a09 100644 --- a/public/index.js +++ b/public/index.js @@ -25,7 +25,10 @@ console.error('unexpected message type', typeof ev.data) return } - appendGameLog(ev.data) + + if (ev.data === "START") { + appendGameLog("The game has started!") + } }) } async function loggedIn() {