all commands should be working now

main
Brett 2023-04-20 19:01:28 -04:00
parent 1842816ba7
commit 10da34999b
3 changed files with 44 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import ca.cosc3p91.a4.gameobjects.factory.InhabitantFactory;
import ca.cosc3p91.a4.player.*;
import ca.cosc3p91.a4.userinterface.GameDisplay;
import ca.cosc3p91.a4.util.ChallengeAdapter;
import ca.cosc3p91.a4.util.network.Server;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
@ -19,8 +20,6 @@ public class GameEngine {
public static final double IRON_FACTOR = 1;
public static final double WOOD_FACTOR = 0.1;
private float pillageFactor = 0.5f;
private int currentTime;
private final Random random = new Random(System.nanoTime());
@ -30,7 +29,7 @@ public class GameEngine {
public GameEngine() {
}
public void attackVillage(Map attacking, Map defending) {
public boolean attackVillage(Map attacking, Map defending, Server.ConnectedClient client) {
int defenseiveCounter = 1;
int inhabCounter = 0;
for (Building b : defending.contains)
@ -39,16 +38,22 @@ public class GameEngine {
for (Inhabitant i : defending.inhabitants)
if (i instanceof Infantry)
inhabCounter++;
pillageFactor = (float) inhabCounter / (float) defenseiveCounter;
float pillageFactor = (float) inhabCounter / (float) defenseiveCounter;
if (pillageFactor < 0)
pillageFactor = 0;
if (pillageFactor > 1)
pillageFactor = 1;
attacking.getTownHall().addWood((int) (defending.getTownHall().getCurrentWood() * pillageFactor));
attacking.getTownHall().addIron((int) (defending.getTownHall().getCurrentIron() * pillageFactor));
attacking.getTownHall().addGold((int) (defending.getTownHall().getCurrentGold() * pillageFactor));
int wood = (int) (defending.getTownHall().getCurrentWood() * pillageFactor);
int iron = (int) (defending.getTownHall().getCurrentIron() * pillageFactor);
int gold = (int) (defending.getTownHall().getCurrentGold() * pillageFactor);
attacking.getTownHall().addWood(wood);
attacking.getTownHall().addIron(iron);
attacking.getTownHall().addGold(gold);
defending.getTownHall().addWood(-wood);
defending.getTownHall().addIron(-iron);
defending.getTownHall().addGold(-gold);
ChallengeAdapter adapter = new ChallengeAdapter(attacking);
adapter.attack(defending);
return adapter.attack(defending, client);
}
public Map generateInitialMap(){

View File

@ -3,9 +3,11 @@ package ca.cosc3p91.a4.util;
import ChallengeDecision.*;
import ca.cosc3p91.a4.game.Map;
import ca.cosc3p91.a4.gameobjects.*;
import ca.cosc3p91.a4.util.network.Server;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class ChallengeAdapter {
@ -79,7 +81,7 @@ public class ChallengeAdapter {
this.map = map;
}
public void attack(Map enemy){
public boolean attack(Map enemy, Server.ConnectedClient client){
MapChallengeConverter enemyMap = new MapChallengeConverter(enemy);
MapChallengeConverter ourMap = new MapChallengeConverter(this.map);
@ -116,28 +118,41 @@ public class ChallengeAdapter {
// if any fail to attack we need to pretend like it was one big attack that failed
if (!goldResults.getChallengeWon() || !ironResults.getChallengeWon() || !woodResults.getChallengeWon())
return;
return false;
System.out.println("We won gold: ");
System.out.print("We won gold: ");
goldResults.print();
System.out.println("We won iron: ");
System.out.print("We won iron: ");
ironResults.print();
System.out.println("We won wood: ");
System.out.print("We won wood: ");
woodResults.print();
CasaDeNarino th = map.getTownHall();
AtomicInteger totalGold = new AtomicInteger();
AtomicInteger totalIron = new AtomicInteger();
AtomicInteger totalWood = new AtomicInteger();
goldResults.getLoot().forEach(r -> {
th.addGold((int)r.getProperty().doubleValue());
totalGold.addAndGet((int) r.getProperty().doubleValue());
});
ironResults.getLoot().forEach(r -> {
th.addIron((int)r.getProperty().doubleValue());
totalIron.addAndGet((int) r.getProperty().doubleValue());
});
woodResults.getLoot().forEach(r -> {
th.addWood((int)r.getProperty().doubleValue());
totalWood.addAndGet((int) r.getProperty().doubleValue());
});
client.sendAndLogLn("You won gold: " + totalGold.get());
client.sendAndLogLn("You won iron: " + totalIron.get());
client.sendAndLogLn("You won wood: " + totalWood.get());
return true;
}
}

View File

@ -88,7 +88,7 @@ public class Server implements Runnable {
new Server();
}
private static class ConnectedClient implements Runnable {
public static class ConnectedClient implements Runnable {
private final InetAddress address;
private final int port;
private final Queue<Message.Received> pendingRequests = new LinkedBlockingQueue<>();
@ -200,6 +200,10 @@ public class Server implements Runnable {
case PacketTable.EXPLORE:
Random rand = new Random();
int clients = server.clients.size();
if (clients <= 1) {
sendAndLogLn("No other clients are currently connected! Please generate a village!");
break;
}
int pos = rand.nextInt(clients);
while (pos == clientID)
pos = rand.nextInt(clients);
@ -211,12 +215,13 @@ public class Server implements Runnable {
break;
case PacketTable.GENERATE:
exploringMap = usingEngine.generateMap(clientMap);
sendMapData(usingEngine.view.getVillageStateTable(exploringMap, "Other Village"));
sendMapData(usingEngine.view.getVillageStateTable(exploringMap, "Generated Village"));
break;
case PacketTable.ATTACK:
if (exploringMap != null)
usingEngine.attackVillage(clientMap, exploringMap);
else
if (exploringMap != null) {
if (!usingEngine.attackVillage(clientMap, exploringMap, this))
sendAndLogLn("Failed to attack!");
} else
sendAndLogLn("Error: Explored map is null. Did you explored/generated last command?");
exploringMap = null;
break;
@ -293,7 +298,7 @@ public class Server implements Runnable {
}).start();
}
private void sendAndLogLn(String str){
public void sendAndLogLn(String str){
Message.Sent mess = new Message.Sent(PacketTable.MESSAGE, clientID, ++lastSentMessageID);
try {
mess.getWriter().writeUTF(str + "\n");