Merge pull request #31 from turniere/ticket/TURNIERE-207
Implement Starting and Ending a Match (Ticket/turniere 207)
This commit is contained in:
commit
0aa8f307ec
50
js/api.js
50
js/api.js
|
|
@ -221,6 +221,32 @@ const reducerTournamentinfo = (state = defaultStateTournamentinfo, action) => {
|
||||||
return Object.assign({}, state, {});
|
return Object.assign({}, state, {});
|
||||||
case actionTypesTournamentinfo.REHYDRATE:
|
case actionTypesTournamentinfo.REHYDRATE:
|
||||||
|
|
||||||
|
return Object.assign({}, state, {});
|
||||||
|
case actionTypesTournamentinfo.START_MATCH:
|
||||||
|
patchRequest(action.state, '/matches/' + action.parameters.matchId, {
|
||||||
|
state: 'in_progress'
|
||||||
|
}).then(resp => {
|
||||||
|
storeOptionalToken(resp);
|
||||||
|
action.parameters.successCallback();
|
||||||
|
}).catch(error => {
|
||||||
|
if (error.response) {
|
||||||
|
storeOptionalToken(error.response);
|
||||||
|
}
|
||||||
|
action.parameters.errorCallback();
|
||||||
|
});
|
||||||
|
return Object.assign({}, state, {});
|
||||||
|
case actionTypesTournamentinfo.END_MATCH:
|
||||||
|
patchRequest(action.state, '/matches/' + action.parameters.matchId, {
|
||||||
|
state: 'finished'
|
||||||
|
}).then(resp => {
|
||||||
|
storeOptionalToken(resp);
|
||||||
|
action.parameters.successCallback(resp.data.winner);
|
||||||
|
}).catch(error => {
|
||||||
|
if (error.response) {
|
||||||
|
storeOptionalToken(error.response);
|
||||||
|
}
|
||||||
|
action.parameters.errorCallback();
|
||||||
|
});
|
||||||
return Object.assign({}, state, {});
|
return Object.assign({}, state, {});
|
||||||
case actionTypesTournamentinfo.CLEAR:
|
case actionTypesTournamentinfo.CLEAR:
|
||||||
|
|
||||||
|
|
@ -364,6 +390,30 @@ export function updateTeamName(team, successCB, errorCB) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function startMatch(matchId, successCallback, errorCallback) {
|
||||||
|
__store.dispatch({
|
||||||
|
type: actionTypesTournamentinfo.START_MATCH,
|
||||||
|
parameters: {
|
||||||
|
matchId: matchId,
|
||||||
|
successCallback: successCallback,
|
||||||
|
errorCallback: errorCallback
|
||||||
|
},
|
||||||
|
state: __store.getState()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function endMatch(matchId, successCallback, errorCallback) {
|
||||||
|
__store.dispatch({
|
||||||
|
type: actionTypesTournamentinfo.END_MATCH,
|
||||||
|
parameters: {
|
||||||
|
matchId: matchId,
|
||||||
|
successCallback: successCallback,
|
||||||
|
errorCallback: errorCallback
|
||||||
|
},
|
||||||
|
state: __store.getState()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function getState() {
|
export function getState() {
|
||||||
return __store.getState();
|
return __store.getState();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,25 @@ import {
|
||||||
Table
|
Table
|
||||||
} from 'reactstrap';
|
} from 'reactstrap';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import {endMatch, startMatch} from '../api';
|
||||||
|
import {notify} from 'react-notify-toast';
|
||||||
|
|
||||||
|
|
||||||
export class Match extends React.Component {
|
export class Match extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
modal: false
|
modal: false,
|
||||||
|
match: this.props.match
|
||||||
};
|
};
|
||||||
this.toggleModal = this.toggleModal.bind(this);
|
this.toggleModal = this.toggleModal.bind(this);
|
||||||
|
this.startMatch = this.startMatch.bind(this);
|
||||||
|
this.onStartMatchSuccess = this.onStartMatchSuccess.bind(this);
|
||||||
|
this.onStartMatchError = this.onStartMatchError.bind(this);
|
||||||
|
this.endMatch = this.endMatch.bind(this);
|
||||||
|
this.onEndMatchSuccess = this.onEndMatchSuccess.bind(this);
|
||||||
|
this.onEndMatchError = this.onEndMatchError.bind(this);
|
||||||
|
this.getMatchFinishedMessage = this.getMatchFinishedMessage.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleModal() {
|
toggleModal() {
|
||||||
|
|
@ -31,26 +41,68 @@ export class Match extends React.Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
startMatch() {
|
||||||
|
startMatch(this.state.match.id, this.onStartMatchSuccess, this.onStartMatchError);
|
||||||
|
}
|
||||||
|
|
||||||
|
onStartMatchSuccess() {
|
||||||
|
const updatedMatch = this.state.match;
|
||||||
|
updatedMatch.state = 'in_progress';
|
||||||
|
this.setState({match: updatedMatch});
|
||||||
|
this.toggleModal();
|
||||||
|
}
|
||||||
|
|
||||||
|
onStartMatchError() {
|
||||||
|
this.toggleModal();
|
||||||
|
notify.show('Das Match konnte nicht gestartet werden.', 'error', 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
endMatch() {
|
||||||
|
endMatch(this.state.match.id, this.onEndMatchSuccess, this.onEndMatchError);
|
||||||
|
}
|
||||||
|
|
||||||
|
onEndMatchSuccess(winner) {
|
||||||
|
const updatedMatch = this.state.match;
|
||||||
|
updatedMatch.state = 'finished';
|
||||||
|
updatedMatch.winnerTeamId = winner === null ? null : winner.id;
|
||||||
|
this.setState({match: updatedMatch});
|
||||||
|
this.toggleModal();
|
||||||
|
}
|
||||||
|
|
||||||
|
onEndMatchError() {
|
||||||
|
this.toggleModal();
|
||||||
|
notify.show('Das Match konnte nicht beendet werden.', 'error', 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
getMatchFinishedMessage() {
|
||||||
|
const match = this.state.match;
|
||||||
|
if (match.winnerTeamId === null) {
|
||||||
|
return 'Spiel beendet, unentschieden';
|
||||||
|
}
|
||||||
|
if (match.winnerTeamId === match.team1.id) {
|
||||||
|
return 'Gewinner: ' + match.team1.name;
|
||||||
|
}
|
||||||
|
if (match.winnerTeamId === match.team2.id) {
|
||||||
|
return 'Gewinner: ' + match.team2.name;
|
||||||
|
}
|
||||||
|
return 'Spiel beendet';
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
let cardClass;
|
let cardClass;
|
||||||
let smallMessage;
|
let smallMessage;
|
||||||
let borderClass;
|
let borderClass;
|
||||||
// possible states: single_team not_ready not_started in_progress team1_won team2_won undecided
|
// possible states: single_team not_ready not_started in_progress finished
|
||||||
switch (this.props.match.state) {
|
switch (this.state.match.state) {
|
||||||
case 'in_progress':
|
case 'in_progress':
|
||||||
cardClass = 'table-warning';
|
cardClass = 'table-warning';
|
||||||
borderClass = 'border-warning';
|
borderClass = 'border-warning';
|
||||||
smallMessage = 'Spiel läuft';
|
smallMessage = 'Spiel läuft';
|
||||||
break;
|
break;
|
||||||
case 'team1_won':
|
case 'finished':
|
||||||
cardClass = 'table-success';
|
cardClass = 'table-success';
|
||||||
borderClass = 'border-success';
|
borderClass = 'border-success';
|
||||||
smallMessage = 'Gewinner: ' + this.props.match.team1;
|
smallMessage = this.getMatchFinishedMessage();
|
||||||
break;
|
|
||||||
case 'team2_won':
|
|
||||||
cardClass = 'table-success';
|
|
||||||
borderClass = 'border-success';
|
|
||||||
smallMessage = 'Gewinner: ' + this.props.match.team2;
|
|
||||||
break;
|
break;
|
||||||
case 'single_team':
|
case 'single_team':
|
||||||
cardClass = 'table-success';
|
cardClass = 'table-success';
|
||||||
|
|
@ -63,20 +115,16 @@ export class Match extends React.Component {
|
||||||
case 'not_started':
|
case 'not_started':
|
||||||
smallMessage = 'Spiel kann gestartet werden';
|
smallMessage = 'Spiel kann gestartet werden';
|
||||||
break;
|
break;
|
||||||
case 'undecided':
|
|
||||||
cardClass = 'table-success';
|
|
||||||
borderClass = 'border-success';
|
|
||||||
smallMessage = 'Spiel beendet, unentschieden';
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return (<div className='mb-3'>
|
return (<div className='mb-3'>
|
||||||
<Card className='shadow-sm match' onClick={this.toggleModal}>
|
<Card className='shadow-sm match' onClick={this.toggleModal}>
|
||||||
<CardBody className={borderClass + ' border py-2 ' + cardClass}>
|
<CardBody className={borderClass + ' border py-2 ' + cardClass}>
|
||||||
<MatchTable match={this.props.match} borderColor={borderClass}/>
|
<MatchTable match={this.state.match} borderColor={borderClass}/>
|
||||||
</CardBody>
|
</CardBody>
|
||||||
</Card>
|
</Card>
|
||||||
<small className='text-muted'>{smallMessage}</small>
|
<small className='text-muted'>{smallMessage}</small>
|
||||||
<MatchModal title='Match' isOpen={this.state.modal} toggle={this.toggleModal} match={this.props.match}/>
|
<MatchModal title='Match' isOpen={this.state.modal} toggle={this.toggleModal} match={this.state.match}
|
||||||
|
startMatch={this.startMatch} endMatch={this.endMatch}/>
|
||||||
</div>);
|
</div>);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -84,16 +132,13 @@ export class Match extends React.Component {
|
||||||
function MatchModal(props) {
|
function MatchModal(props) {
|
||||||
let title;
|
let title;
|
||||||
let actionButton = '';
|
let actionButton = '';
|
||||||
// possible states: single_team not_ready not_started in_progress team1_won team2_won undecided
|
// possible states: single_team not_ready not_started in_progress finished
|
||||||
switch (props.match.state) {
|
switch (props.match.state) {
|
||||||
case 'in_progress':
|
case 'in_progress':
|
||||||
title = 'Spiel läuft';
|
title = 'Spiel läuft';
|
||||||
actionButton = <Button color='primary' onClick={props.toggle}>Spiel beenden</Button>;
|
actionButton = <Button color='primary' onClick={props.endMatch}>Spiel beenden</Button>;
|
||||||
break;
|
break;
|
||||||
case 'team1_won':
|
case 'finished':
|
||||||
title = 'Spiel beendet';
|
|
||||||
break;
|
|
||||||
case 'team2_won':
|
|
||||||
title = 'Spiel beendet';
|
title = 'Spiel beendet';
|
||||||
break;
|
break;
|
||||||
case 'single_team':
|
case 'single_team':
|
||||||
|
|
@ -104,17 +149,14 @@ function MatchModal(props) {
|
||||||
break;
|
break;
|
||||||
case 'not_started':
|
case 'not_started':
|
||||||
title = 'Spiel kann gestartet werden';
|
title = 'Spiel kann gestartet werden';
|
||||||
actionButton = <Button color='primary' onClick={props.toggle}>Spiel starten</Button>;
|
actionButton = <Button color='primary' onClick={props.startMatch}>Spiel starten</Button>;
|
||||||
break;
|
|
||||||
case 'undecided':
|
|
||||||
title = 'Spiel beendet';
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return (<Modal isOpen={props.isOpen} toggle={props.toggle}>
|
return (<Modal isOpen={props.isOpen} toggle={props.toggle}>
|
||||||
<ModalHeader toggle={props.toggle}>{title}</ModalHeader>
|
<ModalHeader toggle={props.toggle}>{title}</ModalHeader>
|
||||||
<ModalBody>
|
<ModalBody>
|
||||||
{props.match.state === 'in_progress' ? <EditableMatchTable match={props.match}/> :
|
{props.matchState === 'in_progress' ? <EditableMatchTable match={props.match}/> :
|
||||||
<MatchTable match={props.match}/>}
|
<MatchTable match={props.match} matchStatus={props.matchState}/>}
|
||||||
</ModalBody>
|
</ModalBody>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
{actionButton}
|
{actionButton}
|
||||||
|
|
@ -126,17 +168,22 @@ function MatchModal(props) {
|
||||||
function MatchTable(props) {
|
function MatchTable(props) {
|
||||||
let team1Class;
|
let team1Class;
|
||||||
let team2Class;
|
let team2Class;
|
||||||
// possible states: single_team not_ready not_started in_progress team1_won team2_won undecided
|
// possible states: single_team not_ready not_started in_progress finished
|
||||||
switch (props.match.state) {
|
switch (props.matchState) {
|
||||||
case 'in_progress':
|
case 'in_progress':
|
||||||
break;
|
break;
|
||||||
case 'team1_won':
|
case 'finished':
|
||||||
team1Class = 'font-weight-bold';
|
if (props.match.winnerTeamId === undefined) {
|
||||||
team2Class = 'lost-team';
|
break;
|
||||||
break;
|
}
|
||||||
case 'team2_won':
|
if (props.winnerTeamId === props.match.team1.id) {
|
||||||
team1Class = 'lost-team';
|
team1Class = 'font-weight-bold';
|
||||||
team2Class = 'font-weight-bold';
|
team2Class = 'lost-team';
|
||||||
|
}
|
||||||
|
if (props.winnerTeamId === props.match.team2.id) {
|
||||||
|
team1Class = 'lost-team';
|
||||||
|
team2Class = 'font-weight-bold';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'single_team':
|
case 'single_team':
|
||||||
team2Class = 'text-muted';
|
team2Class = 'text-muted';
|
||||||
|
|
@ -145,14 +192,12 @@ function MatchTable(props) {
|
||||||
break;
|
break;
|
||||||
case 'not_started':
|
case 'not_started':
|
||||||
break;
|
break;
|
||||||
case 'undecided':
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (props.match.state === 'single_team') {
|
if (props.match.state === 'single_team') {
|
||||||
return (<Table className='mb-0'>
|
return (<Table className='mb-0'>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td className={'border-top-0 ' + team1Class}>{props.match.team1}</td>
|
<td className={'border-top-0 ' + team1Class}>{props.match.team1.name}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td className={props.borderColor + ' ' + team2Class}>kein Gegner</td>
|
<td className={props.borderColor + ' ' + team2Class}>kein Gegner</td>
|
||||||
|
|
@ -163,12 +208,12 @@ function MatchTable(props) {
|
||||||
return (<Table className='mb-0'>
|
return (<Table className='mb-0'>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<th className='stage border-top-0'>{props.match.scoreTeam1}</th>
|
<th className='stage border-top-0'>{props.match.team1.score}</th>
|
||||||
<td className={'border-top-0 ' + team1Class}>{props.match.team1}</td>
|
<td className={'border-top-0 ' + team1Class}>{props.match.team1.name}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th className={'stage ' + props.borderColor}>{props.match.scoreTeam2}</th>
|
<th className={'stage ' + props.borderColor}>{props.match.team2.score}</th>
|
||||||
<td className={props.borderColor + ' ' + team2Class}>{props.match.team2}</td>
|
<td className={props.borderColor + ' ' + team2Class}>{props.match.team2.name}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</Table>);
|
</Table>);
|
||||||
|
|
@ -180,15 +225,15 @@ function EditableMatchTable(props) {
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td className='scoreInput border-top-0'>
|
<td className='scoreInput border-top-0'>
|
||||||
<ScoreInput score={props.match.scoreTeam1}/>
|
<ScoreInput score={props.match.team1.score}/>
|
||||||
</td>
|
</td>
|
||||||
<td className='align-middle border-top-0'>{props.match.team1}</td>
|
<td className='align-middle border-top-0'>{props.match.team1.name}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td className='scoreInput'>
|
<td className='scoreInput'>
|
||||||
<ScoreInput score={props.match.scoreTeam2}/>
|
<ScoreInput score={props.match.team2.score}/>
|
||||||
</td>
|
</td>
|
||||||
<td className='align-middle'>{props.match.team2}</td>
|
<td className='align-middle'>{props.match.team2.name}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</Table>);
|
</Table>);
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ export const actionTypesTournamentinfo = {
|
||||||
'MODIFY_TOURNAMENT_SUCCESS': 'MODIFY_TOURNAMENT_SUCCESS',
|
'MODIFY_TOURNAMENT_SUCCESS': 'MODIFY_TOURNAMENT_SUCCESS',
|
||||||
'MODIFY_TOURNAMENT_ERROR': 'MODIFY_TOURNAMENT_ERROR',
|
'MODIFY_TOURNAMENT_ERROR': 'MODIFY_TOURNAMENT_ERROR',
|
||||||
|
|
||||||
|
'START_MATCH': 'START_MATCH',
|
||||||
|
'END_MATCH': 'END_MATCH',
|
||||||
|
|
||||||
'REHYDRATE': 'TOURNAMENTINFO_REHYDRATE',
|
'REHYDRATE': 'TOURNAMENTINFO_REHYDRATE',
|
||||||
'CLEAR': 'TOURNAMENTINFO_CLEAR'
|
'CLEAR': 'TOURNAMENTINFO_CLEAR'
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -121,24 +121,42 @@ function convertGroup(apiGroup) {
|
||||||
|
|
||||||
function convertMatch(apiMatch) {
|
function convertMatch(apiMatch) {
|
||||||
const result = {
|
const result = {
|
||||||
id: apiMatch.id, state: apiMatch.state
|
id: apiMatch.id, state: apiMatch.state, winnerTeamId: apiMatch.winner === null ? null : apiMatch.winner.id
|
||||||
};
|
};
|
||||||
|
|
||||||
if (apiMatch.match_scores.length === 2) {
|
if (apiMatch.match_scores.length === 2) {
|
||||||
result.team1 = apiMatch.match_scores[0].team.name;
|
result.team1 = {
|
||||||
result.scoreTeam1 = apiMatch.match_scores[0].points;
|
name: apiMatch.match_scores[0].team.name,
|
||||||
result.team2 = apiMatch.match_scores[1].team.name;
|
id: apiMatch.match_scores[0].team.id,
|
||||||
result.scoreTeam2 = apiMatch.match_scores[1].points;
|
score: apiMatch.match_scores[0].points
|
||||||
|
};
|
||||||
|
result.team2 = {
|
||||||
|
name: apiMatch.match_scores[1].team.name,
|
||||||
|
id: apiMatch.match_scores[1].team.id,
|
||||||
|
score: apiMatch.match_scores[1].points
|
||||||
|
};
|
||||||
} else if (apiMatch.match_scores.length === 1) {
|
} else if (apiMatch.match_scores.length === 1) {
|
||||||
result.team1 = apiMatch.match_scores[0].team.name;
|
result.team1 = {
|
||||||
result.scoreTeam1 = apiMatch.match_scores[0].points;
|
name: apiMatch.match_scores[0].team.name,
|
||||||
result.team2 = 'TBD';
|
id: apiMatch.match_scores[0].team.id,
|
||||||
result.scoreTeam2 = 0;
|
score: apiMatch.match_scores[0].points
|
||||||
|
};
|
||||||
|
result.team2 = {
|
||||||
|
name: 'TBD',
|
||||||
|
id: null,
|
||||||
|
score: 0
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
result.team1 = 'TBD';
|
result.team1 = {
|
||||||
result.scoreTeam1 = 0;
|
name: 'TBD',
|
||||||
result.team2 = 'TBD';
|
id: null,
|
||||||
result.scoreTeam2 = 0;
|
score: 0
|
||||||
|
};
|
||||||
|
result.team2 = {
|
||||||
|
name: 'TBD',
|
||||||
|
id: null,
|
||||||
|
score: 0
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue