com.theosirian.ppioo.util.GameData.java Source code

Java tutorial

Introduction

Here is the source code for com.theosirian.ppioo.util.GameData.java

Source

/*
 * The MIT License (MIT)
 * Copyright (c) 2015-2016, theosirian
 *
 * 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 com.theosirian.ppioo.util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.theosirian.ppioo.football.Crowd;
import com.theosirian.ppioo.football.Fan;
import com.theosirian.ppioo.football.Player;

import java.util.Date;

/**
 * Created by theosirian on 07/01/2016.
 */
public class GameData {
    private String saveName;
    private int money;
    private int victories;
    private int defeats;
    private int teamSize;
    private Array<Player> team;
    private Array<Player> players;
    private Array<Fan> fans;
    private Crowd crowd;

    public GameData() {
        this("default");
    }

    public GameData(String saveName) {
        this.saveName = saveName;
        money = 500;
        victories = 0;
        defeats = 0;
        teamSize = 6;
        team = new Array<>(teamSize);
        players = new Array<>();
        crowd = new Crowd();
        fans = new Array<>();
    }

    public GameData load() {
        Preferences state = Gdx.app.getPreferences(saveName);
        money = state.getInteger("money", money);
        victories = state.getInteger("victories", victories);
        defeats = state.getInteger("defeats", defeats);
        JsonValue json = new JsonReader().parse(Gdx.files.internal("data.json"));
        JsonValue playersJson = json.get("players");
        for (JsonValue playerJson : playersJson.iterator()) {
            Player player = new Player();
            player.setId(playerJson.getString("id"));
            player.setFirstName(playerJson.getString("first_name"));
            player.setLastName(playerJson.getString("last_name"));
            player.setAge(playerJson.getInt("age"));
            player.setCountry(playerJson.getString("country"));
            player.setPrice(playerJson.getInt("price"));
            player.setQuality(playerJson.getFloat("quality"));
            player.setConfidence(playerJson.getFloat("confidence"));
            player.setOwned(state.getBoolean(player.getId(), playerJson.getBoolean("owned", false)));
            String key = "player-" + player.getId();
            player.setDateAcquired(new Date(state.getLong(key + "-date_acquired", new Date().getTime())));
            player.setVictories(state.getInteger(key + "-victories", 0));
            player.setDefeats(state.getInteger(key + "-defeats", 0));
            players.add(player);
        }
        JsonValue fansJson = json.get("fans");
        for (JsonValue fanJson : fansJson.iterator()) {
            Fan fan = new Fan();
            fan.setId(fanJson.getString("id"));
            fan.setName(fanJson.getString("name"));
            fan.setMinSupport(fanJson.getFloat("minSupport"));
            fan.setMaxSupport(fanJson.getFloat("maxSupport"));
            fan.setMinHuff(fanJson.getFloat("minHuff"));
            fan.setMaxHuff(fanJson.getFloat("maxHuff"));
            fan.setPrice(fanJson.getInt("price"));
            String key = "fan-" + fan.getId();
            fan.setDateAcquired(new Date(state.getLong(key + "-date_acquired", new Date().getTime())));
            fan.setVictories(state.getInteger(key + "-victories", 0));
            fan.setDefeats(state.getInteger(key + "-defeats", 0));
            fans.add(fan);
        }
        return this;
    }

    public GameData save() {
        Preferences state = Gdx.app.getPreferences(saveName);
        for (Player player : players) {
            if (player.isOwned()) {
                String key = "player-" + player.getId();
                state.putBoolean(key, true);
                state.putLong(key + "-date_acquired", player.getDateAcquired().getTime());
                state.putInteger(key + "-victories", player.getVictories());
                state.putInteger(key + "-defeats", player.getDefeats());
            }
        }
        for (Fan fan : fans) {
            if (fan.isOwned()) {
                String key = "fan-" + fan.getId();
                state.putBoolean(key, true);
                state.putLong(key + "-date_acquired", fan.getDateAcquired().getTime());
                state.putInteger(key + "-victories", fan.getVictories());
                state.putInteger(key + "-defeats", fan.getDefeats());
            }
        }
        state.putInteger("money", money);
        state.putInteger("victories", victories);
        state.putInteger("defeats", defeats);
        state.flush();
        return this;
    }

    public Array<Fan> getOwnedFans() {
        Array<Fan> owned = new Array<>();
        for (Fan fan : fans) {
            if (fan.isOwned()) {
                owned.add(fan);
            }
        }
        return owned;
    }

    public Array<Fan> getNotOwnedFans() {
        Array<Fan> owned = new Array<>();
        for (Fan fan : fans) {
            if (!fan.isOwned()) {
                owned.add(fan);
            }
        }
        return owned;
    }

    public Fan findFanById(String id) {
        for (Fan fan : fans) {
            if (id.equals(fan.getId())) {
                return fan;
            }
        }
        return null;
    }

    public Array<Player> getOwnedPlayers() {
        Array<Player> owned = new Array<>();
        for (Player player : players) {
            if (player.isOwned())
                owned.add(player);
        }
        return owned;
    }

    public Array<Player> getNotOwnedPlayers() {
        Array<Player> owned = new Array<>();
        for (Player player : players) {
            if (!player.isOwned())
                owned.add(player);
        }
        return owned;
    }

    public Player findPlayerById(String id) {
        for (Player player : players) {
            if (id.equals(player.getId()))
                return player;
        }
        return null;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public int getVictories() {
        return victories;
    }

    public void setVictories(int victories) {
        this.victories = victories;
    }

    public int getDefeats() {
        return defeats;
    }

    public void setDefeats(int defeats) {
        this.defeats = defeats;
    }

    public int getTeamSize() {
        return teamSize;
    }

    public void setTeamSize(int teamSize) {
        this.teamSize = teamSize;
    }

    public Array<Player> getTeam() {
        return team;
    }

    public void setTeam(Array<Player> team) {
        this.team = team;
    }

    public Array<Fan> getFans() {
        return fans;
    }

    public void setFans(Array<Fan> fans) {
        this.fans = fans;
    }

    public Array<Player> getPlayers() {
        return players;
    }

    public void setPlayers(Array<Player> players) {
        this.players = players;
    }

    public void changeMoney(int amount) {
        this.money += amount;
    }

    public void changeDefeats(int amount) {
        this.defeats += amount;
    }

    public void changeVictories(int amount) {
        this.victories += amount;
    }
}