generated from digital-product-jam-2023/template-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
170 lines (158 loc) · 5.58 KB
/
helpers.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
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
import Message from "./components/partials/message";
import SelectConcept from "./components/screens/select-concept";
import SelectStudents from "./components/screens/select-students";
import ShowStudents from "./components/screens/show-students";
import ShowTeams from "./components/screens/show-teams";
import Start from "./components/screens/start";
import { STATE_DESCRIPTORS } from "./config";
function shuffle(arr) {
// https://sebhastian.com/fisher-yates-shuffle-javascript/
let i = arr.length;
while (--i > 0) {
let randIndex = Math.floor(Math.random() * (i + 1));
[arr[randIndex], arr[i]] = [arr[i], arr[randIndex]];
}
return arr;
}
export function selectBackGroundColor(colors) {
return shuffle(colors).slice(0, 1)[0];
}
export function selectTransitionColors(colors) {
return shuffle(colors).slice(0, 6);
}
export function makeTeamDistribution(developerCount, designerCount) {
// this is not a generic algorithm to create team shapes!
// Assumptions:
// - ideal team is 2 x 2
// - always want 2+ devs in a team
// - there are less devs than designers
const developersIsEven = developerCount % 2 === 0;
const teamCount = developersIsEven ? developerCount / 2 : (developerCount - 1) / 2;
const developerTeamTarget = 2;
const designerTeamTarget = Math.floor(designerCount / teamCount);
const designerTeamExtraCount = designerCount - (teamCount * designerTeamTarget);
const distribution = Array(teamCount).fill({}).map((t, idx, arr) => {
// standard team
const team = { developerCount: developerTeamTarget, designerCount: designerTeamTarget };
// developer distribution maybe adjusted
if (idx + 1 === arr.length && !developersIsEven) team.developerCount = developerTeamTarget + 1
// designer distribution maybe adjusted
if (idx + 1 > (arr.length - designerTeamExtraCount)) team.designerCount = designerTeamTarget + 1
return team;
});
return distribution;
}
function selectDesigners(ids, quantity, assignedStudents) {
return shuffle(ids.filter(id => !assignedStudents.includes(id))).slice(0, quantity);
}
function selectDevelopers(ids, quantity, assignedStudents) {
return shuffle(ids.filter(id => !assignedStudents.includes(id))).slice(0, quantity);
}
export function makeConcept(companies, ideas) {
const name = shuffle(companies.map(c => c.name)).slice(0, 1)[0];
const idea = shuffle(ideas.map(i => i.description)).slice(0, 1)[0];
return { name, idea };
}
function groupStudentIds(students) {
return students.reduce((accumulator, current) => {
if (!accumulator.has(current.group.id)) { accumulator.set(current.group.id, []) };
accumulator.get(current.group.id).push(current.id);
return accumulator;
}, new Map());
}
export function selectStudents(students, assignedStudents, teamDistribution) {
const groupedStudentIds = groupStudentIds(students);
return [
...selectDevelopers(groupedStudentIds.get(1), teamDistribution.developerCount, assignedStudents),
...selectDesigners(groupedStudentIds.get(2), teamDistribution.designerCount, assignedStudents)
];
}
export function getCurrentComponent(
currentStateId,
session,
data,
teams,
setTeams,
assignedStudents,
setAssignedStudents,
currentTeamMembers,
setCurrentTeamMembers,
transitionToStateFn
) {
const stateDescriptor = STATE_DESCRIPTORS[currentStateId];
switch (currentStateId) {
case 0: {
return <Start
session={session}
data={data}
teams={teams}
setTeams={setTeams}
assignedStudents={assignedStudents}
setAssignedStudents={setAssignedStudents}
currentTeamMembers={currentTeamMembers}
setCurrentTeamMembers={setCurrentTeamMembers}
stateDescriptor={stateDescriptor}
transitionToStateFn={transitionToStateFn}
/>;
}
case 1: {
return <ShowStudents
session={session}
data={data}
teams={teams}
setTeams={setTeams}
assignedStudents={assignedStudents}
setAssignedStudents={setAssignedStudents}
currentTeamMembers={currentTeamMembers}
setCurrentTeamMembers={setCurrentTeamMembers}
stateDescriptor={stateDescriptor}
transitionToStateFn={transitionToStateFn}
/>
}
case 2: {
return <SelectStudents
session={session}
data={data}
teams={teams}
setTeams={setTeams}
assignedStudents={assignedStudents}
setAssignedStudents={setAssignedStudents}
currentTeamMembers={currentTeamMembers}
setCurrentTeamMembers={setCurrentTeamMembers}
stateDescriptor={stateDescriptor}
transitionToStateFn={transitionToStateFn}
/>
}
case 3: {
return <SelectConcept
session={session}
data={data}
teams={teams}
setTeams={setTeams}
assignedStudents={assignedStudents}
setAssignedStudents={setAssignedStudents}
currentTeamMembers={currentTeamMembers}
setCurrentTeamMembers={setCurrentTeamMembers}
stateDescriptor={stateDescriptor}
transitionToStateFn={transitionToStateFn}
/>
}
case 4: {
return <ShowTeams
session={session}
data={data}
teams={teams}
setTeams={setTeams}
assignedStudents={assignedStudents}
setAssignedStudents={setAssignedStudents}
currentTeamMembers={currentTeamMembers}
setCurrentTeamMembers={setCurrentTeamMembers}
stateDescriptor={stateDescriptor}
transitionToStateFn={transitionToStateFn}
/>
}
default: {
return <Message content="Something went wrong. Reload the application to start again." />;
}
}
}