Skip to content

Commit

Permalink
Use felte for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik99999 committed Feb 25, 2024
1 parent 4d88ee2 commit 71417f0
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 319 deletions.
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@sveltejs/adapter-static": "^3.0.1",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"felte": "^1.2.14",
"prettier": "^3.2.5",
"prettier-plugin-svelte": "^3.2.1",
"svelte": "^4.2.7",
Expand Down
12 changes: 6 additions & 6 deletions src/lib/generators/rt/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function toID(str?: string): string {

const Data = {
pokemon: {
all(): PokemonData[] {
return RomData.pokemon.map((pokemon, i) => ({ ...pokemon, index: i }));
all(validOnly = false): PokemonData[] {
return RomData.pokemon.filter((p) => !validOnly || p.valid).map((p, index) => ({ ...p, index }));
},
get(indexOrName: number | string) {
let pokemon: PokemonData | undefined;
Expand Down Expand Up @@ -67,8 +67,8 @@ const Data = {
},
},
dungeons: {
all(): DungeonData[] {
return RomData.dungeons.map((dungeon, i) => ({ ...dungeon, index: i }));
all(validOnly = false): DungeonData[] {
return RomData.dungeons.filter((d) => !validOnly || d.valid).map((d, index) => ({ ...d, index }));
},
get(indexOrName: number | string) {
let dungeon: DungeonData | undefined;
Expand Down Expand Up @@ -123,8 +123,8 @@ const Data = {
},
},
items: {
all(): ItemData[] {
return RomData.items.map((item, i) => ({ ...item, index: i }));
all(validOnly = false): ItemData[] {
return RomData.items.filter((i) => !validOnly || i.valid).map((i, index) => ({ ...i, index }));
},
get(indexOrName: number | string) {
let item: ItemData | undefined;
Expand Down
84 changes: 43 additions & 41 deletions src/lib/generators/rt/wm/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function serialize(data: WonderMailData) {
writer.write(data.missionType, 3);
writer.write(0, 4); // Unknown
writer.write(data.clientPokemon, 9);
writer.write(data.targetPokemon || data.clientPokemon, 9);
writer.write(data.targetPokemon, 9);
writer.write(data.itemToFindOrDeliver || 9, 8);
writer.write(data.rewardType, 4);
writer.write(data.itemReward || 9, 8);
Expand All @@ -47,66 +47,68 @@ export function serialize(data: WonderMailData) {
return shuffled.join('');
}

export function generateWonderMail(
missionType: number,
clientPokemon: number,
targetPokemon: number,
dungeon: number,
floor: number,
itemToFindOrDeliver?: number,
itemReward?: number,
moneyReward?: boolean,
friendAreaReward?: number
) {
if (!Data.pokemon.get(clientPokemon).valid) throw new Error('Invalid client Pokemon');
if (missionType === 1 || missionType === 2) {
if (!Data.pokemon.get(targetPokemon).valid) throw new Error('Invalid target Pokemon');
export function generateWonderMail(data: {
missionType: number;
clientPokemon: number;
targetPokemon: number;
dungeon: number;
floor: number;
itemToFindOrDeliver?: number;
itemReward?: number;
moneyReward?: boolean;
friendAreaReward?: number;
}) {
if (!Data.pokemon.get(data.clientPokemon).valid) throw new Error('Invalid client Pokemon');
if (data.missionType === 1 || data.missionType === 2) {
if (!Data.pokemon.get(data.targetPokemon).valid) throw new Error('Invalid target Pokemon');
} else {
data.targetPokemon = data.clientPokemon;
}

const dungeonData = Data.dungeons.get(dungeon);
const dungeonData = Data.dungeons.get(data.dungeon);
if (!dungeonData.valid) throw new Error('Invalid dungeon');
if (floor < 1 || floor > dungeonData.floors) throw new Error('Invalid floor');
if (data.floor < 1 || data.floor > dungeonData.floors) throw new Error('Invalid floor');

if (missionType === 3 || missionType === 4) {
if (!itemToFindOrDeliver) throw new Error('Item to find/deliver is required for this mission type');
const itemToFindOrDeliverData = Data.items.get(itemToFindOrDeliver);
if (data.missionType === 3 || data.missionType === 4) {
if (!data.itemToFindOrDeliver) throw new Error('Item to find/deliver is required for this mission type');
const itemToFindOrDeliverData = Data.items.get(data.itemToFindOrDeliver);
if (
!itemToFindOrDeliverData.valid ||
(missionType === 3 && !dungeonData.items.includes(itemToFindOrDeliverData.index))
(data.missionType === 3 && !dungeonData.items.includes(itemToFindOrDeliverData.index))
) {
throw new Error('Invalid item to find/deliver');
}
} else {
itemToFindOrDeliver = 9;
data.itemToFindOrDeliver = 9;
}

if (itemReward && !Data.items.get(itemReward).valid) throw new Error('Invalid item reward');
if (friendAreaReward) {
if (![10, 14, 35, 36].includes(friendAreaReward)) throw new Error('Invalid Friend Area reward');
if (Data.dungeons.getDifficulty(dungeonData, floor, missionType) === 0) {
if (data.itemReward && !Data.items.get(data.itemReward).valid) throw new Error('Invalid item reward');
if (data.friendAreaReward) {
if (![10, 14, 35, 36].includes(data.friendAreaReward)) throw new Error('Invalid Friend Area reward');
if (Data.dungeons.getDifficulty(dungeonData, data.floor, data.missionType) === 0) {
throw new Error('The mission must have at least D difficulty to recieve a Friend Area reward');
}
}

let rewardType = 5;
if (friendAreaReward) {
if (data.friendAreaReward) {
rewardType = 9;
itemReward = 9;
} else if (itemReward) {
rewardType = moneyReward ? 6 : 8;
friendAreaReward = 0;
data.itemReward = 9;
} else if (data.itemReward) {
rewardType = data.moneyReward ? 6 : 8;
data.friendAreaReward = 0;
}

const data: WonderMailData = {
missionType,
clientPokemon,
targetPokemon,
itemToFindOrDeliver,
const wmData: WonderMailData = {
missionType: data.missionType,
clientPokemon: data.clientPokemon,
targetPokemon: data.targetPokemon,
itemToFindOrDeliver: data.itemToFindOrDeliver,
rewardType,
itemReward,
friendAreaReward,
dungeon,
floor,
itemReward: data.itemReward,
friendAreaReward: data.friendAreaReward,
dungeon: data.dungeon,
floor: data.floor,
};
return serialize(data);
return serialize(wmData);
}
6 changes: 4 additions & 2 deletions src/lib/generators/rtdx/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ function toID(str?: string): string {

const Data = {
pokemon: {
all() {
all(validOnly = false) {
const pokemonList: PokemonData[] = [];
for (const [i, pokemon] of RomData.pokemon.entries()) {
if (validOnly && !pokemon.valid) continue;
let pokemonName = pokemon.name;
if (pokemonName === 'Unown') {
pokemonName += ` (${pokemon.const.slice(7).replace('EXC', '!').replace('QUE', '?')})`;
Expand Down Expand Up @@ -82,9 +83,10 @@ const Data = {
},

dungeons: {
all() {
all(validOnly = false) {
const dungeons: DungeonData[] = [];
for (const [i, dungeon] of RomData.dungeons.entries()) {
if (validOnly && !dungeon.valid) continue;
const index = parseInt(dungeon.const.slice(1));
dungeons.push({
name: dungeon.name,
Expand Down
43 changes: 12 additions & 31 deletions src/lib/generators/rtdx/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { symbols, checksum, RNG } from './utils';
import { read } from './read';
import Data from './data';

/**
* Dhuffles the password symbols
*/
function shuffle(code: string[]): string[] {
const unshuffledIndex = [
3, 27, 13, 21, 12, 9, 7, 4, 6, 17, 19, 16, 28, 29, 23, 20, 11, 0, 1, 22, 24, 14, 8, 2, 15, 25, 10, 5, 18, 26,
Expand All @@ -17,16 +14,10 @@ function shuffle(code: string[]): string[] {
return shuffled;
}

/**
* Converts indexes to symbols
*/
function toSymbols(indexes: number[]): string[] {
return indexes.map((i) => symbols[i]);
}

/**
* Encrypt the code using rng
*/
function encrypt(code: number[]): number[] {
const newcode = code.slice(0, 2);
const seed = code[0] | (code[1] << 8);
Expand All @@ -40,9 +31,6 @@ function encrypt(code: number[]): number[] {
return newcode;
}

/**
* Data needed to generate a password
*/
interface PasswordData {
timestamp: number;
team: number[];
Expand Down Expand Up @@ -103,46 +91,39 @@ function serialize(data: RescueData | RevivalData): string {
* Generates a rescue password. The timestamp, gender of the pokemon being rescued, and the reward type
* are all hardcoded, since you rarely would want to change them.
*/
export function generateRescue(
dungeonNameOrIndex: string | number,
floor: number,
teamName = 'pmd-gen',
pokemonNameOrIndex: string | number = 1
): string {
if (teamName.length < 1 || teamName.length > 12) throw new Error('Team name must be between 1 and 12 characters');
export function generateRescue(data: { team: string; dungeon: number; floor: number; pokemon: number }): string {
if (data.team.length < 1 || data.team.length > 12) throw new Error('Team name must be between 1 and 12 characters');
const team: number[] = [];
for (const char of teamName) {
for (const char of data.team) {
const index = Data.charmap_text.indexOf(char);
if (index < 0) {
throw new Error(`Invalid character in team name: ${char}`);
}
team.push(index);
}
const dungeon = Data.dungeons.get(dungeonNameOrIndex);
if (!dungeon) {
const dungeonData = Data.dungeons.get(data.dungeon);
if (!dungeonData.valid) {
throw new Error('Invalid dungeon');
}
if (floor < 1 || floor > dungeon.floors) {
if (data.floor < 1 || data.floor > dungeonData.floors) {
throw new Error('Invalid floor');
}

const pokemon = Data.pokemon.get(pokemonNameOrIndex);
// It's probably best to let the user know the Pokemon name they sent was invalid, rather than going to the default
if (!pokemon.valid) {
if (!Data.pokemon.get(data.pokemon).valid) {
throw new Error('Invalid Pokemon');
}

const data: RescueData = {
const rescueData: RescueData = {
timestamp: Math.round(Date.now() / 1000),
type: 0,
team,
dungeon: dungeon.index,
floor,
pokemon: pokemon.index,
dungeon: data.dungeon,
floor: data.floor,
pokemon: data.pokemon,
gender: 0,
reward: 3,
};
return serialize(data);
return serialize(rescueData);
}

/**
Expand Down
48 changes: 25 additions & 23 deletions src/routes/(rt)/rt-sosconverter/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,37 @@
<li>For the ellipsis (…), you may use "."</li>
</ul>

<h6>SOS to A-OK</h6>
<textarea class="form-control" rows="3" bind:value={sosInput} />
<button class="btn btn-primary" on:click={convertSOS}>Convert</button>
<textarea class="form-control" rows="3" readonly>{aokResult || ''}</textarea>
<div class="mb-3">
<div class="form-group">
<label for="sos">SOS to A-OK:</label>
<textarea class="form-control" rows="3" bind:value={sosInput} />
</div>
<button class="btn btn-primary" on:click={convertSOS}>Convert</button>
</div>
<textarea class="form-control" rows="3" readonly bind:value={aokResult} />

<h6>A-OK to Thank-You</h6>
<textarea class="form-control" rows="3" bind:value={aokInput} />
<label for="item-reward">Item reward:</label>
<select class="form-control" id="item-reward" bind:value={itemReward}>
<option selected value></option>
{#each RT.data.items.all() as item}
{#if item.valid}
<option value={item.index}>{item.name}</option>
{/if}
{/each}
</select>
<button class="btn btn-primary" on:click={convertAOK}>Convert</button>
<textarea class="form-control" rows="3" readonly>{thankYouResult || ''}</textarea>
<div class="mt-3 mb-3">
<div class="form-group">
<label for="sos">A-OK to Thank-You:</label>
<textarea class="form-control" rows="3" bind:value={aokInput} />
</div>
<div class="form-group">
<label for="item-reward">Item reward:</label>
<select class="form-control" id="item-reward" bind:value={itemReward}>
<option selected value></option>
{#each RT.data.items.all(true) as item}
<option value={item.index}>{item.name}</option>
{/each}
</select>
</div>
<button class="btn btn-primary" on:click={convertAOK}>Convert</button>
</div>
<textarea class="form-control" rows="3" readonly bind:value={thankYouResult} />

<style>
textarea {
resize: none;
overflow: hidden;
font-family: monospace;
}
button,
select,
textarea {
margin-bottom: 2%;
}
</style>
Loading

0 comments on commit 71417f0

Please sign in to comment.