ctx.AdjustPlayerMoney(visitorID, -repairCost)
case 14:
for i := range ctx.Players.Alive {
- pID := PlayerID{id: int32(i)}
+ pID := PlayerID{ID: int32(i)}
if pID == visitorID {
ctx.AdjustPlayerMoney(pID, -50*int32(len(ctx.Players.Alive)-1))
} else {
ctx.AdjustPlayerMoney(visitorID, 20)
case 7:
for i := range ctx.Players.Alive {
- pID := PlayerID{id: int32(i)}
+ pID := PlayerID{ID: int32(i)}
if pID == visitorID {
ctx.AdjustPlayerMoney(pID, 10*int32(len(ctx.Players.Alive)-1))
} else {
func (ctx *Context) ProcessOwnedColors() {
for _, oCV := range ctx.Visitors.Color {
- visitorID := oCV.visitorID
- ownerID := oCV.ownerID
- colorID := oCV.colorID
+ visitorID := oCV.VisitorID
+ ownerID := oCV.OwnerID
+ colorID := oCV.ColorID
prop := ColorProperties[colorID]
prices := ColorPropertyRents[colorID]
{Name: "Luxury Tax", Amount: 100},
}
-var BankPlayerID PlayerID = PlayerID{id: -1}
+var BankPlayerID PlayerID = PlayerID{ID: -1}
var SpecialSpaces ChanceSpaceIDs
func init() {
for i, s := range BoardSpaces {
- spaceID := SpaceID{id: int32(i)}
+ spaceID := SpaceID{ID: int32(i)}
propertyType := s.PropertyType
import (
"context"
+ "encoding/json"
"errors"
"fmt"
"github.com/coder/websocket"
randSeed *rand.PCG
}
+func (ctx *Context) logGameCtx() {
+ data, _ := json.MarshalIndent(ctx, "", " ")
+ fmt.Println(string(data))
+}
+
func NewMonopolyServer() *MonopolyServer {
ms := &MonopolyServer{
subscriberMessageBuffer: 16,
}
func (ms *MonopolyServer) roll() {
- fmt.Printf("%#v\n", ms.gameCtx)
+ ms.gameCtx.logGameCtx()
ms.gameCtx.RollDice()
- fmt.Printf("%#v\n", ms.gameCtx)
+
+ ms.gameCtx.logGameCtx()
ms.gameCtx.ProcessMovement()
+
+ ms.gameCtx.logGameCtx()
}
func (ms *MonopolyServer) subscribe(w http.ResponseWriter, r *http.Request) error {
}
func InitCtx(randSeed rand.Source, players []Player) *Context {
- startingPlayerID := PlayerID{id: 0}
+ startingPlayerID := PlayerID{ID: 0}
playerIDs := []PlayerID{}
for i := range players {
- playerIDs = append(playerIDs, PlayerID{id: int32(i)})
+ playerIDs = append(playerIDs, PlayerID{ID: int32(i)})
}
ownableProps := []OwnableProperty{}
for i, s := range BoardSpaces {
- spaceID := SpaceID{id: int32(i)}
+ spaceID := SpaceID{ID: int32(i)}
propertyType := s.PropertyType
if propertyType == TypeColor || propertyType == TypeRailroad || propertyType == TypeUtility {
func InitPlayer(userUUID string) Player {
return Player{
- userUUID: userUUID,
+ UserUUID: userUUID,
Money: StartingMoney,
CurrentSpaceID: SpecialSpaces.Go,
GetOutOfJailCards: StartingGetOutOfJailFreeCards,
}
func (ctx *Context) ValidateIsTurn(userUUID string) bool {
- if ctx.GetCurrentTurnPlayer().userUUID == userUUID {
+ if ctx.GetCurrentTurnPlayer().UserUUID == userUUID {
return true
}
return false
}
func (ctx *Context) ValidateCanRoll(userUUID string) bool {
- if ctx.ValidateIsTurn(userUUID) && ctx.Turn.DiceRollsRemaining > 0 {
+ if ctx.ValidateIsTurn(userUUID) && ctx.EventPeek() == EventRollDice {
return true
}
return false
}
func (ctx *Context) ValidateCanEndTurn(userUUID string) bool {
- if !(ctx.ValidateIsTurn(userUUID) || ctx.Turn.DiceRollsRemaining > 0 || ctx.GetCurrentTurnPlayer().Money < 0) {
+ if ctx.ValidateIsTurn(userUUID) || ctx.EventPeek() == EventEndTurn || ctx.GetCurrentTurnPlayer().Money >= 0 {
return true
}
return false
func (ctx *Context) ValidateCanExitJail(userUUD string) bool {
for _, iJV := range ctx.Visitors.InJail {
- if ctx.Players.Alive[iJV.visitorID.Index()].userUUID == userUUD {
+ if ctx.Players.Alive[iJV.VisitorID.Index()].UserUUID == userUUD {
return true
}
}
}
func (ctx *Context) RollDice() {
+ ctx.EventPop()
+
// Roll Dice
diceRoll1 := ctx.Random.Int32N(6) + 1
diceRoll2 := ctx.Random.Int32N(6) + 1
ctx.PutPlayerInJail(ctx.Turn.Current)
}
+ ctx.Turn.MoveQueue = append(ctx.Turn.MoveQueue, diceRoll1+diceRoll2)
}
func (ctx *Context) EndTurn() {
- nextTurnPlayerID := PlayerID{id: (ctx.Turn.Current.id + 1) % int32(len(ctx.Players.Alive))}
+ nextTurnPlayerID := PlayerID{ID: (ctx.Turn.Current.ID + 1) % int32(len(ctx.Players.Alive))}
ctx.Turn = initTurn(nextTurnPlayerID, !ctx.PlayerCanMove(nextTurnPlayerID))
// TODO: send options list to the next player
}
func (ctx *Context) getPropID(spaceID SpaceID) PropertyID {
for i, prop := range ctx.Properties.Owners {
if spaceID == prop.SpaceID {
- return PropertyID{id: int32(i)}
+ return PropertyID{ID: int32(i)}
}
}
panic("Space is not an ownable property")
func (ctx *Context) RemovePlayerFromJail(playerID PlayerID) {
for i, iJV := range ctx.Visitors.InJail {
- if playerID == iJV.visitorID {
+ 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.Players.CanMove[i] = ctx.Players.CanMove[len(ctx.Players.CanMove)-1]
ctx.Players.CanMove = ctx.Players.CanMove[:len(ctx.Players.CanMove)-1]
ctx.Turn.EventStack = ctx.Turn.EventStack[0:1]
- ctx.Visitors.InJail = append(ctx.Visitors.InJail, InJailVisitor{visitorID: pID, TurnsLeft: JailDefaultTurns})
+ ctx.Visitors.InJail = append(ctx.Visitors.InJail, InJailVisitor{VisitorID: pID, TurnsLeft: JailDefaultTurns})
ctx.Turn.DiceRollsRemaining = 0
}
}
func (ctx *Context) ProcessJail() {
for _, iJV := range ctx.Visitors.InJail {
- visitorID := iJV.visitorID
+ visitorID := iJV.VisitorID
turnsLeft := iJV.TurnsLeft
if turnsLeft <= 0 {
package game
func GetPlayerMoveDistance(start SpaceID, dest SpaceID) int32 {
- distance := dest.id - start.id
+ distance := dest.ID - start.ID
if distance < 0 {
distance += int32(len(BoardSpaces))
}
func (ctx *Context) ProcessMovement() {
cID := ctx.Turn.Current
- if ctx.PlayerCanMove(cID) {
- dist := ctx.Turn.MoveQueue[0]
- ctx.Turn.MoveQueue = ctx.Turn.MoveQueue[1:]
- ctx.AdvancePlayer(cID, ctx.Players.Alive[cID.Index()].CurrentSpaceID, dist)
+ dist := ctx.Turn.MoveQueue[0]
+ ctx.Turn.MoveQueue = ctx.Turn.MoveQueue[1:]
+ ctx.AdvancePlayer(cID, ctx.Players.Alive[cID.Index()].CurrentSpaceID, dist)
- ctx.ProcessLanding()
- }
+ ctx.ProcessLanding()
}
func CalculateNextPos(currentPosition SpaceID, distance int32) SpaceID {
nextPos := Add(currentPosition, distance)
- nextPos.id %= int32(len(BoardSpaces))
+ nextPos.ID %= int32(len(BoardSpaces))
return nextPos
}
func (ctx *Context) AdvancePlayer(playerID PlayerID, currentPosition SpaceID, diceRoll int32) {
nextPos := CalculateNextPos(currentPosition, diceRoll)
+ ctx.Players.Alive[playerID.Index()].CurrentSpaceID = nextPos
- numGoPasses := Add(currentPosition, diceRoll).id / int32(len(BoardSpaces))
+ numGoPasses := Add(currentPosition, diceRoll).ID / int32(len(BoardSpaces))
if numGoPasses > 0 {
if BoardSpaces[nextPos.Index()].PropertyType == TypeGo {
case TypeChance:
ctx.Visitors.Chance = append(ctx.Visitors.Chance, playerID)
case TypeTax:
- ctx.Visitors.Tax = append(ctx.Visitors.Tax, TaxVisitor{visitorID: playerID, taxID: prop.SubIndexID})
+ ctx.Visitors.Tax = append(ctx.Visitors.Tax, TaxVisitor{VisitorID: playerID, TaxID: prop.SubIndexID})
case TypePolice: // hardcoding to send straight to jail
ctx.PutPlayerInJail(playerID)
case TypeColor:
ownerID := ctx.Properties.Owners[propID.Index()].OwnerID
if ownerID != BankPlayerID { // property owned?
if ownerID != playerID && !ctx.IsMortgaged(propID) { // not by you
- ctx.Visitors.Color = append(ctx.Visitors.Color, OwnedColorVisitor{visitorID: playerID, ownerID: ownerID, colorID: prop.SubIndexID})
+ ctx.Visitors.Color = append(ctx.Visitors.Color, OwnedColorVisitor{VisitorID: playerID, OwnerID: ownerID, ColorID: prop.SubIndexID})
}
} else {
- ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{visitorID: playerID, propertyID: propID})
+ ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{VisitorID: playerID, PropertyID: propID})
}
case TypeRailroad:
propID := ctx.getPropID(nextPos)
ownerID := ctx.Properties.Owners[propID.Index()].OwnerID
if ownerID != BankPlayerID { // property owned?
if ownerID != playerID && !ctx.IsMortgaged(propID) { // not by you
- ctx.Visitors.Railroad = append(ctx.Visitors.Railroad, OwnedRailroadVisitor{visitorID: playerID, ownerID: ownerID, railroadID: prop.SubIndexID})
+ ctx.Visitors.Railroad = append(ctx.Visitors.Railroad, OwnedRailroadVisitor{VisitorID: playerID, OwnerID: ownerID, RailroadID: prop.SubIndexID})
}
} else {
- ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{visitorID: playerID, propertyID: propID})
+ ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{VisitorID: playerID, PropertyID: propID})
}
case TypeUtility:
propID := ctx.getPropID(nextPos)
ownerID := ctx.Properties.Owners[propID.Index()].OwnerID
if ownerID != BankPlayerID { // property owned?
if ownerID != playerID && !ctx.IsMortgaged(propID) { // not by you
- ctx.Visitors.Utility = append(ctx.Visitors.Utility, OwnedUtilityVisitor{visitorID: playerID, ownerID: ownerID, utilityID: prop.SubIndexID, diceRoll: diceRoll})
+ ctx.Visitors.Utility = append(ctx.Visitors.Utility, OwnedUtilityVisitor{VisitorID: playerID, OwnerID: ownerID, UtilityID: prop.SubIndexID, DiceRoll: diceRoll})
}
} else {
- ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{visitorID: playerID, propertyID: propID})
+ ctx.Visitors.Unowned = append(ctx.Visitors.Unowned, UnownedPropertyVisitor{VisitorID: playerID, PropertyID: propID})
}
}
}
func (ctx *Context) ProcessOwnedRailroad() {
for _, oRV := range ctx.Visitors.Railroad {
- visitorID := oRV.visitorID
- ownerID := oRV.ownerID
+ visitorID := oRV.VisitorID
+ ownerID := oRV.OwnerID
// railroadID := oRV.railroadID
var rent int32 = RailroadRent[ctx.numRailroadOwned(ownerID)]
func (ctx *Context) ProcessTax() {
for _, tV := range ctx.Visitors.Tax {
- playerID := tV.visitorID
- taxID := tV.taxID
+ playerID := tV.VisitorID
+ taxID := tV.TaxID
ctx.AdjustPlayerMoney(playerID, -TaxSpaces[taxID].Amount)
}
}
)
type Player struct {
- userUUID string
+ UserUUID string
Money int32
CurrentSpaceID SpaceID
GetOutOfJailCards int32
}
type OwnedColorVisitor struct {
- visitorID PlayerID
- ownerID PlayerID
- colorID int32
+ VisitorID PlayerID
+ OwnerID PlayerID
+ ColorID int32
}
type OwnedRailroadVisitor struct {
- visitorID PlayerID
- ownerID PlayerID
- railroadID int32
+ VisitorID PlayerID
+ OwnerID PlayerID
+ RailroadID int32
}
type OwnedUtilityVisitor struct {
- visitorID PlayerID
- ownerID PlayerID
- utilityID int32
- diceRoll int32
+ VisitorID PlayerID
+ OwnerID PlayerID
+ UtilityID int32
+ DiceRoll int32
}
type UnownedPropertyVisitor struct {
- visitorID PlayerID
- propertyID PropertyID
+ VisitorID PlayerID
+ PropertyID PropertyID
}
type InJailVisitor struct {
- visitorID PlayerID
+ VisitorID PlayerID
TurnsLeft int32
}
type TaxVisitor struct {
- visitorID PlayerID
- taxID int32
+ VisitorID PlayerID
+ TaxID int32
}
type Visitors struct { // SubIndexID is the PK for each resp table
// IDs
type PlayerID struct {
- id int32
+ ID int32
}
type PropertyID struct {
- id int32
+ ID int32
}
type SpaceID struct {
- id int32
+ ID int32
}
func (p *PlayerID) Index() int {
- return int(p.id)
+ return int(p.ID)
}
func (p *PropertyID) Index() int {
- return int(p.id)
+ return int(p.ID)
}
func (s *SpaceID) Index() int {
- return int(s.id)
+ return int(s.ID)
}
func Add(a SpaceID, b int32) SpaceID {
- return SpaceID{id: a.id + b}
+ return SpaceID{ID: a.ID + b}
}
// Game Context (Ctx)
func (ctx *Context) ProcessOwnedUtility() {
for _, oUV := range ctx.Visitors.Utility {
- visitorID := oUV.visitorID
- ownerID := oUV.ownerID
+ visitorID := oUV.VisitorID
+ ownerID := oUV.OwnerID
// utilityID := oUV.utilityID
- diceRoll := oUV.diceRoll
+ diceRoll := oUV.DiceRoll
var rent int32 = 0