]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/aos/Seed.js
3e6ac4af4e11bce9575ac8221fa0610016beb9af
[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 = ({ 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                         </Col>
129                 </Row>
130                 <h2 className="mt-5">{i18n.t('aosSeeds.generator')}</h2>
131                 <p>{i18n.t(`aosSeeds.generators.${seed.generator}`)}</p>
132                 {seed.settings ? <>
133                         <h2 className="mt-5">{i18n.t('aosSeeds.settings')}</h2>
134                         <Row>
135                                 {Object.entries(seed.settings).map(([key, value]) =>
136                                         <Col key={key} sm={4} md={3} lg={2} className="mb-2">
137                                                 <small className="text-muted">
138                                                         {i18n.t(`aosSeeds.settingName.${key}`)}
139                                                 </small>
140                                                 <br />
141                                                 {isDefaultSetting(key, value) ?
142                                                         i18n.t(`aosSeeds.settingValue.${key}.${value}`)
143                                                 :
144                                                         <strong>{i18n.t(`aosSeeds.settingValue.${key}.${value}`)}</strong>
145                                                 }
146                                         </Col>
147                                 )}
148                         </Row>
149                 </> : null}
150         </Container>;
151 };
152
153 Seed.propTypes = {
154         patch: PropTypes.instanceOf(ArrayBuffer),
155         seed: PropTypes.shape({
156                 generator: PropTypes.string,
157                 hash: PropTypes.string,
158                 mystery: PropTypes.bool,
159                 preset: PropTypes.string,
160                 race: PropTypes.bool,
161                 seed: PropTypes.string,
162                 settings: PropTypes.shape({
163                 }),
164                 status: PropTypes.string,
165                 updated_at: PropTypes.string,
166         }),
167 };
168
169 export default withTranslation()(Seed);