Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up online ranking code #825

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bemuse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
"@reduxjs/toolkit": "^1.9.1",
"auth0-js": "^9.8.0",
"axios": "^1.1.3",
"baconjs": "^0.7.95",
"bemuse-indexer": "^51.0.2",
"bemuse-notechart": "^50.1.2",
"bemuse-types": "^50.0.2",
Expand Down
79 changes: 7 additions & 72 deletions bemuse/src/app/ui/RankingContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,89 +1,26 @@
import { RankingState, RankingStream } from 'bemuse/online'
import { RankingState } from 'bemuse/online'
import {
useCurrentUser,
useLeaderboardQuery,
usePersonalRankingEntryQuery,
useRecordSubmissionMutation,
} from 'bemuse/online/hooks'
import React, { useContext, useEffect, useRef, useState } from 'react'
import React, { useEffect, useRef } from 'react'

import { Operation, completed, error, loading } from 'bemuse/online/operations'
import { ScoreCount } from 'bemuse/rules/accuracy'
import { MappingMode } from 'bemuse/rules/mapping-mode'
import { OnlineContext } from 'bemuse/online/instance'
import { UseMutationResult, UseQueryResult } from 'react-query'
import Ranking from './Ranking'
import { Result } from './ResultScene'
import { ScoreCount } from 'bemuse/rules/accuracy'
import { isQueryFlagEnabled } from 'bemuse/flags'
import { UseMutationResult, UseQueryResult } from 'react-query'
import { Operation, completed, error, loading } from 'bemuse/online/operations'

export interface RankingContainerProps {
chart: { md5: string }
playMode: MappingMode
result?: Result
}

/** @deprecated */
export const OldRankingContainer = ({
chart,
playMode,
result,
}: RankingContainerProps) => {
const online = useContext(OnlineContext)
const [state, setState] = useState<RankingState>({
data: null,
meta: {
scoreboard: { status: 'loading' },
submission: { status: 'loading' },
},
})
const model = useRef<RankingStream | null>(null)
useEffect(() => {
const onStoreTrigger = (newState: RankingState) =>
setState(() => ({ ...newState }))
const params = {
md5: chart.md5,
playMode: playMode,
...(result
? {
score: result.score,
combo: result.maxCombo,
total: result.totalCombo,
count: [
result['1'],
result['2'],
result['3'],
result['4'],
result.missed,
] as ScoreCount,
log: result.log,
}
: {}),
}
model.current = online.Ranking(params)
const subscription = model.current.state川.subscribe(onStoreTrigger)
return () => {
subscription.unsubscribe()
}
}, [])

const onReloadScoreboardRequest = () => {
model.current?.reloadScoreboard()
}

const onResubmitScoreRequest = () => {
model.current?.resubmit()
}

return (
<Ranking
state={state}
onReloadScoreboardRequest={onReloadScoreboardRequest}
onResubmitScoreRequest={onResubmitScoreRequest}
/>
)
}

