forked from TheGlobalATeam/moodle-block_chessblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplayerNotificatin.js
127 lines (85 loc) · 2.75 KB
/
multiplayerNotificatin.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var canUseNotifications = false;
var lastChallengeTime = -1;
var keepLookingForChallenge = true;
//CODE FILE FOR THE PLAYER RESIVING THE CHALLENGE
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support desktop notification");
}else if (Notification.permission === "granted") { //already granted
canUseNotifications = true;
}else if (Notification.permission !== 'denied') {
console.log("Notification ask");
// we need to ask the user for permission
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
console.log("Notification allowed");
canUseNotifications = true;
}
});
}
function lookForChallenges(){
Y.io(
M.cfg.wwwroot + "/blocks/chessblock/api/get_chess_challenges.php?lastChallengeTime="+lastChallengeTime, {
method: "GET",
on: {
success: function(io, o, arguments) {
var gameData = JSON.parse(o.response);
//console.log("responce");
//console.dir(gameData);
if (gameData.status){
lastChallengeTime = gameData.challenges[0].challenged_time;
//console.log("yess!" + lastChallengeTime);
addNewNotification(gameData.challenges[0].challenger_user_id);
enemyPlayerID = gameData.challenges[0].challenger_user_id;
$('#acceptMultiplayerChallenge').show();
}
}
}
}
);
if(keepLookingForChallenge){
setTimeout(lookForChallenges, challengeLookRefreshTime);
}
}
function addNewNotification(challengerUserID){
if (!canUseNotifications) {
return;
}
var options = {
body: "Challenged by userID: " + challengerUserID,
icon: M.cfg.wwwroot + "/blocks/chessblock/Chess-Game.png",
tag: "#LiveToLearn"
}
var title = "New chess Challenge!";
var notification = new Notification(title, options);
notification.onclick = function() {
notification.close();
console.log("Clicked!");
}
}
$( document ).ready(function() {
$('#acceptMultiplayerChallenge').click(function(){
keepLookingForChallenge = false;
Y.io(
M.cfg.wwwroot + "/blocks/chessblock/api/chess_challenge_status.php", {
method: "POST",
data: 'challengerUserID=' + enemyPlayerID + '&challengeResponce=1' ,
on: {
success: function(io, o, arguments) {
var responce = JSON.parse(o.response);
// console.dir(responce);
if(responce.status){
// console.log("accept status stored!");
//start MP
// console.log("start MP against "+enemyPlayerID);
//found in mainJS. handing controll over
startMultiplayerMatch(false);
}
}
}
}
);
});
});
lookForChallenges();