2024-05-12 05:08:53 +00:00
|
|
|
import { Avatar, IconButton, ImageList, ImageListItem, ListItem, ListItemAvatar, ListItemText, Typography } from '@mui/material';
|
|
|
|
import React, { Fragment, MutableRefObject } from 'react';
|
2024-05-05 18:54:39 +00:00
|
|
|
|
2024-05-12 05:08:53 +00:00
|
|
|
import { Camera } from '@mui/icons-material';
|
2024-05-05 18:54:39 +00:00
|
|
|
import { formatters } from './format.js';
|
2024-05-12 05:08:53 +00:00
|
|
|
import { GameEvent } from './models.js';
|
|
|
|
|
|
|
|
export function openImage(image: string) {
|
|
|
|
const byteCharacters = atob(image);
|
|
|
|
const byteNumbers = new Array(byteCharacters.length);
|
|
|
|
for (let i = 0; i < byteCharacters.length; i++) {
|
|
|
|
byteNumbers[i] = byteCharacters.charCodeAt(i);
|
|
|
|
}
|
|
|
|
const byteArray = new Uint8Array(byteNumbers);
|
|
|
|
const file = new Blob([byteArray], { type: 'image/jpeg;base64' });
|
|
|
|
const fileURL = URL.createObjectURL(file);
|
|
|
|
window.open(fileURL, '_blank');
|
|
|
|
}
|
2024-05-05 18:54:39 +00:00
|
|
|
|
|
|
|
export interface EventItemProps {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
event: any;
|
2024-05-06 01:17:00 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
focusRef?: MutableRefObject<any>;
|
2024-05-12 05:08:53 +00:00
|
|
|
|
|
|
|
renderEvent: (event: GameEvent) => void;
|
2024-05-05 18:54:39 +00:00
|
|
|
}
|
|
|
|
|
2024-05-10 04:45:10 +00:00
|
|
|
export function ActionEventItem(props: EventItemProps) {
|
2024-05-12 05:08:53 +00:00
|
|
|
const { event, renderEvent } = props;
|
|
|
|
const { id, actor, room, type } = event;
|
2024-05-05 18:54:39 +00:00
|
|
|
const content = formatters[type](event);
|
|
|
|
|
2024-05-12 05:08:53 +00:00
|
|
|
return <ListItem
|
|
|
|
alignItems="flex-start"
|
|
|
|
ref={props.focusRef}
|
|
|
|
secondaryAction={
|
|
|
|
<IconButton edge="end" aria-label="render" onClick={() => renderEvent(id)}>
|
|
|
|
<Camera />
|
|
|
|
</IconButton>
|
|
|
|
}
|
|
|
|
>
|
2024-05-05 18:54:39 +00:00
|
|
|
<ListItemAvatar>
|
2024-05-10 04:45:10 +00:00
|
|
|
<Avatar alt={actor.name} src="/static/images/avatar/1.jpg" />
|
2024-05-05 18:54:39 +00:00
|
|
|
</ListItemAvatar>
|
|
|
|
<ListItemText
|
2024-05-10 04:45:10 +00:00
|
|
|
primary={room.name}
|
2024-05-05 18:54:39 +00:00
|
|
|
secondary={
|
|
|
|
<React.Fragment>
|
|
|
|
<Typography
|
|
|
|
sx={{ display: 'block' }}
|
|
|
|
component="span"
|
|
|
|
variant="body2"
|
|
|
|
color="text.primary"
|
|
|
|
>
|
2024-05-10 04:45:10 +00:00
|
|
|
{actor.name}
|
2024-05-05 18:54:39 +00:00
|
|
|
</Typography>
|
|
|
|
{content}
|
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</ListItem>;
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:45:10 +00:00
|
|
|
export function SnapshotEventItem(props: EventItemProps) {
|
2024-05-05 18:54:39 +00:00
|
|
|
const { event } = props;
|
|
|
|
const { step, world } = event;
|
2024-05-12 05:08:53 +00:00
|
|
|
const { name, theme } = world;
|
2024-05-05 18:54:39 +00:00
|
|
|
|
2024-05-06 01:17:00 +00:00
|
|
|
return <ListItem alignItems="flex-start" ref={props.focusRef}>
|
2024-05-05 18:54:39 +00:00
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar alt={step.toString()} src="/static/images/avatar/1.jpg" />
|
|
|
|
</ListItemAvatar>
|
|
|
|
<ListItemText
|
2024-05-12 05:08:53 +00:00
|
|
|
primary={name}
|
2024-05-05 18:54:39 +00:00
|
|
|
secondary={
|
2024-05-12 05:08:53 +00:00
|
|
|
<Fragment>
|
|
|
|
<Typography
|
|
|
|
sx={{ display: 'block' }}
|
|
|
|
component="span"
|
|
|
|
variant="body2"
|
|
|
|
color="text.primary"
|
|
|
|
>
|
|
|
|
Step: {step}
|
|
|
|
</Typography>
|
|
|
|
World Theme: {theme}
|
|
|
|
</Fragment>
|
2024-05-05 18:54:39 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</ListItem>;
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:45:10 +00:00
|
|
|
export function ReplyEventItem(props: EventItemProps) {
|
2024-05-05 18:54:39 +00:00
|
|
|
const { event } = props;
|
2024-05-10 04:45:10 +00:00
|
|
|
const { text } = event;
|
2024-05-05 18:54:39 +00:00
|
|
|
|
2024-05-06 01:17:00 +00:00
|
|
|
return <ListItem alignItems="flex-start" ref={props.focusRef}>
|
2024-05-05 18:54:39 +00:00
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar alt="System" src="/static/images/avatar/1.jpg" />
|
|
|
|
</ListItemAvatar>
|
|
|
|
<ListItemText
|
|
|
|
primary="System"
|
|
|
|
secondary={
|
|
|
|
<Typography
|
|
|
|
sx={{ display: 'block' }}
|
|
|
|
component="span"
|
|
|
|
variant="body2"
|
|
|
|
color="text.primary"
|
|
|
|
>
|
2024-05-10 04:45:10 +00:00
|
|
|
{text}
|
2024-05-05 18:54:39 +00:00
|
|
|
</Typography>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</ListItem>;
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:45:10 +00:00
|
|
|
export function PlayerEventItem(props: EventItemProps) {
|
2024-05-05 18:54:39 +00:00
|
|
|
const { event } = props;
|
2024-05-10 04:45:10 +00:00
|
|
|
const { character, status, client } = event;
|
2024-05-06 01:17:00 +00:00
|
|
|
|
|
|
|
let primary = '';
|
|
|
|
let secondary = '';
|
2024-05-10 04:45:10 +00:00
|
|
|
if (status === 'join') {
|
2024-05-11 22:38:07 +00:00
|
|
|
primary = 'Player Joined';
|
2024-05-10 04:45:10 +00:00
|
|
|
secondary = `${client} is now playing as ${character}`;
|
2024-05-06 01:17:00 +00:00
|
|
|
}
|
2024-05-10 04:45:10 +00:00
|
|
|
if (status === 'leave') {
|
2024-05-06 01:17:00 +00:00
|
|
|
primary = 'Player Left';
|
2024-05-10 04:45:10 +00:00
|
|
|
secondary = `${client} has left the game. ${character} is now controlled by an LLM`;
|
2024-05-06 01:17:00 +00:00
|
|
|
}
|
2024-05-05 18:54:39 +00:00
|
|
|
|
2024-05-12 05:08:53 +00:00
|
|
|
return <ListItem
|
|
|
|
alignItems="flex-start"
|
|
|
|
ref={props.focusRef}
|
|
|
|
>
|
2024-05-05 18:54:39 +00:00
|
|
|
<ListItemAvatar>
|
2024-05-06 01:17:00 +00:00
|
|
|
<Avatar alt={character} src="/static/images/avatar/1.jpg" />
|
2024-05-05 18:54:39 +00:00
|
|
|
</ListItemAvatar>
|
|
|
|
<ListItemText
|
2024-05-06 01:17:00 +00:00
|
|
|
primary={primary}
|
2024-05-05 18:54:39 +00:00
|
|
|
secondary={
|
|
|
|
<Typography
|
|
|
|
sx={{ display: 'block' }}
|
|
|
|
component="span"
|
|
|
|
variant="body2"
|
|
|
|
color="text.primary"
|
|
|
|
>
|
2024-05-06 01:17:00 +00:00
|
|
|
{secondary}
|
2024-05-05 18:54:39 +00:00
|
|
|
</Typography>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</ListItem>;
|
|
|
|
}
|
|
|
|
|
2024-05-12 05:08:53 +00:00
|
|
|
export function RenderEventItem(props: EventItemProps) {
|
|
|
|
const { event } = props;
|
|
|
|
const { images } = event;
|
|
|
|
|
|
|
|
return <ListItem alignItems="flex-start" ref={props.focusRef}>
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar alt="Render" src="/static/images/avatar/1.jpg" />
|
|
|
|
</ListItemAvatar>
|
|
|
|
<ListItemText
|
|
|
|
primary="Render"
|
|
|
|
secondary={<ImageList cols={3} rowHeight={256}>
|
|
|
|
{Object.entries(images).map(([name, image]) => <ImageListItem key={name}>
|
|
|
|
<img src={`data:image/jpeg;base64,${image}`} onClick={() => openImage(image)} alt="Render" />
|
|
|
|
</ImageListItem>)}
|
|
|
|
</ImageList>}
|
|
|
|
/>
|
|
|
|
</ListItem>;
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:54:39 +00:00
|
|
|
export function EventItem(props: EventItemProps) {
|
|
|
|
const { event } = props;
|
|
|
|
const { type } = event;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'action':
|
|
|
|
case 'result':
|
2024-05-12 05:08:53 +00:00
|
|
|
return <ActionEventItem {...props} />;
|
2024-05-10 04:45:10 +00:00
|
|
|
case 'reply':
|
2024-05-12 05:08:53 +00:00
|
|
|
case 'status': // TODO: should have a different component
|
|
|
|
return <ReplyEventItem {...props} />;
|
2024-05-05 18:54:39 +00:00
|
|
|
case 'player':
|
2024-05-12 05:08:53 +00:00
|
|
|
return <PlayerEventItem {...props} />;
|
|
|
|
case 'render':
|
|
|
|
return <RenderEventItem {...props} />;
|
2024-05-10 04:45:10 +00:00
|
|
|
case 'snapshot':
|
2024-05-12 05:08:53 +00:00
|
|
|
return <SnapshotEventItem {...props} />;
|
2024-05-05 18:54:39 +00:00
|
|
|
default:
|
2024-05-06 01:17:00 +00:00
|
|
|
return <ListItem ref={props.focusRef}>
|
2024-05-05 18:54:39 +00:00
|
|
|
<ListItemText primary={`Unknown event type: ${type}`} />
|
|
|
|
</ListItem>;
|
|
|
|
}
|
|
|
|
}
|