Skip to content

Commit

Permalink
Merge pull request #1 from Bobface/missing-participant-fix
Browse files Browse the repository at this point in the history
Fix missing participants
  • Loading branch information
makoto committed Mar 28, 2020
2 parents a66c23f + 0001cba commit 8dd7102
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/participants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,36 @@ export const calculateNumAttended = participants => participants.reduce((m, v) =
return m + (attended ? 1 : 0)
}, 0)

export const calculateFinalizeMaps = participants => {
export const calculateFinalizeMaps = (participants, overrideMissingValue) => {
if (!(overrideMissingValue === undefined ||
overrideMissingValue === PARTICIPANT_STATUS.SHOWED_UP ||
overrideMissingValue === PARTICIPANT_STATUS.REGISTERED)) {
throw new Error(`Invalid overrideMissingValue, expected undefined, SHOWED_UP or REGISTERED , got ${overrideMissingValue}`)
}

// sort participants array
participants.sort((a, b) => (a.index < b.index ? -1 : 1))

// check for missing participants
for (let i = 0; participants.length > i;) {
const currentIndex = participants[i].index
if (currentIndex !== i) {
if (overrideMissingValue === undefined) {
throw new Error(`Participant ${i} is missing`)
}

participants.splice(i, 0, {
overrideMissingValue,
index: i,
user: {
address: '0x0000000000000000000000000000000000000000'
},
})
} else {
i += 1
}
}

const maps = []
let currentMap = toBN(0)
for (let i = 0; participants.length > i; i += 1) {
Expand All @@ -42,8 +68,8 @@ export const calculateFinalizeMaps = participants => {
throw new Error(`Unexpected participant status: ${participants[i].status}`)
}
}
maps.push(currentMap)

maps.push(currentMap)
return maps.map(m => m.toString(10))
}

Expand Down
67 changes: 67 additions & 0 deletions src/participants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,73 @@ describe('.calculateFinalizeMaps', () => {

expect(calculateFinalizeMaps(ps)).toEqual(maps)
})

it('p5 is missing, override REGISTERED', () => {
// We set #6 to SHOWED_UP and remove #5 from the list
const maps = [
toBN(0).bincn(6).toString(10),
toBN(0).toString(10),
]

ps.forEach(p => {
switch (p.index) {
case 6:
p.status = PARTICIPANT_STATUS.SHOWED_UP
break
default:
break
}
})

ps.sort((a, b) => (a.index < b.index ? -1 : 1))
ps.splice(5, 1)

expect(calculateFinalizeMaps(ps, PARTICIPANT_STATUS.REGISTERED)).toEqual(maps)
})

it('p5 is missing, override SHOWED_UP', () => {
// We set #6 to SHOWED_UP and remove #5 from the list
const maps = [
toBN(0).bincn(6).bincn(5).toString(10),
toBN(0).toString(10),
]

ps.forEach(p => {
switch (p.index) {
case 6:
p.status = PARTICIPANT_STATUS.SHOWED_UP
break
default:
break
}
})

ps.sort((a, b) => (a.index < b.index ? -1 : 1))
ps.splice(5, 1)

expect(calculateFinalizeMaps(ps, PARTICIPANT_STATUS.SHOWED_UP)).toEqual(maps)
})

it('p5 is missing, override default value', () => {
ps.forEach(p => {
switch (p.index) {
case 6:
p.status = PARTICIPANT_STATUS.SHOWED_UP
break
default:
break
}
})

ps.sort((a, b) => (a.index < b.index ? -1 : 1))
ps.splice(5, 1)

expect(() => calculateFinalizeMaps(ps)).toThrow()
})

it('override invalid value', () => {
expect(() => calculateFinalizeMaps(ps, 123)).toThrow()
})
})


Expand Down

0 comments on commit 8dd7102

Please sign in to comment.