Show player structures

This commit is contained in:
Kenneth Allen 2021-10-20 19:44:13 +11:00
parent 7f2405fd29
commit 5a48f01b8f
4 changed files with 56 additions and 26 deletions

View File

@ -1,11 +1,11 @@
import { useState } from 'react'; import { useState } from 'react'
import useKetchup from 'ketchup-react' import useKetchup from 'ketchup-react'
import { addPlayerAction, loadSampleAction, reducer, removePlayerAction, resetAction } from 'wonders-common' import { addPlayerAction, loadSampleAction, reducer, removePlayerAction, resetAction } from 'wonders-common'
import './App.css'; import './App.css'
import Game from '../Game/Game'; import Game from '../Game/Game'
import UserSelector from '../UserSelector/UserSelector'; import UserSelector from '../UserSelector/UserSelector'
import 'bootstrap/dist/css/bootstrap.min.css'; import 'bootstrap/dist/css/bootstrap.min.css'
import Container from 'react-bootstrap/Container'; import Container from 'react-bootstrap/Container'
import Row from 'react-bootstrap/Row' import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col' import Col from 'react-bootstrap/Col'

View File

@ -4,6 +4,10 @@
border: black solid; border: black solid;
} }
.wonder-civ-outer ol {
list-style-type: none;
}
.wonder-civ-struct { .wonder-civ-struct {
font-weight: bold; font-weight: bold;
} }

View File

@ -1,7 +1,10 @@
import { useMemo } from 'react'; import { useMemo } from 'react'
import { Player, playerStats, Resource, Science, sumMaps } from 'wonders-common'; import { Player, playerStats, Resource, Science, Structure, structureTypes } from 'wonders-common'
import fill from 'lodash/fill' import fill from 'lodash/fill'
import './Civ.css' import './Civ.css'
import Container from 'react-bootstrap/Container'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
const resEmojis = new Map<Resource, string>([ const resEmojis = new Map<Resource, string>([
['brick', '🧱'], ['brick', '🧱'],
@ -19,23 +22,47 @@ const sciEmojis = new Map<Science, string>([
]) ])
const wonderBgExts = ['jpg', 'webp'] const wonderBgExts = ['jpg', 'webp']
export default function Civ({ player, style }: { player: Player, style: 'player' | 'neighbor' | 'compact' }) { function structurePeek(struct: Structure): string {
switch (struct.type) {
case 'science':
return [...(struct.sciences ?? [])].map(sci => sciEmojis.get(sci)!).join('/')
case 'advanced industry':
case 'basic industry':
return [...(struct.resources?.entries() ?? [])]
.map(([res, num]) => fill(Array(num), resEmojis.get(res)!).join(''))
.join('/')
case 'military':
return fill(Array(struct.shields ?? 0), '🛡️').join('')
case 'culture':
return `${struct.points}`
default:
return ''
}
}
export default function Civ({ player, civStyle }: { player: Player, civStyle: 'player' | 'neighbor' | 'compact' }) {
const pStats = useMemo(() => playerStats(player), [player]) const pStats = useMemo(() => playerStats(player), [player])
const outerStyle: React.CSSProperties = player.wonder === undefined ? {} : { const outerStyle: React.CSSProperties = player.wonder === undefined ? {} : {
backgroundImage: ['linear-gradient(to left, rgba(255, 255, 255, 0.25), rgba(255, 255, 255, 0.75))', ...wonderBgExts.map(ext => `url("/assets/wonders/${player.wonder}.${ext}")`)].join(', '), backgroundImage: ['linear-gradient(to left, rgba(255, 255, 255, 0.25), rgba(255, 255, 255, 0.75))', ...wonderBgExts.map(ext => `url("/assets/wonders/${player.wonder}.${ext}")`)].join(', '),
} }
return <div className='wonder-civ-outer' style={outerStyle}> return <Container fluid className='wonder-civ-outer' style={outerStyle}>
<div>{player.name}{player.wonder && `${player.wonder}`}</div> <Row>
<div>{[...pStats.resources].flatMap(([res, n]) => fill(Array(n), resEmojis.get(res)!)).join('')}</div> <Col>{player.name}{player.wonder && `${player.wonder}`}</Col>
<div>{pStats.shields}🛡</div> </Row>
<div>${player.gold}</div> <Row>
<div>{pStats.sciences.map(s => [...s].map(sci => sciEmojis.get(sci)!)).map(s => {structureTypes.map(type =>
s.length === 1 ? s[0] : `(${s.join('/')})` <Col key={type}>
)}</div> <ol>
<div>{[...sumMaps(pStats.structObjs.map(s => new Map([[s.type, 1]])))].map(([type, n]) => {pStats.structObjs.filter(o => o.type === type).map((s, i) =>
<span className={`wonder-civ-struct wonder-civ-struct-type-${type.replaceAll(' ', '-')}`}>{n}</span> <li key={i} className={`wonder-civ-struct wonder-civ-struct-type-${type.replaceAll(' ', '-')}`}>
)}</div> {s.name} {structurePeek(s)}
</div> </li>
)}
</ol>
</Col>
)}
</Row>
</Container>
} }

View File

@ -1,4 +1,3 @@
/* eslint-disable react/style-prop-object */
import { Dispatch } from 'ketchup-react' import { Dispatch } from 'ketchup-react'
import { Action, chooseWonderAction, maxPlayers, minPlayers, Player, startGameAction, State, structures, wonders } from 'wonders-common' import { Action, chooseWonderAction, maxPlayers, minPlayers, Player, startGameAction, State, structures, wonders } from 'wonders-common'
import Civ from '../Civ/Civ' import Civ from '../Civ/Civ'
@ -18,7 +17,7 @@ function getDistant<T>(arr: T[], idx: number) {
function DistantCivs({ players }: { players: Player[] }) { function DistantCivs({ players }: { players: Player[] }) {
return <Row>{players.map(p => return <Row>{players.map(p =>
<Col key={p.name}> <Col key={p.name}>
<Civ player={p} style='compact' /> <Civ player={p} civStyle='compact' />
</Col> </Col>
)}</Row> )}</Row>
} }
@ -42,15 +41,15 @@ export default function Game({ state, playerName, dispatch }: { state: State, pl
return <> return <>
<DistantCivs players={started.distant} /> <DistantCivs players={started.distant} />
<Row> <Row>
<Col><Civ player={started.left} style='neighbor' /></Col> <Col><Civ player={started.left} civStyle='neighbor' /></Col>
<Col> <Col>
<div>{state.age === undefined ? '' : `Age ${state.age + 1} ${state.age % 2 === 0 ? '🔃' : '🔄'}`}</div> <div>{state.age === undefined ? '' : `Age ${state.age + 1} ${state.age % 2 === 0 ? '🔃' : '🔄'}`}</div>
<div><button>Discards</button></div> <div><button>Discards</button></div>
</Col> </Col>
<Col><Civ player={started.right} style='neighbor' /></Col> <Col><Civ player={started.right} civStyle='neighbor' /></Col>
</Row> </Row>
<Row> <Row>
<Col><Civ player={player} style='player' /></Col> <Col><Civ player={player} civStyle='player' /></Col>
</Row> </Row>
<Row>{player.hand?.map(s => structures.get(s)!)?.map(s => <Row>{player.hand?.map(s => structures.get(s)!)?.map(s =>
<Col> <Col>