Skip to content

Commit

Permalink
Merge pull request #10 from MichaelKim/default-beatmaps
Browse files Browse the repository at this point in the history
Add default beatmaps
  • Loading branch information
MichaelKim authored Oct 25, 2021
2 parents 40a2080 + d847ebf commit ffab318
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 44 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
- skin change
- spinner bonus
- add official osu api search (server needed, add cors!)
- default beatmaps
- volume settings
4 changes: 3 additions & 1 deletion src/Game/Loader/BeatmapLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ function switchcase<T = string>(cases: Record<string, (value: T) => void>) {
return (key: string, value: T) => cases[key]?.(value);
}

export function parseBeatmap(file: string[]) {
export function parseBeatmap(text: string) {
const file = text.split('\n').map(l => l.trim());

const b: BeatmapData = { ...JSON.parse(JSON.stringify(DEFAULTS)), file };

const parseGeneral = switchcase({
Expand Down
11 changes: 3 additions & 8 deletions src/UI/Components/BeatmapUpload/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../../../Game/Loader/BeatmapLoader';
import { getFilesFromDrop } from './dragDrop';
import style from './index.module.scss';
import { Directory, getBeatmaps, loadBeatmapInfo } from './loadBeatmaps';
import { Directory, getBeatmaps, loadBeatmapDiff } from './loadBeatmaps';
import { getFilesFromInput } from './webkitDirectory';

type Props = {
Expand Down Expand Up @@ -60,14 +60,9 @@ export default function BeatmapUpload({ onSelect }: Props) {

for (const diff of beatmap.diffs) {
const text = await diff.text();
const data = parseBeatmap(text.split('\n').map(l => l.trim()));

const data = parseBeatmap(text);
if (data.mode === GameMode.STANDARD) {
const info = loadBeatmapInfo(data, beatmap.files);
diffs.push({
info,
data
});
diffs.push(loadBeatmapDiff(data, beatmap.files));
}
setProgress(p => p + 1);
}
Expand Down
19 changes: 13 additions & 6 deletions src/UI/Components/BeatmapUpload/loadBeatmaps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BeatmapDiff } from '.';
import { BeatmapFile } from '../../../Game';
import { BeatmapData } from '../../../Game/Loader/BeatmapLoader';
import { loadHitObjectsData } from '../../../Game/Loader/HitObjectLoader';
Expand Down Expand Up @@ -82,7 +83,10 @@ export function getBeatmaps(root: Directory) {
return Object.values(beatmaps);
}

export function loadBeatmapInfo(data: BeatmapData, files: BeatmapFile[]) {
export function loadBeatmapDiff(
data: BeatmapData,
files: BeatmapFile[]
): BeatmapDiff {
// Load background image
const bgFilename = data.background.filename;
const bgFile = files.find(f => f.name === bgFilename);
Expand All @@ -96,10 +100,13 @@ export function loadBeatmapInfo(data: BeatmapData, files: BeatmapFile[]) {
);

return {
creator: data.creator,
version: data.version,
stars: 0,
background,
length: (last - first) / 1000
info: {
creator: data.creator,
version: data.version,
stars: 0,
background,
length: (last - first) / 1000
},
data
};
}
51 changes: 49 additions & 2 deletions src/UI/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { BeatmapFile } from '../Game';
import { BeatmapData } from '../Game/Loader/BeatmapLoader';
import {
BeatmapData,
GameMode,
parseBeatmap
} from '../Game/Loader/BeatmapLoader';
import BeatmapListing from './Components/BeatmapListing';
import { BeatmapFiles } from './Components/BeatmapUpload';
import { loadBeatmapDiff } from './Components/BeatmapUpload/loadBeatmaps';
import Header from './Components/Header';
import OptionsContext, { Options } from './options';
import { fetchOSZ } from './util';

const DEFAULT_MAPS = [
'25828 44teru-k - F.I',
'108470 xi - Parousia',
'336099 LeaF - Wizdomiot [no video]',
'1183900 Powerless feat. Sennzai - Lost Desire',
'1281563 FELT - Vagueness & JOURNEY',
'1245686 TUYU - Kako ni Torawarete Iru [no video]',
'1329716 YOASOBI - Kaibutsu (TV Size) [no video]'
];

type Props = {
options: Options & {
Expand All @@ -21,6 +37,37 @@ export default function Menu({ options, onSelect }: Props) {
[]
);

useEffect(() => {
// Load default maps on first load
DEFAULT_MAPS.forEach(async url => {
const { diffFiles, otherFiles } = await fetchOSZ(
`assets/beatmaps/${url}.osz`
);

const texts = await Promise.all(diffFiles.map(f => f.text()));
const diffs = texts
.map(parseBeatmap)
.filter(data => data.mode === GameMode.STANDARD)
.map(data => loadBeatmapDiff(data, otherFiles));

if (diffs.length > 0) {
addBeatmaps([
{
info: {
id: diffs[0].data.beatmapSetID,
title: diffs[0].data.title,
artist: diffs[0].data.artist,
creator: diffs[0].data.creator,
background: diffs[0].info.background
},
difficulties: diffs,
files: otherFiles
}
]);
}
});
}, [addBeatmaps]);

return (
<OptionsContext.Provider value={options}>
<Header onAdd={addBeatmaps} />
Expand Down
26 changes: 13 additions & 13 deletions src/UI/Sources/CheeseGull/loadRippleBeatmaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CheeseGullBeatmap, CheeseGullSet } from '../../API/CheeseGullAPI';
import { BeatmapDiff, BeatmapFiles } from '../../Components/BeatmapUpload';
import { fetchOSZ } from '../../util';

function loadBeatmapInfo(
function loadRippleBeatmapDiff(
data: BeatmapData,
info: CheeseGullSet,
diffInfo: CheeseGullBeatmap | undefined,
Expand All @@ -20,11 +20,14 @@ function loadBeatmapInfo(
const background = bgFile != null ? URL.createObjectURL(bgFile.blob) : '';

return {
creator: info.Creator || data.creator,
version: diffInfo?.DiffName || data.version,
stars: diffInfo?.DifficultyRating || 0,
background,
length: diffInfo?.HitLength ?? 0
info: {
creator: info.Creator || data.creator,
version: diffInfo?.DiffName || data.version,
stars: diffInfo?.DifficultyRating || 0,
background,
length: diffInfo?.HitLength ?? 0
},
data
};
}

Expand All @@ -41,18 +44,15 @@ export async function fetchRipple(

for (const diff of diffFiles) {
const text = await diff.text();
const data = parseBeatmap(text.split('\n').map(l => l.trim()));
const data = parseBeatmap(text);

if (data.mode === GameMode.STANDARD) {
const diffInfo = cheeseGullInfo.ChildrenBeatmaps.find(
d => d.DiffName === data.version
);
const info = loadBeatmapInfo(data, cheeseGullInfo, diffInfo, otherFiles);

diffs.push({
info,
data
});
diffs.push(
loadRippleBeatmapDiff(data, cheeseGullInfo, diffInfo, otherFiles)
);
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/UI/Sources/Sayobot/loadSayobotBeatmaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SayobotBeatmapInfo, SayobotDiffInfo } from '../../API/SayobotAPI';
import { BeatmapDiff, BeatmapFiles } from '../../Components/BeatmapUpload';
import { fetchOSZ } from '../../util';

function loadBeatmapInfo(
function loadSayobotBeatmapDiff(
data: BeatmapData,
info: SayobotBeatmapInfo,
diffInfo: SayobotDiffInfo | undefined,
Expand All @@ -20,11 +20,14 @@ function loadBeatmapInfo(
const background = bgFile != null ? URL.createObjectURL(bgFile.blob) : '';

return {
creator: info.creator || data.creator,
version: diffInfo?.version || data.version,
stars: diffInfo?.star || 0,
background,
length: diffInfo?.length ?? 0
info: {
creator: info.creator || data.creator,
version: diffInfo?.version || data.version,
stars: diffInfo?.star || 0,
background,
length: diffInfo?.length ?? 0
},
data
};
}

Expand All @@ -40,18 +43,15 @@ export async function fetchSayobot(

for (const diff of diffFiles) {
const text = await diff.text();
const data = parseBeatmap(text.split('\n').map(l => l.trim()));
const data = parseBeatmap(text);

if (data.mode === GameMode.STANDARD) {
const diffInfo = sayobotInfo.bid_data.find(
d => d.version === data.version
);
const info = loadBeatmapInfo(data, sayobotInfo, diffInfo, otherFiles);

diffs.push({
info,
data
});
diffs.push(
loadSayobotBeatmapDiff(data, sayobotInfo, diffInfo, otherFiles)
);
}
}

Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const CssoWebpackPlugin = require('csso-webpack-plugin').default;
const fs = require('fs');

module.exports = async (env, argv) => {
const isDev = argv.mode !== 'production';
Expand Down

0 comments on commit ffab318

Please sign in to comment.