Skip to content

Commit

Permalink
Merge branch 'andrea' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto-Negro committed Feb 21, 2024
2 parents 20f4e17 + fe99c51 commit 30f03e5
Show file tree
Hide file tree
Showing 19 changed files with 367 additions and 170 deletions.
14 changes: 14 additions & 0 deletions src/main/app/src/application/AppController.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ class AppController {
});
console.log(`Setup per il turno della partita: ${idPartita} inviato:`, territoriAssegnati);
}

attack(idPartita, attackBody) {
if (!this.client || !this.client.connected) {
console.error("Client STOMP non connesso");
return;
}
console.log("attackBody", attackBody);
this.client.publish({
destination: `/app/partite/${idPartita}/attack`,
body: JSON.stringify(attackBody),
});
console.log(`Attacco per la partita ${idPartita} inviato:`, attackBody);
}

}


Expand Down
2 changes: 1 addition & 1 deletion src/main/app/src/application/PartitaObserverSingleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PartitaObserverSingleton {

notifyListenersEsiti(esiti) {
console.log("esiti in observer", esiti);
this.listenersEsiti.forEach((listener) => listener.updateEsiti(esiti));
this.listenersEsiti.forEach((listener) => listener(esiti));
}

// Implementazione del pattern Singleton
Expand Down
5 changes: 3 additions & 2 deletions src/main/app/src/component/layout/TopBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { Navbar } from "react-bootstrap";
import { useAuth } from "../../auth/AuthContext";
import { MdPerson4 } from "react-icons/md";
import { IoMdPerson } from "react-icons/io";
import { useParams } from "react-router-dom";

function TopBar() {
Expand All @@ -14,12 +15,12 @@ function TopBar() {
{user ? (
<>
<span className="text-white">{user}</span>
<MdPerson4 style={{ color: "#ffffff", fontSize: "40px" }} />
<IoMdPerson style={{ color: "#ffffff", fontSize: "40px" }} />
</>
) : (
<>
<span className="text-white">{id}</span>
<MdPerson4 style={{ color: "#ffffff", fontSize: "40px" }} />
<IoMdPerson style={{ color: "#ffffff", fontSize: "40px" }} />
</>
)}
</Navbar.Collapse>
Expand Down
108 changes: 91 additions & 17 deletions src/main/app/src/component/mappa/BattleState.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@ import React, { useEffect, useState } from "react";

import SvgMap from "./SvgMap";
import Console from "../mappa/Console";
import { Alert } from "react-bootstrap";
import { Alert, Modal, Button } from "react-bootstrap";
import PartitaObserverSingleton from "../../application/PartitaObserverSingleton";


function BattleState({ idPlayer, game }) {
const [mappa, setMappa] = useState([]);
const [player, setPlayer] = useState();
const [troopAssignments, setTroopAssignments] = useState({});
const [armateTerritorio, setArmateTerritorio] = useState(0);
const [isPlayersTurn, setIsPlayersTurn] = useState(true);

console.log("game", game);
const [territorioAttacante, setTerritorioAttacante] = useState("");
const [territorioDifensore, setTerritorioDifensore] = useState("");
const [territoriVicini, setTerritoriVicini] = useState();
const [sxSelected, setSxSelected] = useState(false); // Aggiungi questo stato
const [territoriAttaccabili, setTerritoriAttaccabili] = useState([]);
const [objective, setObjective] = useState();
const [showAlert, setShowAlert] = useState(true);
const [esiti, setEsiti] = useState({});
const [showEsiti, setShowEsiti] = useState(false);


const handleClose = () => setShowEsiti(false);

useEffect(() => {
function updateEsiti(esiti) {
console.log("Esiti in console: ", esiti);
setEsiti(esiti);
setShowEsiti(true);
}

PartitaObserverSingleton.addListenerEsiti(updateEsiti);

const pathD = [];
const newTroopAssignments = {};

Expand All @@ -35,38 +55,69 @@ function BattleState({ idPlayer, game }) {

const currentPlayer = game.players.find((p) => p.userName === idPlayer);
setPlayer(currentPlayer);
}, [idPlayer, game]);
return () => {
PartitaObserverSingleton.removeListenerEsiti(updateEsiti);
};
}, [idPlayer, game,]);

console.log("mappa battle state", mappa);
console.log("truppe assegnate", troopAssignments);
console.log("giocatore", player);

const handleTerritoryClick = (territoryName, event) => {
console.log("tipo di evento on click", event);
console.log("Click sinistro");
console.log("Clicked territory attaccante:", territoryName);
const armateDelTerritorio = troopAssignments[territoryName];
console.log("Clicked armate territory:", armateDelTerritorio);
setArmateTerritorio(armateDelTerritorio);

if (event.type === "contextmenu") {
if (event === "add") {
const territorio = player.territories.find((t) => t.name === territoryName)
const continenteTrovato = territorio.continent;
console.log("Continente trovato", continenteTrovato);
console.log("Click sinistro");
setTerritorioAttacante(territoryName);
console.log("Clicked territory attaccante:", territorioAttacante);
const armateDelTerritorio = troopAssignments[territoryName];
console.log("Clicked armate territory:", armateDelTerritorio);
setArmateTerritorio(armateDelTerritorio);
setSxSelected(true);
setTerritorioDifensore("");
const neighbors = game.continents
.find((c) => c.continentId === continenteTrovato)
.territories.find((t) => t.name === territoryName).neighbors;
console.log("neighbors", neighbors);
const territoriGiocatore = player.territories.map((t) => t.name);
const attackableTerritory = neighbors.filter(neighbor => !territoriGiocatore.includes(neighbor));
setTerritoriAttaccabili(attackableTerritory);
console.log("territori attaccabili", territoriAttaccabili);

}else if (event === "addNonOwned" && sxSelected && territoriAttaccabili.includes(territoryName)) {
console.log("Click destro");
console.log("Clicked territory difensore:", territoryName);
}
setTerritorioDifensore(territoryName);
console.log("Clicked territory difensore:", territorioDifensore);
setSxSelected(false);
}
};

return (
<div style={{ width: "100%", height: "100%" }}>
<p>componente battle state</p>
<div>
<p>componente battle state</p>
</div>
{isPlayersTurn ? (
<>
<SvgMap
paths={mappa}
gioco={game}
onTerritoryClick={handleTerritoryClick}
truppeAssegnate={troopAssignments}
territoryAttack={territorioAttacante}
territoryDefense={territorioDifensore}
territoryNeighbors={territoriAttaccabili}
sxSelected={sxSelected}
/>
<Console
carriTerritorio={armateTerritorio}
territoryAttack={territorioAttacante}
territoryDefense={territorioDifensore}
game = {game}
player = {player}
/>
<Console carriTerritorio={armateTerritorio} />
</>
) : (
<>
Expand All @@ -80,9 +131,32 @@ function BattleState({ idPlayer, game }) {
onTerritoryClick={() => {}}
truppeAssegnate={troopAssignments}
/>
<Console carriTerritorio={armateTerritorio} />
<Console
carriTerritorio={armateTerritorio}
game={game}
player = {player}
/>
</>
)}
{ <Modal show={showEsiti} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Esito dell'Attacco</Modal.Title>
</Modal.Header>
<Modal.Body>
{esiti.isConquered ? (
<p>Il territorio è stato conquistato.</p>
) : (
<p>Il territorio non è stato conquistato.</p>
)}
<p>Truppe perse dall'attaccante: {esiti.lostAttTroops}</p>
<p>Truppe perse dal difensore: {esiti.lostDefTroops}</p>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Chiudi
</Button>
</Modal.Footer>
</Modal> }
</div>
);
}
Expand Down
Loading

0 comments on commit 30f03e5

Please sign in to comment.