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--
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() {
card := ctx.Random.IntN(len(ChanceCards))
+ ctx.Turn.Log = append(ctx.Turn.Log, LogLandChance)
+
currentPos := ctx.Players.Alive[visitorID.Index()].CurrentSpaceID
switch card {
}
card := ctx.Random.IntN(len(ChanceCards))
+ ctx.Turn.Log = append(ctx.Turn.Log, LogLandChest)
+
currentPos := ctx.Players.Alive[visitorID.Index()].CurrentSpaceID
switch card {
package game
+import "fmt"
+
func (ctx *Context) HasColorMonopoly(playerID PlayerID, targetGroup ColorGroup) bool {
var ownedCount int32
for _, prop := range ctx.Properties.Owners {
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) {
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 (
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()
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() {
ms.gameCtx.ProcessMovement()
ms.gameCtx.logGameCtx()
+ ms.broadcastLog()
}
func (ms *MServer) buy() {
ms.gameCtx.Buy()
ms.gameCtx.logGameCtx()
+ ms.broadcastLog()
}
func (ctx *Context) ProcessGo() {
for _, playerID := range ctx.Visitors.Go {
+ ctx.Turn.Log = append(ctx.Turn.Log, LogLandGo)
+
ctx.AdjustPlayerMoney(playerID, GoSalary)
}
}
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
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)
}
}
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)
}
}
}
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)
package game
+import "fmt"
+
func (ctx *Context) numRailroadOwned(playerID PlayerID) int32 {
var ownedCount int32 = 0
for _, prop := range ctx.Properties.Owners {
}
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)]
package game
+import "fmt"
+
func (ctx *Context) ProcessTax() {
for {
tV, done := QueuePop(&ctx.Visitors.Tax)
}
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)
}
}
** 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
EventStack []EventType
InDebt bool
Modifier Modifiers
+ Log []string
}
type Properties struct {
package game
+import "fmt"
+
func (ctx *Context) NumUtilities(playerID PlayerID) int32 {
var ownedCount int32 = 0
for _, prop := range ctx.Properties.Owners {
}
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 {
console.error('unexpected message type', typeof ev.data)
return
}
- appendGameLog(ev.data)
+
+ if (ev.data === "START") {
+ appendGameLog("The game has started!")
+ }
})
}
async function loggedIn() {