This repository was archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.rules.bolt
More file actions
153 lines (123 loc) · 3.38 KB
/
Copy pathdatabase.rules.bolt
File metadata and controls
153 lines (123 loc) · 3.38 KB
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
path / {
read() { isAutomationUser() }
write() { isAutomationUser() }
}
path /users {
read() { isUserManager() }
write() { isUserManager() }
}
path /users/{userid} {
read() { isSignedIn() }
write() { isCurrentUser(userid) }
}
path /users/{userid}/balance/{seasonid} is Number {
validate() {
isAutomationUser() ||
(prior(this) == null && this == 100) || // Initialization
this == prior(this) || // No change, updating comment
(prior(this) != null && this != null && this >= 0) // Not initializing, updating wager value.
}
}
path /users/{userid}/wagers/{wagerid} {
validate() {
!hasAired(getEpisodeFromWager(wagerid))
}
}
path /users/{userid}/wagers/{wagerid}/wager {
validate() {
(!exists(this) && prior(this) == 0) ||
(this >= 0 &&
getBalanceForWager(prior(root), userid, wagerid) + default(prior(this), 0) - this ==
getBalanceForWager(root, userid, wagerid)
)
}
}
path /secure/{userid} {
read() { isCurrentUser(userid) }
write() { isCurrentUser(userid) }
}
path /names/{nameid} {
read() { true }
write() {
isSignedIn() &&
(prior(this) == null ||
(prior(this) != null && this == null && prior(this) == auth.uid))
}
}
path /series {
read() { true }
write() { isDataManager() }
}
path /seasons {
read() { true }
write() { isDataManager() }
}
path /episodes {
read() { true }
write() { isDataManager() }
}
path /events {
read() { true }
}
path /bets {
read() { true }
write() { isDataManager() }
}
path /stats {
read() { true }
}
path /labels/{labelid} {
read() { true }
}
path /leaderboard {
read() { true }
}
path /leaderboard/{uid}/displayName {
update() { isSignedIn() }
}
path /leaderboard/{uid}/anon {
update() { isSignedIn() }
}
// Auth-related functionality
//
isSignedIn() { auth != null }
// Is current user id the logged-in user?
isCurrentUser(uid) { isSignedIn() && auth.uid == uid }
// Is the automation user
isAutomationUser() { auth.uid == 'secret-service-worker' }
// Is the admin user
isAdmin() { isSignedIn() && root.roles.admins.child(auth.uid).val() == true }
// Is allowed to input wagers for other users
isUserManager() { isSignedIn() && root.roles.usermanagers.child(auth.uid).val() == true }
// Is allowed to input wagers for other users
isDataManager() { isSignedIn() && root.roles.datamanagers.child(auth.uid).val() == true }
// Timestamp-related functionality.
//
type Timestamped<T> extends T {
modified: CurrentTimestamp,
created: InitialTimestamp
}
type CurrentTimestamp extends Number {
validate() { this == now }
}
type InitialTimestamp extends Number {
validate() { initial(this, now) }
}
// Helper functions - most are pretty specific to rules use cases.
//
// Returns true if the value is intialized to init, or retains it's prior
// value, otherwise.
initial(value, init) { value == (prior(value) == null ? init : prior(value)) }
// Returns the episode id from wager
getEpisodeFromWager(wagerid) { root.bets[wagerid].episode }
// Get Season id from Episode id
getSeasonFromWager(wagerid) { root.bets[wagerid].season }
// Has this episode already aired?
hasAired(episodeid) { root.episodes[episodeid].air_at <= now }
// Property has a value
exists(item) { item != null }
// Returns the value or a default
default(item, default) { item != null ? item : default }
getBalanceForWager(item, userid, wagerid) {
item.users[userid].balance[getSeasonFromWager(wagerid)]
}