Android Open Source - capture-the-flag Game






From Project

Back to project page capture-the-flag.

License

The source code is released under:

Copyright ? 2014 Microsoft Mobile Oy. All rights reserved. Microsoft is a registered trademark of Microsoft Corporation. Nokia and HERE are trademarks and/or registered trademarks of Nokia Corporati...

If you think the Android project capture-the-flag 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

/*
 * Copyright (c) 2014 Microsoft Mobile. All rights reserved.
 * See the license text file provided with this project for more information.
 */// ww  w  .j av  a 2  s  . c  o  m

package com.nokia.example.capturetheflag.network.model;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Object representing the game, contains the players and the flags.
 */
public class Game {
    public static final int NEW_GAME = 0;

    private ArrayList<Player> mPlayers;
    private String mName;
    private Flag mRedFlag;
    private Flag mBlueFlag;
    private int mId;
    private boolean mHasEnded = false;
    private boolean mIsPremium = false;

    public Game(int id) {
        mId = id;
        mPlayers = new ArrayList<Player>();
    }

    public Game(final JSONObject jsonObj) throws JSONException {
        this(jsonObj.getInt(ModelConstants.ID_KEY));
        setName(jsonObj.getString(ModelConstants.NAME_KEY));
        JSONArray jsonArray = jsonObj.getJSONArray(ModelConstants.PLAYERS_KEY);

        for (int i = 0; i < jsonArray.length(); i++) {
            Player p = new Player(jsonArray.getJSONObject(i));
            addPlayer(p);
        }

        JSONObject redflag = jsonObj.getJSONObject(ModelConstants.RED_FLAG_KEY);
        JSONObject blueflag = jsonObj.getJSONObject(ModelConstants.BLUE_FLAG_KEY);

        mRedFlag = new Flag(
                redflag.getDouble(ModelConstants.LATITUDE_KEY),
                redflag.getDouble(ModelConstants.LONGITUDE_KEY));
        mBlueFlag = new Flag(
                blueflag.getDouble(ModelConstants.LATITUDE_KEY),
                blueflag.getDouble(ModelConstants.LONGITUDE_KEY));

        mIsPremium = jsonObj.optBoolean(ModelConstants.IS_PREMIUM_KEY, false);
    }

    @Override
    public String toString() {
        return mName;
    }

    public JSONObject toJSON() throws JSONException {
        JSONObject obj = new JSONObject();
        obj.put(ModelConstants.NAME_KEY, getName());
        obj.put(ModelConstants.ID_KEY, getId());
        obj.put(ModelConstants.RED_FLAG_KEY, mRedFlag.toJSON());
        obj.put(ModelConstants.BLUE_FLAG_KEY, mBlueFlag.toJSON());
        obj.put(ModelConstants.PLAYERS_KEY, new JSONArray());
        return obj;
    }

    public void setId(int mId) {
        this.mId = mId;
    }

    public int getId() {
        return mId;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getName() {
        return mName;
    }

    public void addPlayer(Player p) {
        mPlayers.add(p);
    }

    public ArrayList<Player> getPlayers() {
        return mPlayers;
    }

    public void setRedFlag(Flag flag) {
        mRedFlag = flag;
    }

    public Flag getRedFlag() {
        return mRedFlag;
    }

    public void setBlueFlag(Flag flag) {
        mBlueFlag = flag;
    }

    public Flag getBlueFlag() {
        return mBlueFlag;
    }

    public void setHasEnded(boolean hasEnded) {
        mHasEnded = hasEnded;
    }

    public boolean getHasEnded() {
        return mHasEnded;
    }

    public void setPremium(boolean isPremium) {
        mIsPremium = isPremium;
    }

    public boolean isPremium() {
        return mIsPremium;
    }
}




Java Source Code List

com.nokia.example.capturetheflag.AboutActivity.java
com.nokia.example.capturetheflag.Controller.java
com.nokia.example.capturetheflag.CreateGameFragment.java
com.nokia.example.capturetheflag.GameEndedDialogFragment.java
com.nokia.example.capturetheflag.GameMenuFragment.java
com.nokia.example.capturetheflag.HelpActivity.java
com.nokia.example.capturetheflag.JoinGameFragment.java
com.nokia.example.capturetheflag.MainActivity.java
com.nokia.example.capturetheflag.PauseDialog.java
com.nokia.example.capturetheflag.PurchasePremiumFragment.java
com.nokia.example.capturetheflag.ServerSettingsDialog.java
com.nokia.example.capturetheflag.Settings.java
com.nokia.example.capturetheflag.location.LocationManagerBase.java
com.nokia.example.capturetheflag.location.LocationManagerFactory.java
com.nokia.example.capturetheflag.location.LocationManagerInterface.java
com.nokia.example.capturetheflag.location.LocationManagerListener.java
com.nokia.example.capturetheflag.location.LocationUtils.java
com.nokia.example.capturetheflag.location.google.LocationManagerGoogle.java
com.nokia.example.capturetheflag.location.here.LocationManagerHere.java
com.nokia.example.capturetheflag.map.GameMapFactory.java
com.nokia.example.capturetheflag.map.GameMapInterface.java
com.nokia.example.capturetheflag.map.GameMapUtils.java
com.nokia.example.capturetheflag.map.MarkerFactoryBase.java
com.nokia.example.capturetheflag.map.google.GameMapGoogle.java
com.nokia.example.capturetheflag.map.google.MarkerFactoryGoogle.java
com.nokia.example.capturetheflag.map.here.GameMapHere.java
com.nokia.example.capturetheflag.map.here.MarkerFactoryHere.java
com.nokia.example.capturetheflag.network.FlagCapturedResponse.java
com.nokia.example.capturetheflag.network.GameListRequest.java
com.nokia.example.capturetheflag.network.GameListResponse.java
com.nokia.example.capturetheflag.network.JSONRequest.java
com.nokia.example.capturetheflag.network.JSONResponse.java
com.nokia.example.capturetheflag.network.JoinRequest.java
com.nokia.example.capturetheflag.network.JoinedResponse.java
com.nokia.example.capturetheflag.network.NetworkClient.java
com.nokia.example.capturetheflag.network.OfflineClient.java
com.nokia.example.capturetheflag.network.SocketIONetworkClient.java
com.nokia.example.capturetheflag.network.UpdatePlayerRequest.java
com.nokia.example.capturetheflag.network.UpdatePlayerResponse.java
com.nokia.example.capturetheflag.network.model.Flag.java
com.nokia.example.capturetheflag.network.model.Game.java
com.nokia.example.capturetheflag.network.model.ModelConstants.java
com.nokia.example.capturetheflag.network.model.Player.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerBase.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerFactory.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface.java
com.nokia.example.capturetheflag.notifications.NotificationsUtils.java
com.nokia.example.capturetheflag.notifications.google.GcmBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.google.GcmIntentService.java
com.nokia.example.capturetheflag.notifications.google.NotificationsManagerGoogle.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsIntentService.java
com.nokia.example.capturetheflag.notifications.nokia.NotificationsManagerNokia.java