/
actions_scrub.go
124 lines (103 loc) · 3.51 KB
/
actions_scrub.go
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
package main
// CheckScrub removes some information from the action to prevent players having more knowledge
// than they should have, if necessary (e.g. when a card is drawn to a player's hand)
func CheckScrub(t *Table, action interface{}, userID int) interface{} {
cardIdentityAction, ok := action.(ActionCardIdentity)
if ok && cardIdentityAction.Type == "cardIdentity" {
cardIdentityAction.Scrub(t, userID)
return cardIdentityAction
}
discardAction, ok := action.(ActionDiscard)
if ok && discardAction.Type == "discard" {
discardAction.Scrub(t, userID)
return discardAction
}
drawAction, ok := action.(ActionDraw)
if ok && drawAction.Type == "draw" {
drawAction.Scrub(t, userID)
return drawAction
}
playAction, ok := action.(ActionPlay)
if ok && playAction.Type == "play" {
playAction.Scrub(t, userID)
return playAction
}
return action
}
// Scrub removes some information from a draw so that we do not reveal the identity of drawn
// cards to the players drawing those cards
func (a *ActionDraw) Scrub(t *Table, userID int) {
// Local variables
g := t.Game
p := getEquivalentPlayer(t, userID)
if p == nil {
// Spectators get to see the identities of all drawn cards
return
}
if a.PlayerIndex == p.Index || // They are drawing the card
// They are playing a special character that should not be able to see the card
characterHideCard(a, g, p) {
a.Rank = -1
a.SuitIndex = -1
}
}
// Scrub removes some information from played cards so that we do not reveal the identity of played
// cards to anybody (in some specific variants)
func (a *ActionPlay) Scrub(t *Table, userID int) {
// Local variables
p := getEquivalentPlayer(t, userID)
variant := variants[t.Options.VariantName]
if p == nil {
// Spectators get to see the identities of played cards
return
}
if variant.IsThrowItInAHole() {
a.Rank = -1
a.SuitIndex = -1
}
}
// Scrub removes some information from discarded cards so that we do not reveal the identity of
// discarded cards to anybody (in some specific variants)
func (a *ActionDiscard) Scrub(t *Table, userID int) {
// Local variables
p := getEquivalentPlayer(t, userID)
variant := variants[t.Options.VariantName]
if p == nil {
// Spectators get to see the identities of discarded cards
return
}
if variant.IsThrowItInAHole() && a.Failed {
// For the purposes of hiding information, failed discards are equivalent to plays
a.Rank = -1
a.SuitIndex = -1
}
}
// Scrub removes some information from a card identity action so that we do not reveal the identity
// of sliding cards to the players who are holding those cards
func (a *ActionCardIdentity) Scrub(t *Table, userID int) {
// Local variables
p := getEquivalentPlayer(t, userID)
if p == nil {
// Spectators get to see the identities of all cards
return
}
if a.PlayerIndex == p.Index { // They are holding the card
a.Rank = -1
a.SuitIndex = -1
}
}
func getEquivalentPlayer(t *Table, userID int) *GamePlayer {
// Local variables
g := t.Game
playerIndex := t.GetPlayerIndexFromID(userID)
spectatorIndex := t.GetSpectatorIndexFromID(userID)
if playerIndex > -1 {
// The action is going to be sent to one of the active players
return g.Players[playerIndex]
} else if spectatorIndex > -1 && t.Spectators[spectatorIndex].ShadowingPlayerIndex != -1 {
// The action is going to be sent to a spectator that is shadowing one of the active players
return g.Players[t.Spectators[spectatorIndex].ShadowingPlayerIndex]
}
// The action is going to be sent to a spectator that can see every hand
return nil
}