]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/aos/Seed.js
efc113015eb2990217126043d395191305162e82
[alttp.git] / resources / js / components / aos / 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 { useAosBaseRom } from '../../helpers/AosBaseRomContext';
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('aosSeeds.patchError', { msg: e.message }));
22         }
23 };
24
25 const Seed = ({ patch, seed }) => {
26         const { rom } = useAosBaseRom();
27
28         return <Container>
29                 <h1>{i18n.t('aosSeeds.heading')}</h1>
30                 <Row>
31                         <Col md={{ order: 2 }}>
32                                 {rom ?
33                                         <Button
34                                                 disabled={!seed || seed.status !== 'generated' || !patch}
35                                                 onClick={() => applyPatch(
36                                                         rom,
37                                                         patch,
38                                                         `${i18n.t('aosSeeds.filename', {
39                                                                 hash: seed.hash,
40                                                                 preset: seed.preset,
41                                                         })}.gba`,
42                                                 )}
43                                                 variant="primary"
44                                         >
45                                                 {i18n.t(patch ? 'aosSeeds.patch' : 'aosSeeds.fetchingPatch')}
46                                         </Button>
47                                 :
48                                         <BaseRomButton />
49                                 }
50                         </Col>
51                         <Col md={{ order: 1 }}>
52                                 <p>
53                                         {i18n.t('aosSeeds.preset')}:
54                                         {' '}
55                                         <strong>{i18n.t(`aosSeeds.presets.${seed.preset}`)}</strong>
56                                 </p>
57                                 {seed.seed ?
58                                         <p>
59                                                 {i18n.t('aosSeeds.seed')}:
60                                                 {' '}
61                                                 <strong>{seed.seed}</strong>
62                                         </p>
63                                 : null}
64                                 {seed.race ?
65                                         <p>{i18n.t('aosSeeds.race')}</p>
66                                 : null}
67                                 {seed.mystery ?
68                                         <p>{i18n.t('aosSeeds.mystery')}</p>
69                                 : null}
70                                 {seed.status === 'generated' ?
71                                         <p>
72                                                 {i18n.t('aosSeeds.generated')}:
73                                                 {' '}
74                                                 <strong>
75                                                         {i18n.t('aosSeeds.date', { date: new Date(seed.updated_at) })}
76                                                 </strong>
77                                         </p>
78                                 :
79                                         <p>
80                                                 {i18n.t('aosSeeds.status')}:
81                                                 {' '}
82                                                 <strong>{i18n.t(`aosSeeds.statuses.${seed.status}`)}</strong>
83                                         </p>
84                                 }
85                         </Col>
86                 </Row>
87                 <h2 className="mt-5">{i18n.t('aosSeeds.generator')}</h2>
88                 <p>{i18n.t(`aosSeeds.generators.${seed.generator}`)}</p>
89         </Container>;
90 };
91
92 Seed.propTypes = {
93         patch: PropTypes.instanceOf(ArrayBuffer),
94         seed: PropTypes.shape({
95                 generator: PropTypes.string,
96                 hash: PropTypes.string,
97                 mystery: PropTypes.bool,
98                 preset: PropTypes.string,
99                 race: PropTypes.bool,
100                 seed: PropTypes.string,
101                 status: PropTypes.string,
102                 updated_at: PropTypes.string,
103         }),
104 };
105
106 export default withTranslation()(Seed);