-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.component.ts
245 lines (215 loc) · 7.46 KB
/
timer.component.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { SettingsService } from '../../services/settings/settings.service';
import { Component, ElementRef, OnInit, OnDestroy } from '@angular/core';
import { Participant } from '../../models/Participant';
import { ParticipantService } from '../../services/participant/participant.service';
import { NavbarService } from '../../services/navbar/navbar.service';
import { Subscription, timer } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-timer',
templateUrl: './timer.component.html',
styleUrls: ['./timer.component.css']
})
export class TimerComponent implements OnInit {
timerSubscription: Subscription;
participantList: Participant[];
absentParticipants: Participant[] = Array();
doneParticipants: Participant[] = Array();
// variables for session
totalMaxTime = this.settingsService.getGlobalMaxTime();
recommendedIndividualTime = 120;
totalPercent = 0;
totalTimePercent = 0;
totalElapsed = 0;
currentTotalElapsed = 0;
// variables for indiviual
individualMaxTime = 120;
individualTime: number = this.individualMaxTime;
currentPercent = 0;
currentParticipant: Participant;
future: Date;
futureString: string;
diff: number;
currentDiff: number;
currentElapsed = 0;
timerActive = false;
constructor(
private activatedRoute: ActivatedRoute,
private nav: NavbarService,
public participantService: ParticipantService,
public settingsService: SettingsService
) {
}
ngOnInit() {
this.participantList = this.participantService.getParticipants();
this.sortParticipants();
if (this.settingsService.getUseGlobalMaxTime()) {
this.individualTime = Math.round(
this.settingsService.getGlobalMaxTime() /
(this.participantList.length + this.doneParticipants.length)
);
} else {
this.individualTime = this.individualMaxTime;
}
console.info('[Scrumtimer] Timer init');
if(this.activatedRoute['_routerState'].snapshot.url === '/popin'){
this.nav.hide();
}else{
this.nav.show();
}
}
stopTimer() {
// save time for participant
this.currentParticipant.time = this.currentElapsed;
// this.currentPercent = 0;
// move first Participant to done participants
if (this.participantList.length > 0) {
this.doneParticipants.push(this.currentParticipant);
}
// remove the top participant
this.participantList.splice(0, 1);
// set progressbar to 100%
this.totalPercent = 100;
this.timerActive = false;
// stop timer
this.timerSubscription.unsubscribe();
}
startTimer() {
const source = timer(1000, 2000);
this.currentParticipant = this.participantList[0];
this.future = new Date();
// set the timer to a time in the future, based on "individualTime" seconds
if (this.settingsService.getUseGlobalMaxTime()) {
if (this.totalElapsed >= this.settingsService.getGlobalMaxTime()) {
// 1 sec grace time :)
this.individualTime = 1;
} else {
this.individualTime = Math.round(
(this.settingsService.getGlobalMaxTime() - this.totalElapsed )/
(this.participantList.length)
);
}
} else {
this.individualTime = this.individualMaxTime;
}
this.future.setSeconds(this.future.getSeconds() + this.individualTime);
this.timerSubscription = source.subscribe(val => {
this.currentDiff = Math.floor((this.future.getTime() - new Date().getTime()) / 1000);
this.currentElapsed = this.individualTime - this.currentDiff;
this.currentPercent = Math.round((this.currentElapsed / this.individualTime) * 100);
this.currentTotalElapsed = this.totalElapsed + this.currentElapsed;
this.totalTimePercent = Math.round(((this.totalElapsed + this.currentElapsed) / this.totalMaxTime) * 100) ;
});
this.timerActive = true;
}
nextParticipant() {
// save time for participant
this.participantList[0].time = this.currentElapsed;
// add up total elapsed time
this.totalElapsed += this.currentElapsed;
this.currentElapsed = 0;
this.currentDiff = 0;
this.currentPercent = 0;
// move first Participant to done participants
this.doneParticipants.push(this.participantList[0]);
// remove the top participant
this.participantList.splice(0, 1);
if (this.participantList.length > 0) {
this.currentParticipant = this.participantList[0];
} else {
this.currentParticipant = new Participant('', '');
}
this.totalPercent = Math.round((this.doneParticipants.length / (this.participantList.length + this.doneParticipants.length)) * 100);
this.totalTimePercent = Math.round(((this.totalElapsed + this.currentElapsed) / this.totalMaxTime) * 100) ;
this.currentPercent = 0;
this.currentDiff = 0;
this.currentPercent = 0;
this.timerActive = false;
// stop timer
this.timerSubscription.unsubscribe();
this.startTimer();
}
resetTimer() {
this.stopTimer();
// move all participants from done to present
for (let i = this.doneParticipants.length - 1; i >= 0; i--) {
// reset time taken
this.doneParticipants[i].time = 0;
this.participantList.push(this.doneParticipants[i]);
this.doneParticipants.splice(i, 1);
}
// reset timers
this.totalElapsed = 0;
this.totalPercent = 0;
this.totalTimePercent = 0;
this.currentTotalElapsed = 0;
this.currentPercent = 0;
this.currentDiff = 0;
this.currentParticipant = this.participantList[0];
this.currentPercent = 0;
this.currentElapsed = 0;
this.individualTime = Math.round(
this.settingsService.getGlobalMaxTime() /
this.participantList.length
);
this.sortParticipants();
}
/**
* Shuffles an array pseudo-randomly
*
* @param array
*/
shuffle(array: any[]) {
let currentIndex = array.length, temporaryValue: any, randomIndex: number;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
shuffleParticipants() {
this.participantList = this.shuffle(this.participantList);
this.currentParticipant = this.participantList[0];
}
sortParticipants() {
this.participantList.sort(function(a, b){
return a.name.localeCompare(b.name);
});
this.currentParticipant = this.participantList[0];
}
/**
* Mark participant as absent.
*
* note that the last two participants cannot be marked as absent.
*
* @param participant Participant to mark as absent
*/
markAbsent(participant: Participant): boolean {
if (this.participantList.length <= 2) {
console.warn('Cannot mark the last two participants absent.');
return false;
} else {
const idx: number = this.participantList.indexOf(participant);
this.absentParticipants.push(participant);
this.participantList.splice(idx, 1);
this.currentParticipant = this.participantList[0];
return true;
}
}
/**
* Mark participant as present.
*
* @param participant Participant to mark as present
*/
markPresent(participant: Participant) {
const idx: number = this.absentParticipants.indexOf(participant);
this.participantList.push(participant);
this.absentParticipants.splice(idx, 1);
}
}