Skip to content

Commit

Permalink
improving #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindau committed Oct 31, 2017
1 parent 28c975d commit 448dde4
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void onClick(View v) {
Log.d(Constants.LOGT,game.getSettings().toString());

Log.d(Constants.LOGT,"gameidstring "+game.getSettings().getGameIDString());
client = new PubNubClient(new UserProfile(Constants.HOST_USERNAME),game.getSettings().getGameIDString());
client = new PubNubClient(new UserProfile(Constants.HOST_USERNAME),game.getSettings().getGameIDString(),true);

//listView.setAdapter(this.mPresence);

Expand Down
125 changes: 47 additions & 78 deletions application/app/src/main/java/isdp/guess_a_song/JoinGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.pubnub.api.PNConfiguration;
import com.pubnub.api.PubNub;
import com.pubnub.api.callbacks.PNCallback;
import com.pubnub.api.callbacks.SubscribeCallback;
Expand All @@ -28,105 +27,83 @@
import android.widget.ProgressBar;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import isdp.guess_a_song.model.Action;
import isdp.guess_a_song.controller.PlayerGame;
import isdp.guess_a_song.controller.PubNubClient;
import isdp.guess_a_song.model.UserProfile;
import isdp.guess_a_song.utils.Constants;

public class JoinGame extends AppCompatActivity {

private EditText gameID_field;
private EditText gamePIN_field;
private EditText name_field;
private TextView info_field;

Button btnJoin;
private ProgressBar spinner;

private PlayerGame game;

private String gameID;
private String gamePIN;
private String userName;
ImageView checkImg;

private PubNubClient client;

UserProfile player;


@Override
protected void onCreate(Bundle savedInstanceState) {

// INIT
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_join_game);
gameID_field= (EditText)findViewById(R.id.etGameId);
gamePIN_field = (EditText) findViewById(R.id.etPin);
name_field = (EditText) findViewById(R.id.etName);
info_field = (TextView) findViewById(R.id.tvInfo);

btnJoin = (Button) findViewById(R.id.btJoin);

checkImg = (ImageView) findViewById(R.id.imageView);

this.game = new PlayerGame(0,0);

spinner=(ProgressBar)findViewById(R.id.progressBar);
checkImg.setVisibility(View.INVISIBLE);
spinner.setVisibility(View.GONE);


//this.game = new PlayerGame(0,0);
//Some adjustments
btnJoin.setVisibility(View.VISIBLE);
info_field.setText("Please fill all fields and click Jooin");
info_field.setText("Please fill all fields and click join");



}

public void join(View view) {

spinner.setVisibility(View.VISIBLE);

gameID = gameID_field.getText().toString();
gamePIN = gamePIN_field.getText().toString();
userName = name_field.getText().toString();

spinner=(ProgressBar)findViewById(R.id.progressBar);
String uniqueID= Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);

this.client = new PubNubClient(new UserProfile(this.userName),this.gameID);

// channel subscribed. Now waiting for players.

if(userName==null){
this.client.getPubnub().unsubscribeAll();
}

// Map<String, JsonElement> r = this.client.getPresenceState();
//
// for (Map.Entry<String, JsonElement> entry : r.entrySet())
// {
// System.out.println(entry.getKey() + "/" + entry.getValue());
// Log.v(Constants.LOGT, "key: (" +entry.getKey() + ") value: "+entry.getValue().toString());
//
// }
player = new UserProfile(userName,uniqueID,false,false);
//show spinner after button click
spinner.setVisibility(View.VISIBLE);

//init PubNub Client
PNConfiguration pnConfiguration = new PNConfiguration();

this.client = new PubNubClient(player,this.gameID,false);


this.client.getRoomPlayers();
//this.client.getRoomPlayers();

this.client.getPubnub().addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
if (status.getCategory() == PNStatusCategory.PNConnectedCategory){
//USER CONNECTED

//{ "action": "auth_checl", "value": "pin...","publisher": "nickname..","recipient": "THE BOSS"}
pubnub.publish().channel(gameID).message(new Action(Constants.A_LOG_IN, gamePIN, userName, Constants.HOST_USERNAME) ).async(new PNCallback<PNPublishResult>() {
// USER CONNECTED
// Ask Host for authentication on connected
//{ "action": "auth_check", "value": "pin...","publisher": "nickname..","recipient": "THE BOSS"}
Action ask_auth = new Action(Constants.A_LOG_IN, gamePIN, userName, Constants.HOST_USERNAME);
pubnub.publish().channel(gameID).message( ask_auth ).async(new PNCallback<PNPublishResult>() {
@Override
public void onResponse(PNPublishResult result, PNStatus status) {
// handle publish response
Expand All @@ -143,23 +120,18 @@ public void message(PubNub pubnub, PNMessageResult message) {
if(action.getRecipient().equals(userName) || action.getRecipient().equals(Constants.A_FOR_ALL) ){
if (action.getAction().equals(Constants.A_AUTH_RESPONSE)) {
if (action.getValue().equals(Constants.TRUE)) {

JsonObject state = new JsonObject();
state.addProperty("is_auth", true);

//IF HOST sends to this user and action is authorisation and auth value is TRUE
player.setAuth(true);
pubnub.setPresenceState()
.channels(Arrays.asList(gameID))
//.uuid(userName)
.state(state)
.state(player.getState())
.async(new PNCallback<PNSetStateResult>() {
@Override
public void onResponse(final PNSetStateResult result, PNStatus status) {
Log.d(Constants.LOGT, "*** state updated");
Log.d(Constants.LOGT, "set auth state true OK");
}
});


Log.d(Constants.LOGT, "PLAYER MESSAGE LISTENER: AUTH SUCCESS");
runOnUiThread(new Runnable() {
@Override
public void run() {
Expand All @@ -170,14 +142,19 @@ public void run() {
gameID_field.setVisibility(View.GONE);
gamePIN_field.setVisibility(View.GONE);
info_field.setText("Authentication Success! The Host will start soon the game");

}
});

game.setAuth(1);
} else {

Log.d(Constants.LOGT, "PLAYER MESSAGE LISTENER: AUTH FALSE");
pubnub.setPresenceState()
.channels(Arrays.asList(gameID))
//.uuid(userName)
.state(player.getState())
.async(new PNCallback<PNSetStateResult>() {
@Override
public void onResponse(final PNSetStateResult result, PNStatus status) {
Log.d(Constants.LOGT, "set auth state false OK");
}
});
client.getPubnub().unsubscribeAll();

runOnUiThread(new Runnable() {
Expand Down Expand Up @@ -216,16 +193,13 @@ public void run() {

@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
Log.d(Constants.LOGT, "[USER PRESENCE] event "+ presence.getEvent()+
"state:=" +presence.getState()+ " uuid:= "+ presence.getUuid());



//Log.d(Constants.LOGT, "[USER PRESENCE] event "+ presence.getEvent()+
// "state:=" +presence.getState()+ " uuid:= "+ presence.getUuid());
}
});


this.client.subscribe(this.gameID,Constants.WITH_PRESENCE);
this.client.subscribe(this.gameID,Constants.NO_PRESENCE);


//Intent for the join screen
Expand Down Expand Up @@ -254,17 +228,12 @@ public void onClick(DialogInterface dialog, int which) {
);
builder.show();
}


private ObjectNode createState() {
JsonNodeFactory factory = JsonNodeFactory.instance;
ObjectNode payload = factory.objectNode();


payload.put("auth", 1);
payload.put("fullname", "Mindaugas");


return payload;
@Override
protected void onStop() {
super.onStop();
if(this.client.getPubnub()!=null){
this.client.getPubnub().unsubscribeAll();
}
}

}
34 changes: 30 additions & 4 deletions application/app/src/main/java/isdp/guess_a_song/_4GameRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

import com.google.gson.Gson;
import com.pubnub.api.PubNub;
import com.pubnub.api.callbacks.PNCallback;
import com.pubnub.api.callbacks.SubscribeCallback;
import com.pubnub.api.enums.PNStatusCategory;
import com.pubnub.api.models.consumer.PNStatus;
import com.pubnub.api.models.consumer.presence.PNSetStateResult;
import com.pubnub.api.models.consumer.pubsub.PNMessageResult;
import com.pubnub.api.models.consumer.pubsub.PNPresenceEventResult;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import isdp.guess_a_song.model.Action;
Expand Down Expand Up @@ -82,7 +86,10 @@ protected void onCreate(Bundle savedInstanceState) {
this.gamePIN = Helpers.randomNumberString(Constants.RANDOM_MIN,Constants.RANDOM_MAX);

//client
client = new PubNubClient(new UserProfile(Constants.HOST_USERNAME),this.gameID);
String uniqueID= android.provider.Settings.Secure.getString(getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ANDROID_ID);
final UserProfile host = new UserProfile(Constants.HOST_USERNAME,uniqueID,true,true);
client = new PubNubClient(host,this.gameID,true);



Expand All @@ -91,6 +98,19 @@ protected void onCreate(Bundle savedInstanceState) {
public void status(PubNub pubnub, PNStatus status) {
if (status.getCategory() == PNStatusCategory.PNConnectedCategory){

pubnub.setPresenceState()
.channels(Arrays.asList(gameID))
//.uuid(userName)
.state(host.getState())
.async(new PNCallback<PNSetStateResult>() {
@Override
public void onResponse(final PNSetStateResult result, PNStatus status) {
Log.d(Constants.LOGT, "set auth state true OK");
}
});



// pubnub.publish().channel("123456").message("status.getCategory() == PNStatusCategory.PNConnectedCategory").async(new PNCallback<PNPublishResult>() {
// @Override
// public void onResponse(PNPublishResult result, PNStatus status) {
Expand All @@ -113,9 +133,10 @@ public void message(PubNub pubnub, PNMessageResult message) {

if(action.getValue().equals(gamePIN)){
Log.d(Constants.LOGT, "HOST MESSAGE LISTENER Player "+ action.getPublisher() + " auth true SUCCESS!");
client.publish(gameID,new Action(Constants.A_AUTH_RESPONSE,Constants.TRUE,Constants.HOST_USERNAME,action.getPublisher()));

client.publish(gameID,new Action(Constants.A_AUTH_RESPONSE,Constants.TRUE,Constants.HOST_USERNAME,action.getPublisher()),signHostMeta());
}else{
client.publish(gameID,new Action(Constants.A_AUTH_RESPONSE,Constants.FALSE,Constants.HOST_USERNAME,action.getPublisher()));
client.publish(gameID,new Action(Constants.A_AUTH_RESPONSE,Constants.FALSE,Constants.HOST_USERNAME,action.getPublisher()),signHostMeta());
Log.d(Constants.LOGT, "HOST MESSAGE LISTENER Player "+ action.getPublisher() + " auth false (bad pin)");
}
}
Expand Down Expand Up @@ -183,7 +204,7 @@ public void startGame(View view) {
players.add(new UserProfile(entry.getValue().getSender()));
}
}
client.publish(gameID,new Action(Constants.A_START_GAME,Constants.TRUE,Constants.HOST_USERNAME,Constants.A_FOR_ALL));
client.publish(gameID,new Action(Constants.A_START_GAME,Constants.TRUE,Constants.HOST_USERNAME,Constants.A_FOR_ALL),signHostMeta());

// PresencePojo temp;
// for (int i=0;i<this.mPresence.getCount();i++){
Expand All @@ -207,5 +228,10 @@ public void startGame(View view) {
startActivity(intent);

}
public Map<String, Object> signHostMeta(){
Map<String, Object> meta = new HashMap<>();
meta.put("from", Constants.HOST_USERNAME);
return meta;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class PubNubClient{
private UserProfile user;
private boolean usePost;

public PubNubClient(UserProfile user,String gameID) {
public PubNubClient(UserProfile user,String gameID, boolean isHost) {

this.gameID = gameID;
this.user = user;
Expand All @@ -54,19 +54,27 @@ public PubNubClient(UserProfile user,String gameID) {
PNConfiguration config = new PNConfiguration();
config.setPublishKey(Constants.PKEY);
config.setSubscribeKey(Constants.SKEY);
config.setUuid(this.user.getName());
//config.setSecure(true);

if (isHost){
config.setUuid(this.user.getName());
}else{
config.setUuid(this.user.getUuid());
config.setFilterExpression("from == '" + Constants.HOST_USERNAME+"'");
}

this.pubnub = new PubNub(config);
Log.d(Constants.LOGT, user.getName() + " PubNub Client init");

}


public void publish(final String gameID, Action m) {
public void publish(final String gameID, Action m,Map<String, Object> meta) {
try {
pubnub.publish()
.message(m)
.channel(gameID)
.meta(meta)
.shouldStore(true)
.usePOST(this.usePost)
.async(new PNCallback<PNPublishResult>() {
Expand Down
Loading

0 comments on commit 448dde4

Please sign in to comment.