Skip to content

Commit

Permalink
Split operator randomizer into attacker and defender randomizers, and…
Browse files Browse the repository at this point in the history
… made it so that they can no longer select the same operator twice in a row.
  • Loading branch information
Emzi0767 committed Aug 21, 2018
1 parent 87ff240 commit 57ac840
Show file tree
Hide file tree
Showing 14 changed files with 1,083 additions and 21 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog for R6: Strat Roulette

### v1.2.0 (released: 2018-08-21 16:50 +02:00)
- NEW: Operator randomizer was split into 2 fragments
- There's now Attacker Randomizer and Defender Randomizer
- Operator Randomizer is no longer available by itself
- NEW: Operator randomizers now also randomize operator loadout
- They will select a random primary and secondary weapons, as well as gadget.
- NEW: Operator randomizers will no longer select the same operator more than once in a row.

### v1.1.2-r2 (released: 2018-07-28 07:27 +02:00)
- FIX: Remove PayPal and Patreon links, apparently Google is mad about them now
- Why weren't they before? 🤔
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ android {
applicationId "com.emzi0767.r6stratroulette"
minSdkVersion 21
targetSdkVersion 27
versionCode 7
versionName "1.1.2-r2"
versionCode 8
versionName "1.2.0"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.emzi0767.r6stratroulette;


import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.emzi0767.r6stratroulette.data.RandomizedOperator;
import com.emzi0767.r6stratroulette.models.*;

import java.io.File;
import java.util.*;

/**
* A simple {@link Fragment} subclass.
*/
public class AttackerRandomizerFragment extends Fragment {
private RouletteData rouletteData = null;
private File assetLocation = null;
private ArrayList<OperatorData> ops = null;

private RandomizedOperator op = null;

private ImageView img = null;
private TextView name = null, ctu = null, weapon1 = null, weapon2 = null, gadget = null;

private MainActivity mainActivity = null;

private final Random rng = new Random();

public AttackerRandomizerFragment() {
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
this.initOperators(null);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

this.mainActivity = (MainActivity)this.getActivity();
this.rouletteData = this.mainActivity.getRouletteData();
this.assetLocation = this.mainActivity.getAssetLocation();

this.initOperators(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_attacker_randomizer, container, false);

this.img = v.findViewById(R.id.randomatk_img);
this.name = v.findViewById(R.id.randomatk_name);
this.ctu = v.findViewById(R.id.randomatk_ctu);
this.weapon1 = v.findViewById(R.id.randomop_atk_weapon1);
this.weapon2 = v.findViewById(R.id.randomop_atk_weapon2);
this.gadget = v.findViewById(R.id.randomop_atk_gadget);

Button btn = v.findViewById(R.id.randomatk_randomize);
btn.setOnClickListener(b -> {
RandomizedOperator op = this.getRandomOperator(this.op.getOperator());
this.setOperator(op);
});

return v;
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
if (this.op != null) {
Bundle savedOp = new Bundle();

savedOp.putString("NAME", op.getOperator().getName());
savedOp.putString("WEAPON1", op.getPrimary());
savedOp.putString("WEAPON2", op.getSecondary());
savedOp.putString("GADGET", op.getGadget());

outState.putBundle("ATKOP", savedOp);
}

super.onSaveInstanceState(outState);
}

private void setOperator(RandomizedOperator op) {
this.op = op;

Map<String, CtuData> ctus = this.rouletteData.getCtus();

CtuData ctu = ctus.get(op.getOperator().getCtu());

this.name.setText(op.getOperator().getName());
this.ctu.setText(String.format("%s (%s)", ctu.getName(), ctu.getAbbreviation()));
this.weapon1.setText(op.getPrimary());
this.weapon2.setText(op.getSecondary());
this.gadget.setText(op.getGadget());

File img = new File(this.assetLocation, op.getOperator().getIcon());
Bitmap bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
this.img.setImageBitmap(bmp);
}

private void initOperators(Bundle savedInstanceState) {
if (this.mainActivity == null)
return;

SharedPreferences prefs = this.mainActivity.getSharedPreferences(this.getString(R.string.settings_filename), Context.MODE_PRIVATE);
Set<String> disabledOps = prefs.getStringSet(SettingsActivity.DISABLED_OPS_KEY, new HashSet<>());

OperatorsData ops = this.rouletteData.getOperators();
List<OperatorData> xops = ops.getAttackers();
this.ops = new ArrayList<>();

for (OperatorData op : xops)
if (!disabledOps.contains(op.getName()))
this.ops.add(op);

RandomizedOperator rop = null;
OperatorData op = null;

if (savedInstanceState != null && savedInstanceState.containsKey("ATKOP")) {
Bundle savedOp = savedInstanceState.getBundle("ATKOP");
String opName = savedOp.getString("NAME");

for (OperatorData xop : this.ops)
if (xop.getName().equals(opName)) {
op = xop;
break;
}

if (op != null) {
String w1 = savedOp.getString("WEAPON1");
String w2 = savedOp.getString("WEAPON2");
String g = savedOp.getString("GADGET");

if (w1 != null && w2 != null && g != null)
rop = new RandomizedOperator(op, w1, w2, g);
}
}

if (rop == null) {
rop = this.getRandomOperator(null);
}

this.setOperator(rop);
}

private RandomizedOperator getRandomOperator(@Nullable OperatorData previous) {
OperatorData op = null;
while (op == null || (previous != null && op.getName().equals(previous.getName()))) {
int ro = this.rng.nextInt(this.ops.size());
op = this.ops.get(ro);
}

LoadoutData loadout = op.getLoadout();
int w1i = this.rng.nextInt(loadout.getPrimaryWeapons().size()),
w2i = this.rng.nextInt(loadout.getSecondaryWeapons().size()),
gi = this.rng.nextInt(loadout.getGadgets().size());

String w1 = loadout.getPrimaryWeapons().get(w1i),
w2 = loadout.getSecondaryWeapons().get(w2i),
g = loadout.getGadgets().get(gi);

return new RandomizedOperator(op, w1, w2, g);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.emzi0767.r6stratroulette;


import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.emzi0767.r6stratroulette.data.RandomizedOperator;
import com.emzi0767.r6stratroulette.models.*;

import java.io.File;
import java.util.*;

/**
* A simple {@link Fragment} subclass.
*/
public class DefenderRandomizerFragment extends Fragment {
private RouletteData rouletteData = null;
private File assetLocation = null;
private ArrayList<OperatorData> ops = null;

private RandomizedOperator op = null;

private ImageView img = null;
private TextView name = null, ctu = null, weapon1 = null, weapon2 = null, gadget = null;

private MainActivity mainActivity = null;

private final Random rng = new Random();

public DefenderRandomizerFragment() {
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
this.initOperators(null);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

this.mainActivity = (MainActivity)this.getActivity();
this.rouletteData = this.mainActivity.getRouletteData();
this.assetLocation = this.mainActivity.getAssetLocation();

this.initOperators(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_defender_randomizer, container, false);

this.img = v.findViewById(R.id.randomdef_img);
this.name = v.findViewById(R.id.randomdef_name);
this.ctu = v.findViewById(R.id.randomdef_ctu);
this.weapon1 = v.findViewById(R.id.randomop_def_weapon1);
this.weapon2 = v.findViewById(R.id.randomop_def_weapon2);
this.gadget = v.findViewById(R.id.randomop_def_gadget);

Button btn = v.findViewById(R.id.randomdef_randomize);
btn.setOnClickListener(b -> {
RandomizedOperator op = this.getRandomOperator(this.op.getOperator());
this.setOperator(op);
});

return v;
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
if (this.op != null) {
Bundle savedOp = new Bundle();

savedOp.putString("NAME", op.getOperator().getName());
savedOp.putString("WEAPON1", op.getPrimary());
savedOp.putString("WEAPON2", op.getSecondary());
savedOp.putString("GADGET", op.getGadget());

outState.putBundle("DEFOP", savedOp);
}

super.onSaveInstanceState(outState);
}

private void setOperator(RandomizedOperator op) {
this.op = op;

Map<String, CtuData> ctus = this.rouletteData.getCtus();

CtuData ctu = ctus.get(op.getOperator().getCtu());

this.name.setText(op.getOperator().getName());
this.ctu.setText(String.format("%s (%s)", ctu.getName(), ctu.getAbbreviation()));
this.weapon1.setText(op.getPrimary());
this.weapon2.setText(op.getSecondary());
this.gadget.setText(op.getGadget());

File img = new File(this.assetLocation, op.getOperator().getIcon());
Bitmap bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
this.img.setImageBitmap(bmp);
}

private void initOperators(Bundle savedInstanceState) {
if (this.mainActivity == null)
return;

SharedPreferences prefs = this.mainActivity.getSharedPreferences(this.getString(R.string.settings_filename), Context.MODE_PRIVATE);
Set<String> disabledOps = prefs.getStringSet(SettingsActivity.DISABLED_OPS_KEY, new HashSet<>());

OperatorsData ops = this.rouletteData.getOperators();
List<OperatorData> xops = ops.getDefenders();
this.ops = new ArrayList<>();

for (OperatorData op : xops)
if (!disabledOps.contains(op.getName()))
this.ops.add(op);

RandomizedOperator rop = null;
OperatorData op = null;

if (savedInstanceState != null && savedInstanceState.containsKey("DEFOP")) {
Bundle savedOp = savedInstanceState.getBundle("DEFOP");
String opName = savedOp.getString("NAME");

for (OperatorData xop : this.ops)
if (xop.getName().equals(opName)) {
op = xop;
break;
}

if (op != null) {
String w1 = savedOp.getString("WEAPON1");
String w2 = savedOp.getString("WEAPON2");
String g = savedOp.getString("GADGET");

if (w1 != null && w2 != null && g != null)
rop = new RandomizedOperator(op, w1, w2, g);
}
}

if (rop == null) {
rop = this.getRandomOperator(null);
}

this.setOperator(rop);
}

private RandomizedOperator getRandomOperator(@Nullable OperatorData previous) {
OperatorData op = null;
while (op == null || (previous != null && op.getName().equals(previous.getName()))) {
int ro = this.rng.nextInt(this.ops.size());
op = this.ops.get(ro);
}

LoadoutData loadout = op.getLoadout();
int w1i = this.rng.nextInt(loadout.getPrimaryWeapons().size()),
w2i = this.rng.nextInt(loadout.getSecondaryWeapons().size()),
gi = this.rng.nextInt(loadout.getGadgets().size());

String w1 = loadout.getPrimaryWeapons().get(w1i),
w2 = loadout.getSecondaryWeapons().get(w2i),
g = loadout.getGadgets().get(gi);

return new RandomizedOperator(op, w1, w2, g);
}
}
Loading

0 comments on commit 57ac840

Please sign in to comment.