Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
vladoleksiyenko authored Mar 14, 2024
0 parents commit f08e436
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CharacterProtocol.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added out/production/CharacterProtocol/Main.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
88 changes: 88 additions & 0 deletions src/CharacterClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
This program tests the character server.
*/
public class CharacterClient
{
public static void main(String[] args) throws IOException
{
final int SBAP_PORT = 8888;

// localhost means we are connecting to our own device
// that we are currently running on (loopback to ourselves)
// localhost is a name for IP address 127.0.0.1
try (Socket s = new Socket("localhost", SBAP_PORT))
{
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);

String health_command = "HEALTH_INCREASE 1 500000.0";
System.out.println("Sending: " + health_command);
out.print(health_command + "\n");
out.flush();
String response = in.nextLine();
System.out.println("Receiving: " + response);

System.out.println("");

String damage_command = "DAMAGE_INCREASE 1 500000.0";
System.out.println("Sending: " + damage_command);
out.print(damage_command + "\n");
out.flush();
response = in.nextLine();
System.out.println("Receiving: " + response);

System.out.println("");

health_command = "HEALTH_REDUCE 1 500000.0";
System.out.println("Sending: " + health_command);
out.print(health_command + "\n");
out.flush();
response = in.nextLine();
System.out.println("Receiving: " + response);

System.out.println("");

damage_command = "DAMAGE_REDUCE 1 500000.0";
System.out.println("Sending: " + damage_command);
out.print(damage_command + "\n");
out.flush();
response = in.nextLine();
System.out.println("Receiving: " + response);

System.out.println();

health_command = "GET_HEALTH 1";
System.out.println("Sending: " + health_command);
out.print(health_command + "\n");
out.flush();
response = in.nextLine();
System.out.println("Receiving: " + response);

damage_command = "GET_DAMAGE 1";
System.out.println("Sending: " + damage_command);
out.print(damage_command + "\n");
out.flush();
response = in.nextLine();
System.out.println("Receiving: " + response);

damage_command = "INVALID COMMAND";
System.out.println("Sending: " + damage_command);
out.print(damage_command);
out.flush();

String quit_command = "QUIT";
System.out.println("Sending: " + quit_command);
out.print(quit_command + "\n");
out.flush();
}
}
}
27 changes: 27 additions & 0 deletions src/CharacterServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
A server that executes the Character Access Protocol.
*/
public class CharacterServer
{
public static void main(String[] args) throws IOException
{
final int ACCOUNTS_LENGTH = 10;
UserCharacters characters = new UserCharacters(ACCOUNTS_LENGTH);
final int SBAP_PORT = 8888;
ServerSocket server = new ServerSocket(SBAP_PORT);
System.out.println("Waiting for users to connect...");

while (true)
{
Socket s = server.accept();
System.out.println("User connected.");
CharacterService service = new CharacterService(s, characters);
Thread t = new Thread(service);
t.start();
}
}
}
107 changes: 107 additions & 0 deletions src/CharacterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
Executes Character Access Protocol commands
from a socket.
*/
public class CharacterService implements Runnable
{
private Socket s;
private Scanner in;
private PrintWriter out;
private UserCharacters characters;

/**
Constructs a service object that processes commands
from a socket for a character.
@param aSocket the socket
@param userCharacters the character container
*/
public CharacterService(Socket aSocket, UserCharacters userCharacters)
{
s = aSocket;
characters = userCharacters;
}

public void run()
{
try
{
try
{
in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream());
doService();
}
finally
{
s.close();
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
}

/**
Executes all commands until the QUIT command or the
end of input.
*/
public void doService() throws IOException {
while (true) {
if (!in.hasNext()) {
return;
}

String command = in.next();

if (command.equals("QUIT")) {
return;
} else {
executeCommand(command);
}
}
}

/**
Executes a single command.
@param command the command to execute
*/
public void executeCommand(String command)
{
int characterID = in.nextInt();
if (command.equals("HEALTH_INCREASE")) {
double amount = in.nextDouble();
characters.HEALTH_INCREASE(characterID, amount);
receivingOutput(characterID);
} else if (command.equals("HEALTH_REDUCE")) {
double amount = in.nextDouble();
characters.HEALTH_REDUCE(characterID, amount);
receivingOutput(characterID);
} else if (command.equals("DAMAGE_INCREASE")) {
double amount = in.nextDouble();
characters.DAMAGE_INCREASE(characterID, amount);
receivingOutput(characterID);
} else if (command.equals("DAMAGE_REDUCE")) {
double amount = in.nextDouble();
characters.DAMAGE_REDUCE(characterID, amount);
receivingOutput(characterID);
} else if (command.equals("GET_HEALTH")) {
out.println("CURRENT HEALTH: " + characters.getHealth(characterID));
out.flush();
} else if (command.equals("GET_DAMAGE")) {
out.println("CURRENT DAMAGE: " + characters.getDamage(characterID));
out.flush();
}
}

public void receivingOutput(int characterID) {
out.println("Character ID: " + characterID + " | Character Health: " + characters.getHealth(characterID)
+ " | Character Damage: " + characters.getDamage(characterID));
out.flush();
}
}
5 changes: 5 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {

}
}
115 changes: 115 additions & 0 deletions src/UserCharacter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
A character has health and damage fields, which can be altered
by using included methods
*/
public class UserCharacter {
private int characterID;
private double health;
private double damage;
private Lock characterLock;

/**
Constructs a character with 0 health, 0 damage and a random id
*/
Random rand = new Random();
public UserCharacter() {
this.characterID = rand.nextInt(99999); // Not a great way to create unique id's, this is just temporary
this.health = 0;
this.damage = 0;
characterLock = new ReentrantLock();
}

/**
Constructs a character with a given health amount and damage amount.
@param initialHealth the amount of health the character is created with
@param initialDamage the amount of damage the character is created with
*/
public UserCharacter(double initialHealth, double initialDamage) {
this.characterID = rand.nextInt(99999); // Not a great way to create unique id's, this is just temporary
this.health = initialHealth;
this.damage = initialDamage;
characterLock = new ReentrantLock();
}

/**
Reduces the health of the character by given amount
@param amount the amount of health that is reduced
*/
public void HEALTH_REDUCE(double amount) {
characterLock.lock();
try {
if (health == 0) {
health = 0;
} else {
double newHealth = health - amount;
health = newHealth;
}
}
finally {
characterLock.unlock();
}
}

/**
Increases the health by given amount
@param amount the amount the health is increased by
*/
public void HEALTH_INCREASE(double amount) {
characterLock.lock();
try {
double newHealth = health + amount;
health = newHealth;
}
finally {
characterLock.unlock();
}
}

/**
* Increases the damage by given amount
* @param amount the amount that damage is increased by
*/
public void DAMAGE_INCREASE(double amount) {
characterLock.lock();
try {
double newDamage = damage + amount;
damage = newDamage;
} finally {
characterLock.unlock();
}
}

/**
* Reduces the damage by given amount
* @param amount the amount that damage is reduces by
*/
public void DAMAGE_REDUCE(double amount) {
characterLock.lock();
try {
double newDamage = damage - amount;
damage = newDamage;
} finally {
characterLock.unlock();
}
}

/**
Gets the current health amount of a character.
@return the current amount
*/
public double getHealth() {
return health;
}

/**
Gets the current damage amount of a character.
@return the current amount
*/
public double getDamage() {
return damage;
}
}
Loading

0 comments on commit f08e436

Please sign in to comment.