-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sarunas-k
committed
Apr 30, 2017
1 parent
9e9709a
commit c6b3396
Showing
2 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
var Game; | ||
(function (Game) { | ||
var FacebookHandler = (function () { | ||
function FacebookHandler() { | ||
var _this = this; | ||
this.appId = '127428917793107'; | ||
$.ajaxSetup({ | ||
cache: true | ||
}); | ||
$.getScript("//connect.facebook.net/lt_LT/sdk.js", function () { return _this.initFB(); }); | ||
} | ||
FacebookHandler.prototype.initFB = function () { | ||
FB.init({ | ||
appId: this.appId, | ||
version: 'v2.9', | ||
status: true, | ||
cookie: true, | ||
xfbml: true | ||
}); | ||
this.checkLoginStatus(); | ||
}; | ||
FacebookHandler.prototype.login = function () { | ||
var _this = this; | ||
FB.login(function (response) { | ||
_this.loginStatus = response.status; | ||
if (response.authResponse) | ||
eventManager.publish(Game.EventNames.FacebookUserLoggedIn); | ||
else | ||
console.log('User cancelled login or did not fully authorize.'); | ||
}, { scope: 'public_profile,email,publish_actions' }); | ||
}; | ||
FacebookHandler.prototype.checkLoginStatus = function () { | ||
var _this = this; | ||
FB.getLoginStatus(function (response) { return _this.loginStatus = response.status; }); | ||
}; | ||
FacebookHandler.prototype.openShareDialog = function (href) { | ||
FB.ui({ | ||
method: 'share', | ||
href: href | ||
}, function (response) { }); | ||
}; | ||
FacebookHandler.prototype.loadUserData = function () { | ||
var _this = this; | ||
if (this.loginStatus !== 'connected') { | ||
console.log('Can\'t load user data, because user is not logged in.'); | ||
return; | ||
} | ||
FB.api('/me?fields=first_name,scores,picture', function (response) { | ||
if (!response || response.error) { | ||
console.log('Error getting user data'); | ||
return; | ||
} | ||
_this.username = response.first_name; | ||
_this.imageUrl = response.picture.data.url; | ||
_this.score = response.scores.data[0]; | ||
eventManager.publish(Game.EventNames.FacebookUserDataReady); | ||
}); | ||
}; | ||
FacebookHandler.prototype.getUserData = function () { | ||
return { | ||
name: this.username, | ||
imageUrl: this.imageUrl, | ||
score: this.score | ||
}; | ||
}; | ||
FacebookHandler.prototype.updateScore = function (score) { | ||
FB.api('/me/scores', 'post', { score: score }, function (response) { }); | ||
}; | ||
return FacebookHandler; | ||
}()); | ||
Game.FacebookHandler = FacebookHandler; | ||
})(Game || (Game = {})); | ||
var facebookHandler = new Game.FacebookHandler(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/// <reference path="../node_modules/@types/facebook-js-sdk/index.d.ts" /> | ||
|
||
module Game { | ||
|
||
export class FacebookHandler { | ||
|
||
public loginStatus: string; | ||
private appId: string = '127428917793107'; | ||
private username: string; | ||
private imageUrl: string; | ||
private score: IFacebookGameScore; | ||
|
||
constructor() { | ||
$.ajaxSetup({ | ||
cache: true | ||
}); | ||
$.getScript("//connect.facebook.net/lt_LT/sdk.js", () => this.initFB()); | ||
} | ||
|
||
private initFB(): void { | ||
FB.init({ | ||
appId: this.appId, | ||
version: 'v2.9', | ||
status: true, | ||
cookie: true, | ||
xfbml: true | ||
}); | ||
this.checkLoginStatus(); | ||
} | ||
|
||
public login(): void { | ||
FB.login( | ||
(response) => { | ||
this.loginStatus = response.status; | ||
if (response.authResponse) | ||
eventManager.publish(EventNames.FacebookUserLoggedIn); | ||
else | ||
console.log('User cancelled login or did not fully authorize.'); | ||
}, | ||
{scope: 'public_profile,email,publish_actions'} | ||
); | ||
} | ||
|
||
public checkLoginStatus(): void { | ||
FB.getLoginStatus(response => this.loginStatus = response.status); | ||
} | ||
|
||
public openShareDialog(href: string): void { | ||
FB.ui({ | ||
method: 'share', | ||
href: href | ||
}, (response: any) => {}); | ||
} | ||
|
||
public loadUserData(): void { | ||
if (this.loginStatus !== 'connected') { | ||
console.log('Can\'t load user data, because user is not logged in.'); | ||
return; | ||
} | ||
FB.api('/me?fields=first_name,scores,picture', (response: any) => { | ||
if (!response || response.error) { | ||
console.log('Error getting user data'); | ||
return; | ||
} | ||
|
||
this.username = response.first_name; | ||
this.imageUrl = response.picture.data.url; | ||
this.score = response.scores.data[0]; | ||
|
||
eventManager.publish(EventNames.FacebookUserDataReady); | ||
}); | ||
} | ||
|
||
public getUserData(): IFacebookUser { | ||
return { | ||
name: this.username, | ||
imageUrl: this.imageUrl, | ||
score: this.score | ||
}; | ||
} | ||
|
||
public updateScore(score: number): void { | ||
FB.api( | ||
'/me/scores', | ||
'post', | ||
{ score: score }, | ||
(response: any) => {} | ||
); | ||
} | ||
|
||
// public sendTest(): void { | ||
// FB.api('/me/feed', (response: any) => { | ||
// console.log('Response for /me/feed', response); | ||
// }); | ||
// } | ||
|
||
} | ||
|
||
export interface IFacebookUser { | ||
name: string; | ||
imageUrl: string; | ||
score: IFacebookGameScore; | ||
} | ||
|
||
export interface IFacebookGameScore { | ||
score: number; | ||
} | ||
|
||
} | ||
|
||
var facebookHandler: Game.FacebookHandler = new Game.FacebookHandler(); |