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