-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate-points.php
49 lines (35 loc) · 1.52 KB
/
calculate-points.php
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
<?php
require_once 'vendor/autoload.php';
use parallel\Runtime;
use parallel\Channel;
$repository = new \Alura\Threads\Student\InMemoryStudentRepository();
$studentList = $repository->all();
$studentChunks = array_chunk($studentList, ceil(count($studentList) / 4));
$totalPoints = 0;
$runTimes = [];
$futures = [];
$channel = Channel::make('points');
foreach($studentChunks as $key => $studentChunk) {
$runTimes[$key] = new Runtime(__DIR__ . '/vendor/autoload.php');
foreach($studentChunk as $student) {
$activities = $repository->activitiesInADay($student);
$futures[] = $runTimes[$key]->run(function (array $activities, \Activity\Threads\Student\Student $student, Channel $channel) {
$points = array_reduce(
$activities,
fn (int $total, \Activity\Threads\Activity\Activity $activity) => $total + $activity->points(),
0
);
$channel->send($points);
printf('%s made %d poinst today%s', $student->fullName(), $points, PHP_EOL);
return $points;
}, [$activities, $student, $channel]);
}
}
$totalPointsWithChannel = 0;
for($i = 0; $i < count($studentList); $i++) {
$totalPointsWithChannel += $channel->recv();
}
$channel->close();
$totalPoints = array_reduce($futures, fn($totalPoints, $future) => $totalPoints += $future->value(), 0) . PHP_EOL;
printf('We had a total of %d points today%s', $totalPointsWithChannel, PHP_EOL);
printf('We had a total of %d points today%s', $totalPoints, PHP_EOL);