cat.wuyingren.rorhelper.fragments.GameStatusFragment.java Source code

Java tutorial

Introduction

Here is the source code for cat.wuyingren.rorhelper.fragments.GameStatusFragment.java

Source

/** Copyright (c) 2014 Jordi Lpez <@wuyingren>
 *
 * MIT License:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
package cat.wuyingren.rorhelper.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import cat.wuyingren.rorhelper.R;
import cat.wuyingren.rorhelper.activities.MainActivity;
import cat.wuyingren.rorhelper.profiles.Faction;
import cat.wuyingren.rorhelper.profiles.Game;
import cat.wuyingren.rorhelper.profiles.Senator;
import cat.wuyingren.rorhelper.sql.GameDataSource;
import cat.wuyingren.rorhelper.utils.StatusAdapter;

public class GameStatusFragment extends Fragment {

    public static final String ARG_GAME_ID = "game_id";

    private RecyclerView mRecyclerView;
    private TextView statusTitleText;
    private TextView statusScenarioText;
    private TextView mostAvSenatorText;
    private TextView totalVotesText;
    private TextView statusFactions;

    private StatusAdapter mAdapter;
    private GameDataSource dataSource;
    private Context context;

    private long gameID;
    private Game currentGame;

    /*public static GameStatusFragment newInstance(long gameId) {
    GameStatusFragment fragment = new GameStatusFragment();
    Bundle args = new Bundle();
    //args.putInt(ARG_SECTION_NUMBER, position);
    args.putLong(ARG_GAME_ID, gameId);
    fragment.setArguments(args);
    return fragment;
    }*/

    public GameStatusFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = MainActivity.getContext();
        dataSource = new GameDataSource(context);
        dataSource.open();

        gameID = getArguments().getLong(ARG_GAME_ID);
        Log.w("TAG", "showing info for " + gameID);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_status, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);

        statusTitleText = (TextView) rootView.findViewById(R.id.statusTitleText);
        statusScenarioText = (TextView) rootView.findViewById(R.id.statusScenarioText);
        mostAvSenatorText = (TextView) rootView.findViewById(R.id.mostAvSenatorText);
        totalVotesText = (TextView) rootView.findViewById(R.id.totalVotesText);
        statusFactions = (TextView) rootView.findViewById(R.id.statusFactions);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        currentGame = dataSource.getGameById(gameID);
        int numplayers = currentGame.getPlayers();
        int totalVotes = 0;
        String phase = Game.getPhaseName(currentGame.getPhase(), this.getResources());

        ArrayList<Faction> factions = dataSource.getAllFactions(currentGame.getId());

        if (factions.size() != 0) {
            /*prepareFactionInfo(factions.get(0), tb1title, tb1subt, tb1senat, dataSource);
            prepareFactionInfo(factions.get(1), tb2title, tb2subt, tb2senat, dataSource);
            prepareFactionInfo(factions.get(2), tb3title, tb3subt, tb3senat, dataSource);
            if(factions.size()>3) { // 2 players -> 4 neutrals
            prepareFactionInfo(factions.get(3), tb4title, tb4subt, tb4senat, dataSource);
            }*/
            for (int i = 0; i < factions.size(); i++) {
                totalVotes = totalVotes + factions.get(i).getVotes();
            }
        }

        Senator s = dataSource.getSenatorById(currentGame.getMostAvSenatorID());

        if (currentGame.getGame() == null) {
            statusTitleText.setText(getString(R.string.status_nogame));
            statusScenarioText.setText("");
            mostAvSenatorText.setText("");
            totalVotesText.setText("");
            statusFactions.setVisibility(View.GONE);
        } else {

            switch (numplayers) {
            case 1:
                statusTitleText.setText("\"" + currentGame.getGame() + "\" " + getString(R.string.status) + " - "
                        + numplayers + getString(R.string.player));
                break;
            default:
                statusTitleText.setText("\"" + currentGame.getGame() + "\" " + getString(R.string.status) + " - "
                        + numplayers + getString(R.string.players));
                break;
            }
            statusScenarioText.setText(currentGame.getScenario() + " - " + getString(R.string.turn) + ": "
                    + currentGame.getTurn() + " (" + phase + ")");

            if (s != null) {
                int base = s.getLoyalty() + s.getTreasury();
                mostAvSenatorText.setText(getString(R.string.mostAvSenator) + " " + getString(R.string.is) + " "
                        + s.getName() + " " + getString(R.string.with) + " " + base * (-1));
            }

            totalVotesText.setText(getString(R.string.totalVotes) + ": " + totalVotes + "(" + totalVotes / 2 + ")");

            statusFactions.setVisibility(View.VISIBLE);
        }

        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mAdapter = new StatusAdapter(dataSource.getAllFactions(gameID), R.layout.cards_status, context);
        mRecyclerView.setAdapter(mAdapter);
    }
}