Skip to content

Commit

Permalink
working further on #43
Browse files Browse the repository at this point in the history
  • Loading branch information
grambas committed Oct 31, 2017
1 parent 6812022 commit be5e325
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 49 deletions.
2 changes: 2 additions & 0 deletions application/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ captures/
# Intellij
*.iml
.idea/workspace.xml
.idea/misc.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/dictionaries
.idea/libraries


# Keystore files
*.jks

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public void onClick(View v) {
//ListView listView = (ListView) findViewById(R.id.presence_list);

//client
Log.d("mylogger",game.getSettings().toString());
Log.d(Constants.LOGT,game.getSettings().toString());

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

//listView.setAdapter(this.mPresence);
Expand Down
165 changes: 137 additions & 28 deletions application/app/src/main/java/isdp/guess_a_song/JoinGame.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package isdp.guess_a_song;

import android.content.DialogInterface;
import android.os.Bundle;
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.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.PNPublishResult;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import isdp.guess_a_song.db.DatabaseHandler;
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;
Expand All @@ -33,16 +45,20 @@ 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 gameName;
private String userName;
ImageView checkImg;

private PubNubClient client;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -51,6 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
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);
Expand All @@ -62,13 +80,9 @@ protected void onCreate(Bundle savedInstanceState) {
spinner.setVisibility(View.GONE);


runOnUiThread(new Runnable() {
@Override
public void run() {
//TODO DOES NOT WORK
btnJoin.setVisibility(View.VISIBLE);
}
});
btnJoin.setVisibility(View.VISIBLE);
info_field.setText("Please fill all fields and click Jooin");



}
Expand All @@ -79,23 +93,40 @@ public void join(View view) {

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

spinner=(ProgressBar)findViewById(R.id.progressBar);

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

client.subscribe(this.gameID,Constants.NO_PRESENCE);
// 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());
//
// }




this.client.getRoomPlayers();

client.getPubnub().addListener(new SubscribeCallback() {
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, gameName, Constants.HOST_USERNAME) ).async(new PNCallback<PNPublishResult>() {
pubnub.publish().channel(gameID).message(new Action(Constants.A_LOG_IN, gamePIN, userName, Constants.HOST_USERNAME) ).async(new PNCallback<PNPublishResult>() {
@Override
public void onResponse(PNPublishResult result, PNStatus status) {
// handle publish response
Expand All @@ -109,22 +140,61 @@ public void message(PubNub pubnub, PNMessageResult message) {
Log.v(Constants.LOGT, "PLAYER MESSAGE LISTENER: (" +message.toString() + ")");
Gson gson = new Gson();
Action action= gson.fromJson(message.getMessage(), Action.class);
if(action.getRecipient().equals(gameName) ){
if(action.getRecipient().equals(userName) ){
if (action.getAction().equals(Constants.A_AUTH_RESPONSE)) {
if (action.getValue().equals(Constants.TRUE)) {

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

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


Log.d(Constants.LOGT, "PLAYER MESSAGE LISTENER: AUTH SUCCESS");
checkImg.setVisibility(View.VISIBLE);
spinner.setVisibility(View.GONE);
runOnUiThread(new Runnable() {
@Override
public void run() {
checkImg.setVisibility(View.VISIBLE);
spinner.setVisibility(View.GONE);
btnJoin.setVisibility(View.GONE);
name_field.setVisibility(View.GONE);
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);
// NOTIFY USER FOR SUCCESSFULY AUTH.
} else {
Log.d(Constants.LOGT, "PLAYER MESSAGE LISTENER: AUTH FALSE");
spinner.setVisibility(View.GONE);
btnJoin.setVisibility(View.VISIBLE);
btnJoin.invalidate();

Log.d(Constants.LOGT, "PLAYER MESSAGE LISTENER: AUTH FALSE");
client.getPubnub().unsubscribeAll();

runOnUiThread(new Runnable() {
@Override
public void run() {
spinner.setVisibility(View.GONE);
checkImg.setVisibility(View.GONE);
btnJoin.setVisibility(View.VISIBLE);
name_field.setVisibility(View.VISIBLE);
gameID_field.setVisibility(View.VISIBLE);
gamePIN_field.setVisibility(View.VISIBLE);
info_field.setText("Your Authentication failed. Please check all fields and try one more time!");

}
});
}

}else if (action.getAction().equals(Constants.A_START_GAME)) {
if ( action.getValue().equals(Constants.TRUE) && action.getPublisher().equals(Constants.HOST_USERNAME)) {
//TODO START A GAME TRIGGER HERE
Expand All @@ -139,16 +209,55 @@ public void message(PubNub pubnub, PNMessageResult message) {

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



}
});


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



//test publish
client.publish(gameID, new Action("log_in", gamePIN, game.getUser().getName(), Constants.HOST_USERNAME) );
//Intent for the join screen
}

@Override
public void onBackPressed() {
//Asking the player to quit or something
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Do you really want to quit?");
builder.setMessage("This will leave the room");
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
client.getPubnub().unsubscribeAll();
JoinGame.super.onBackPressed();

}
});
builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Doing nothing...
}
}
);
builder.show();
}


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


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


return payload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected void onCreate(Bundle savedInstanceState) {
ListView listView = (ListView) findViewById(R.id.presence_list);


this.gameID = "123456";
this.gameID = Constants.DEMO_CHANNEL;
this.gamePIN = Helpers.randomNumberString(Constants.RANDOM_MIN,Constants.RANDOM_MAX);

//client
Expand Down Expand Up @@ -109,6 +109,8 @@ public void message(PubNub pubnub, PNMessageResult message) {
//{ "action": "log_in", "value": "4852","publisher": "TestPlayer1","recipient": "THE BOSS"}
if (action.getRecipient() != null && action.getRecipient().equals(Constants.HOST_USERNAME)){
if(action.getAction().equals(Constants.A_LOG_IN)){
Log.d(Constants.LOGT, "Action value= "+ action.getValue() + " host pin="+gamePIN);

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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public PlayerGame(int status, int auth, UserProfile user) {
public PlayerGame(int status, int auth) {
this.status = status;
this.auth = auth;
this.user = new UserProfile("No Name");
this.user = new UserProfile();

}
public int getStatus() {
Expand Down
Loading

0 comments on commit be5e325

Please sign in to comment.