forked from PrismarineJS/prismarine-web-client
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
165 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
.scoreboard-container { | ||
z-index: -2; | ||
pointer-events: none; | ||
white-space: nowrap; | ||
position: fixed; | ||
right: 0px; | ||
top: 50%; | ||
transform: translateY(-50%); | ||
background-color: rgba(0, 0, 0, 0.4); | ||
min-width: 100px; | ||
max-width: 400px; | ||
display: flex; | ||
flex-direction: column; | ||
padding-inline: 5px; | ||
font-size: 0.5rem; | ||
max-height: 70%; | ||
overflow: hidden; | ||
} | ||
|
||
@media (max-width: 800px) { | ||
.scoreboard-container { | ||
font-size: 0.4rem; | ||
} | ||
} | ||
|
||
.scoreboard-title { | ||
text-align: center; | ||
} | ||
|
||
.item-container { | ||
display: flex; | ||
} | ||
|
||
.item-value { | ||
margin-left: auto; | ||
color: lightcoral; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
|
||
import Scoreboard from './Scoreboard' | ||
|
||
const meta: Meta<typeof Scoreboard> = { | ||
component: Scoreboard | ||
} | ||
|
||
export default meta | ||
type Story = StoryObj<typeof Scoreboard>; | ||
|
||
export const Primary: Story = { | ||
args: { | ||
title: 'Scoreboard', | ||
items: [ | ||
{ | ||
name: 'item 1', | ||
value: 9 | ||
}, | ||
{ | ||
name: 'item 2', | ||
value: 8 | ||
} | ||
], | ||
open: true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import './Scoreboard.css' | ||
import MessageFormattedString from './MessageFormattedString' | ||
|
||
|
||
export type ScoreboardItems = Array<{name: string, value: number, displayName?: any}> | ||
|
||
type ScoreboardProps = { | ||
title: string, | ||
items: ScoreboardItems, | ||
open: boolean | ||
} | ||
|
||
export const reactKeyForMessage = (message) => { | ||
return typeof message === 'string' ? message : JSON.stringify(message) | ||
} | ||
|
||
export default function Scoreboard ({ title, items, open }: ScoreboardProps) { | ||
|
||
if (!open) return null | ||
return ( | ||
<div className='scoreboard-container'> | ||
<div className='scoreboard-title'> | ||
<MessageFormattedString message={title} /> | ||
</div> | ||
{ | ||
items.map((item) => { | ||
const message = item.displayName ?? item.name | ||
return <div key={reactKeyForMessage(message) + '_' + item.value} className='item-container'> | ||
<div className='item-name'> | ||
<MessageFormattedString message={message} /> | ||
</div> | ||
<div className='item-value'> | ||
{item.value} | ||
</div> | ||
</div> | ||
}) | ||
} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useMemo, useState } from 'react' | ||
import Scoreboard from './Scoreboard' | ||
import type { ScoreboardItems } from './Scoreboard' | ||
|
||
|
||
export default function ScoreboardProvider () { | ||
const [title, setTitle] = useState('Scoreboard') | ||
const [items, setItems] = useState<ScoreboardItems>([]) | ||
const [open, setOpen] = useState(false) | ||
|
||
useMemo(() => { // useMemo instead of useEffect to register them asap and not after the initial dom render | ||
const updateSidebarScoreboard = () => { | ||
if (bot.scoreboard.sidebar) { | ||
setTitle(bot.scoreboard.sidebar.title) | ||
setItems([...bot.scoreboard.sidebar.items]) | ||
setOpen(true) | ||
} else { | ||
setOpen(false) | ||
} | ||
} | ||
|
||
bot.on('scoreboardCreated', updateSidebarScoreboard) // not used atm but still good to have | ||
bot.on('scoreboardTitleChanged', updateSidebarScoreboard) | ||
bot.on('scoreUpdated', updateSidebarScoreboard) | ||
bot.on('scoreRemoved', updateSidebarScoreboard) | ||
bot.on('scoreboardDeleted', updateSidebarScoreboard) | ||
bot.on('scoreboardPosition', () => { | ||
void Promise.resolve().then(() => { | ||
updateSidebarScoreboard() | ||
}) // mineflayer bug: wait for the next tick to get the correct scoreboard position | ||
}) | ||
}, []) | ||
|
||
return( | ||
<Scoreboard | ||
title={title} | ||
items={items} | ||
open={open} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters