Skip to content

Commit

Permalink
Small refactors. Fix getPlayer.
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 12, 2024
1 parent 23d501d commit 448a405
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 202 deletions.
164 changes: 6 additions & 158 deletions frontend/src/component/PlayerTable.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { useContext, useMemo, useState, MouseEvent } from 'react';
import { useMemo, useState } from 'react';
import Box from '@mui/material/Box';
import IconButton from '@mui/material/IconButton';
import Paper from '@mui/material/Paper';
import Popover from '@mui/material/Popover';
import Stack from '@mui/material/Stack';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import TableSortLabel from '@mui/material/TableSortLabel';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import ViewColumnIcon from '@mui/icons-material/ViewColumn';
import { Trans } from 'react-i18next';
import { PlayerTableContext } from '../context/PlayerTableContext';
import { useGameState } from '../context/GameStateContext.ts';
import { Player } from '../api.ts';
import {
Expand All @@ -30,17 +22,11 @@ import {
} from '@tanstack/react-table';
import { TableHeading } from './TableHeading.tsx';
import { Table as TSTable } from '@tanstack/react-table';
import { intervalToDuration } from 'date-fns/intervalToDuration';
import { humanDuration } from '../util.ts';

export type Order = 'asc' | 'desc';

interface HeadCell {
disablePadding: boolean;
id: validColumns;
label: string;
numeric: boolean;
tooltip: string;
}

export type validColumns =
| 'user_id'
| 'name'
Expand All @@ -54,146 +40,6 @@ export type validColumns =
| 'health'
| 'alive';

const headCells: readonly HeadCell[] = [
{
id: 'user_id',
numeric: true,
disablePadding: false,
label: 'uid',
tooltip: 'Players in-Game user id'
},
{
id: 'name',
numeric: false,
disablePadding: false,
label: 'name',
tooltip: 'Players current name, as reported by the game server'
},
{
id: 'score',
numeric: true,
disablePadding: false,
label: 'score',
tooltip: 'Players current score'
},
{
id: 'kills',
numeric: true,
disablePadding: false,
label: 'kills',
tooltip: 'Players current kills'
},
{
id: 'deaths',
numeric: true,
disablePadding: false,
label: 'deaths',
tooltip: 'Players current deaths'
},
{
id: 'kpm',
numeric: true,
disablePadding: false,
label: 'kpm',
tooltip:
'Players kills per minute. Calculated from when you first see the player in the server, not how long they have actually been in the server'
},
{
id: 'health',
numeric: true,
disablePadding: false,
label: 'health',
tooltip: 'Shows player current health'
},
{
id: 'connected',
numeric: true,
disablePadding: false,
label: 'time',
tooltip: 'How long the player has been connected to the server'
},
{
id: 'map_time',
numeric: true,
disablePadding: false,
label: 'map time',
tooltip: 'How long ist been since you first joined the map'
},
{
id: 'ping',
numeric: true,
disablePadding: false,
label: 'ping',
tooltip: 'Players current latency'
}
];

export const ColumnConfigButton = () => {
const { saveSelectedColumns, enabledColumns } =
useContext(PlayerTableContext);
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);

const handleClick = (event: MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};

const handleClose = () => {
setAnchorEl(null);
};

const handleColumnsChange = (
_: MouseEvent<HTMLElement>,
newFormats: validColumns[]
) => {
saveSelectedColumns(newFormats);
};

const open = Boolean(anchorEl);
return (
<>
<IconButton onClick={handleClick}>
<ViewColumnIcon color={'primary'} />
</IconButton>
<Popover
open={open}
onClose={handleClose}
id={'column-config-popover'}
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left'
}}
>
<Paper>
<Stack>
<ToggleButtonGroup
color="primary"
orientation={'vertical'}
value={enabledColumns}
onChange={handleColumnsChange}
aria-label="Visible Columns"
>
{headCells.map((r) => {
return (
<ToggleButton
name={r.label}
value={r.id}
key={`column-toggle-${r.id}`}
>
<Trans
i18nKey={`player_table.column.${r.id}`}
/>
</ToggleButton>
);
})}
</ToggleButtonGroup>
</Stack>
</Paper>
</Popover>
</>
);
};

const PlayerTableHead = <T,>({ table }: { table: TSTable<T> }) => {
const order = table.getState().sorting[0];
return (
Expand Down Expand Up @@ -321,7 +167,9 @@ const makeColumns = () => {
}),
columnHelper.accessor('connected', {
header: () => <TableHeading>Conn.</TableHeading>,
cell: (info) => <TableCell>{`${info.getValue()}`}</TableCell>
cell: (info) => (
<TableCell>{`${humanDuration(intervalToDuration({ start: 0, end: info.getValue() / 1000 / 1000 / 1000 }))}`}</TableCell>
)
}),
columnHelper.accessor('map_time', {
header: () => <TableHeading>Map Time</TableHeading>,
Expand Down
10 changes: 0 additions & 10 deletions frontend/src/component/TableCell.tsx

This file was deleted.

6 changes: 0 additions & 6 deletions frontend/src/component/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import StopIcon from '@mui/icons-material/Stop';
import Typography from '@mui/material/Typography';
import { getLaunchOptions, getQuitOptions, Team } from '../api';
import { ColumnConfigButton } from './PlayerTable';
import { PlayerTableContext } from '../context/PlayerTableContext';
import { useQueryClient } from '@tanstack/react-query';
import { useGameState } from '../context/GameStateContext.ts';
Expand Down Expand Up @@ -52,11 +51,6 @@ export const Toolbar = () => {
</Box>
</Tooltip>

<Tooltip title={t('toolbar.button.shown_columns')}>
<Box>
<ColumnConfigButton />
</Box>
</Tooltip>
{!isSettings ? (
<Tooltip title={t('toolbar.button.open_settings')}>
<Box>
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export const createThemeByMode = () => {
MuiTableCell: {
styleOverrides: {
root: {
borderLeft: '1px solid #000000',
//borderLeft: '1px solid #000000',
borderBottom: 'none',
borderRight: '1px solid #000000',
paddingLeft: '6px',
paddingRight: '6px'
//borderRight: '1px solid #000000',
paddingLeft: '3px',
paddingRight: '3px'
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Link } from './api';
import SteamID from 'steamid';
import { formatDuration } from 'date-fns/formatDuration';
import { Duration } from 'date-fns';

export const writeToClipboard = async (rawData: string) => {
const data = [
Expand Down Expand Up @@ -48,3 +50,16 @@ export const noop = (): void => {};
export const uniqCI = (values: string[]): string[] => [
...new Map(values.map((s) => [s.toLowerCase(), s])).values()
];

const zeroPad = (num: number) => String(num).padStart(2, '0');

export const humanDuration = (duration: Duration) => {
return formatDuration(duration, {
format: ['hours', 'minutes', 'seconds'],
zero: true,
delimiter: ':',
locale: {
formatDistance: (_token, count) => zeroPad(count)
}
});
};
3 changes: 3 additions & 0 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func loadPlayerOrCreate(ctx context.Context, db store.Querier, sid64 steamid.Ste
playerRow.AvatarHash = defaultAvatarHash
playerRow.CreatedOn = time.Now()
playerRow.UpdatedOn = playerRow.CreatedOn
if playerRow.Visibility == 0 {
playerRow.Visibility = int64(steamweb.VisibilityPublic)
}

if _, errInsert := db.PlayerInsert(ctx, store.PlayerInsertParams{
// Most values are not really required to be set as zero values are ok
Expand Down
62 changes: 38 additions & 24 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ func (s *gameState) start(ctx context.Context) {
}
s.applyRemoteData(ctx, playerData, settings)
case evt := <-s.eventChan:
if evt.PlayerSID.Valid() {
if _, errCreate := s.getPlayerOrCreate(ctx, evt.PlayerSID); errCreate != nil {
slog.Error("Failed to get event player", errAttr(errCreate))

continue
}
}
slog.Debug("received event", slog.Int("type", int(evt.Type)))
switch evt.Type { //nolint:exhaustive
case EvtMap:
Expand All @@ -199,21 +206,7 @@ func (s *gameState) start(ctx context.Context) {
case EvtTags:
s.onTags(tagsEvent{tags: strings.Split(evt.MetaData, ",")})
case EvtAddress:
pcs := strings.Split(evt.MetaData, ":")

_, errPort := strconv.ParseUint(pcs[1], 10, 16)
if errPort != nil {
slog.Error("Failed to parse port: %v", errAttr(errPort), slog.String("port", pcs[1]))

continue
}

parsedIP := net.ParseIP(pcs[0])
if parsedIP == nil {
slog.Error("Failed to parse ip", slog.String("ip", pcs[0]))

continue
}
s.onEventAddress(evt)
case EvtStatusID:
s.onStatus(ctx, evt.PlayerSID, statusEvent{
ping: evt.PlayerPing,
Expand All @@ -224,12 +217,7 @@ func (s *gameState) start(ctx context.Context) {
case EvtDisconnect:
s.onMapChange()
case EvtKill:
settings, errSettings := s.settings.settings(ctx)
if errSettings != nil {
slog.Error("Failed to read settings", errAttr(errSettings))
continue
}
s.onKill(killEvent{victimName: evt.Victim, sourceName: evt.Player}, settings)
s.onKill(ctx, evt)
case EvtMsg:
case EvtConnect:
case EvtLobby:
Expand All @@ -241,6 +229,24 @@ func (s *gameState) start(ctx context.Context) {
}
}

func (s *gameState) onEventAddress(evt LogEvent) {
pcs := strings.Split(evt.MetaData, ":")

_, errPort := strconv.ParseUint(pcs[1], 10, 16)
if errPort != nil {
slog.Error("Failed to parse port: %v", errAttr(errPort), slog.String("port", pcs[1]))

return
}

parsedIP := net.ParseIP(pcs[0])
if parsedIP == nil {
slog.Error("Failed to parse ip", slog.String("ip", pcs[0]))

return
}
}

// cleanupHandler is used to track of players and their expiration times. It will remove and reset expired players
// and server from the current known state once they have been disconnected for the timeout periods.
func (s *gameState) startCleanupHandler(ctx context.Context, settingsMgr *configManager) {
Expand Down Expand Up @@ -353,13 +359,21 @@ func (s *gameState) CurrentServerState() serverState {
return s.server
}

func (s *gameState) onKill(evt killEvent, settings userSettings) {
src, srcErr := s.players.byName(evt.sourceName)
func (s *gameState) onKill(ctx context.Context, evt LogEvent) {
settings, errSettings := s.settings.settings(ctx)
if errSettings != nil {
slog.Error("Failed to read settings", errAttr(errSettings))

return
}

ke := killEvent{victimName: evt.Victim, sourceName: evt.Player}
src, srcErr := s.players.byName(ke.sourceName)
if srcErr != nil {
return
}

target, targetErr := s.players.byName(evt.sourceName)
target, targetErr := s.players.byName(ke.sourceName)
if targetErr != nil {
return
}
Expand Down

0 comments on commit 448a405

Please sign in to comment.