Android Open Source - PowerGridCompanion Turn Order Summary






From Project

Back to project page PowerGridCompanion.

License

The source code is released under:

Apache License

If you think the Android project PowerGridCompanion listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mintcode.kris.powergridhelper.Activities;
/*from ww w .  ja  v  a 2  s  .  com*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.mintcode.kris.powergridhelper.Adapters.PlayerOrderAdapter;
import com.mintcode.kris.powergridhelper.Adapters.PlayerSetupAdapter;
import com.mintcode.kris.powergridhelper.Models.Player;
import com.mintcode.kris.powergridhelper.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Random;

public class TurnOrderSummary extends Activity {

    public ArrayList<Player> playerList;
    public HashMap<String, Integer> resourceList;
    Context mContext;
    int stage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_turn_order_summary);

        mContext = this;

        final Bundle extras = getIntent().getExtras();
        resourceList = new HashMap<String, Integer>();
        playerList = extras.getParcelableArrayList("PlayerList");

        resourceList.put("Coal", extras.getInt("Coal"));
        resourceList.put("Oil", extras.getInt("Oil"));
        resourceList.put("Garbage", extras.getInt("Garbage"));
        resourceList.put("Uranium", extras.getInt("Uranium"));

        stage = extras.getInt("Stage");


        int trig = 10;
        if (playerList.size() > 2) {
            if (playerList.size() > 5) {
                trig = 6;
            } else {
                trig = 7;
            }
        }
        for (Player player : playerList) {
            if (player.getCities_owned() >= trig && stage == 1) {
                stage = 2;
            }
            if(stage == 3) {
                switch (playerList.size()){
                    case 2:
                        if(player.getCities_owned() >= 21){
                            stage = 4;
                        }
                        break;
                    case 3:
                        if(player.getCities_owned() >= 17){
                            stage = 4;
                        }
                        break;
                    case 4:
                        if(player.getCities_owned() >= 17){
                            stage = 4;
                        }
                        break;
                    case 5:
                        if(player.getCities_owned() >= 15){
                            stage = 4;
                        }
                        break;
                    case 6:
                        if(player.getCities_owned() >= 14){
                            stage = 4;
                        }
                        break;
                }
            }

        }


        if(stage > 0 && stage < 4) {
            //Payout
            for (Player p : playerList) {

                switch (p.getCities_powering()) {
                    case 0:
                        p.setMoney(p.getMoney() + 10);
                        break;
                    case 1:
                        p.setMoney(p.getMoney() + 22);
                        break;
                    case 2:
                        p.setMoney(p.getMoney() + 33);
                        break;
                    case 3:
                        p.setMoney(p.getMoney() + 44);
                        break;
                    case 4:
                        p.setMoney(p.getMoney() + 54);
                        break;
                    case 5:
                        p.setMoney(p.getMoney() + 73);
                        break;
                    case 6:
                        p.setMoney(p.getMoney() + 82);
                        break;
                    case 7:
                        p.setMoney(p.getMoney() + 90);
                        break;
                    case 8:
                        p.setMoney(p.getMoney() + 98);
                        break;
                    case 9:
                        p.setMoney(p.getMoney() + 105);
                        break;
                    case 10:
                        p.setMoney(p.getMoney() + 112);
                        break;
                    case 11:
                        p.setMoney(p.getMoney() + 118);
                        break;
                    case 12:
                        p.setMoney(p.getMoney() + 124);
                        break;
                    case 13:
                        p.setMoney(p.getMoney() + 129);
                        break;
                    case 14:
                        p.setMoney(p.getMoney() + 134);
                        break;
                    case 15:
                        p.setMoney(p.getMoney() + 138);
                        break;
                    case 16:
                        p.setMoney(p.getMoney() + 142);
                        break;
                    case 17:
                        p.setMoney(p.getMoney() + 145);
                        break;
                    case 18:
                        p.setMoney(p.getMoney() + 148);
                        break;
                    case 19:
                        p.setMoney(p.getMoney() + 150);
                        break;
                    case 20:
                        p.setMoney(p.getMoney() + 10);
                        break;
                }
            }
        }


        ListView lv = (ListView) findViewById(R.id.TurnOrderListView);
        Collections.sort(playerList, new Comparator<Player>() {
            @Override
            public int compare(Player lhs, Player rhs) {


                if (stage == 0) {
                    return rhs.getOrder() - lhs.getOrder();
                }

                //Largest Powerplant
                if (lhs.getCities_owned() == rhs.getCities_owned()) {
                    return  rhs.getLargest_power_plant() - lhs.getLargest_power_plant();
                }

                return  rhs.getCities_owned() - lhs.getCities_owned();
            }
        });

        //TODO: Payout Logic
        if (stage > 0 && stage < 4) {
            switch (stage) {
                case 1:
                    //Reload resources
                    if (playerList.size() == 2) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 3);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 2);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 1);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 1);
                    } else if (playerList.size() == 3) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 4);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 2);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 1);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 1);
                    } else if (playerList.size() == 4) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 5);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 3);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 2);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 1);
                    } else if (playerList.size() == 5) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 5);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 4);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 3);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 2);
                    } else if (playerList.size() == 6) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 7);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 5);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 3);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 2);
                    }
                    break;
                case 2:
                    //Reload resources
                    if (playerList.size() == 2) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 4);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 2);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 2);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 1);
                    } else if (playerList.size() == 3) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 5);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 3);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 2);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 1);
                    } else if (playerList.size() == 4) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 6);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 4);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 3);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 2);
                    } else if (playerList.size() == 5) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 7);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 5);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 3);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 3);
                    } else if (playerList.size() == 6) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 9);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 6);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 5);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 3);
                    }
                    break;
                case 3:
                    //Reload resources
                    if (playerList.size() == 2) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 3);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 4);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 3);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 1);
                    } else if (playerList.size() == 3) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 3);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 4);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 3);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 1);
                    } else if (playerList.size() == 4) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 4);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 5);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 4);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 2);
                    } else if (playerList.size() == 5) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 5);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 6);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 5);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 2);
                    } else if (playerList.size() == 6) {
                        resourceList.put("Coal", resourceList.get("Coal").intValue() + 6);
                        resourceList.put("Oil", resourceList.get("Oil").intValue() + 7);
                        resourceList.put("Garbage", resourceList.get("Garbage").intValue() + 6);
                        resourceList.put("Uranium", resourceList.get("Uranium").intValue() + 3);
                    }
                    break;
            }

            if(resourceList.get("Coal").intValue() > 24){
                resourceList.put("Coal", 24);
            }

            if(resourceList.get("Oil").intValue() > 24){
                resourceList.put("Oil", 24);
            }

            if(resourceList.get("Garbage").intValue() > 24){
                resourceList.put("Garbage", 24);
            }

            if(resourceList.get("Uranium").intValue() > 12){
                resourceList.put("Uranium", 12);
            }
        } else {
            if(stage == 0)
                stage = 1;
        }

        if(stage != 4) {
            this.getActionBar().setTitle("Stage: " + stage);
        }else{
            this.getActionBar().setTitle("The Winner is "+playerList.get(0).getName()+"!");
            findViewById(R.id.SetupFinishButton).setVisibility(View.GONE);
        }
        lv.setAdapter(new PlayerOrderAdapter(this, R.id.TurnOrderListView, playerList));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.turn_order_summary, menu);
        return true;
    }


    @Override
    public void onBackPressed(){

        LayoutInflater inflater = LayoutInflater.from(mContext);
        final View dialoglayout = inflater.inflate(R.layout.back_dialog, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
        alertDialogBuilder.setView(dialoglayout);

        //set buttons
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        alertDialogBuilder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                closeActivity();
            }
        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

    public void closeActivity(){
        this.finish();
    }


    public void FinishedSummary(final View v) {

        for (Player p : playerList) {
            if (p.getColor() == Color.GRAY) {
                Toast.makeText(this, "Please Finish Setup!", Toast.LENGTH_LONG).show();
                return;
            }
        }


        Intent mainIntent = new Intent(v.getContext(), PowerPlantAuctionActivity.class);

        Bundle extras = new Bundle();
        extras.putInt("Coal", resourceList.get("Coal"));
        extras.putInt("Oil", resourceList.get("Oil"));
        extras.putInt("Garbage", resourceList.get("Garbage"));
        extras.putInt("Uranium", resourceList.get("Uranium"));
        extras.putInt("Stage", stage);
        extras.putParcelableArrayList("PlayerList", playerList);
        mainIntent.putExtras(extras);

        startActivity(mainIntent);

    }
}




Java Source Code List

com.mintcode.kris.powergridhelper.Activities.BuildingActivity.java
com.mintcode.kris.powergridhelper.Activities.PlayerSelection.java
com.mintcode.kris.powergridhelper.Activities.PlayerSetup.java
com.mintcode.kris.powergridhelper.Activities.PowerPlantAuctionActivity.java
com.mintcode.kris.powergridhelper.Activities.ResourcePurchaseActivity.java
com.mintcode.kris.powergridhelper.Activities.StartScreen.java
com.mintcode.kris.powergridhelper.Activities.TurnOrderSummary.java
com.mintcode.kris.powergridhelper.Adapters.BuildingAdapter.java
com.mintcode.kris.powergridhelper.Adapters.PlayerOrderAdapter.java
com.mintcode.kris.powergridhelper.Adapters.PlayerSetupAdapter.java
com.mintcode.kris.powergridhelper.Adapters.PowerPlantAdapter.java
com.mintcode.kris.powergridhelper.Adapters.ResourceAdapter.java
com.mintcode.kris.powergridhelper.Models.Player.java