all commands should be working now
parent
1842816ba7
commit
10da34999b
|
@ -6,6 +6,7 @@ import ca.cosc3p91.a4.gameobjects.factory.InhabitantFactory;
|
||||||
import ca.cosc3p91.a4.player.*;
|
import ca.cosc3p91.a4.player.*;
|
||||||
import ca.cosc3p91.a4.userinterface.GameDisplay;
|
import ca.cosc3p91.a4.userinterface.GameDisplay;
|
||||||
import ca.cosc3p91.a4.util.ChallengeAdapter;
|
import ca.cosc3p91.a4.util.ChallengeAdapter;
|
||||||
|
import ca.cosc3p91.a4.util.network.Server;
|
||||||
|
|
||||||
import java.beans.XMLEncoder;
|
import java.beans.XMLEncoder;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
|
@ -19,8 +20,6 @@ public class GameEngine {
|
||||||
public static final double IRON_FACTOR = 1;
|
public static final double IRON_FACTOR = 1;
|
||||||
public static final double WOOD_FACTOR = 0.1;
|
public static final double WOOD_FACTOR = 0.1;
|
||||||
|
|
||||||
private float pillageFactor = 0.5f;
|
|
||||||
|
|
||||||
private int currentTime;
|
private int currentTime;
|
||||||
|
|
||||||
private final Random random = new Random(System.nanoTime());
|
private final Random random = new Random(System.nanoTime());
|
||||||
|
@ -30,7 +29,7 @@ public class GameEngine {
|
||||||
public GameEngine() {
|
public GameEngine() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attackVillage(Map attacking, Map defending) {
|
public boolean attackVillage(Map attacking, Map defending, Server.ConnectedClient client) {
|
||||||
int defenseiveCounter = 1;
|
int defenseiveCounter = 1;
|
||||||
int inhabCounter = 0;
|
int inhabCounter = 0;
|
||||||
for (Building b : defending.contains)
|
for (Building b : defending.contains)
|
||||||
|
@ -39,16 +38,22 @@ public class GameEngine {
|
||||||
for (Inhabitant i : defending.inhabitants)
|
for (Inhabitant i : defending.inhabitants)
|
||||||
if (i instanceof Infantry)
|
if (i instanceof Infantry)
|
||||||
inhabCounter++;
|
inhabCounter++;
|
||||||
pillageFactor = (float) inhabCounter / (float) defenseiveCounter;
|
float pillageFactor = (float) inhabCounter / (float) defenseiveCounter;
|
||||||
if (pillageFactor < 0)
|
if (pillageFactor < 0)
|
||||||
pillageFactor = 0;
|
pillageFactor = 0;
|
||||||
if (pillageFactor > 1)
|
if (pillageFactor > 1)
|
||||||
pillageFactor = 1;
|
pillageFactor = 1;
|
||||||
attacking.getTownHall().addWood((int) (defending.getTownHall().getCurrentWood() * pillageFactor));
|
int wood = (int) (defending.getTownHall().getCurrentWood() * pillageFactor);
|
||||||
attacking.getTownHall().addIron((int) (defending.getTownHall().getCurrentIron() * pillageFactor));
|
int iron = (int) (defending.getTownHall().getCurrentIron() * pillageFactor);
|
||||||
attacking.getTownHall().addGold((int) (defending.getTownHall().getCurrentGold() * 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);
|
ChallengeAdapter adapter = new ChallengeAdapter(attacking);
|
||||||
adapter.attack(defending);
|
return adapter.attack(defending, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map generateInitialMap(){
|
public Map generateInitialMap(){
|
||||||
|
|
|
@ -3,9 +3,11 @@ package ca.cosc3p91.a4.util;
|
||||||
import ChallengeDecision.*;
|
import ChallengeDecision.*;
|
||||||
import ca.cosc3p91.a4.game.Map;
|
import ca.cosc3p91.a4.game.Map;
|
||||||
import ca.cosc3p91.a4.gameobjects.*;
|
import ca.cosc3p91.a4.gameobjects.*;
|
||||||
|
import ca.cosc3p91.a4.util.network.Server;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class ChallengeAdapter {
|
public class ChallengeAdapter {
|
||||||
|
|
||||||
|
@ -79,7 +81,7 @@ public class ChallengeAdapter {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attack(Map enemy){
|
public boolean attack(Map enemy, Server.ConnectedClient client){
|
||||||
MapChallengeConverter enemyMap = new MapChallengeConverter(enemy);
|
MapChallengeConverter enemyMap = new MapChallengeConverter(enemy);
|
||||||
MapChallengeConverter ourMap = new MapChallengeConverter(this.map);
|
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 any fail to attack we need to pretend like it was one big attack that failed
|
||||||
if (!goldResults.getChallengeWon() || !ironResults.getChallengeWon() || !woodResults.getChallengeWon())
|
if (!goldResults.getChallengeWon() || !ironResults.getChallengeWon() || !woodResults.getChallengeWon())
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
System.out.println("We won gold: ");
|
System.out.print("We won gold: ");
|
||||||
goldResults.print();
|
goldResults.print();
|
||||||
System.out.println("We won iron: ");
|
System.out.print("We won iron: ");
|
||||||
ironResults.print();
|
ironResults.print();
|
||||||
System.out.println("We won wood: ");
|
System.out.print("We won wood: ");
|
||||||
woodResults.print();
|
woodResults.print();
|
||||||
|
|
||||||
CasaDeNarino th = map.getTownHall();
|
CasaDeNarino th = map.getTownHall();
|
||||||
|
|
||||||
|
AtomicInteger totalGold = new AtomicInteger();
|
||||||
|
AtomicInteger totalIron = new AtomicInteger();
|
||||||
|
AtomicInteger totalWood = new AtomicInteger();
|
||||||
|
|
||||||
goldResults.getLoot().forEach(r -> {
|
goldResults.getLoot().forEach(r -> {
|
||||||
th.addGold((int)r.getProperty().doubleValue());
|
th.addGold((int)r.getProperty().doubleValue());
|
||||||
|
totalGold.addAndGet((int) r.getProperty().doubleValue());
|
||||||
});
|
});
|
||||||
|
|
||||||
ironResults.getLoot().forEach(r -> {
|
ironResults.getLoot().forEach(r -> {
|
||||||
th.addIron((int)r.getProperty().doubleValue());
|
th.addIron((int)r.getProperty().doubleValue());
|
||||||
|
totalIron.addAndGet((int) r.getProperty().doubleValue());
|
||||||
});
|
});
|
||||||
|
|
||||||
woodResults.getLoot().forEach(r -> {
|
woodResults.getLoot().forEach(r -> {
|
||||||
th.addWood((int)r.getProperty().doubleValue());
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ public class Server implements Runnable {
|
||||||
new Server();
|
new Server();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ConnectedClient implements Runnable {
|
public static class ConnectedClient implements Runnable {
|
||||||
private final InetAddress address;
|
private final InetAddress address;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Queue<Message.Received> pendingRequests = new LinkedBlockingQueue<>();
|
private final Queue<Message.Received> pendingRequests = new LinkedBlockingQueue<>();
|
||||||
|
@ -200,6 +200,10 @@ public class Server implements Runnable {
|
||||||
case PacketTable.EXPLORE:
|
case PacketTable.EXPLORE:
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
int clients = server.clients.size();
|
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);
|
int pos = rand.nextInt(clients);
|
||||||
while (pos == clientID)
|
while (pos == clientID)
|
||||||
pos = rand.nextInt(clients);
|
pos = rand.nextInt(clients);
|
||||||
|
@ -211,12 +215,13 @@ public class Server implements Runnable {
|
||||||
break;
|
break;
|
||||||
case PacketTable.GENERATE:
|
case PacketTable.GENERATE:
|
||||||
exploringMap = usingEngine.generateMap(clientMap);
|
exploringMap = usingEngine.generateMap(clientMap);
|
||||||
sendMapData(usingEngine.view.getVillageStateTable(exploringMap, "Other Village"));
|
sendMapData(usingEngine.view.getVillageStateTable(exploringMap, "Generated Village"));
|
||||||
break;
|
break;
|
||||||
case PacketTable.ATTACK:
|
case PacketTable.ATTACK:
|
||||||
if (exploringMap != null)
|
if (exploringMap != null) {
|
||||||
usingEngine.attackVillage(clientMap, exploringMap);
|
if (!usingEngine.attackVillage(clientMap, exploringMap, this))
|
||||||
else
|
sendAndLogLn("Failed to attack!");
|
||||||
|
} else
|
||||||
sendAndLogLn("Error: Explored map is null. Did you explored/generated last command?");
|
sendAndLogLn("Error: Explored map is null. Did you explored/generated last command?");
|
||||||
exploringMap = null;
|
exploringMap = null;
|
||||||
break;
|
break;
|
||||||
|
@ -293,7 +298,7 @@ public class Server implements Runnable {
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendAndLogLn(String str){
|
public void sendAndLogLn(String str){
|
||||||
Message.Sent mess = new Message.Sent(PacketTable.MESSAGE, clientID, ++lastSentMessageID);
|
Message.Sent mess = new Message.Sent(PacketTable.MESSAGE, clientID, ++lastSentMessageID);
|
||||||
try {
|
try {
|
||||||
mess.getWriter().writeUTF(str + "\n");
|
mess.getWriter().writeUTF(str + "\n");
|
||||||
|
|
Loading…
Reference in New Issue