-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection-event-handlers.js
48 lines (40 loc) · 1.54 KB
/
connection-event-handlers.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
app.factory('ConnectionEventHandlers', function (GameEvents) {
return function ($scope) {
return {
onRegisterIdWithPeerServer: function (id) {
$scope.myId = id;
$scope.status = 'You are registered as ' + id;
$scope.$apply();
},
onPeerConnectToYou: function (conn) {
$scope.connected = true;
conn.send({
'type': 'turnCheck',
'value': $scope.myGo
});
$scope.status = 'Opponent connected to you!';
$scope.$apply();
},
onDataReceivedFromPeer: function (data) {
$scope.status = 'Data receieved from opponent';
switch (data.type) {
case 'turnCheck':
// Check whose turn it is against the handshake data
// hacky as shit but it fucking well works :)
$scope.myGo = !data.value;
break;
case 'card':
$scope.$emit(GameEvents.CARD_RECEIVED_FROM_OPPONENT, data.value);
break;
}
$scope.$apply();
},
onConnectionToPeerSuccess: function (conn) {
$scope.status = 'Connected to ' + conn.peer;
$scope.connected = true;
$scope.$emit(GameEvents.READY_TO_PLAY);
$scope.$apply();
}
};
};
});