feat: render game grid as React component via SymfonyUX
This commit is contained in:
27
assets/react/controllers/GameGrid.jsx
Normal file
27
assets/react/controllers/GameGrid.jsx
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
export default function GameGrid({ grid, width, middle }) {
|
||||||
|
return (
|
||||||
|
<table id="actors">
|
||||||
|
<tbody>
|
||||||
|
{grid.map((row, rowIndex) => (
|
||||||
|
<tr key={rowIndex}>
|
||||||
|
{Array.from({ length: width + 1 }, (_, colIndex) => {
|
||||||
|
const start = middle - row.pos;
|
||||||
|
const charIndex = colIndex - start;
|
||||||
|
const name = row.actorName;
|
||||||
|
const isInRange = charIndex >= 0 && charIndex < name.length;
|
||||||
|
const isHighlighted = charIndex === row.pos;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<td key={colIndex} style={isHighlighted ? { color: 'red' } : undefined}>
|
||||||
|
{isInRange ? name[charIndex].toUpperCase() : ''}
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -64,9 +64,15 @@ class HomepageController extends AbstractController
|
|||||||
$width = $rightSize + $leftSize + 1;
|
$width = $rightSize + $leftSize + 1;
|
||||||
$middle = $leftSize;
|
$middle = $leftSize;
|
||||||
|
|
||||||
|
// Build JSON-serializable grid for React
|
||||||
|
$grid = array_map(fn (array $actorData) => [
|
||||||
|
'actorName' => $actorData['actor']->getName(),
|
||||||
|
'actorId' => $actorData['actor']->getId(),
|
||||||
|
'pos' => $actorData['pos'],
|
||||||
|
], $actors);
|
||||||
|
|
||||||
return $this->render('homepage/index.html.twig', [
|
return $this->render('homepage/index.html.twig', [
|
||||||
'mainActor' => $mainActor,
|
'grid' => $grid,
|
||||||
'actors' => $actors,
|
|
||||||
'width' => $width,
|
'width' => $width,
|
||||||
'middle' => $middle,
|
'middle' => $middle,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -1,26 +1,9 @@
|
|||||||
{% extends 'base.html.twig' %}
|
{% extends 'base.html.twig' %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<table id="actors">
|
{{ react_component('GameGrid', {
|
||||||
{% set iActor = 0 %}
|
grid: grid,
|
||||||
{% for mainChar in mainActor.name|split('') %}
|
width: width,
|
||||||
{% if not mainChar|match('/[a-zA-Z]/') %}
|
middle: middle,
|
||||||
<tr><td></td></tr>
|
}) }}
|
||||||
{% else %}
|
|
||||||
{% set actor = actors[iActor] %}
|
|
||||||
<tr>
|
|
||||||
{% set i = 0 %}
|
|
||||||
{% set start = middle - actor.pos %}
|
|
||||||
{% for c in range(0, width) %}
|
|
||||||
{% if c >= start and c - start < actor.actor.name|length %}
|
|
||||||
<td {% if c - start == actor.pos %}style="color:red;"{% endif %}>{{ actor.actor.name|slice(c - start, 1)|upper }}</td>
|
|
||||||
{% else %}
|
|
||||||
<td></td>
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
</tr>
|
|
||||||
{% set iActor = iActor + 1 %}
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
</table>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user