-
Notifications
You must be signed in to change notification settings - Fork 3
/
activity_code.js
125 lines (111 loc) · 2.9 KB
/
activity_code.js
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
const events = require('./events')
class ActivityCode {
constructor(eventId, roundNumber, groupNumber, attemptNumber) {
this.eventId = eventId
this.roundNumber = roundNumber
this.groupNumber = groupNumber
this.attemptNumber = attemptNumber
}
round(roundNumber) {
return new ActivityCode(this.eventId, roundNumber, this.groupNumber, this.attemptNumber)
}
group(groupNumber) {
return new ActivityCode(this.eventId, this.roundNumber, groupNumber, this.attemptNumber)
}
attempt(attemptNumber) {
return new ActivityCode(this.eventId, this.roundNumber, this.groupNumber, attemptNumber)
}
toString() {
var out = [events.idToName[this.eventId]]
if (this.roundNumber) {
out.push('Round ' + this.roundNumber)
}
if (this.groupNumber || this.groupNumber === 0) {
out.push('Group ' + this.groupNumber)
}
if (this.attemptNumber) {
out.push('Attempt ' + this.attemptNumber)
}
return out.join(' ')
}
toStringShort() {
if (this.attemptNumber) {
return 'A' + this.attemptNumber
} else {
return 'R' + this.roundNumber
}
}
id() {
var out = [this.eventId]
if (this.roundNumber) {
out.push('r' + this.roundNumber)
}
if (this.groupNumber || this.groupNumber === 0) {
out.push('g' + this.groupNumber)
}
if (this.attemptNumber) {
out.push('a' + this.attemptNumber)
}
return out.join('-')
}
isActivity() {
return true
}
contains(other) {
if (this.eventId !== other.eventId) {
return false
}
if (this.roundNumber !== null && this.roundNumber !== other.roundNumber) {
return false
}
if (this.groupNumber !== null && this.groupNumber !== other.groupNumber) {
return false
}
if (this.activityNumber !== null && this.activityNumber !== other.activityNumber) {
return false
}
return true
}
}
class OtherActivity {
constructor(code) {
this.code = code
}
toString() {
return this.code
}
isActivity() {
return false
}
}
function parse(code) {
var codeSplit = code.split('-')
if (codeSplit[0] == 'other') {
return new OtherActivity(codeSplit[1])
}
var eventId = codeSplit[0]
if (!events.idToName[eventId]) {
console.log('Invalid ActivityCode ' + code)
return null
}
var roundNumber = null
var groupNumber = null
var attemptNumber = null
for (var i = 1; i < codeSplit.length; i++) {
if (codeSplit[i].startsWith('r')) {
roundNumber = +codeSplit[i].slice(1)
} else if (codeSplit[i].startsWith('g')) {
groupNumber = +codeSplit[i].slice(1)
} else if (codeSplit[i].startsWith('a')) {
attemptNumber = +codeSplit[i].slice(1)
} else {
console.log('Invalid ActivityCode ' + code)
return null
}
}
return new ActivityCode(eventId, roundNumber, groupNumber, attemptNumber)
}
module.exports = {
parse: parse,
ActivityCode: ActivityCode
}