One Card! - Multiplayer Card Game
import QtQuick 2.0
import Felgo 3.0
import "common"
import "scenes"
import "interface"
Item {
id: mainItem
width: window.width
height: window.height
property alias menuScene: menuScene
property alias gameScene: gameScene
property alias instructionScene: instructionScene
property alias cardScene: cardScene
property alias matchmakingScene: matchmakingScene
property alias gameNetworkScene: gameNetworkScene
property alias storeScene: storeScene
MenuScene {
id: menuScene
onMenuButtonPressed: {
var timeUntilBonus = getTimeUntilBonus()
var timeUntilBonusStr = (timeUntilBonus.hours !== 0 ? timeUntilBonus.hours+" Hours" : timeUntilBonus.minutes !== 0 ? timeUntilBonus.minutes+ " Minutes" : timeUntilBonus.seconds+ " Seconds")
switch (button){
case "single":
checkTokens(function() {
multiplayer.createSinglePlayerGame()
window.state = "game"
}, "Wait "+timeUntilBonusStr, checkDailyBonus)
break
case "matchmaking":
checkTokens(function() {
multiplayer.showMatchmaking()
window.state = "multiplayer"
}, "Wait "+timeUntilBonusStr, checkDailyBonus)
break
case "quick":
checkTokens(function() {
multiplayer.joinOrCreateGame()
multiplayer.showMatchmaking()
window.state = "multiplayer"
}, "Wait "+timeUntilBonusStr, checkDailyBonus)
break
case "invites":
multiplayer.showInvitesList()
window.state = "multiplayer"
break
case "inbox":
multiplayer.showInbox()
window.state = "multiplayer"
break
case "friends":
multiplayer.showFriends()
window.state = "multiplayer"
break
case "leaderboard":
gameNetwork.showLeaderboard()
window.state = "gn"
break
case "profile":
gameNetwork.showProfileView()
window.state = "gn"
break
case "store":
storeScene.previousState = window.state
window.state = "store"
break
default:
window.state = button
}
}
property bool shownQuitDialog: false
Keys.onBackPressed: {
menuScene.shownQuitDialog = true
nativeUtils.displayMessageBox(qsTr("Really quit the game?"), "", 2)
}
Connections {
target: nativeUtils
onMessageBoxFinished: {
if (accepted && menuScene.shownQuitDialog) {
Qt.quit()
}
menuScene.shownQuitDialog = false
}
}
}
GameScene {
id: gameScene
onBackButtonPressed: {
if(!gameScene.leaveGame.visible && !noTokenDialog.visible)
gameScene.leaveGame.visible = true
else {
adMobInterstitial.displayInterstitial(true, false, "leaveGame")
}
}
}
InstructionScene {
id: instructionScene
onBackButtonPressed: window.state = "menu"
onMenuButtonPressed: {
switch (button){
case "cards":
window.state = "cards"
break
}
}
}
CardScene {
id: cardScene
onBackButtonPressed: window.state = "instructions"
onMenuButtonPressed: {
switch (button){
case "menu":
window.state = "menu"
break
}
}
}
MultiplayerScene {
id: matchmakingScene
onBackButtonPressed: window.state = "menu"
}
GameNetworkScene{
id: gameNetworkScene
onBackButtonPressed: window.state = "menu"
}
StoreScene {
id: storeScene
property string previousState: ""
onBackButtonPressed: window.state = previousState
}
AdMobBanner {
id: adMobBanner
adUnitId: Constants.adMobBannerUnitId
banner: window.tablet ? AdMobBanner.Large : AdMobBanner.Standard
anchors.top: parent.top
x: gameScene.rightPlayerTag.mapToItem(mainItem, 0, 0).x - adMobBanner.width
visible: window.showAdvertisements && gameScene.opacity === 1
onAdReceived: {
if(adMobBanner.visible) {
ga.logEvent("System", "Display AdMob Banner")
flurry.logEvent("AdMobBanner.Display")
}
}
onAdOpened: {
if(adMobBanner.visible) {
ga.logEvent("User", "AdMob Banner Clicked")
flurry.logEvent("AdMobBanner.Clicked")
}
}
testDeviceIds: Constants.adMobTestDeviceIds
}
MouseArea {
id: lockScreenArea
visible: false
enabled: Constants.lockScreenForInterstitial
anchors.fill: parent
}
AdMobInterstitial {
id: adMobInterstitial
adUnitId: Constants.adMobInterstitialUnitId
property real startTime: 0
property real elapsedTime: 0
property bool hasInterstitial: false
property bool displayWhenLoaded: false
onInterstitialReceived: {
hasInterstitial = true
if(displayWhenLoaded)
showInterstitialIfLoaded()
}
onInterstitialFailedToReceive: {
hasInterstitial = false
if(displayWhenLoaded) {
displayWhenLoaded = false
lockScreenArea.visible = false
noVideoDialog.visible = true
}
}
Component.onCompleted: {
loadInterstitial()
}
function displayInterstitial(openMenu, forceAd, fromWhereShown) {
if(!system.desktopPlatform && (window.showAdvertisements || forceAd)) {
if(openMenu) {
window.state = "menu"
}
lockScreenArea.visible = true
watchVideoDialog.visible = true
flurry.logEvent("AdMobInterstitial.Show", {"fromWhereShown": fromWhereShown})
}
else if(openMenu) {
window.state = "menu"
}
}
onInterstitialOpened: {
ga.logEvent("System", "Display AdMob Interstitial")
flurry.logEvent("AdMobInterstitial.Display")
flurry.logTimedEvent("Interstitial.Running")
startTime = new Date().getTime()
elapsedTime = 0
displayWhenLoaded = false
lockScreenArea.visible = false
}
onInterstitialClosed: {
if(elapsedTime == 0) {
elapsedTime = new Date().getTime() - startTime
startTime = 0
flurry.endTimedEvent("Interstitial.Running")
if(elapsedTime > 10000) {
storeScene.giveTokens(gameTokenEarnedPerVideoWatch)
earnedTokenDialog.visible = true
}
}
ga.logEvent("User", "AdMob Interstitial Closed", "watched (ms)", elapsedTime)
flurry.logEvent("AdMobInterstitial.Closed", "watched (ms)", elapsedTime)
adMobInterstitial.hasInterstitial = false
adMobInterstitial.loadInterstitial()
}
onInterstitialLeftApplication: {
if(elapsedTime == 0) {
elapsedTime = new Date().getTime() - startTime
startTime = 0
flurry.endTimedEvent("Interstitial.Running")
if(elapsedTime > 10000) {
storeScene.giveTokens(gameTokenEarnedPerVideoWatch)
earnedTokenDialog.visible = true
}
}
ga.logEvent("User", "AdMob Interstitial Clicked", "watched (ms)", elapsedTime)
flurry.logEvent("AdMobInterstitial.Clicked", "watched (ms)", elapsedTime)
}
testDeviceIds: Constants.adMobTestDeviceIds
}
OnuDialog {
id: watchVideoDialog
title: "Earn Game Tokens"
description: "Watch the following video to earn " + gameTokenEarnedPerVideoWatch + " game token!"
options: ["Ok"]
visible: false
onOptionSelected: {
if(adMobInterstitial.hasInterstitial)
adMobInterstitial.showInterstitialIfLoaded()
else {
adMobInterstitial.displayWhenLoaded = true
adMobInterstitial.loadInterstitial()
}
watchVideoDialog.visible = false
}
}
OnuDialog {
id: earnedTokenDialog
title: "Awesome!"
description: "You earned " + gameTokenEarnedPerVideoWatch + " game token."
options: ["Ok"]
visible: false
onOptionSelected: {
menuScene.tokenInfo.startAnimation()
visible = false
}
}
OnuDialog {
id: dailyTokenDialog
title: "Daily Bonus"
description: "Welcome Back! You earned " + gameTokensEarnedPerDay + " game token for playing today."
options: ["Ok"]
visible: false
onOptionSelected: {
menuScene.tokenInfo.startAnimation()
visible = false
}
}
OnuDialog {
id: noTokenDialog
title: "No Game Tokens"
description: "Playing a game requires one game token."
options: ["Watch Video (+" + gameTokenEarnedPerVideoWatch + " Token)", "Buy Tokens"]
visible: false
property var customHandler
onOptionSelected: {
if(index === 0)
adMobInterstitial.displayInterstitial(false, false, "beforeStartFromMainMenu")
else if(index === 1) {
storeScene.previousState = window.state
window.state = "store"
}
else if(index === 2 && customHandler !== undefined)
customHandler()
visible = false
}
}
OnuDialog {
id: noVideoDialog
title: "No Video"
description: "The video could not be loaded. Please try again later."
options: ["Ok"]
visible: false
onOptionSelected: visible = false
}
Loader {
property string updateCheckUrl: system.publishBuild ? "https://felgo.com/qml-sources/OnuVersionCheck.qml" : "https://felgo.com/qml-sources/OnuVersionCheck-test.qml"
visible: false
source: !system.desktopPlatform ? updateCheckUrl : ""
property var menuScene: mainItem.menuScene
property Component dialogComponent: Qt.createComponent(Qt.resolvedUrl("interface/OnuDialog.qml"))
onLoaded: item.parent = mainItem
}
function checkTokens(allowedHandler, customOption, optionHandler) {
if(storeScene.tokens > 0 || !enableStoreAndAds)
allowedHandler()
else {
if(customOption === undefined) {
noTokenDialog.options = noTokenDialog.options.slice(0, 2)
noTokenDialog.customHandler = undefined
}
else {
noTokenDialog.options[2] = customOption
noTokenDialog.customHandler = optionHandler
}
noTokenDialog.optionsChanged()
noTokenDialog.visible = true
}
}
function getTimeUntilBonus() {
var now = new Date().getTime()
var next = menuScene.localStorage.lastLogin + (24 * 60 * 60 * 1000)
var remaining = next - now
var seconds = Math.ceil((remaining / 1000) % 60)
var minutes = Math.floor((remaining / 1000 / 60) % 60)
var hours = Math.floor((remaining / 1000 / 60) / 60)
if(seconds === 60) {
minutes++
seconds = 0
}
if(minutes === 60) {
hours++
minutes = 0
}
if(hours < 0)
hours = 0
if(minutes < 0)
minutes = 0
if(seconds < 0)
seconds = 0