forked from dolfin/checkers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdamas.js
More file actions
222 lines (201 loc) · 6.07 KB
/
damas.js
File metadata and controls
222 lines (201 loc) · 6.07 KB
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
// Auxiliar.
function $(sel) { var list = document.querySelectorAll(sel); return (list.length == 1 ? list[0] : list); }
// Al cargarse el documento...
document.addEventListener('DOMContentLoaded', function()
{
// Cuando juega el usuario...
$("#confirm").addEventListener('click', function() {
var valid = false;
var target = 0;
var vacios = $("input[type=radio][name=empty]");
for(var act=0; act<vacios.length; act++)
{
if(vacios[act].checked)
{
valid = true;
target = vacios[act].value;
break;
}
}
if(valid)
{
valid = false;
var source = 0;
var fichas = $("input[type=radio][name=white]");
if(fichas.length)
{
for(var act=0; act<fichas.length; act++)
{
if(fichas[act].checked)
{
valid = true;
source = fichas[act].value;
break;
}
}
}
else
{
valid = fichas.checked;
}
if(valid)
{
var fData = new FormData();
fData.append("source", source);
fData.append("target", target);
var pAjax = new RAjax('ia_turn', fData);
pAjax.done = function()
{
if(pAjax.respuesta == "invalid")
{
var vacios = $("input[type=radio][name=empty]");
for(var act=0; act<vacios.length; act++)
{
if(vacios[act].checked)
{
vacios[act].className = "invalid";
break;
}
}
}
else
{
var changes = pAjax.respuesta.split("|");
for(var act=0; act<changes.length; act++)
{
var newInfo = changes[act].split("-");
var td = $("#c_"+newInfo[0]);
switch(newInfo[1])
{
case "x" :
td.innerHTML = ("<label data-tipo=\"peon\" for=\"o_"+newInfo[0]+"\">♟</label>");
break;
case "xx" :
td.innerHTML = ("<label data-tipo=\"dama\" for=\"o_"+newInfo[0]+"\">♚</label>");
break;
case "o" :
td.innerHTML = ("<input data-tipo=\"peon\" type=\"radio\" name=\"white\" value=\""+newInfo[0]+"\" id=\"o_"+newInfo[0]+"\">"+
"<label for=\"o_"+newInfo[0]+"\">♙</label>");
break;
case "oo" :
td.innerHTML = ("<input data-tipo=\"dama\" type=\"radio\" name=\"white\" value=\""+newInfo[0]+"\" id=\"o_"+newInfo[0]+"\">"+
"<label for=\"o_"+newInfo[0]+"\">♔</label>");
break;
case "e" :
td.innerHTML = ("<input type=\"radio\" name=\"empty\" value=\""+newInfo[0]+"\" id=\"o_"+newInfo[0]+"\">"+
"<label for=\"o_"+newInfo[0]+"\"></label>");
break;
}
}
var targets = $("input[type=radio][name=empty], input[type=radio][name=white]");
for(var act=0; act<targets.length; act++)
{
targets[act].checked = false;
targets[act].className = "";
}
}
}
pAjax.run();
}
else
{
alert("Debes seleccionar una ficha.");
}
}
else
{
alert("El objetivo no es valido.");
}
}, false);
}, false);
//#####################################################################################################################
//######################################### HERRAMIENTAS ##############################################################
//#####################################################################################################################
//Prototipo de asincronia.
var RAjax = function ( archivoPHP , parametros , metodo , asincronico) {
//Valores por defecto.
parametros || ( parametros = 'Origen=RAjax' );
metodo || ( metodo = 'POST' );
asincronico || ( asincronico = true );
//Propiedades publicas.
this.archivoPHP = new String(archivoPHP);
this.parametros = parametros;
this.metodo = new String(metodo);
this.respuesta = '';
this.estado = 'Construida';
//Propiedades privadas.
var ThisRAjax = this;
//metodos publicos genericos.
this.Preparando = function() { }
this.Esperando = function() { }
this.Cargando = function() { }
this.done = function() { }
//metodos publicos de archivos.
this.InicioTransmicion = function() { }
this.Progreso = function(estado) { }
this.FinTransmicion = function() { }
this.Error = function(DetalleError) {
alert(DetalleError);
}
this.run = function() {
//Creo la peticion.
var Peticion = new XMLHttpRequest();
//Abro el nexo.
Peticion.open ( ThisRAjax.metodo , archivoPHP , asincronico);
//Monitoreo la subida de archivos.
Peticion.upload.addEventListener('loadstart' , this.InicioTransmicion , false);
Peticion.upload.addEventListener('progress' , this.Progreso , false);
Peticion.upload.addEventListener('load' , this.FinTransmicion , false);
//Monitoreo el estado.
Peticion.onreadystatechange = function () {
//Cuando se completa correctamente.
switch (Peticion.readyState) {
case 0:
//No inicializado (el método open no a sido llamado).
ThisRAjax.estado = 'No iniciada';
ThisRAjax.Preparando();
break;
case 1:
//Cargando (se llamó al método open).
ThisRAjax.estado = 'Nexo abierto';
ThisRAjax.Preparando();
break;
case 2:
//Cargado (se llamó al método send y ya tenemos la cabecera de la petición HTTP y el status).
ThisRAjax.estado = 'Esperando respuesta';
ThisRAjax.Esperando();
break;
case 3:
//Interactivo (la propiedad responseText tiene datos parciales).
ThisRAjax.estado = 'Cargando datos';
ThisRAjax.Cargando();
break;
case 4:
//Completado (la propiedad responseText tiene todos los datos pedidos al servidor).
ThisRAjax.estado = 'Completada';
//Si se realizo correctamente.
if (Peticion.status == 200)
{
console.log(Peticion.responseText);
ThisRAjax.respuesta = Peticion.responseText;
ThisRAjax.done();
} else {
switch (Peticion.status) {
case 404 :
ThisRAjax.Error('No se encontro la pagina solicitada.');
break;
case 500 :
ThisRAjax.Error('Error interno del servidor.');
break;
default:
ThisRAjax.Error('La peticion se completo con un status: ' + Peticion.status);
break;
}
}
break;
}
};
//La envio con los parametros.
Peticion.send(parametros);
}
}