export const NewRankingContainer = ({
export const RankingContainer = ({
chart,
playMode,
result,
Expand Down Expand Up @@ -154,6 +91,4 @@ function operationFromResult<T>(
return completed(result.data!)
}

export default (isQueryFlagEnabled('old-ranking')
? OldRankingContainer
: NewRankingContainer) as FC<RankingContainerProps>
export default RankingContainer as FC<RankingContainerProps>
90 changes: 0 additions & 90 deletions bemuse/src/flux/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion bemuse/src/omni-input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class OmniInput {
}
}

// Public: Returns a Bacon EventStream of keys pressed.
// Public: Returns an RxJS Observable of keys pressed.
//
export function key川(
input = new OmniInput(),
Expand Down
114 changes: 17 additions & 97 deletions bemuse/src/online/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function tests(onlineServiceOptions) {
this.timeout(20000)

describe('signup', function () {
/** @type {ReturnType<typeof createOnline>} */
let online
let info
before(function () {
Expand All @@ -75,48 +76,42 @@ function tests(onlineServiceOptions) {
})

describe('initially', function () {
/** @type {ReturnType<typeof createOnline>} */
let online
before(async () => {
await createOnline().logOut()
})
beforeEach(async () => {
online = createOnline()
})
describe('user川', function () {
it('should be null', function (done) {
online.user川.pipe(first()).subscribe((user) => {
assert(user === null)
done()
})
describe('getCurrentUser', function () {
it('should return null', function () {
assert.equal(online.getCurrentUser(), null)
})
})
})

describe('when signed up', function () {
/** @type {ReturnType<typeof createOnline>} */
let online
before(function () {
online = createOnline()
})
describe('user川', function () {
describe('getCurrentUser', function () {
it('should change to signed-up user, and also start with it', async function () {
const info = createAccountInfo()

await online.signUp(info)
const user = online.getCurrentUser()
assert.equal(user.username, info.username)

const user = await firstValueFrom(
online.user川.pipe(filter((u) => !!u))
)
expect(user.username).to.equal(info.username)

const firstUser = await firstValueFrom(
createOnline().user川.pipe(filter((u) => !!u))
)
expect(firstUser.username).to.equal(info.username)
const anotherUser = createOnline().getCurrentUser()
assert.equal(anotherUser.username, info.username)
})
})
})

describe('with an active user', function () {
/** @type {ReturnType<typeof createOnline>} */
let online
const info = createAccountInfo()
before(function () {
Expand All @@ -127,19 +122,15 @@ function tests(onlineServiceOptions) {
return online.logIn(info)
})
describe('when log out', function () {
it('should change user川 back to null', async function () {
online.logOut()

const user = await firstValueFrom(
online.user川.pipe(filter((u) => !u)).pipe(first())
)

assert(user === null)
it('should change getCurrentUser result back to null', async function () {
await online.logOut()
assert.equal(online.getCurrentUser(), null)
})
})
})

describe('submitting high scores', function () {
/** @type {ReturnType<typeof createOnline>} */
let online
before(function () {
online = createOnline()
Expand Down Expand Up @@ -243,6 +234,7 @@ function tests(onlineServiceOptions) {
})

describe('the scoreboard', function () {
/** @type {ReturnType<typeof createOnline>} */
let online
before(function () {
online = createOnline()
Expand Down Expand Up @@ -295,78 +287,6 @@ function tests(onlineServiceOptions) {
step('log out...', function () {
return online.logOut()
})

let ranking
step('subscribe to scoreboard...', function () {
ranking = online.Ranking({
md5: prefix + 'song1',
playMode: 'BM',
score: 111111,
combo: 123,
total: 456,
count: [0, 123, 0, 0, 333],
log: '',
})
})

function when(predicate) {
return firstValueFrom(ranking.state川.pipe(filter(predicate)))
}

step('should have scoreboard loading status', function () {
return when((state) => state.meta.scoreboard.status === 'loading')
})
step('no new score should be submitted', function () {
return when(
(state) =>
state.meta.scoreboard.status === 'completed' &&
state.meta.submission.status === 'unauthenticated'
).then((state) => {
expect(state.data).to.have.length(2)
})
})
step('sign up user3...', function () {
return online.signUp(user3)
})
step('should start sending score', function () {
return when((state) => state.meta.submission.status === 'loading')
})
step('should finish sending score', function () {
return when(
(state) => state.meta.submission.status === 'completed'
).then((state) => {
expect(state.meta.submission.value.rank).to.equal(3)
})
})
step('should start loading scoreboard', function () {
return when((state) => state.meta.scoreboard.status === 'loading')
})
step('should finish reloading scoreboard', function () {
return when(
(state) => state.meta.scoreboard.status === 'completed'
).then((state) => {
expect(state.data).to.have.length(3)
})
})
step('resubscribe with read only', function () {
ranking = online.Ranking({
md5: prefix + 'song1',
playMode: 'BM',
})
})
step('should not submit new score', function () {
return when(
(state) =>
state.meta.scoreboard.status === 'completed' &&
state.meta.submission.status === 'completed'
).then(function (state) {
expect(state.data).to.have.length(3)
expect(state.meta.submission.value.playCount).to.equal(1)
})
})
after(function () {
online.logOut()
})
})
})
})
Expand Down
Loading