From 0d47c1908dac3ea1055ca061cb7e7ff21dc64f69 Mon Sep 17 00:00:00 2001 From: attssystem Date: Tue, 1 Dec 2015 18:57:50 +0100 Subject: [PATCH] Text colors in the terminal fixed. Comments added. --- .../xyz/attssystem/attssac/MainActivity.java | 73 ++++++++++++------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/xyz/attssystem/attssac/MainActivity.java b/app/src/main/java/xyz/attssystem/attssac/MainActivity.java index eed483e..5c2eda4 100644 --- a/app/src/main/java/xyz/attssystem/attssac/MainActivity.java +++ b/app/src/main/java/xyz/attssystem/attssac/MainActivity.java @@ -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; @@ -20,7 +21,7 @@ public class MainActivity extends AppCompatActivity { TextView aServers; TextView vServers; Button mitm; - Button overCpu; + Button overCPU; Button fix; @Override @@ -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); @@ -80,10 +97,10 @@ 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); @@ -91,20 +108,26 @@ public void overCpuAtts() { 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); @@ -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); @@ -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); @@ -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)