Skip to content

Commit

Permalink
Initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsyuka committed Jan 7, 2018
0 parents commit 610f5ff
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.spec.js
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"extends": "standard"
};
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.vscode
.git
CVS
.svn
.hg
.lock-wscript
.wafpickle-N
.*.swp
.DS_Store
._*
npm-debug.log
.npmrc
node_modules
config.gypi
*.orig
package-lock.json
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Dillon Modine-Thuen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Osu Beatmap Parser, Difficulty and Performance Calculator

A soon to be full fledged system to allow parsing of beatmaps in .osu (or JSON built in), difficulty and performance calculations.

## Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

### Prerequisites

- Node.js 8+

### Installing

In your project add the dependency

```javascript
npm i osu-bpdpc
```

and require inside your javascript file

```javascript
const OsuBPDPC = require('osu-bpdpc');
```

or for specific elements using selective require

```javascript
const {Beatmap} = require('osu-bpdpc');
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "osu-bpdpc",
"version": "0.0.1",
"description": "Osu beatmap parser, difficulty and performance calculator",
"main": "src/index.js",
"scripts": {
"test": "mocha src/**/*.spec.js"
},
"keywords": [
"osu!",
"osu",
"osu! beatmap",
"beatmap",
"parser",
"calculator"
],
"author": "Dillon Modine-Thuen",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/Itsyuka/osu-bpdpc.git"
},
"devDependencies": {
"eslint": "^4.15.0",
"eslint-config-standard": "^11.0.0-beta.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"mocha": "^4.1.0",
"request": "^2.83.0",
"request-promise-native": "^1.0.5"
}
}
237 changes: 237 additions & 0 deletions src/Beatmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
const Colour = require('./Colour')

class Beatmap {
constructor () {
this.version = 0

this.general = {
audioFilename: null,
audioLeadIn: 0,
previewTime: 0,
countdown: false,
sampleSet: 'None', // None, Normal, Soft, etc
stackLeniency: 0,
mode: 0,
letterboxInBreaks: false,
widescreenStoryboard: false
}

this.difficulty = {
hpDrainRate: 0,
circleSize: 0,
overallDifficulty: 0,
approachRate: 0,
sliderMultiplier: 0,
sliderTickRate: 0
}

this.editor = {
bookmarks: [],
distanceSpacing: 0,
beatDivisor: 0,
gridSize: 0,
timelineZoom: 0
}

this.metadata = {
title: null,
titleUnicode: null,
artist: null,
artistUnicode: null,
creator: null,
version: null,
source: null,
tags: [],
beatmapId: 0,
beatmapSetId: 0
}

this.colours = []

this.hitObjects = []
this.timingPoints = []
}

/**
* Parses an .osu file and returns a new Beatmap instance
* @param {string|Buffer} body
* @returns {Promise<Beatmap>}
*/
static async fromOsu (body) {
if (!body) throw new Error('No beatmap found')
if (body instanceof Buffer) body = body.toString()
let beatmap = new Beatmap()
let section = null
let hitObjectsLines = []
let lines = body.split('\n').map(v => v.trim()) // Cache this for better performance of the loop
for (let line of lines) {
if (line.startsWith('\\\\')) continue // Ignore comments
if (!line) continue // Empty lines can pewf
if (!section && line.includes('osu file format v')) { // get the version of the beatmap
beatmap.version = parseInt(line.split('osu file format v')[1], 10) // Parse only as an int
continue
}
if (/^\s*\[(.+?)\]\s*$/.test(line)) {
section = /^\s*\[(.+?)\]\s*$/.exec(line)[1]
continue
}
switch (section) {
case 'General': {
let [key, value] = line.split(':').map(v => v.trim())
switch (key) {
case 'AudioFilename':
beatmap.general.audioFilename = value
break
case 'AudioLeadIn':
beatmap.general.audioLeadIn = parseInt(value, 10)
break
case 'PreviewTime':
beatmap.general.previewTime = parseInt(value, 10)
break
case 'Countdown':
beatmap.general.countdown = value === '1'
break
case 'SampleSet':
beatmap.general.sampleSet = value
break
case 'StackLeniency':
beatmap.general.stackLeniency = parseFloat(value)
break
case 'Mode':
beatmap.general.mode = parseInt(value, 10)
break
case 'LetterboxInBreaks':
beatmap.general.letterboxInBreaks = value === '1'
break
case 'WidescreenStoryboard':
beatmap.general.widescreenStoryboard = value === '1'
break
}
break
}
case 'Editor': {
let [key, value] = line.split(':').map(v => v.trim())
switch (key) {
case 'Bookmarks':
beatmap.editor.bookmarks = value.split(',').map(v => parseInt(v, 10))
break
case 'DistanceSpacing':
beatmap.editor.distanceSpacing = parseFloat(value)
break
case 'BeatDivisor':
beatmap.editor.beatDivisor = parseInt(value, 10)
break
case 'GridSize':
beatmap.editor.gridSize = parseInt(value, 10)
break
case 'TimelineZoom':
beatmap.editor.timelineZoom = parseInt(value, 10)
break
}
break
}
case 'Metadata': {
let [key, value] = line.split(':').map(v => v.trim())
switch (key) {
case 'Title':
beatmap.metadata.title = value
break
case 'TitleUnicode':
beatmap.metadata.titleUnicode = value
break
case 'Artist':
beatmap.metadata.artist = value
break
case 'ArtistUnicode':
beatmap.metadata.artistUnicode = value
break
case 'Creator':
beatmap.metadata.creator = value
break
case 'Version':
beatmap.metadata.version = value
break
case 'Source':
beatmap.metadata.source = value
break
case 'Tags':
beatmap.metadata.tags = value.split(' ')
break
case 'BeatmapID':
beatmap.metadata.beatmapId = parseInt(value, 10)
break
case 'BeatmapSetID':
beatmap.metadata.beatmapSetId = parseInt(value, 10)
break
}
break
}
case 'Difficulty': {
let [key, value] = line.split(':').map(v => v.trim())
switch (key) {
case 'HPDrainRate':
beatmap.difficulty.hpDrainRate = parseFloat(value)
break
case 'CircleSize':
beatmap.difficulty.circleSize = parseFloat(value)
break
case 'OverallDifficulty':
beatmap.difficulty.overallDifficulty = parseFloat(value)
break
case 'ApproachRate':
beatmap.difficulty.approachRate = parseFloat(value)
break
case 'SliderMultiplier':
beatmap.difficulty.sliderMultiplier = parseFloat(value)
break
case 'SliderTickRate':
beatmap.difficulty.sliderTickRate = parseFloat(value)
break
}
break
}
case 'HitObjects':
hitObjectsLines.push(line)
break
case 'TimingPoints': {
let args = line.split(',')
beatmap.timingPoints.push({
time: parseInt(args[0], 10),
beatLength: parseFloat(args[1]),
meter: args.length >= 2 ? parseInt(args[2]) : 4,
sampleSet: args.length >= 3 ? parseInt(args[3]) : 0,
sampleIndex: args.length >= 4 ? parseInt(args[4]) : 0,
volume: args.length >= 5 ? parseInt(args[5]) : 100,
inherited: args.length >= 6 ? args[6] === '1' : false,
kiai: args.length >= 7 ? args[7] === '1' : false
})
break
}
case 'Colours':
let [, value] = line.split(':').map(v => v.trim())
beatmap.colours.push(new Colour(...value.split(',')))
break
}
}
return beatmap
}

/**
* Parses a JSON string and returns a new Beatmap Instance
* @param {string} jsonData
* @returns {Promise<Beatmap>}
*/
static async fromJSON (jsonData) {
let data = JSON.parse(jsonData)
let beatmap = new Beatmap()
beatmap.version = data.version || beatmap.version
beatmap.general = {...beatmap.general, ...data.general}
beatmap.metadata = {...beatmap.metadata, ...data.metadata}
beatmap.editor = {...beatmap.editor, ...data.editor}
beatmap.colours = data.colours ? data.colours.map(c => new Colour(...c)) : []
beatmap.timingPoints = data.timingPoints || []
return beatmap
}
}

module.exports = Beatmap
30 changes: 30 additions & 0 deletions src/Beatmap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Beatmap = require('./Beatmap')
const Request = require('request-promise-native')

let testOsu
let testJson
let beatmap

before (async () => {
testMapFile = await Request.get('https://osu.ppy.sh/osu/1262832')
})

describe ('Beatmap', () => {
describe ('#fromOsu()', () => {
it ('should return a new Beatmap instance', async () => {
beatmap = await Beatmap.fromOsu(testMapFile)
return beatmap instanceof Beatmap
})

after (() => {
testJson = JSON.stringify(beatmap) // Used for 'fromJSON' function
})
})

describe ('#fromJSON()', () => {
it ('should return a new Beatmap instance', async () => {
let testJsonBeatmap = await Beatmap.fromJSON(testJson)
return testJsonBeatmap instanceof Beatmap
})
})
})
Loading

0 comments on commit 610f5ff

Please sign in to comment.