Skip to content

Commit

Permalink
Text colors in the terminal fixed. Comments added.
Browse files Browse the repository at this point in the history
  • Loading branch information
attssystem committed Dec 1, 2015
1 parent a18b648 commit 0d47c19
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions app/src/main/java/xyz/attssystem/attssac/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ForegroundColorSpan;
Expand All @@ -20,7 +21,7 @@ public class MainActivity extends AppCompatActivity {
TextView aServers;
TextView vServers;
Button mitm;
Button overCpu;
Button overCPU;
Button fix;

@Override
Expand All @@ -32,41 +33,57 @@ protected void onCreate(Bundle savedInstanceState) {
aServers = (TextView) findViewById(R.id.aServers);
vServers = (TextView) findViewById(R.id.vServers);
mitm = (Button) findViewById(R.id.mitm);
overCpu = (Button) findViewById(R.id.overcpu);
overCPU = (Button) findViewById(R.id.overcpu);
fix = (Button) findViewById(R.id.fix);

// It says "Welcome" to the user.
Spannable welcome = new SpannableString(">Welcome Admin.");
welcome.setSpan(new ForegroundColorSpan(Color.BLUE), 0, welcome.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
term.setText(welcome);

// If "MITM" button is clicked.
mitm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// It makes Verizon losing a server,
verizonServers--;
// It writes it in the "Terminal",
textUpdate(term, "\n>MITM-Verizon servers -1", Color.GREEN);
// It makes Verizon hitting back.
vAttack();
// It verifies if someone won.
checkWin();
}

});
overCpu.setOnClickListener(new View.OnClickListener() {
// If "OverCPU" button is clicked.
overCPU.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
overCpuAtts();
// It calls the OverCPU function.
overCPUAtts();
}
});
// If "Fix" button is clicked.
fix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// It makes the user fixing a server.
attsServers++;
// It makes Verizon hitting back.
vAttack();
// It verifies if someone won.
checkWin();
}
});

}

public void overCpuAtts() {
public void overCPUAtts() {
// It takes a random number (1 to 4 included),
Random r = new Random();
rand = r.nextInt(5 - 1) + 1;

// Depending on the number value, Verizon will lose 1 to 4 server(s).
if (rand == 1) {
verizonServers--;
textUpdate(term, "\n>OverCPU-Verizon servers -1", Color.GREEN);
Expand All @@ -80,31 +97,37 @@ public void overCpuAtts() {
verizonServers -= 4;
textUpdate(term, "\n>OverCPU-Verizon servers -4", Color.GREEN);
}

// Then it takes another random value (1 to 2 included)
Random r1 = new Random();
rand = r1.nextInt(4 - 1) + 1;

// If it is 0 nothing happens else the user will lose 1 to 2 server(s).
if (rand == 1) {
attsServers--;
textUpdate(term, "\n>OverCPU-ATTS servers -1", Color.RED);
} else if (rand == 2) {
attsServers -= 2;
textUpdate(term, "\n>OverCPU-ATTS servers -2", Color.RED);
}
// It makes Verizon hitting back.
vAttack();
// It verifies if someone won.
checkWin();
}

// It is the "hitting back" function of Verizon.
public void vAttack() {
// It takes a random number (1 to 3 included),
Random r = new Random();
rand = r.nextInt(4 - 1) + 1;
// Depending on the number value, Verizon will use a specific attack.
if (rand == 1) {
attsServers--;
textUpdate(term, "\n>MITM-ATTS servers -1", Color.RED);
} else if (rand == 2) {
// It takes a random number (1 to 4 included),
Random r1 = new Random();
rand = r1.nextInt(5 - 1) + 1;

// Depending on the number value, the user will lose 1 to 4 server(s).
if (rand == 1) {
attsServers--;
textUpdate(term, "\n>OverCPU-ATTS servers -1", Color.RED);
Expand All @@ -118,10 +141,10 @@ public void vAttack() {
attsServers -= 4;
textUpdate(term, "\n>OverCPU-ATTS servers -4", Color.RED);
}

// It takes another random number (1 to 2 included),
Random r2 = new Random();
rand = r2.nextInt(4 - 1) + 1;

// If it is 0 nothing happens else Verizon will lose 1 to 2 server(s).
if (rand == 1) {
verizonServers--;
textUpdate(term, "\n>OverCPU-Verizon servers -1", Color.GREEN);
Expand All @@ -134,7 +157,7 @@ public void vAttack() {
}
}
}

// This function verifies if someone won.
public void checkWin() {
if (attsServers <= 0) {
textUpdate(term, "\n>You lose!", Color.RED);
Expand All @@ -150,22 +173,22 @@ public void checkWin() {
}
}

// This function updates the "Terminal" by using the terminal, the text to add and the chosen color as arguments.
public void textUpdate(TextView tv, String textToAdd, int color) {

SpannableStringBuilder builder = new SpannableStringBuilder();
String textContentView = tv.getText().toString();
SpannableString textSpannable = new SpannableString(textToAdd);
textSpannable.setSpan(new ForegroundColorSpan(color), 0, textSpannable.length(), 0);
builder.append(new SpannableString(textContentView));
builder.append(textSpannable);
tv.setText(builder, TextView.BufferType.SPANNABLE);
tv.setMovementMethod(new ScrollingMovementMethod());
scroll();
aServers.setText(String.valueOf(attsServers));
vServers.setText(String.valueOf(verizonServers));

// It takes the text you want and make it spannable,
SpannableString spannableText=new SpannableString(textToAdd);
spannableText.setSpan(new ForegroundColorSpan(color), 0, spannableText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Then it adds the text to the TextView.
tv.append(spannableText);
// It scrolls the "Terminal" if necessary.
tv.setMovementMethod(new ScrollingMovementMethod());
scroll();
// Then, it updates the players scores.
aServers.setText(String.valueOf(attsServers));
vServers.setText(String.valueOf(verizonServers));
}

// It makes the "Terminal" scrolling when it is full.
public void scroll() {
final int scrollAmount = term.getLayout().getLineTop(term.getLineCount()) - term.getHeight();
if (scrollAmount > 0)
Expand Down

0 comments on commit 0d47c19

Please sign in to comment.