1
0

Chatlist smaller text

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-01-31 13:25:33 -08:00
parent 1619c185d9
commit 7b65db2449
3 changed files with 64 additions and 53 deletions

View File

@ -108,6 +108,30 @@
scroll-behavior: smooth; scroll-behavior: smooth;
} }
#ChatList .MuiListItem-gutters {
padding: 2px 0 2px 0;
}
#ChatList .MuiTypography-body1 {
font-size: 0.8rem;
}
#ChatList .MuiTypography-body2 {
font-size: 0.7rem;
}
#ChatList .MuiListItemText-multiline {
margin-top: 0;
margin-bottom: 0;
padding: 4px 0px 4px 4px;
}
#ChatList .PlayerColor {
width: 1em;
height: 1em;
padding: 0px;
}
.Players { .Players {
padding: 0.5em; padding: 0.5em;
user-select: none; user-select: none;
@ -123,6 +147,10 @@
cursor: pointer; cursor: pointer;
} }
.Players .PlayerSelector[data-selected=true] {
background-color: rgba(255,255,255,0.5);
}
.Players .PlayerToggle { .Players .PlayerToggle {
min-width: 5em; min-width: 5em;
display: inline-flex; display: inline-flex;
@ -229,13 +257,13 @@ button {
user-select: none; user-select: none;
} }
.Message div { .Message .PlayerColor {
display: inline-flex; width: 1em;
height: 1em;
} }
.Message .PlayerColor { .Message div {
position: relative; display: inline-flex;
top: -0.25em;
} }
.PlayerName { .PlayerName {

View File

@ -6,12 +6,11 @@ import Button from '@material-ui/core/Button';
import TextField from '@material-ui/core/TextField'; import TextField from '@material-ui/core/TextField';
import List from '@material-ui/core/List'; import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem'; import ListItem from '@material-ui/core/ListItem';
import ListItemAvatar from '@material-ui/core/ListItemAvatar';
import ListItemText from '@material-ui/core/ListItemText'; import ListItemText from '@material-ui/core/ListItemText';
import ListItemAvatar from '@material-ui/core/ListItemAvatar';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import { deepOrange, lightBlue, red, grey } from '@material-ui/core/colors'; import { deepOrange, lightBlue, red, grey } from '@material-ui/core/colors';
import Avatar from '@material-ui/core/Avatar'; import Avatar from '@material-ui/core/Avatar';
import Switch from '@material-ui/core/Switch';
import Moment from 'react-moment'; import Moment from 'react-moment';
//import moment from 'moment'; //import moment from 'moment';
@ -72,16 +71,9 @@ const useStyles = makeStyles((theme) => ({
})); }));
const PlayerColor = ({ color }) => { const PlayerColor = ({ color }) => {
const colors = getPlayerColors(color) const classes = useStyles();
return ( return (
<div className="PlayerColor" <Avatar className={['PlayerColor', classes[color]]}/>
style={{
backgroundColor: colors.fillStyle,
borderColor: colors.strokeStyle,
color: colors.strokeStyle
}}>
<div>{color}</div>
</div>
); );
}; };
@ -293,8 +285,6 @@ const Chat = ({ board, promoteGameState }) => {
} }
}; };
const classes = useStyles();
useEffect(() => { useEffect(() => {
const chatList = document.getElementById("ChatList"), const chatList = document.getElementById("ChatList"),
currentTop = Math.round(chatList.scrollTop); currentTop = Math.round(chatList.scrollTop);
@ -318,13 +308,10 @@ const Chat = ({ board, promoteGameState }) => {
//const timeDelta = game.timestamp - Date.now(); //const timeDelta = game.timestamp - Date.now();
const messages = board.game.chat.map((item, index) => { const messages = board.game.chat.map((item, index) => {
/* If the date is in the future, set it to now */ /* If the date is in the future, set it to now */
item.date = item.date > Date.now() ? Date.now() : item.date;
return ( return (
<ListItem key={`msg-${index}`}> <ListItem key={`msg-${item.date}`}>
<ListItemAvatar> <PlayerColor color={item.from}/>
<Avatar className={classes[item.from]}>{item.from}</Avatar> <ListItemText primary={item.message} secondary={(<Moment fromNow date={item.date > Date.now() ? Date.now() : item.date} interval={1000}/>)} />
</ListItemAvatar>
<ListItemText primary={item.message} secondary={(<Moment fromNow date={item.date} interval={1000}/>)} />
</ListItem> </ListItem>
); );
}); });
@ -436,8 +423,6 @@ const Players = ({ board }) => {
board.setSelected(board.game.color === key ? "" : key); board.setSelected(board.game.color === key ? "" : key);
} }
const classes = useStyles();
const players = []; const players = [];
for (let color in board.game.players) { for (let color in board.game.players) {
const item = board.game.players[color]; const item = board.game.players[color];
@ -445,35 +430,22 @@ const Players = ({ board }) => {
continue; continue;
} }
const name = getPlayerName(board.game.sessions, color), const name = getPlayerName(board.game.sessions, color),
selectable = item.status === 'Not active' || board.game.color === color; selectable = board.game.state === 'lobby' && (item.status === 'Not active' || board.game.color === color);
item.lastActive = item.lastActive > Date.now() ? Date.now() : item.lastActive; let toggleText = name ? name : "Available";
let toggleText;
if (selectable) {
if (board.game.state !== 'lobby') {
toggleText = 'Exit to lobby.';
} else {
toggleText = item.status === 'Not active'
? <span>Play as <PlayerColor color={color}/>{board.game.color ? ' instead' : ''}.</span>
: <span>Stop playing as <PlayerColor color={color}/>.</span>;
}
}
players.push(( players.push((
<ListItem <ListItem
data-selectable={selectable} data-selectable={selectable}
onClick={() => { selectable && toggleSelected(color) }} className="PlayerSelector" key={`player-${color}`}> data-selected={board.game.color === color}
<ListItemAvatar> className="PlayerSelector"
<Avatar className={classes[color]}>{color}</Avatar> key={`player-${color}`}>
</ListItemAvatar> <PlayerColor onClick={() => { selectable && toggleSelected(color) }} color={color}/>
<ListItemText primary={name} secondary={( <ListItemText onClick={() => { selectable && toggleSelected(color) }}primary={toggleText} secondary={(
<> <>
{ item.status + ' ' } { item.status + ' ' }
{ item.status !== 'Not active' && <Moment fromNow date={item.lastActive}/>} { item.status !== 'Not active' && <Moment fromNow date={item.lastActive > Date.now() ? Date.now() : item.lastActive}/>}
</>)} /> </>)} />
{ selectable && { board.game.state !== 'lobby' && board.game.color === color &&
<div className='PlayerToggle'> <Button onClick={() => toggleSelected(color)}>exit to lobby</Button>
<Switch edge="end" checked={board.game.color === color}/>
{toggleText}
</div>
} }
</ListItem> </ListItem>
)); ));

View File

@ -501,6 +501,7 @@ const getActiveCount = (game) => {
const sendGame = async (req, res, game, error) => { const sendGame = async (req, res, game, error) => {
const active = getActiveCount(game); const active = getActiveCount(game);
/* Enforce game limit of >= 2 players */
if (active < 2 && game.state != 'lobby' && game.state != 'invalid') { if (active < 2 && game.state != 'lobby' && game.state != 'invalid') {
let message = "Insufficient players in game. Setting back to lobby." let message = "Insufficient players in game. Setting back to lobby."
game.chat.push({ date: Date.now(), message: message }); game.chat.push({ date: Date.now(), message: message });
@ -509,6 +510,7 @@ const sendGame = async (req, res, game, error) => {
} }
game.active = active; game.active = active;
/* Update the session lastActive clock */
let session; let session;
if (req.session) { if (req.session) {
session = getSession(game, req.session); session = getSession(game, req.session);
@ -522,6 +524,15 @@ const sendGame = async (req, res, game, error) => {
}; };
} }
/* Ensure chat messages have a unique date: stamp as it is used as the index key */
let lastTime = 0;
if (game.chat) game.chat.forEach((message) => {
if (message.date <= lastTime) {
message.date = lastTime + 1;
}
lastTime = message.date;
});
/* Shallow copy game, filling its sessions with a shallow copy of sessions so we can then /* Shallow copy game, filling its sessions with a shallow copy of sessions so we can then
* delete the player field from them */ * delete the player field from them */
const reducedGame = Object.assign({}, game, { sessions: {} }), const reducedGame = Object.assign({}, game, { sessions: {} }),