Semesterarbeit-1/main/Race.java

230 lines
8.2 KiB
Java

package minigame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeMap;
public class Race {
private static ArrayList<Race> past_races = new ArrayList<Race>();
public ArrayList <Horse> end = new ArrayList<>();
public ArrayList<Horse> cur_horses = new ArrayList<>();
public static TreeMap<Integer,Race> races = new TreeMap<>();
public static int curmaxRace = 0;
private int rlen;
private String rname;
private int max_horses;
private int status;
public int placement;
public int reward;
public Horse ch_horse;
public Race(Player player, String name) {
this.rlen = player.level * 10 + (2*player.level);
this.rname = name;
this.max_horses = Player.player_horses.values().size() * ((int)(Math.random()*4))+4;
this.cur_horses = Horse.genHorses(max_horses, player.level, "Race");
this.status = 0;
this.reward = 0;
}
public String toStartString() {
return "Get ready for " + this.rname + ". Race length: " + this.rlen + ". Horses: " + (this.max_horses+1);
}
public String toString() {
return String.format("Location: %s, Length: %d, FINISH POSITION: %d, Horses: %d, Reward: %d",this.rname,this.rlen,this.placement,(this.max_horses+1),this.reward);
}
public static void showHistory() {
System.out.println();
past_races.forEach(race -> Tools.printSlow30("RACE | " + race));
}
public static void firstRace(Player player,BufferedReader ter, Horse c_horse) {
Race firstRace = new Race(player, "Mannheim");
past_races.add(firstRace);
curmaxRace++;
races.put(curmaxRace,firstRace);
firstRace.getParts(player, 1);
Tools.printSlow30("\nSYSTEM | Type \"/r start\" to start the Race: ");
while(true) {
try {
String input = ter.readLine();
String [] full_com = input.split(" ");
String com = full_com[0];
if (com.equalsIgnoreCase("/r")) {
if (full_com[1].equalsIgnoreCase("start")) {
break;
} else {
Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: ");
}
} else {
Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
firstRace.doRace(player, c_horse);
firstRace.finishRace(player, c_horse, ter);
}
public static void genNextRace(Player player, BufferedReader ter) throws IOException {
Tools.printSlow30("\n"+Tools.lineGap()+"\n");
curmaxRace++;
races.put(curmaxRace, new Race(player,getNextName()));
Tools.printSlow30("INFO | Prepare for the Race and choose your horse!\n");
Tools.printSlow30(Tools.lineGap()+"\n");
Tools.waitT(1, "s");
while (true) {
Tools.printSlow30("\nSYSTEM | Choose a horses with \"/c <ID>\"\n\n");
player.showHorses();
Tools.printSlow30("\nSYSTEM | Player Input:");
String input = ter.readLine();
String [] parts = input.split(" ");
String com = parts[0];
if (com.equalsIgnoreCase("/c")) {
try {
if (Player.player_horses.containsKey(Integer.valueOf(parts[1]))) {
Tools.printSlow30("\n\n"+Tools.lineGap()+"\n\n");
races.get(curmaxRace).getParts(player, Integer.valueOf(parts[1]));
Tools.printSlow30("\nSYSTEM | Type \"/r start\" to start the Race: ");
while(true) {
try {
String input1 = ter.readLine();
String [] full_com = input1.split(" ");
String com1 = full_com[0];
if (com1.equalsIgnoreCase("/r")) {
if (full_com[1].equalsIgnoreCase("start")) {
break;
} else {
Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: ");
}
} else {
Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
races.get(curmaxRace).doRace(player, Player.player_horses.get(Integer.valueOf(parts[1])));
races.get(curmaxRace).finishRace(player,Player.player_horses.get(Integer.valueOf(parts[1])) ,ter);
break;
} else {
Tools.printSlow30("\nSYSTEM | Couldnt find a Horse with the ID: "+parts[1]+"\n\n------------------------------------------------------------------------------------------------------\n");
}
} catch (Exception e) {
Tools.printSlow30("\nChoose one of your horses with \"/c <ID>\"\n");
}
} else {
Tools.printSlow30("\nChoose one of your horses with \"/c <ID>\"\n");
}
}
}
private void finishRace(Player player, Horse c_horse, BufferedReader ter) {
try (BufferedReader ter2 = new BufferedReader(new InputStreamReader(System.in))){
c_horse.current_pos = 0; //Resest for next Race
System.out.println(Tools.lineGap()+"\n\nRACE | Rew: "+this.reward+" crd. Pos: "+this.placement+"");
player.visLevelProgress();
while (true) {
Tools.printSlow30("\n\nSYSTEM | Type \"/r end\" to leave the end screen: ");
String input = ter2.readLine();
if (input.equalsIgnoreCase("/r end")) {
Tools.printSlow30("SYSTEM | Updating Data");
Shop.updateShop(player);
Tools.printSlow800("...");
break;
}
}
GameControls.runMenu(0, ter);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getNextName() {
return String.format("Race %d",curmaxRace);
}
public void getParts(Player player, int c_horse) {
cur_horses.add(Player.player_horses.get(c_horse));
Tools.printSlow30("INFO | Contestants:\n\n");
cur_horses.forEach(horse -> System.out.println((horse.tag.equalsIgnoreCase("Race") ? "RACE | " : player.username + " | ") + horse.toBotString()));
}
private void doRace(Player player, Horse c_horse) {
Tools.printSlow30("\n"+toStartString()+"\n\n");
Tools.waitT(1, "s");
int cyc_count = 0;
while(status==0) {
cyc_count++;
doCycle(player, cyc_count);
Tools.waitT(1000, "m");
}
}
private void doCycle(Player player, int cyc_count) {
System.out.println("---------------------------------------------------\nRACE | Time from start: "+cyc_count+"\n");
Collections.sort(cur_horses, Comparator.comparingDouble(Horse::getCurrentPos).reversed());
for (Horse horse : cur_horses) {
if (end.contains(horse)) {
continue;
} else {
horse.current_pos += calcStep(horse, cyc_count);
if (horse.current_pos >= this.rlen) {
horse.current_pos = this.rlen;
System.out.println("FINISH | "+(horse.tag.equalsIgnoreCase("Race") ? "BOT | " : player.username + " | ") + horse.toRacePosString());
end.add(horse);
if (horse.tag.equalsIgnoreCase("Player")) {
status = 1;
this.placement = end.size();
calcRaceRewards(horse, player);
System.out.println();
break; // exit the loop when status is 1
}
} else {
System.out.println((cur_horses.indexOf(horse)+1)+" | " + (horse.tag.equalsIgnoreCase("Race") ? "OPP | " : player.username + " | ") + horse.toRacePosString());
}
}
}
}
private double calcStep(Horse horse,int cyc_count) {
double speed = (((Math.random()*10)+5)*horse.speedx);
double bonus = horse.stamina/10 - cyc_count/10;
return (bonus>=0) ? speed + bonus : speed;
}
private void calcRaceRewards(Horse horse, Player player) {
int reward = (int) (player.level*(Math.random()+2))+((10/this.placement)*3);
this.reward = reward;
player.credits += reward;
player.lvlpg+=(100/this.placement)-(player.level<90 ? player.level : 0);
if (player.lvlpg >= 100) {
player.levelUp();
}
}
// public static void readLocations(String filename) throws IOException {
// try (BufferedReader file1 = new BufferedReader(new FileReader(filename))) {
// String line;
// while ((line = file1.readLine()) != null) {
// raceId++;
// races.put(raceId, line);
// } races.values().forEach(x -> System.out.println(x));
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// }
}