Integrated bare client-server functionality
parent
9208fdcde6
commit
0372f2e2c3
|
@ -1,10 +1,7 @@
|
||||||
package ca.cosc3p91.a4;
|
package ca.cosc3p91.a4;
|
||||||
|
|
||||||
import ca.cosc3p91.a4.game.GameEngine;
|
|
||||||
import ca.cosc3p91.a4.util.Client;
|
import ca.cosc3p91.a4.util.Client;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,7 @@ import ca.cosc3p91.a4.userinterface.GameDisplay;
|
||||||
import ca.cosc3p91.a4.util.ChallengeAdapter;
|
import ca.cosc3p91.a4.util.ChallengeAdapter;
|
||||||
|
|
||||||
import java.beans.XMLEncoder;
|
import java.beans.XMLEncoder;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.*;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
@ -22,21 +21,25 @@ public class GameEngine implements Runnable {
|
||||||
|
|
||||||
private Player player;
|
private Player player;
|
||||||
boolean running = true;
|
boolean running = true;
|
||||||
|
public boolean execFlag = true;
|
||||||
|
|
||||||
private float pillageFactor = 0.5f;
|
private float pillageFactor = 0.5f;
|
||||||
|
|
||||||
private int currentTime;
|
private int currentTime;
|
||||||
|
|
||||||
private final InputStream input;
|
private final InputStream input;
|
||||||
|
private final OutputStream output;
|
||||||
|
public ObjectOutputStream mapOutput;
|
||||||
private final Random random = new Random(System.nanoTime());
|
private final Random random = new Random(System.nanoTime());
|
||||||
|
|
||||||
public Map map;
|
public Map map;
|
||||||
public GameDisplay view;
|
public GameDisplay view;
|
||||||
|
|
||||||
public GameEngine(InputStream commandStream) {
|
public GameEngine(InputStream commandStream, OutputStream outputBuf) {
|
||||||
player = new Player();
|
player = new Player();
|
||||||
map = generateInitialMap();
|
map = generateInitialMap();
|
||||||
input = commandStream;
|
input = commandStream;
|
||||||
|
output = outputBuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attackVillage(Map map) {
|
public void attackVillage(Map map) {
|
||||||
|
@ -159,15 +162,15 @@ public class GameEngine implements Runnable {
|
||||||
Map exploringMap = null;
|
Map exploringMap = null;
|
||||||
boolean deleteMyHeart = true;
|
boolean deleteMyHeart = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
|
execFlag = true;
|
||||||
for (Building b : this.map.contains){
|
for (Building b : this.map.contains){
|
||||||
if ((b instanceof ResourceBuilding)) {
|
if ((b instanceof ResourceBuilding)) {
|
||||||
((ResourceBuilding) b).update(this.map.getTownHall());
|
((ResourceBuilding) b).update(this.map.getTownHall());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if ((in = view.nextInput()) != null) {
|
if ((in = view.nextInput()) != null && !in.isEmpty()) {
|
||||||
String[] args = in.split(" ");
|
String[] args = in.split(" ");
|
||||||
|
|
||||||
if (in.charAt(0) == '0') continue;
|
if (in.charAt(0) == '0') continue;
|
||||||
|
|
||||||
view.printLastInput();
|
view.printLastInput();
|
||||||
|
@ -177,52 +180,53 @@ public class GameEngine implements Runnable {
|
||||||
switch (in.charAt(0)) {
|
switch (in.charAt(0)) {
|
||||||
case '1':
|
case '1':
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
System.err.println("Args must include type!");
|
output.write("Args must include type".getBytes());
|
||||||
} else {
|
} else {
|
||||||
BuildingFactory bfactory = new BuildingFactory();
|
BuildingFactory bfactory = new BuildingFactory();
|
||||||
Building type = bfactory.getBuilding(args[1]);
|
Building type = bfactory.getBuilding(args[1]);
|
||||||
if (type == null)
|
if (type == null) {
|
||||||
System.err.println("Args are not a valid building!");
|
output.write("Args are not a valid building!".getBytes());
|
||||||
else if (this.map.build(new Tile(), type) ) {
|
} else if (this.map.build(new Tile(), type) ) {
|
||||||
System.out.println(type.getClass().getSimpleName()+" successfully built\n");
|
output.write((type.getClass().getSimpleName()+" successfully built\n").getBytes());
|
||||||
} else
|
} else {
|
||||||
System.out.println("Missing resources to build "+type.getClass().getSimpleName());
|
output.write(("Missing resources to build "+type.getClass().getSimpleName()).getBytes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '2':
|
case '2':
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
System.err.println("Args must include type!");
|
output.write("Args must include type".getBytes());
|
||||||
} else {
|
} else {
|
||||||
InhabitantFactory ifactory = new InhabitantFactory();
|
InhabitantFactory ifactory = new InhabitantFactory();
|
||||||
Inhabitant type = ifactory.getInhabitant(args[1]);
|
Inhabitant type = ifactory.getInhabitant(args[1]);
|
||||||
if (type == null)
|
if (type == null) {
|
||||||
System.err.println("Args are not a valid inhabitant!");
|
output.write("Args are not a valid inhabitant!".getBytes());
|
||||||
else if (this.map.train(type) ) {
|
} else if (this.map.train(type) ) {
|
||||||
System.out.println("successfully trained a(n) "+type.getClass().getSimpleName());
|
output.write(("successfully trained a(n) "+type.getClass().getSimpleName()).getBytes());
|
||||||
} else System.out.println("Missing gold to train "+type.getClass().getSimpleName());
|
} else output.write(("Missing resources to train "+type.getClass().getSimpleName()).getBytes());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '3':
|
||||||
if (args.length < 2) {
|
if (args.length < 2) {
|
||||||
System.err.println("Args must include type!");
|
output.write("Args must include type".getBytes());
|
||||||
} else {
|
} else {
|
||||||
int unitIndex = Integer.parseInt(args[1].substring(1));
|
int unitIndex = Integer.parseInt(args[1].substring(1));
|
||||||
|
|
||||||
if (unitIndex < 0) {
|
if (unitIndex < 0) {
|
||||||
System.err.println("Invalid Index");
|
output.write("Invalid Index".getBytes());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args[1].contains("i") && (unitIndex < map.inhabitants.size()) ) {
|
if (args[1].contains("i") && (unitIndex < map.inhabitants.size()) ) {
|
||||||
if ( map.upgradeInhabitant(unitIndex) ) {
|
if ( map.upgradeInhabitant(unitIndex) ) {
|
||||||
System.out.println("successfully upgraded a(n) "+map.inhabitants.get(unitIndex).getClass().getSimpleName());
|
output.write(("successfully upgraded a(n) "+map.inhabitants.get(unitIndex).getClass().getSimpleName()).getBytes());
|
||||||
} else System.out.println("Missing Resources to upgrade "+map.inhabitants.get(unitIndex).getClass().getSimpleName());
|
} else output.write(("Missing Resources to upgrade "+map.inhabitants.get(unitIndex).getClass().getSimpleName()).getBytes());
|
||||||
} else if (args[1].contains("b") && (unitIndex < map.contains.size()) ) {
|
} else if (args[1].contains("b") && (unitIndex < map.contains.size()) ) {
|
||||||
if ( map.upgradeBuilding(unitIndex) ) {
|
if ( map.upgradeBuilding(unitIndex) ) {
|
||||||
System.out.println("successfully upgraded a(n) "+map.contains.get(unitIndex).getClass().getSimpleName());
|
output.write(("successfully upgraded a(n) "+map.contains.get(unitIndex).getClass().getSimpleName()).getBytes());
|
||||||
} else System.out.println("Missing Resources to upgrade "+map.contains.get(unitIndex).getClass().getSimpleName());
|
} else output.write(("Missing Resources to upgrade "+map.contains.get(unitIndex).getClass().getSimpleName()).getBytes());
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Args are not a valid unit!");
|
output.write("Args is not a valid unit".getBytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -253,6 +257,7 @@ public class GameEngine implements Runnable {
|
||||||
}
|
}
|
||||||
if (deleteMyHeart)
|
if (deleteMyHeart)
|
||||||
exploringMap = null;
|
exploringMap = null;
|
||||||
|
execFlag = false;
|
||||||
}
|
}
|
||||||
save("test.xml", this.map);
|
save("test.xml", this.map);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,11 @@ package ca.cosc3p91.a4.game;
|
||||||
|
|
||||||
import ca.cosc3p91.a4.gameobjects.*;
|
import ca.cosc3p91.a4.gameobjects.*;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Map {
|
public class Map implements Serializable {
|
||||||
|
|
||||||
static int MAXSIZE = 50;
|
static int MAXSIZE = 50;
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,34 @@
|
||||||
package ca.cosc3p91.a4.util;
|
package ca.cosc3p91.a4.util;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import ca.cosc3p91.a4.userinterface.GameDisplay;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
GameDisplay view = new GameDisplay(System.in);
|
||||||
|
|
||||||
public Client(int port) throws IOException {
|
public Client(int port) throws IOException {
|
||||||
BufferedReader inFromUser =
|
|
||||||
new BufferedReader(new InputStreamReader(System.in));
|
|
||||||
DatagramSocket clientSocket = new DatagramSocket();
|
DatagramSocket clientSocket = new DatagramSocket();
|
||||||
InetAddress IPAddress = InetAddress.getByName("localhost");
|
InetAddress IPAddress = InetAddress.getByName("localhost");
|
||||||
|
String prompt;
|
||||||
byte[] sendData = new byte[1024];
|
byte[] sendData = new byte[1024];
|
||||||
byte[] receiveData = new byte[1024];
|
byte[] receiveData = new byte[1024];
|
||||||
String sentence = inFromUser.readLine();
|
while (true) {
|
||||||
sendData = sentence.getBytes();
|
if ((prompt = view.nextInput()) != null) {
|
||||||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
|
if (!prompt.isEmpty() && prompt.charAt(0) == '6') break;
|
||||||
clientSocket.send(sendPacket);
|
sendData = prompt.getBytes();
|
||||||
/*
|
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
|
||||||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
clientSocket.send(sendPacket);
|
||||||
clientSocket.receive(receivePacket);
|
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
||||||
String modifiedSentence = new String(receivePacket.getData());
|
clientSocket.receive(receivePacket);
|
||||||
System.out.println("FROM SERVER:" + modifiedSentence);
|
String serverOutput = new String(receivePacket.getData()).trim();
|
||||||
*/
|
System.out.println(">" + serverOutput);
|
||||||
|
view.printGameMenu();
|
||||||
|
}
|
||||||
|
}
|
||||||
clientSocket.close();
|
clientSocket.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,34 +4,34 @@ import ca.cosc3p91.a4.game.GameEngine;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
|
|
||||||
public static final int SERVER_PORT = 42069;
|
public static final int SERVER_PORT = 42069;
|
||||||
|
|
||||||
public static ByteArrayInputStream stream_in;
|
public static ByteArrayInputStream stream_in;
|
||||||
|
public static ByteArrayOutputStream stream_out;
|
||||||
|
public static GameEngine mainEngine;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException, InterruptedException {
|
||||||
public static void main(String[] args) throws IOException {
|
|
||||||
DatagramSocket serverSocket = new DatagramSocket(SERVER_PORT);
|
DatagramSocket serverSocket = new DatagramSocket(SERVER_PORT);
|
||||||
byte[] receiveData = new byte[1284];
|
byte[] receiveData = new byte[1284];
|
||||||
receiveData[0] = (byte)'0';
|
byte[] sendData = new byte[1284];
|
||||||
stream_in = new ByteArrayInputStream(receiveData);
|
stream_in = new ByteArrayInputStream(receiveData);
|
||||||
byte[] sendData = new byte[1024];
|
stream_out = new ByteArrayOutputStream(1284);
|
||||||
new Thread(new GameEngine(stream_in)).start();
|
new Thread(mainEngine = new GameEngine(stream_in,stream_out)).start();
|
||||||
while(true) {
|
while(true) {
|
||||||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
|
||||||
serverSocket.receive(receivePacket);
|
serverSocket.receive(receivePacket);
|
||||||
stream_in.reset();
|
stream_in.reset();
|
||||||
/*
|
Thread.sleep(500);
|
||||||
InetAddress IPAddress = receivePacket.getAddress();
|
InetAddress IPAddress = receivePacket.getAddress();
|
||||||
int port = receivePacket.getPort();
|
int port = receivePacket.getPort();
|
||||||
String capitalizedSentence = sentence.toUpperCase();
|
sendData = stream_out.toByteArray();
|
||||||
sendData = capitalizedSentence.getBytes();
|
stream_out.reset();
|
||||||
DatagramPacket sendPacket =
|
DatagramPacket sendPacket =
|
||||||
new DatagramPacket(sendData, sendData.length, IPAddress, port);
|
new DatagramPacket(sendData, sendData.length, IPAddress, port);
|
||||||
serverSocket.send(sendPacket); */
|
serverSocket.send(sendPacket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue