Skip to content

Commit

Permalink
match log is now sent and upcoming match is available
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenofono committed Feb 18, 2020
1 parent 69ca6e3 commit 28591c8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fighter.demo.entities.TournamentEntity;
import com.fighter.demo.models.dto.Fighter;
import com.fighter.demo.models.dto.FighterMatch;
import com.fighter.demo.models.dto.Tournament;
import com.fighter.demo.models.dto.TournamentConverter;
import com.fighter.demo.models.response.TournamentResponse;
Expand Down Expand Up @@ -38,8 +39,13 @@ public ResponseEntity<TournamentEntity> getOldTournament(@PathVariable String id
return new ResponseEntity<>(tournamentService.getOldTournament(id), HttpStatus.OK);
}

@GetMapping("/{id}/upcoming")
public ResponseEntity<FighterMatch> getNextFight(@PathVariable String id){
return new ResponseEntity<>(tournamentService.getNextMatch(id), HttpStatus.OK);
}

@GetMapping("/{id}/fight")
public ResponseEntity<Fighter> startFight(@PathVariable String id){
public ResponseEntity<FighterMatch> startFight(@PathVariable String id){
return new ResponseEntity<>(tournamentService.fight(id), HttpStatus.OK);
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/fighter/demo/models/dto/FighterMatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fighter.demo.models.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FighterMatch {

private Fighter fighter1;
private Fighter fighter2;
private Fighter winner;
private List<String> fightLog;
}
48 changes: 42 additions & 6 deletions src/main/java/com/fighter/demo/models/dto/Tournament.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class Tournament {
private FighterService fighterService;
private List<Fighter> allFighters;
private LinkedList<Fighter> fightersRemaining;
private List<FighterMatch> fighterMatches;
private FighterMatch nextMatch;
private Consumer<Fighter> fighterLoss = (fighter -> {
fighter.setLosses((short) (fighter.getLosses()+1));
fightersRemaining.remove(fighter);
Expand All @@ -34,32 +36,66 @@ public Tournament(String id, FighterService fighterService) {
this.allFighters = new ArrayList<>(fighterService.findAll());
Collections.shuffle(this.allFighters);
this.fightersRemaining = new LinkedList<>(allFighters);
this.fighterMatches = new ArrayList<>();
this.setupMatches();
}

public Fighter fight(){
Fighter fighter1 = this.fightersRemaining.get(0);
Fighter fighter2 = this.fightersRemaining.get(1);
private void setupMatches(){
for (int i = 0; i < fightersRemaining.size()-1; i=i+2) {
FighterMatch fighterMatch = new FighterMatch();
fighterMatch.setFighter1(fightersRemaining.get(i));
fighterMatch.setFighter2(fightersRemaining.get(i+1));
fighterMatch.setFightLog(new ArrayList<>());
fighterMatches.add(fighterMatch);
}
this.nextMatch = fighterMatches.get(0);
}


public FighterMatch fight(){
FighterMatch match = this.fighterMatches.get(0);
Fighter fighter1 = match.getFighter1();
Fighter fighter2 = match.getFighter2();

int hp1 = fighter1.getHealth();
int hp2 = fighter2.getHealth();
while(hp1 > 0 && hp2 > 0){
hp1 -= (int) (Math.random()*10);
final int attackDamage = (int) (Math.random()*10);
match.getFightLog().add("Fighter 1 blev slagen för " + attackDamage);
hp1 -= attackDamage;

final int attackDamage2 = (int) (Math.random()*10);
match.getFightLog().add("Fighter 2 blev slagen för " + attackDamage2);
hp2 -= (int) (Math.random()*10);

if(hp1<=0){
fighterLoss.accept(fighter1);
match.setWinner(fighter2);
}
else if(hp2 <=0){
fighterLoss.accept(fighter2);
match.setWinner(fighter1);

}
}

//Move winning fighter to back of the list
fightersRemaining.addLast(fightersRemaining.removeFirst());
fighterWin.accept(fightersRemaining.getLast());
if(fightersRemaining.size() == 1) tournamentOver();
FighterMatch matchToReturn = fighterMatches.remove(0);
if(fighterMatches.isEmpty()){
if(fightersRemaining.size() == 1){
tournamentOver();
}
else{
this.setupMatches();
}
}
else{
this.nextMatch = fighterMatches.get(0);
}

return fightersRemaining.getLast();
return matchToReturn;
}

private void tournamentOver(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.fighter.demo.entities.TournamentEntity;
import com.fighter.demo.models.dto.Fighter;
import com.fighter.demo.models.dto.FighterMatch;
import com.fighter.demo.models.dto.Tournament;

public interface TournamentService {

Tournament newTournament();
Tournament tournamentStatus(String id);
TournamentEntity getOldTournament(String id);
Fighter fight(String id);
FighterMatch fight(String id);

FighterMatch getNextMatch(String id);
}
13 changes: 11 additions & 2 deletions src/main/java/com/fighter/demo/service/TournamentServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fighter.demo.service;

import com.fighter.demo.models.dto.Fighter;
import com.fighter.demo.models.dto.FighterMatch;
import com.fighter.demo.models.dto.Tournament;
import com.fighter.demo.entities.TournamentEntity;
import com.fighter.demo.exception.TournamentNotFoundException;
Expand Down Expand Up @@ -48,22 +49,30 @@ public TournamentEntity getOldTournament(String id) {
}

@Override
public Fighter fight(String id) {
public FighterMatch fight(String id) {
Tournament tournament = Optional.ofNullable(tournamentCache.get(id))
.orElseThrow(TournamentNotFoundException::new);

if(tournament.getFightersRemaining().size() > 2){
return tournament.fight();
}
else{
Fighter winner = tournament.fight();
FighterMatch winner = tournament.fight();
saveTournament(tournament);
tournamentCache.remove(id);
System.out.println(winner);
return winner;
}
}

@Override
public FighterMatch getNextMatch(String id) {
Tournament tournament = Optional.ofNullable(tournamentCache.get(id))
.orElseThrow(TournamentNotFoundException::new);

return tournament.getNextMatch();
}

private void saveTournament(Tournament tournament) {
System.out.println("Saving");
TournamentEntity entity = new TournamentEntity();
Expand Down

0 comments on commit 28591c8

Please sign in to comment.