]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/alttp-seeds/Seed.js
tracker layout
[alttp.git] / resources / js / components / alttp-seeds / Seed.js
1 import FileSaver from 'file-saver';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button, Col, Container, Row } from 'react-bootstrap';
5 import { withTranslation } from 'react-i18next';
6 import toastr from 'toastr';
7
8 import BaseRomButton from './BaseRomButton';
9 import { useAlttpBaseRom } from '../../helpers/AlttpBaseRomContext';
10 import BPS from '../../helpers/bps';
11 import i18n from '../../i18n';
12
13 const applyPatch = (rom, patch, filename) => {
14         try {
15                 const bps = new BPS();
16                 bps.setPatch(patch);
17                 bps.setSource(rom);
18                 const result = bps.applyPatch();
19                 FileSaver.saveAs(new Blob([result], { type: 'application/octet-stream' }), filename);
20         } catch (e) {
21                 toastr.error(i18n.t('alttpSeeds.patchError', { msg: e.message }));
22         }
23 };
24
25 const isDefaultSetting = () => false;
26
27 const Seed = ({ onRetry, patch, seed }) => {
28         const { rom } = useAlttpBaseRom();
29
30         return <Container>
31                 <h1>{i18n.t('alttpSeeds.heading')}</h1>
32                 <Row>
33                         <Col md={{ order: 2 }}>
34                                 {rom ?
35                                         <Button
36                                                 disabled={!seed || seed.status !== 'generated' || !patch}
37                                                 onClick={() => applyPatch(
38                                                         rom,
39                                                         patch,
40                                                         `${i18n.t('alttpSeeds.filename', {
41                                                                 hash: seed.hash,
42                                                                 preset: seed.preset,
43                                                         })}.sfc`,
44                                                 )}
45                                                 variant="primary"
46                                         >
47                                                 {i18n.t(patch ? 'alttpSeeds.patch' : 'alttpSeeds.fetchingPatch')}
48                                         </Button>
49                                 :
50                                         <BaseRomButton />
51                                 }
52                         </Col>
53                         <Col md={{ order: 1 }}>
54                                 <p>
55                                         {i18n.t('alttpSeeds.preset')}:
56                                         {' '}
57                                         <strong>{i18n.t(`alttpSeeds.presets.${seed.preset}`)}</strong>
58                                 </p>
59                                 {seed.seed ?
60                                         <p>
61                                                 {i18n.t('alttpSeeds.seed')}:
62                                                 {' '}
63                                                 <strong>{seed.seed}</strong>
64                                         </p>
65                                 : null}
66                                 {seed.race ?
67                                         <p>{i18n.t('alttpSeeds.race')}</p>
68                                 : null}
69                                 {seed.mystery ?
70                                         <p>{i18n.t('alttpSeeds.mystery')}</p>
71                                 : null}
72                                 {seed.status === 'generated' ?
73                                         <p>
74                                                 {i18n.t('alttpSeeds.generated')}:
75                                                 {' '}
76                                                 <strong>
77                                                         {i18n.t('alttpSeeds.date', { date: new Date(seed.updated_at) })}
78                                                 </strong>
79                                         </p>
80                                 :
81                                         <p>
82                                                 {i18n.t('alttpSeeds.status')}:
83                                                 {' '}
84                                                 <strong>{i18n.t(`alttpSeeds.statuses.${seed.status}`)}</strong>
85                                         </p>
86                                 }
87                                 {seed.status === 'error' ?
88                                         <p>
89                                                 <Button
90                                                         onClick={onRetry}
91                                                         variant="secondary"
92                                                 >
93                                                         {i18n.t('button.retry')}
94                                                 </Button>
95                                         </p>
96                                 : null}
97                         </Col>
98                 </Row>
99                 <h2 className="mt-5">{i18n.t('alttpSeeds.generator')}</h2>
100                 <p>{i18n.t(`alttpSeeds.generators.${seed.generator}`)}</p>
101                 {seed.settings ? <>
102                         <h2 className="mt-5">{i18n.t('alttpSeeds.settings')}</h2>
103                         <Row>
104                                 {Object.entries(seed.settings).map(([key, value]) =>
105                                         <Col key={key} sm={4} md={3} lg={2} className="mb-2">
106                                                 <small className="text-muted">
107                                                         {i18n.t(`alttpSeeds.settingName.${key}`)}
108                                                 </small>
109                                                 <br />
110                                                 {isDefaultSetting(key, value) ?
111                                                         i18n.t(`alttpSeeds.settingValue.${key}.${value}`)
112                                                 :
113                                                         <strong>{i18n.t(`alttpSeeds.settingValue.${key}.${value}`)}</strong>
114                                                 }
115                                         </Col>
116                                 )}
117                         </Row>
118                 </> : null}
119         </Container>;
120 };
121
122 Seed.propTypes = {
123         onRetry: PropTypes.func,
124         patch: PropTypes.instanceOf(ArrayBuffer),
125         seed: PropTypes.shape({
126                 generator: PropTypes.string,
127                 hash: PropTypes.string,
128                 mystery: PropTypes.bool,
129                 preset: PropTypes.string,
130                 race: PropTypes.bool,
131                 seed: PropTypes.string,
132                 settings: PropTypes.shape({
133                 }),
134                 status: PropTypes.string,
135                 updated_at: PropTypes.string,
136         }),
137 };
138
139 export default withTranslation()(Seed);