-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarterFunctions.js
254 lines (231 loc) · 7.57 KB
/
carterFunctions.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
document.addEventListener('DOMContentLoaded',testML);
document.addEventListener('DOMContentLoaded',getNameData);
class Game {
constructor(name) {
//var req = new XMLHttpRequest();
this.name = name;
this.ID = 0;
this.price = 0;
this.json = "";
this.ready = false;
var instance = this;
var phpReq = new XMLHttpRequest();
phpReq.open("GET","nameTranslation.php?game="+this.name,true);
//this.phpThing = phpReq
phpReq.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText)
instance.ID = this.responseText
var newURL = "http://www.programer.club/nodejs/getnews/?"+instance.ID;
var req = new XMLHttpRequest();
req.open("GET",newURL,true);
req.addEventListener('load',function() {
instance.json = JSON.parse(req.responseText);
instance.price = getPriceJSON(instance.json,instance.ID);
instance.pic = getPic(instance.json,instance.ID);
instance.ready = true;
displayGame(instance.json,instance.ID,instance.name);
});
req.send(null);
}
};
phpReq.send();
//this.phpThing = phpReq;
}
}
function testML() {
//$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
//console.log($SCRIPT_ROOT)
document.getElementById("testML").addEventListener('click',function(event) {
var sBar = (document.getElementById("getNameInput").value).split(" ");
//console.log($SCRIPT_ROOT)
var w1 = ""
var w2 = ""
if (sBar.length != 0) {
if (sBar.length == 1) {
w1 = sBar[0]
} else {
w1 = sBar[sBar.length-2]
w2 = sBar[sBar.length-1]
}
var URL = "http://www.programer.club/macPhp.php?word1="+w1+"&word2="+w2;
var req = new XMLHttpRequest();
//req.open("GET",URL,true);
//console.log("On ready state change");
req.onreadystatechange = function() {
if (this.readyState == 4) {
var rep = this.responseText.trim()
var repA = ["None"]
if (rep != "None") {
rep = rep.substring(1,rep.length-1)
repA = rep.split(", ")
for (var i = 0; i<repA.length;i++) {
repA[i] = repA[i].substring(1,repA[i].length-1)
}
console.log(repA);
}
displaySuggestions(repA,"suggestions");
}
//console.log(this)
//console.log(this.responseText)
}
//console.log("Opening")
req.open("GET",URL,true);
//console.log("Sending")
req.send(null);
}
event.preventDefault();
});
}
function checkLogin() {
console.log("Checking login");
document.getElementById('login').addEventListener('click',function(event) {
var user = document.getElementById("username").value;
var pass = document.getElementById("password").value;
var phpURL = "login.php?user="+user+"&pass="+pass;
var req = new XMLHttpRequest();
req.open("GET",phpURL,true);
req.onreadystatechange = function() {
console.log(this.responseText);
if (this.responseText == "1") {
goodInfo()
} else {
badInfo()
}
}
});
}
function goodInfo() {
//Make an document element say good info
//send back to index.html
console.log("Good info")
}
function badInfo() {
//Make a document element say bad info
console.log("Bad info")
document.getElementById("bad").value = "Username or password was incorrect."
}
function getNameData() {
console.log("Starting getNameData new");
document.getElementById('getName').addEventListener('click',function(event){
var homeURL = "nameTranslation.php?game=";
var userInput = document.getElementById('getNameInput').value;
var queryString = "?para1="+userInput;
//window.location.href = "searchpage.html"+queryString;
var newURL = homeURL + userInput;
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText != "0") {
window.location.href = "searchpage.html"+queryString;
var req2 = new XMLHttpRequest();
var gameID = this.responseText;
var url2 = "http://www.programer.club/nodejs/getnews/?"+gameID;
req2.open("GET",url2,true);
req2.addEventListener('load',function() {
if (req2.status>=200 && req2.status < 400) {
var response = JSON.parse(req2.responseText);
displayGame(response,gameID,userInput);
}
});
req2.send(null);
} else {
console.log("Invalid game");
document.getElementById("Invalid game").innerHTML = "Invalid game name. Try using the suggest feature to avoid misspellings."
}
}
};
req.open("GET",newURL,true)
req.send();
event.preventDefault();
console.log("Finished");
});
}
/*
function getData() {
console.log("Starting getNews");
document.getElementById('getPrice').addEventListener('click', function(event) {
var homeURL = "http://www.programer.club/nodejs/getnews/?"
var userInput = document.getElementById('getPriceInput').value;
var newURL = homeURL+userInput;
console.log("Making request");
var req = new XMLHttpRequest();
req.open("GET", newURL, true);
req.addEventListener('load', function(){
if(req.status>= 200 && req.status<400){
var response = JSON.parse(req.responseText);
console.log(response);
//console.log(response.applist.apps.app);
//console.log(response.applist.apps.app[1]);
//console.log(response.applist.apps.app["test2"]);
console.log("Getting Price: ");
console.log(getPriceJSON(response,userInput));
setText("outputPrice",getPriceJSON(response,userInput));
}
else {
console.log("Error in network request: ");
}
});
req.send(null);
event.preventDefault();
console.log("Finished");
});
}
*/
function displayGame(data,id,name) {
var header = document.createElement("h1");
//var gameName = document.createTextNode(name)
var gameName = name.toLowerCase();
var gameNameL = gameName.split(" ");
for (var i = 0; i< gameNameL.length; i++) {
gameNameL[i] = gameNameL[i].substring(0,1).toUpperCase()+gameNameL[i].substring(1,gameNameL[i].length);
}
gameName = gameNameL.join(" ");
var gameNameObj = document.createTextNode(gameName)
header.appendChild(gameNameObj);
var img = document.createElement("img");
img.src = getPic(data,id);
var price = document.createElement("h3");
var pData = document.createTextNode("Price: "+getPriceJSON(data,id)+getSale(data,id));
price.appendChild(pData);
var gameDescDiv = document.createElement('div');
gameDescDiv.innerHTML = (data[id].data.detailed_description)
document.getElementById('output').appendChild(header);
document.getElementById('output').appendChild(img);
document.getElementById('output').appendChild(gameDescDiv);
document.getElementById('output').appendChild(price);
document.getElementById("id").innerHTML = id;
document.getElementById("game").innerHTML = gameName;
}
function displaySuggestions(arr, dID) {
var suggest = arr.join(", ");
document.getElementById("Invalid game").innerHTML = ""
document.getElementById(dID).innerHTML = suggest;
}
function getPriceJSON(obj, id) {
if (obj[id].data.price_overview != undefined) {
return(obj[id].data.price_overview.final/100);
} else {
return "Free!"
}
}
function setText(elem,text) {
document.getElementById(elem).innerHTML = text;
}
function getPic(obj,id) {
return(obj[id].data.header_image);
}
function checkSale(obj,id) {
if (obj[id].data.price_overview != undefined) {
return (obj[id].data.price_overview.discount_percent != 0)
} else {
return false
}
}
function getSale(obj,id) {
var sale = ""
if (checkSale(obj,id)) {
sale = " down from: " + obj[id].data.price_overview.initial/100;
}
return sale
}