-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
139 lines (119 loc) · 4.11 KB
/
main.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
document.addEventListener("DOMContentLoaded", function() {
document.getElementById("submit").onclick=function(event) {
if (document.getElementById('player1_name').value=="Guest") {
if(!confirm("If you want your results to be stored sign in or sign up!"))
return;
}
if(document.getElementById('player2_name').checked==true)
document.getElementById('player2_name').value="Computer";
else
document.getElementById('player2_name').value="";
var connect4=new Connect4('#connect4', document.getElementById('rows').value, document.getElementById('columns').value,
document.getElementById('player1_name').value, document.getElementById('player2_name').value,
document.getElementById('firstToStart').value, document.getElementById('level').value,
document.getElementById('pass').value);
var btn = document.getElementById('submit');
if (btn.innerHTML=="Submit") {
btn.innerHTML = "Restart";
}
else if(btn.innerHTML=="Restart") {
var myNode=document.getElementById("connect4");
while(myNode.firstChild){
myNode.removeChild(myNode.firstChild);
}
connect4=new Connect4('#connect4', document.getElementById('rows').value, document.getElementById('columns').value,
document.getElementById('player1_name').value, document.getElementById('player2_name').value,
document.getElementById('firstToStart').value);
}
document.getElementById('turn').innerHTML='';
document.getElementById("turn").style.display="block";
event.preventDefault();
}
});
function on() {
document.getElementById("overlay").style.display = "block";
}
function off() {
document.getElementById("overlay").style.display = "none";
}
function signIn(){
const url = "http://twserver.alunos.dcc.fc.up.pt:8008/register";
var data = JSON.stringify({"nick":document.getElementById('login').value, "pass":document.getElementById('pass').value});
postData(url,data);
off();
event.preventDefault();
}
function signUp() {
let login=document.getElementById('login').value;
let pass=document.getElementById('pass').value;
off();
console.log(login, pass);
const url = "http://twserver.alunos.dcc.fc.up.pt:8008/register";
var data = JSON.stringify({"nick":login, "pass":pass});
console.log(data);
postData(url, data);
}
function postData(url, data) {
const xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.send(data);
xhr.onreadystatechange=function() {
if(xhr.readyState===4){
if(xhr.status==200)
auth(xhr.responseText);
if(xhr.status==400)
auth(null);
}
}
}
function auth(response) {
var res=JSON.parse(response);
if(res==null){
alert("Bad login or password!");
return;
}
if(Object.keys(res).length === 0 && res.constructor === Object){
document.getElementById('player1_name').value=document.getElementById('login').value;
document.getElementById('player1_name').readOnly=true;
}
}
function showResults(){
document.getElementById("ratingsList").innerHTML='';
var temp='';
var i=0;
for(var key in sessionStorage) {
if(i<sessionStorage.length) {
i++;
temp='<br>'+i+'. '+key+' '+sessionStorage[key]+'';
document.getElementById("ratingsList").innerHTML+=temp;
}
else {
break;
}
}
event.preventDefault();
}
function showResults2() {
const url = "http://twserver.alunos.dcc.fc.up.pt:8008/ranking";
var data = JSON.stringify({"size": { "rows": Number(document.getElementById('rows').value), "columns": Number(document.getElementById('columns').value)}});
console.log(data);
const xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.send(data);
xhr.onreadystatechange=function() {
if(xhr.readyState>3 && xhr.status==200){
printResults(JSON.parse(String(xhr.responseText)));
}
}
}
function printResults(data) {
console.log(data);
console.log(data.ranking[0]);
let i=0;
document.getElementById("ratingsList").innerHTML='';
for (var key in data.ranking) {
i++;
document.getElementById("ratingsList").innerHTML+='<br>'+i+'. '+data.ranking[key].nick;
}
event.preventDefault();
}