Skip to content

Commit

Permalink
functions
Browse files Browse the repository at this point in the history
  • Loading branch information
naman14 committed Oct 13, 2018
1 parent 57cd1c3 commit 6b15ed9
Show file tree
Hide file tree
Showing 13 changed files with 5,015 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
@@ -0,0 +1,5 @@
{
"projects": {
"default": "fir-example-64f02"
}
}
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,4 +8,5 @@
.DS_Store
/build
/captures
/functions/node_modules
.externalNativeBuild
3 changes: 3 additions & 0 deletions app/src/main/java/com/tosc/pkbg/ar/Game.java
Expand Up @@ -6,6 +6,9 @@ public class Game {

public String id;

public GamePlayer player1;
public GamePlayer player2;

public Game() {

}
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/tosc/pkbg/ar/GameHit.java
@@ -0,0 +1,17 @@
package com.tosc.pkbg.ar;

public class GameHit {

public String hitBy;
public int hitType;


public GameHit() {

}

public GameHit(String hitBy, int hitType) {
this.hitBy = hitBy;
this.hitType = hitType;
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/tosc/pkbg/ar/GamePlayer.java
@@ -0,0 +1,11 @@
package com.tosc.pkbg.ar;

public class GamePlayer {

public String playerId;
public int health;

public GamePlayer() {

}
}
39 changes: 39 additions & 0 deletions app/src/main/java/com/tosc/pkbg/ar/MainActivity.java
Expand Up @@ -9,6 +9,7 @@
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.ar.core.Anchor;
import com.google.ar.core.HitResult;
Expand Down Expand Up @@ -53,16 +54,21 @@ private enum AppAnchorState {

private DatabaseReference gameRef = FirebaseDatabase.getInstance().getReference("games");
private DatabaseReference gameWorldObjectsRef;
private DatabaseReference gamePlayersRef;
private DatabaseReference gameHitsRef;

private Game game;
private MLKit mlKit;
private String gameId;

private TextView tvHealth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvHealth = findViewById(R.id.tv_health);

game = new Game();
mlKit = new MLKit(this);
Expand Down Expand Up @@ -104,6 +110,7 @@ public void onClick(View view) {
@Override
public void onClick(View v) {
fragment.captureBitmap(null, true);
onHitAttempted();
}
});

Expand Down Expand Up @@ -243,6 +250,26 @@ private void setupNewGame(int shortCode) {
gameId = String.valueOf(shortCode);
game.id = gameId;
gameWorldObjectsRef = gameRef.child(gameId).child("objects");
gamePlayersRef = gameRef.child(gameId).child("players");
gameHitsRef = gameRef.child(gameId).child("hits");

GamePlayer player = new GamePlayer();
player.playerId = getDeviceId();
player.health = 100;

gamePlayersRef.child(getDeviceId()).setValue(player);

gamePlayersRef.child(getDeviceId()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
updateGameState(dataSnapshot.getValue(GamePlayer.class));
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

private void syncNewObject(AnchorNode anchorNode) {
Expand Down Expand Up @@ -328,4 +355,16 @@ private String getDeviceId() {
return Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID).substring(0, 5);
}

private void onHitAttempted() {
boolean isHit = true;
if (isHit) {
GameHit hit = new GameHit(getDeviceId(), 0);
gameHitsRef.push().setValue(hit);
}
}

private void updateGameState(GamePlayer player) {
tvHealth.setText(String.valueOf(player.health));
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/tosc/pkbg/ar/Utils.java
Expand Up @@ -7,6 +7,7 @@
import android.os.Environment;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.provider.Settings;
import android.util.Log;

import java.io.File;
Expand Down Expand Up @@ -48,4 +49,5 @@ static void playHitSound(Context context) {
mPlayer = MediaPlayer.create(context, R.raw.hit_sound);
mPlayer.start();
}

}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -43,5 +43,13 @@

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="15dp"
android:id="@+id/tv_health"
android:textColor="@android:color/white"/>


</FrameLayout>
7 changes: 7 additions & 0 deletions firebase.json
@@ -0,0 +1,7 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
}
123 changes: 123 additions & 0 deletions functions/.eslintrc.json
@@ -0,0 +1,123 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 6
},
"plugins": [
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",

// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",

// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",

// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",

// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",

// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",

// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,

// Warn if return statements do not either always or never specify values
"consistent-return": 1,

// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,

// Require the use of === and !==
"eqeqeq": 2,

// Disallow the use of alert, confirm, and prompt
"no-alert": 2,

// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,

// Disallow null comparisons without type-checking operators
"no-eq-null": 2,

// Disallow the use of eval()
"no-eval": 2,

// Warn against extending native types
"no-extend-native": 1,

// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,

// Warn against unnecessary labels
"no-extra-label": 1,

// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,

// Warn against shorthand type conversions
"no-implicit-coercion": 1,

// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,

// Disallow new operators with the Function object
"no-new-func": 2,

// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,

// Disallow throwing literals as exceptions
"no-throw-literal": 2,

// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,

// Enforce “for” loop update clause moving the counter in the right direction
"for-direction": 2,

// Enforce return statements in getters
"getter-return": 2,

// Disallow await inside of loops
"no-await-in-loop": 2,

// Disallow comparing against -0
"no-compare-neg-zero": 2,

// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,

// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,

// Enforce return statements in callbacks of array methods
"callback-return": 2,

// Require error handling in callbacks
"handle-callback-err": 2,

// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,

// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,

// Return inside each then() to create readable and reusable Promise chains.
// Forces developers to return console logs and http calls in promises.
"promise/always-return": 2,

//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,

// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}
40 changes: 40 additions & 0 deletions functions/index.js
@@ -0,0 +1,40 @@
const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();


exports.checkAndUpdateHit = functions.database.ref('/games/{gameId}/hits/{hitsId}')
.onCreate((snapshot, context) => {
const hit = snapshot.val();

const ref = admin.database().ref('/games').child(context.params.gameId).child('players')

return ref.once('value', function(snapshot) {

let health;
var hitTo;
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();

if (childKey !== hit.hitBy) {
hitTo = childKey

if (hit.type === 0) {
health = childData.health - 100;
} else if (hit.type === 1) {
health = childData.health - 50;
}

}

});

snapshot.ref.parent.child('players').child(hitTo).child('health').set(50)

});



});

0 comments on commit 6b15ed9

Please sign in to comment.