summaryrefslogtreecommitdiffstats
path: root/game/helpers.go
blob: f3ea4c97c3a7d82e30519cff41063bd91738148d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package game

import (
	"encoding/json"
	"fmt"
	"math/rand/v2"
)

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
		ctx.Turn.InDebt = false
	}

}

func (ctx *Context) IsMortgaged(propID PropertyID) bool {
	for _, oPID := range ctx.Properties.Mortgages {
		if oPID == propID {
			return true
		}
	}
	return false
}

func (ctx *Context) IsOwned(spaceID SpaceID) bool {
	for _, prop := range ctx.Properties.Owners {
		if spaceID == prop.SpaceID {
			if prop.OwnerID == BankPlayerID {
				return false
			} else {
				return true
			}
		}
	}
	panic("Space is not an ownable property")
}

func (ctx *Context) getOwnablePropID(spaceID SpaceID) PropertyID {
	for i, prop := range ctx.Properties.Owners {
		if spaceID == prop.SpaceID {
			return PropertyID{ID: int32(i)}
		}
	}
	panic("Space is not an ownable property")
}

func (ctx *Context) GetCurrentTurnPlayer() *Player {
	return &ctx.Players.Alive[ctx.Turn.Current.Index()]
}

func (ctx *Context) PlayerCanMove(pID PlayerID) bool {
	for i, _ := range ctx.Players.CanMove {
		if i == pID.Index() {
			return true
		}
	}
	return false
}

func initTurn(pID PlayerID, inJail bool) Turn {

	eS := []EventType{EventEndTurn}
	if inJail {
		eS = append(eS, EventJail)
	} else {
		eS = append(eS, EventRollDice)
	}

	return Turn{
		Current:            pID,
		Ended:              false,
		DiceRollsRemaining: StartingDiceRolls,
		NumDiceRolled:      0,
		RolledDoubles:      false,
		MoveQueue:          []int32{},
		EventStack:         eS,
		InDebt:             false,
		Modifier: Modifiers{
			RailroadRentMultiplier:     1,
			UtilityForceRentMultiplier: false,
		},
	}
}

func InitCtx(randSeed rand.Source, players []Player) *Context {
	startingPlayerID := PlayerID{ID: 0}

	playerIDs := []PlayerID{}
	for i := range players {
		playerIDs = append(playerIDs, PlayerID{ID: int32(i)})
	}

	ownableProps := []OwnableProperty{}
	for i, s := range BoardSpaces {
		spaceID := SpaceID{ID: int32(i)}

		propertyType := s.PropertyType
		if propertyType == TypeColor || propertyType == TypeRailroad || propertyType == TypeUtility {
			ownableProps = append(ownableProps, OwnableProperty{
				OwnerID: BankPlayerID,
				SpaceID: spaceID,
			})
		}
	}

	return &Context{
		Random: rand.New(randSeed),
		Players: Players{
			Alive:   players,
			CanMove: playerIDs,
		},
		Turn: initTurn(startingPlayerID, false),
		Visitors: Visitors{
			Unowned:  []UnownedPropertyVisitor{},
			Color:    []OwnedColorVisitor{},
			Railroad: []OwnedRailroadVisitor{},
			Utility:  []OwnedUtilityVisitor{},
			Go:       []PlayerID{},
			Tax:      []TaxVisitor{},
			Chance:   []PlayerID{},
			Chest:    []PlayerID{},
			InJail:   []InJailVisitor{},
			// Parking:  []PlayerID{},
			// Police:   []PlayerID{},
		},
		Properties: Properties{
			Owners:    ownableProps,
			Mortgages: []PropertyID{},
		},
	}
}

func InitPlayer(userUUID string) Player {
	return Player{
		UserUUID:       userUUID,
		Money:          StartingMoney,
		CurrentSpaceID: SpecialSpaces.Go,
		PardonCards:    StartingPardonCards,
	}
}

func (ctx *Context) logGameCtx() {

	type miniContext struct {
		Players    Players
		Turn       Turn
		Visitors   Visitors
		properties Properties
	}

	data, _ := json.MarshalIndent(miniContext{
		Players:    ctx.Players,
		Turn:       ctx.Turn,
		Visitors:   ctx.Visitors,
		properties: ctx.Properties,
	}, "", "  ")
	fmt.Println(string(data))
}

func (ms *MServer) logUsers() {
	fmt.Println("Users:")
	for k, v := range ms.Users {
		fmt.Printf("Key:%s, val:%v\n", k, v)
	}
}

func (ms *MServer) logMsubs() {
	fmt.Println("Subs:")
	for k, v := range ms.Subscribers {
		fmt.Printf("Key:%p, val:%s\n", k, v)
	}
}