liveops para games usando o firebase
TRANSCRIPT
Remote Config
Cloud Storage
Realtime Database
Cloud Messaging
Cloud Functions
Android Dev Conference 2017
fun hasLatestVersion(): Boolean {return FirebaseRemoteConfig.getInstance()
.getLong("latest_version")
.toInt() == BuildConfig.VERSION_CODE}
fun onCreate() {if (hasLatestVersion()) {
launchGameActivity()} else {
launchForceUpdateDialog()}
}
fun onGameStart() {val latestNewsId = FirebaseRemoteConfig.getInstance()
.getLong("latest_news_id")if (latestNewsId > loadLastNewsFromDb().id) {
loadNews(latestNewsId, { news -> displayNews(news) })}
}
fun loadNews(newsId: Long, callback: (newsTitle: News) -> Unit) {FirebaseDatabase.getInstance().getReference("news")
.child(newsId.toString())
.addListenerForSingleValueEvent {data -> data.getValue(News::class.java)?.let(callback)
}}
const functions = require('firebase-functions');const moment = require('moment');const cors = require('cors')({origin: true});
exports.date = functions.https.onRequest((req, res) => {cors(req, res, () => {res.status(200).send(moment().format());
});});
https://us-central1-meujogo.cloudfunctions.net/date
fun onGameStart() {getServerTime({ serverTime -> setGameTime(serverTime) })
}
fun getServerTime(callback: (serverTime: Instant) -> Unit) {"https://us-central1-meujogo.cloudfunctions.net/date".httpGet()
.responseString { request, response, result ->when (result) {
is Result.Success -> {callback(Instant.parse(result.getAs()))
}}
}}
https://github.com/kittinunf/Fuel
fun onGameResume() {FirebaseDatabase.getInstance().getReference("events")
.orderByChild("fim")
.limitToLast(5)
.addListenerForSingleValueEvent { data ->val now = Instant.now()data?.children?.find {
val event = it.getValue(EventConfig::class.java)event.fim.isAfter(now) && event.inicio.isBefore(now)
}?.let {startNewEvent(it.getValue(EventConfig::class.java))
}}
}
fun onVictory(gold: Long, xp: Long) {val event = gameState.currentEventplayer.earnGold(if (event != null) (gold * event.multGold).toLong() else gold)player.earnXp(if (event != null) (xp * event.multXp).toLong() else xp)
}
class Item(val sku: String, val image: String)class Shop {
val items = ArrayList<Item>()}
private fun onShopOpened(shop: Shop) {FirebaseDatabase.getInstance().getReference("shop")
.addListenerForSingleValueEvent { data ->data?.children?.forEach { itemConfig ->
val itemDbKey = FirebaseRemoteConfig.getInstance().getString(itemConfig.key)
addItemToShop(itemDbKey, shop)}
}}
private fun addItemToShop(dbKey: String, shop: Shop) {FirebaseDatabase.getInstance().getReference("items")
.addListenerForSingleValueEvent { data ->data?.child(dbKey)?.getValue(Item::class.java)?.let {
shop.items.add(it)}
}}
Android Dev Conference 2017
Obrigado pela presença!
Contato:
Imagens e ferramentas:
● https://unsplash.com/● https://www.pexels.com/● http://typicalhope.tumblr.com/● http://www.thokamaer.com/● http://jonasmosesson.se/● https://romannurik.github.io/SlidesCodeHighlighter/