Last active
June 29, 2018 21:46
-
-
Save xdegtyarev/b0c010e062620b2bed7c64ef1a9cf400 to your computer and use it in GitHub Desktop.
firebase gemjoy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
admin.initializeApp(functions.config().firebase); | |
const tournamentOffsetInDays = 172800000; | |
exports.updateTournamentDate = functions.https.onRequest((request, response) => { | |
var tournamentRef = admin.database().ref("currentTournament"); | |
var prevtournamentRef = admin.database().ref("prevTournament"); | |
var fakeusersRef = admin.database().ref("fake_users"); | |
tournamentRef.once("value").then(function(snapshot) { | |
const startDate = new Date().getTime(); | |
var currentEndDate = 0; | |
var tournamentId = 0; | |
var currentTournamentEndDate = snapshot.child("endDate").val(); | |
if(currentTournamentEndDate != null && currentTournamentEndDate != undefined){ | |
currentEndDate = currentTournamentEndDate; | |
} | |
var currentTournamentId = snapshot.child("tournamentId").val(); | |
if(currentTournamentId!=null && currentTournamentId != undefined){ | |
tournamentId = currentTournamentId; | |
} | |
if(currentEndDate<startDate){ | |
////NOW WE WILL CHANGE TOURNAMENT DATE | |
//TODO: Set 23:00 of some day | |
fakeusersRef.once("value") | |
.then(function(snapshot){ | |
prevtournamentRef.remove() | |
.then(function() { | |
prevtournamentRef.set(snapshot.toJSON()); | |
}) | |
.catch(function(error) { | |
response.redirect(400, "Error"); | |
}); | |
snapshot.forEach(function(data) {fakeusersRef.child(data.key).set(0);}); | |
}); | |
const endDate = startDate + tournamentOffsetInDays; | |
tournamentId += 1; | |
tournamentRef.set({startDate:startDate,endDate:endDate,tournamentId: tournamentId}).then(snapshot =>{ | |
response.redirect(200, "success"); | |
}); | |
//TODO:responseet scores of all fake users | |
}else{ | |
response.redirect(400, "activeTournament"); | |
} | |
}); | |
}); | |
exports.testCopyData = functions.https.onRequest((request, response) => { | |
var prevtournamentRef = admin.database().ref("prevTournament"); | |
var fakeusersRef = admin.database().ref("fake_users"); | |
fakeusersRef.once("value") | |
.then(function(snapshot){ | |
prevtournamentRef.remove() | |
.then(function() { | |
prevtournamentRef.set(snapshot.toJSON()); | |
}) | |
.catch(function(error) { | |
response.redirect(400, "Remove Failed" + error.message); | |
}); | |
snapshot.forEach(function(data) {fakeusersRef.child(data.key).set(0);}); | |
}); | |
response.redirect(200, "success"); | |
}); | |
const maxFakeUserScore = 60000; | |
exports.updateRandomFakeUserScore = functions.https.onRequest((request, response) => { | |
var fakeusersRef = admin.database().ref("fake_users"); | |
fakeusersRef.once("value").then(function(snapshot){ | |
snapshot.forEach(function(data) { | |
var newRandScore = Math.floor(Math.random() * maxFakeUserScore); | |
if(newRandScore>data.val()){ | |
fakeusersRef.child(data.key).set(newRandScore); | |
} | |
}); | |
response.redirect(200, "success"); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment