-
Notifications
You must be signed in to change notification settings - Fork 0
/
myarcadebridge.js
84 lines (67 loc) · 1.89 KB
/
myarcadebridge.js
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
/*!
* MyArcade Scores and Achievement Bridge
*
* @version 1.0.0
* @author Daniel Bakovic
* @url https://myarcadeplugin.com
*/
var MyArcadeConfig;
/**
* Get MyArcadeBridgeData. Checks if MyArcadeBridge data are available.
* Those data are embedded by MyArcadePluin on the parent page.
*
* @return void
*/
function myarcade_get_config() {
if ( typeof parent.MyArcadeScoreBridgeConfig === "function" ) {
MyArcadeConfig = parent.MyArcadeScoreBridgeConfig();
return true;
}
return false;
}
/**
* Submit new score
*
* @param int score
*/
function myarcade_submit_score( score ) {
if ( myarcade_get_config() ) {
var params = 'game_id=' + MyArcadeConfig.game_id
+ '&user_id=' + MyArcadeConfig.user_id
+ '&event=score'
+ '&score=' + score
+ '&session=' + MyArcadeConfig.session;
myarcade_ajax_call( params );
}
}
/**
* Submit a new achievement
*
* @access public
* @param object achievement Achievement contains a title, description, score and an icon URL
*/
function myarcade_submit_achievement( achievement ) {
if ( myarcade_get_config() ) {
var params = 'game_id=' + MyArcadeConfig.game_id
+ '&user_id=' + MyArcadeConfig.user_id
+ '&event=achievement'
+ '&score=' + achievement.score
+ '&title=' + achievement.title
+ '&description=' + achievement.description
+ '&icon=' + achievement.icon
+ '&session=' + MyArcadeConfig.session;
myarcade_ajax_call( params );
}
}
/**
* Do the XMLHttpRequest request and send data to server
*
* @param string post Data to be sent
*/
function myarcade_ajax_call( post ) {
var xhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
var url = MyArcadeConfig.site_url + "?action=MyArcade&do=ScoreBridge"
xhttp.open( "POST", url, true );
xhttp.setRequestHeader( "Content-type","application/x-www-form-urlencoded" );
xhttp.send( post );
}