-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.js
More file actions
135 lines (118 loc) · 3.73 KB
/
Copy pathsequence.js
File metadata and controls
135 lines (118 loc) · 3.73 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
// vars
var last_tip_username = null;
var next_tip_amount = 1;
var goal_reached = false;
// var version = 10
// Limit goal description as we add some text
cb.settings_choices = [
{ name: 'goal_description', type: 'str', minLength: 1, maxLength: 190 },
{ name: 'goal_value', type: 'int', minValue: 1, maxValue: 100, default: 50 },
{ name: 'order', type: 'choice',
choice1: 'ascending',
choice2: 'descending',
default: 'ascending' }
];
cb.onTip(
function (tip) {
var diff = tip['amount'];
while ((diff > 0) && (!checkGoalReached())) {
diff -= next_tip_amount;
if (diff >= 0) {
// We set the last tipper only if the tip was counted
// (not optimal to do it as many times as the tip counted though)
last_tip_username = tip['from_user'];
setNextTipNeeded();
}
}
update_subject();
cb.drawPanel();
}
);
cb.onDrawPanel(
function (user) {
if (checkGoalReached()) {
return {
'template': '3_rows_11_21_31',
'row1_value': 'Goal reached!',
'row2_value': '',
'row3_value': 'Thanks to all tippers'
};
} else {
if (isAscendingOrder()) {
return {
'template': '3_rows_of_labels',
'row1_label': 'Next Tip Needed:',
'row1_value': next_tip_amount,
'row2_label': 'Last Tip From:',
'row2_value': format_username(last_tip_username),
'row3_label': 'Ascending:',
'row3_value': 'From 1 to ' + cb.settings.goal_value
};
} else {
return {
'template': '3_rows_of_labels',
'row1_label': 'Next Tip Needed:',
'row1_value': next_tip_amount,
'row2_label': 'Last Tip From:',
'row2_value': format_username(last_tip_username),
'row3_label': 'Descending:',
'row3_value': 'From ' + cb.settings.goal_value + ' to 0'
};
}
}
}
);
// helper functions
function update_subject() {
if (goal_reached) {
return;
}
var new_subject = "";
if (checkGoalReached()) {
new_subject = cb.settings.goal_description + " [Goal reached! Thanks to all tippers.]";
goal_reached = true;
} else {
if (isAscendingOrder()) {
new_subject = cb.settings.goal_description +
" [Tip in ascending order from 1 to " + cb.settings.goal_value + ". Next tip needed: " + next_tip_amount + "]";
} else {
new_subject = cb.settings.goal_description +
" [Tip in descending order from " + cb.settings.goal_value + " to 0. Next tip needed: " + next_tip_amount + "]";
}
}
cb.changeRoomSubject(new_subject);
}
function format_username(val) {
if (val === null) {
return "--";
} else {
return val.substring(0, 12);
}
}
function isAscendingOrder() {
return (cb.settings.order == 'ascending');
}
function setNextTipNeeded() {
if (isAscendingOrder()) {
next_tip_amount++;
} else {
next_tip_amount--;
}
}
function checkGoalReached() {
if (isAscendingOrder()) {
return (next_tip_amount > cb.settings.goal_value);
} else {
return (next_tip_amount <= 0);
}
}
function init() {
if (isAscendingOrder()) {
next_tip_amount = 1;
} else {
next_tip_amount = cb.settings.goal_value;
}
update_subject();
}
if (!!AppDevKit == false )
init();