Skip to content

Commit

Permalink
[Kan 50] all duels bug (#39)
Browse files Browse the repository at this point in the history
* start

* null bug

* null bug

---------

Co-authored-by: Krzysztof Lasecki <krzysiu0059@gmail.com>
  • Loading branch information
c-i-a-s-t-e-k and KLR2002 authored Jun 10, 2024
1 parent 418a77e commit 68f3d51
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 179 deletions.
8 changes: 4 additions & 4 deletions backend/src/main/java/com/rikishi/rikishi/model/Fight.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public static Fight fromJson(

public Duel toJson() {
return new Duel(
firstUser.id(),
firstUser.name(),
secondUser.id(),
secondUser.name(),
firstUser != null ? firstUser.id() : -1,
firstUser != null ? firstUser.name() : "",
secondUser != null ? secondUser.id() : -1,
secondUser != null ? secondUser.name() : "",
number,
score1,
score2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ private List<Fight> getDuelsFromRange(int a, int b) {
winnerId = players.get(0).id();
}
while (players.size() < 2) players.add(null);
int score1 = (winnerId == players.get(0).id()) ? 1 : 0;
int score2 = (winnerId == players.get(1).id()) ? 1 : 0;
long player1_id = players.get(0) != null ? players.get(0).id() : -2;
long player2_id = players.get(1) != null ? players.get(1).id() : -2;
int score1 = (winnerId == player1_id) ? 1 : 0;
int score2 = (winnerId == player2_id) ? 1 : 0;
duels.add(new Fight(
i,
players.get(0),
Expand Down
465 changes: 304 additions & 161 deletions frontend/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
}
},
"dependencies": {
"@g-loot/react-tournament-brackets": "^1.0.31-rc",
"axios": "^1.6.8",
"electron-debug": "^3.2.0",
"electron-log": "^4.4.8",
Expand Down
10 changes: 1 addition & 9 deletions frontend/release/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 39 additions & 2 deletions frontend/src/components/ContestantsCategoryList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function ContestansCategoryList() {
} = useFetch(`${config.backendUrl}/contestants`);
const history = useNavigate();
const [everyWithEvery, setEveryWithEvery] = useState(false);
const [pairs, setPairs] = useState([]);

const handleExit = () => {
// eslint-disable-next-line no-console
Expand All @@ -24,7 +25,7 @@ function ContestansCategoryList() {
const handleEveryWithEveryChange = (event) => {
setEveryWithEvery(event.target.checked);
};
const handleSubmitLadder = () => {
const handleSubmitLadder = async () => {
// Implement your submit logic here
// COMUNICATE WITH BACKEND
// trzeba wziac pod uwage w ktorej stronie drabonki jest zawodnik i czy jest zaznaczona opcja
Expand All @@ -40,10 +41,46 @@ function ContestansCategoryList() {
// Desygnować można (nie trzeba) po jednym zawodniku do każdej ze stron.
// Nie robimy zaznaczania opcji każdy z każdym,
// mamy predefiniowane zasady dla danej liczby zawodników
let requestBody = {}
if(pairs.length >= 1) {
const pair = pairs[0];
requestBody = {
weightCategory: category,
firstBracketContestant: pair[0], // id lub null
secondBracketContestant: pair[1], // id lub null
};
} else {
requestBody = {
weightCategory: category,
firstBracketContestant: -1, // id lub null
secondBracketContestant: -1, // id lub null
};
}

try {
const response = await fetch(
`${config.backendUrl}/duels/generateLadder`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( requestBody ),
},
);

if (response.ok) {
history(`/bracket/${category}`);
} else {
console.log('Failed to generate ladder');
}
} catch (postError) {
console.error('Error while generating ladder:', postError);
}
};

const [selected, setSelected] = useState([]);
const [pairs, setPairs] = useState([]);


const generatePairs = (selectedContestants) => {
const pairs = [];
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/DuelList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function DuelList() {
data: data,
isPending,
error,
} = useFetch(`${config.backendUrl}/duels/curr`);
} = useFetch(`${config.backendUrl}/duels/${category}`);
const history = useNavigate();

const handleExit = () => {
Expand Down
135 changes: 135 additions & 0 deletions frontend/src/components/TournamentBracket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { SingleEliminationBracket, Match, SVGViewer } from '@g-loot/react-tournament-brackets';
import useFetch from '../hooks/useFetch';
import config from '../config';

function TournamentBracket() {
const [matches, setMatches] = useState(null);
const { weightCategory } = useParams();
const {
data: duels,
isPending,
error
} = useFetch(`${config.backendUrl}/duels/${weightCategory}`);

function addParticipant(id, winner, name, score) {
const participant = {};
participant.id = id;
participant.name = name;
participant.resultText = score;

if(winner === -1) {
participant.isWinner = false;
participant.status = null;
} else if(winner === participant.id) {
participant.isWinner = true;
participant.status = 'PLAYED';
} else {
participant.isWinner = false;
participant.status = 'PLAYED';
}

return participant;
}

// zakładam że maksymalnie do 16 zawodników jest turniej
function roundName(id) {
switch (id) {
case 0:
return 'Final';
case 1:
case 2:
return 'Semi-Final';
case 3:
case 4:
case 5:
case 6:
return 'Quarter-Final';
default:
return '1 Round';
}
}

// zakładam że maksymalnie do 16 zawodników jest turniej
function roundNumber(id, length) {
if (id === 0) {
if (length >= 8) {
return '4';
}
return '3';
}
if (id <= 2) {
if (length >= 8) {
return '3';
}
return '2';
}
if (id <= 6) {
if (length >= 8) {
return '2';
}
return '1';
}
return '1';
}

// ten hook służy do aktualizowania matches
useEffect(() => {
if(duels != null){
console.log(duels);
/*
const matchesList = [];
for (const duel of duels) {
const match = {};
match.id = duel.id;
match.name = roundName(match.id);
if(match.id === 0) {
match.nextMatchId = null;
} else {
match.nextMatchId = Math.floor((match.id - 1) / 2);
}
match.tournamentRoundText = roundNumber(match.id, duels.length);
const todayDate = new Date();
match.startTime = `${todayDate.getDate()}-${todayDate.getMonth()}-${todayDate.getFullYear()}`;
if(duel.winner === -1) {
match.state = 'SCHEDULED';
} else {
match.state = 'SCORE_DONE';
}
match.participants = [];
if(duel.idContestant1 != null) {
match.participants.push(addParticipant(duel.idContestant1, duel.winner, duel.name1, duel.score1));
}
if(duel.idContestant2 != null) {
match.participants.push(addParticipant(duel.idContestant2, duel.winner, duel.name2, duel.score2));
}
matchesList.push(match);
}
setMatches(matchesList);*/

}

}, [duels]);

return (
<>
{matches && (
<SingleEliminationBracket
matches={matches}
matchComponent={Match}
svgWrapper={({ children, ...props }) => (
<SVGViewer width={500} height={500} {...props}>
{children}
</SVGViewer>
)}
/>
)}
</>
);
}

export default TournamentBracket;
2 changes: 2 additions & 0 deletions frontend/src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import WeightInForm from '../components/WeightInForm';
import CategoryToLadder from '../components/CategoryToLadder.js';
import ContestantsCategoryList from '../components/ContestantsCategoryList.js';
import ChangeCategoryForm from '../components/ChangeCategoryForm.js';
import TournamentBracket from "../components/TournamentBracket";
export default function App() {
// TODO: this healthcheck should be called periodically
useEffect(() => {
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function App() {
path="/contestantsWeight/:category"
element={<ContestantsCategoryList />}
/>
<Route exact path="/bracket/:weightCategory" element={<TournamentBracket/>} />
</Routes>
</Router>
</div>
Expand Down

0 comments on commit 68f3d51

Please sign in to comment.