Newer
Older
import Database from '../../database/database';
import { Exclusion } from '../../classes/exclusion';
import { ExclusionReason } from '../../types/exclusionReason';
import GiftType from '../../types/giftType';
import Member from '../../classes/member';
import { RelationshipData } from '../../classes/relationship';
/** The member that could possibly become the taker Wichtel of the assignment. */
taker: Member;
/** The score for the taker Wichtel. The higher the better. */
interface Pairing
{
/** The future giver Wichtel. */
giver: Member;
/** The list of possible candidates for this assignment. */
candidates: Candidate[];
}
interface Assignment
{
giver: Member;
taker: Member;
}
export class AssignmentModule
{
private database: Database;
constructor (database: Database)
{
this.database = database;
}
/**
* Run the assignment.
* @returns True if successful, false otherwise.
*/
public runAssignment (): boolean // TODO: Return the reason for failure.
if (members.length === 0)
{
return false;
}
const pairings = this.assemblePairings(members);
const preparationResult = this.prepareCandidates(pairings);
if (!preparationResult)
const assignments = this.assignCandidates(pairings);
if (assignments.length === 0)
{
return false;
}
const relationships: RelationshipData[] = assignments.map(
assignment =>
return {
giverId: assignment.giver.id,
takerId: assignment.taker.id,
};
this.database.saveRelationships(relationships);
private assemblePairings (members: Member[]): Pairing[]
for (const member of members)
{
score: 0,
}
);
}
pairings.push(
{
giver: member,
candidates: candidates,
}
);
/**
* Prepare the canidates assignment map by removing the incompatible members and calculating the scores/rank.
* @returns True if successful, false otherwise.
*/
private prepareCandidates (pairings: Pairing[]): boolean
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
const exclusions = this.database.getUserExclusions();
const wishExclusions = this.extractWishExclusions(exclusions);
const wichtelFromThePastExclusions = this.extractWichtelFromThePastExclusions(exclusions);
for (const pairing of pairings)
{
for (let i = pairing.candidates.length - 1; i >= 0; i--)
{
const candidate = pairing.candidates[i];
if (this.isGiftTypeIncompatible(pairing.giver, candidate.taker)
|| this.isInternationalIncompatible(pairing.giver, candidate.taker)
|| this.isExcluded(pairing.giver, candidate.taker, wishExclusions))
{
pairing.candidates.splice(i, 1);
}
candidate.score = this.calculateScore(pairing.giver, candidate.taker, wichtelFromThePastExclusions);
}
if (pairing.candidates.length === 0)
{
// Empty candidates list means no valid solution for the assignment:
return false;
}
this.sortCandidatesByScore(pairing.candidates);
}
return true;
}
private extractWishExclusions (exclusions: Exclusion[]): Exclusion[]
{
return exclusions.filter(exclusion => exclusion.reason === ExclusionReason.Wish);
}
private extractWichtelFromThePastExclusions (exclusions: Exclusion[]): Exclusion[]
{
return exclusions.filter(exclusion => exclusion.reason === ExclusionReason.WichtelFromThePast);
}
private isGiftTypeIncompatible (giverWichtel: Member, takerWichtel: Member): boolean
{
if (giverWichtel.information.giftTypeAsGiver === takerWichtel.information.giftTypeAsTaker)
{
return false;
}
const result = (giverWichtel.information.giftTypeAsGiver != GiftType.All)
&& (takerWichtel.information.giftTypeAsTaker != GiftType.All);
return result;
}
private isInternationalIncompatible (giverWichtel: Member, takerWichtel: Member): boolean
if (giverWichtel.information.internationalAllowed)
{
return false;
}
if ((giverWichtel.information.giftTypeAsGiver != GiftType.Analogue)
&& (takerWichtel.information.giftTypeAsTaker != GiftType.Analogue))
{
return false;
}
// NOTE: If one or both have "all" as gift type, they are compatible but should have a lower score if from different countries.
const result = (giverWichtel.information.country != takerWichtel.information.country);
return result;
}
private isExcluded (giverWichtel: Member, takerWichtel: Member, exclusions: Exclusion[]): boolean
{
for (const exclusion of exclusions)
{
if ((exclusion.giverId === giverWichtel.id) && (exclusion.takerId === takerWichtel.id))
{
return true;
}
}
return false;
}
private calculateScore (giverWichtel: Member, takerWichtel: Member, exclusions: Exclusion[]): number
if (giverWichtel.information.giftTypeAsGiver === takerWichtel.information.giftTypeAsTaker)
{
score += 4;
}
if (giverWichtel.information.country === takerWichtel.information.country)
else if (!giverWichtel.information.internationalAllowed)
if ((giverWichtel.information.giftTypeAsGiver == GiftType.All) && (takerWichtel.information.giftTypeAsTaker == GiftType.All))
{
// Both have "all" but the analogue way is blocked, thus a decrease:
score -= 3;
}
}
for (const exclusion of exclusions)
{
if ((exclusion.giverId === giverWichtel.id) && (exclusion.takerId === takerWichtel.id))
{
score -= 8;
break;
}
}
return score;
}
private sortCandidatesByScore (candidates: Candidate[]): void
(a, b) =>
{
return a.score - b.score;
}
);
private assignCandidates (pairings: Pairing[]): Assignment[]
{
const priorities: Set<Member> = new Set();
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const remaining = this.clonePairings(pairings);
this.sortPairings(remaining, priorities);
const completed: Assignment[] = [];
while (true)
{
const pairing = remaining.shift();
if (pairing === undefined)
{
// All pairings have been assigned.
break;
}
if (pairing.candidates.length == 0)
{
// If there are no candidates left, we have an incomplete solution.
// In this case, we add the member to the priority list and rerun the assignment.
if (priorities.has(pairing.giver))
{
// If the member is already in the priority list, there is no possible solution with this algorithm:
return [];
}
else
{
priorities.add(pairing.giver);
}
break;
}
const [firstCandidate] = pairing.candidates;
const assignment: Assignment = {
giver: pairing.giver,
taker: firstCandidate.taker,
};
completed.push(assignment);
for (const pairing of remaining)
{
for (let i = pairing.candidates.length - 1; i >= 0; i--)
{
const candidate = pairing.candidates[i];
// Remove the candidate from all other remaining pairings for it to not be assigned twice and
// the giver from the taker's candidates to prevent a one-to-one circular assignment:
if ((candidate.taker === assignment.taker)
|| ((pairing.giver === assignment.taker) && (candidate.taker === assignment.giver)))
{
pairing.candidates.splice(i, 1);
}
}
}
// Sort again because the assignment changed all pairing scores:
this.sortPairings(remaining, priorities);
}
if (completed.length === pairings.length)
{
// Found a solution!
return completed;
}
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// If all members are prioritised, there could no possible solution be found:
return [];
}
private clonePairings (pairings: Pairing[]): Pairing[]
{
const clones: Pairing[] = [];
for (const pairing of pairings)
{
const clone: Pairing = {
giver: pairing.giver,
candidates: [...pairing.candidates],
};
clones.push(clone);
}
return clones;
}
private sortPairings (pairing: Pairing[], priorities: Set<Member>): void
{
pairing.sort(
(pairingA: Pairing, pairingB: Pairing): number =>
{
const aIsPriority = priorities.has(pairingA.giver);
const bIsPriority = priorities.has(pairingB.giver);
if (aIsPriority || bIsPriority)
{
if (aIsPriority && bIsPriority)
{
return 0;
}
else if (aIsPriority)
{
return -1;
}
else
{
return 1;
}
}
// The value of the chain is determined by the accumulated weighting of all the wichtel plus their total number.
// The higher the value, the less important the user is for the selection.
// This sets a balance between "find the most valuable combinations", "make sure everyone is served" and "try to avoid
// making too poor assignments".
const accumulateScores = (totalScore: number, candidate: Candidate): number => totalScore + candidate.score;
const totalScoreA = pairingA.candidates.reduce(accumulateScores, 0) + pairingA.candidates.length;
const totalScoreB = pairingB.candidates.reduce(accumulateScores, 0) + pairingB.candidates.length;
let result = totalScoreA - totalScoreB;
// If both scores are the same, the number of candidates is used as a tie breaker:
if (result === 0)
{
result = pairingA.candidates.length - pairingB.candidates.length;
}
return result;
}
);