forked from binary-riviera/GroupSoftwareFrontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudentCam.js
127 lines (107 loc) · 3.46 KB
/
studentCam.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
/**
* A JavaScript file to update the game with the clue found using the QR reader/camera on a phone
*
*
*
* @author Connor_Forsyth, Mbongeni Gulu.
* @since 20/2/2020
*/
var current = parseInt(localStorage.getItem("current"));
console.log(current);
document.getElementById('waypointNumber').innerText = current;
// pick random route out of all Routes
//find out how many Routes
var count = 0;
var routeIDs = [];
db.collection('Routes').get().then((snapshot) => {
snapshot.docs.forEach(doc => {
count += 1;
routeIDs.push(doc.id);
});
var ran = Math.floor(Math.random() * count);
currentRoute = routeIDs[ran];
var col = db.collection('Routes').doc(currentRoute).get();
localStorage.setItem("currentRoute", currentRoute);
//get id of first location
col.then(function(doc) {
var loc = doc.data().Locations;
var id = loc[0].id.replace(/\s/g, '');
//set first clue
var locClues = db.collection('Locations').doc(id).get();
locClues.then(function(doc) {
var setClue = doc.data().Clue;
document.getElementById('clue').innerText = setClue;
});
});
});
/**
* Update the database with the clue found using the QR code and generates the next waypoint (clue) to be found.
*
* @param {String} result Identifier for the current waypoint found from the QR scanner.
* @return Returns an alert, updates the database/log with the found waypoint.
*/
function updateClue(result) {
var ran = Math.floor(Math.random() * 11);
var currentRoute = localStorage.getItem('currentRoute');
var col = db.collection('Routes').doc(currentRoute).get();
console.log(col);
//get current id
col.then(function(doc) {
if (doc.exists) {
var loc = doc.data().Locations;
var id = loc[current].id.replace(/\s/g, '');
if (result == id) {
const clue = document.getElementById('clue');
current = current + 1;
localStorage.setItem("current",current);
var name = localStorage.getItem("studentName");
var len = parseInt(localStorage.getItem("lengthFeed"));
if(current==5){
alert("You have won the game")
firebase.database().ref().child('feed').update({
[len]:"Player " + name + " : has found all waypoints 👍"
});
}else{
firebase.database().ref().child('feed').update({
[len]:"Player " + name + " : found clue " +current +" 👍"
});
}
console.log('real time database updated with clue');
db.collection('Locations').get().then((snapshot) => {
snapshot.docs.forEach(doc => {
var id = loc[current].id.replace(/\s/g, '');
if (doc.id == id)
clue.innerHTML = doc.data().Clue;
});
});
document.getElementById('waypointNumber').innerText = current;
alert("well done you found the location");
}
}
});
name = localStorage.getItem("studentName");
var userName = name.replace('@exeter.ac.uk', '');
firebase.database().ref('players/' + userName).update({
clues: current + 1,
});
//document.location.href = "studentGame.html";
}
/**
* QR code reader that uses the user's phone camera.
*
*
* @return Returns the text from the scanner.
*/
function openCamera() {
let scanner = null;
Dynamsoft.BarcodeScanner.createInstance({
// GETS THE RESULT
onFrameRead: results => {},
onUnduplicatedRead: (txt, result) => {
updateClue(txt);
}
}).then(s => {
scanner = s;
scanner.show();
});
}