File size: 2,369 Bytes
bd98362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var Clock = require('famous/core/Clock');

function OnlineFeatures (storage){
  this.isConnected = false;
  this.storage = storage;
}

OnlineFeatures.prototype.login = function login() {
  try {
    Cocoon
  } catch (e) {
    if(!Cocoon) throw new Error("No Cocoon!")
  }
  if(Cocoon.getPlatform() == 'ios') {
    window.social = Cocoon.Social.GameCenter.init();
    window.social = Cocoon.Social.GameCenter.getSocialInterface();
    this.loggedIn = window.social.isLoggedIn();
    if (!window.social.isLoggedIn()) {
      window.social.login();
    }
  }
}

OnlineFeatures.prototype.leaderboard = function() {
  if (window.social){
    window.social.on("loginStatusChanged", function(loggedIn, error) {
      if (this.loggedIn) {
        window.social.requestScore(function(score, error) {
          if (error) {
            console.error("Error getting user score: " + error.message);
          }else if (score) {
            console.log("score: " + score.score);
            window.localUserScore = score.score;
            if(game.storage) window.localStorage.high_score = score.score
          }
        },{
          leaderboardID: "com.karaman.crossfire.highScore"
        });
      }
    });
  }
  this.login();
}

OnlineFeatures.prototype.ads = function() {
  if (typeof(Cocoon) != 'undefined' && Cocoon.getPlatform() == 'ios'){
    if(Cocoon.Ad.AdMob && game.interstitial){
      game.interstitial.show();
    }
  }
}
OnlineFeatures.prototype.submitScore = function() {
  if (typeof(Cocoon) != 'undefined' && Cocoon.getPlatform() == 'ios'
  && window.social && window.social.isLoggedIn()) {
    socialScore = window.localStorage.high_score;
    if((window.localUserScore && window.localUserScore < window.localStorage.high_score)
    || !window.localUserScore){
      window.social.submitScore(socialScore, function(error){
        if (error) console.error("submitScore error: " + error.message);
      },{
        leaderboardID: "com.karaman.crossfire.highScore"
      });
    }
  }
}





if(typeof(Cocoon) != 'undefined' && Cocoon.getPlatform() == 'ios'
&& Cocoon.Ad.AdMob){
    Cocoon.Ad.AdMob.configure({
        ios: {
            banner:"ca-app-pub-4693632452063283/2829774253",
            interstitial:"ca-app-pub-4693632452063283/4023645854"
        }
    });
    loadAndShowBannerAd();
    loadInterstitialAd();
}

module.exports = OnlineFeatures;