Skip to content

Commit

Permalink
Abstracted CryptoKitties firebase methods into its own service ready …
Browse files Browse the repository at this point in the history
…for extraction
  • Loading branch information
vince0656 committed Nov 27, 2019
1 parent b1b30e7 commit d6d3980
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
42 changes: 42 additions & 0 deletions src/Firebase/CryptoKitties/KittiesService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// todo: change this path when imported into the CK tinder app
import {db} from '../index';

export default new class KittiesService {
async getAllKittiesForAddress(network, address) {
return await db.collection('kitties')
.doc('network')
.collection(network)
.where('owner', '==', address)
.get()
.then(snapshots => {
if (snapshots.empty) {
return [];
}
return snapshots.docs.map(doc => doc.id);
});
}

async poke(pokeId, kittieId, message, from, studId) {
return db.collection('kitties')
.doc('network')
.collection('mainnet')
.doc(kittieId)
.collection('poke')
.doc(pokeId)
.set({msg: message, from, stud: studId}, {
merge: true
});
}

async swipeRight(kittieId, studId, message, from) {
return db.collection('kitties')
.doc('network')
.collection('mainnet')
.doc(kittieId)
.collection('swipeRight')
.doc(studId)
.set({msg: message, from, stud: studId}, {
merge: true
});
}
}
38 changes: 10 additions & 28 deletions src/components/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<script>
import {ethers} from "ethers";
import Accounts from '@/Firebase/Accounts';
import {db, messaging, firebase} from '@/Firebase/index';
import {db, messaging} from '@/Firebase/index';
import KittiesService from '@/Firebase/CryptoKitties/KittiesService';
import env from '@/_config/env';
messaging.usePublicVapidKey(env.messaging.publicVapidKey);
Expand Down Expand Up @@ -71,42 +72,23 @@
poke: function (kittieId) {
const uuid = this.$uuid;
const pokeId = uuid.v4();
db.collection('kitties').doc('network').collection('mainnet').doc(kittieId)
.collection('poke')
.doc(pokeId)
.set({msg: 'Hello treakle, want to tinder?', from: this.accounts.user, stud: this.kitties.user[0]}, {
merge: true
});
const msg = 'Hello treakle, want to tinder?';
KittiesService.poke(pokeId, kittieId, msg, this.accounts.user, this.kitties.user[0]);
console.log(`poke [[${pokeId}]] to kittie ${kittieId}`);
},
swipeRight: function (kittieId) {
const stud = this.kitties.user[0];
db.collection('kitties').doc('network').collection('mainnet').doc(kittieId)
.collection('swipeRight')
.doc(stud)
.set({msg: `Hello treakle, want to breed with ${stud}?`, from: this.accounts.user, stud}, {
merge: true
});
const msg = `Hello treakle, want to breed with ${stud}?`;
KittiesService.swipeRight(kittieId, stud, msg, this.accounts.user);
console.log(`swipe right from ${stud} to ${kittieId}`);
},
find: async function() {
this.kitties.other = await this.getKittiesForAddressFromDB('mainnet', this.accounts.other);
},
getKittiesForAddressFromDB: async function(network, address) {
return await db.collection('kitties')
.doc('network')
.collection(network)
.where('owner', '==', address)
.get()
.then(snapshots => {
if (snapshots.empty) {
return [];
}
return snapshots.docs.map(doc => doc.id);
});
this.kitties.other = await KittiesService.getAllKittiesForAddress('mainnet', this.accounts.other);
},
getUserKitties: async function() {
this.kitties.user = await this.getKittiesForAddressFromDB('mainnet', this.accounts.user);
this.kitties.user = await KittiesService.getAllKittiesForAddress('mainnet', this.accounts.user);
}
},
async mounted() {
Expand Down

0 comments on commit d6d3980

Please sign in to comment.