);
}
areGroupsIncomplete() {
return this.state.groups.filter(group => group.length !== this.state.groupSize).length !== 0;
}
teamListUpdate(list) {
this.setState({teams: list});
}
groupListUpdate(list) {
this.setState({groups: list});
}
increaseGroupAdvance() {
const newGroupAdvance = this.state.groupAdvance * 2;
this.setState({
groupAdvance: newGroupAdvance
});
}
decreaseGroupAdvance() {
const newGroupAdvance = Math.floor(this.state.groupAdvance / 2);
if (newGroupAdvance >= 1) {
this.setState({
groupAdvance: newGroupAdvance
});
}
}
increaseGroupSize() {
this.setState({groupSize: this.state.groupSize + 1});
}
decreaseGroupSize() {
const newGroupSize = this.state.groupSize - 1;
if (newGroupSize >= 3) {
this.setState({groupSize: newGroupSize});
}
}
handleGroupPhaseEnabledInput(input) {
this.setState({groupPhaseEnabled: input.target.checked});
}
handleNameInput(input) {
this.setState({name: input.target.value});
}
handleDescriptionInput(input) {
this.setState({description: input.target.value});
}
handlePublicInput(input) {
this.setState({public: input.target.checked});
}
create() {
if (this.valuesAreCredible()) {
createTournament(this.generateTournamentCreationObject(), data => {
Router.push('/t/' + data.id);
}, () => {
notify.show('Das Turnier konnte nicht erstellt werden.', 'warning', 5000);
});
} else {
notify.show('Bitte korrigiere deine Eingaben zuerst.', 'warning', 5000);
}
}
valuesAreCredible() {
return this.state.teams.length >= this.state.groupAdvance && !this.areGroupsIncomplete();
}
generateTournamentCreationObject() {
const tournament = {
'name': this.state.name,
'description': this.state.description,
'public': this.state.public,
'group_stage': this.state.groupPhaseEnabled,
'teams': createTeamArray(this.state.groupPhaseEnabled, this.state.groups, this.state.teams)
};
if (this.state.groupPhaseEnabled) {
tournament.playoff_teams_amount = this.state.groupAdvance;
}
return tournament;
}
}
/**
* This method creates an array of team objects that conform to the currently
* api specs available at https://apidoc.turnie.re/
*
* @param {boolean} groupphase Whether a group phase is to be created
* @param {string[][]} groups The teams split into the groups that are
* to be used in the group phase of the tournament. Please note that
* according to the api every team can only occur once (not enforced
* by this method) and that every team from {@param teams} will have
* to be in one of the groups (also not enforced by this method, but
* might lead to inconsistencies)
* @param {string[]} teams An array containing all names of the teams
* that are to be created for the tournament
* @return {Object[]} an array of teams that can be directly sent to the
* backend
*/
function createTeamArray(groupphase, groups, teams) {
const result = [];
if (groupphase) {
for (let groupNumber = 0; groupNumber < groups.length; groupNumber++) {
for (let groupMember = 0; groupMember < groups[groupNumber].length; groupMember++) {
result[result.length] = {
'name': groups[groupNumber][groupMember], 'group': groupNumber
};
}
}
} else {
for (let i = 0; i < teams.length; i++) {
result[i] = {'name': teams[i]};
}
}
return result;
}