]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/aos/Seed.js
513bdd380bddcd3a3588aaddc2494f275f3fd0f8
[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 isDefaultSetting = (name, value) => {
26         switch (name) {
27                 case 'area':
28                         return value === 'Vanilla';
29                 case 'boss':
30                         return value === 'Vanilla';
31                 case 'doublechaos':
32                         return value === 'false';
33                 case 'enemy':
34                         return value === 'Vanilla';
35                 case 'grahm':
36                         return value === 'BookSouls';
37                 case 'itempool':
38                         return value === 'Standard';
39                 case 'kicker':
40                         return value === 'false';
41                 case 'levelexp':
42                         return value === 'Vanilla';
43                 case 'logic':
44                         return value === 'AreaTechTiers';
45                 case 'mapassist':
46                         return value === 'false';
47                 case 'nodupes':
48                         return value === 'false';
49                 case 'noww':
50                         return value === 'false';
51                 case 'palette':
52                         return value === 'Vanilla';
53                 case 'panther':
54                         return value === 'Rand70Dup';
55                 case 'reqallsouls':
56                         return value === 'false';
57                 case 'shopprice':
58                         return value === 'Vanilla';
59                 case 'shopSouls':
60                         return value === 'Vanilla';
61                 case 'startshop':
62                         return value === 'Vanilla';
63                 case 'telestart':
64                         return value === 'false';
65                 case 'weight':
66                         return value === '2.5';
67                 default:
68                         return false;
69         }
70 };
71
72 const Seed = ({ onRetry, patch, seed }) => {
73         const { rom } = useAosBaseRom();
74
75         return <Container>
76                 <h1>{i18n.t('aosSeeds.heading')}</h1>
77                 <Row>
78                         <Col md={{ order: 2 }}>
79                                 {rom ?
80                                         <Button
81                                                 disabled={!seed || seed.status !== 'generated' || !patch}
82                                                 onClick={() => applyPatch(
83                                                         rom,
84                                                         patch,
85                                                         `${i18n.t('aosSeeds.filename', {
86                                                                 hash: seed.hash,
87                                                                 preset: seed.preset,
88                                                         })}.gba`,
89                                                 )}
90                                                 variant="primary"
91                                         >
92                                                 {i18n.t(patch ? 'aosSeeds.patch' : 'aosSeeds.fetchingPatch')}
93                                         </Button>
94                                 :
95                                         <BaseRomButton />
96                                 }
97                         </Col>
98                         <Col md={{ order: 1 }}>
99                                 <p>
100                                         {i18n.t('aosSeeds.preset')}:
101                                         {' '}
102                                         <strong>{i18n.t(`aosSeeds.presets.${seed.preset}`)}</strong>
103                                 </p>
104                                 {seed.seed ?
105                                         <p>
106                                                 {i18n.t('aosSeeds.seed')}:
107                                                 {' '}
108                                                 <strong>{seed.seed}</strong>
109                                         </p>
110                                 : null}
111                                 {seed.race ?
112                                         <p>{i18n.t('aosSeeds.race')}</p>
113                                 : null}
114                                 {seed.mystery ?
115                                         <p>{i18n.t('aosSeeds.mystery')}</p>
116                                 : null}
117                                 {seed.status === 'generated' ?
118                                         <p>
119                                                 {i18n.t('aosSeeds.generated')}:
120                                                 {' '}
121                                                 <strong>
122                                                         {i18n.t('aosSeeds.date', { date: new Date(seed.updated_at) })}
123                                                 </strong>
124                                         </p>
125                                 :
126                                         <p>
127                                                 {i18n.t('aosSeeds.status')}:
128                                                 {' '}
129                                                 <strong>{i18n.t(`aosSeeds.statuses.${seed.status}`)}</strong>
130                                         </p>
131                                 }
132                                 {seed.status === 'error' ?
133                                         <p>
134                                                 <Button
135                                                         onClick={onRetry}
136                                                         variant="secondary"
137                                                 >
138                                                         {i18n.t('button.retry')}
139                                                 </Button>
140                                         </p>
141                                 : null}
142                         </Col>
143                 </Row>
144                 <h2 className="mt-5">{i18n.t('aosSeeds.generator')}</h2>
145                 <p>{i18n.t(`aosSeeds.generators.${seed.generator}`)}</p>
146                 {seed.settings ? <>
147                         <h2 className="mt-5">{i18n.t('aosSeeds.settings')}</h2>
148                         <Row>
149                                 {Object.entries(seed.settings).map(([key, value]) =>
150                                         <Col key={key} sm={4} md={3} lg={2} className="mb-2">
151                                                 <small className="text-muted">
152                                                         {i18n.t(`aosSeeds.settingName.${key}`)}
153                                                 </small>
154                                                 <br />
155                                                 {isDefaultSetting(key, value) ?
156                                                         i18n.t(`aosSeeds.settingValue.${key}.${value}`)
157                                                 :
158                                                         <strong>{i18n.t(`aosSeeds.settingValue.${key}.${value}`)}</strong>
159                                                 }
160                                         </Col>
161                                 )}
162                         </Row>
163                 </> : null}
164         </Container>;
165 };
166
167 Seed.propTypes = {
168         onRetry: PropTypes.func,
169         patch: PropTypes.instanceOf(ArrayBuffer),
170         seed: PropTypes.shape({
171                 generator: PropTypes.string,
172                 hash: PropTypes.string,
173                 mystery: PropTypes.bool,
174                 preset: PropTypes.string,
175                 race: PropTypes.bool,
176                 seed: PropTypes.string,
177                 settings: PropTypes.shape({
178                 }),
179                 status: PropTypes.string,
180                 updated_at: PropTypes.string,
181         }),
182 };
183
184 export default withTranslation()(Seed);