]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/results/Item.js
server calculated scoring
[alttp.git] / resources / js / components / results / Item.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3 import { withTranslation } from 'react-i18next';
4
5 import Icon from '../common/Icon';
6 import Box from '../users/Box';
7 import { formatTime } from '../../helpers/Result';
8 import { findResult } from '../../helpers/Participant';
9 import { maySeeResults } from '../../helpers/permissions';
10 import { withUser } from '../../helpers/UserContext';
11
12 const getIcon = (result, maySee) => {
13         if (!result || !result.has_finished) {
14                 return <Icon.PENDING className="text-muted" size="lg" />;
15         }
16         if (result.forfeit && maySee) {
17                 return <Icon.FORFEIT className="text-danger" size="lg" />;
18         }
19         if (result.placement === 1) {
20                 return <Icon.FIRST_PLACE className="text-gold" size="lg" />;
21         }
22         if (result.placement === 2) {
23                 return <Icon.SECOND_PLACE className="text-silver" size="lg" />;
24         }
25         if (result.placement === 3) {
26                 return <Icon.THIRD_PLACE className="text-bronze" size="lg" />;
27         }
28         return <Icon.FINISHED className="text-success" size="lg" />;
29 };
30
31 const getTime = (result, maySee) => {
32         if (!result || !maySee) {
33                 return null;
34         }
35         if (result.time) {
36                 return formatTime(result);
37         }
38         if (result.forfeit) {
39                 return 'DNF';
40         }
41         return '?';
42 };
43
44 const Item = ({
45         participant,
46         round,
47         tournament,
48         user,
49 }) => {
50         const result = findResult(participant, round);
51         const maySee = maySeeResults(user, tournament, round);
52         return <div className="result">
53                 <Box user={participant.user} />
54                 <div className={`status ${result && result.has_finished ? 'finished' : 'pending'}`}>
55                         <span className="time">
56                                 {getTime(result, maySee)}
57                         </span>
58                         {getIcon(result, maySee)}
59                 </div>
60         </div>;
61 };
62
63 Item.propTypes = {
64         participant: PropTypes.shape({
65                 user: PropTypes.shape({
66                 }),
67         }),
68         round: PropTypes.shape({
69         }),
70         tournament: PropTypes.shape({
71         }),
72         user: PropTypes.shape({
73         }),
74 };
75
76 export default withTranslation()(withUser(Item));