-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
114 lines (106 loc) · 3.01 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
var token = "",
uid = -1,
data = null,
lastAttack = [ -1, -1 ],
tempAttack = [ -1, -1 ],
lastUpdate = 0,
attacking = false,
mode = "";
function GetCell( x, y ) {
if( x >= 0 && y >= 0 && x < 30 && y < 30 ) {
return data[ "cells" ][ y * 30 + x ];
} else {
return null;
}
}
function Attack( x, y, boost = false ) {
if( !attacking ) {
attacking = true;
tempAttack = [ x, y ];
var xhr = new XMLHttpRequest();
xhr.open( "POST", "https://colorfight.herokuapp.com/attack" );
xhr.setRequestHeader( "Content-Type", "application/json;charset=UTF-8" );
xhr.onreadystatechange = function() {
if( this.readyState == XMLHttpRequest.DONE && this.status == 200 ) {
attacking = false;
var d = JSON.parse( this.responseText );
if( d[ "err_code" ] == 0 ) {
lastAttack = tempAttack;
}
}
}
xhr.send( JSON.stringify( { "cellx" : x, "celly" : y, "token" : token } ) );
}
}
function ExampleAI() {
var c,
d,
cc,
adjacent = [];
for( var x = 0; x < 30; x++ ) {
for( var y = 0; y < 30; y++ ) {
c = data[ "cells" ][ 30 * y + x ];
if( c[ "o" ] == uid ) {
d = [[0,-1],[1,0],[0,1],[-1,0]][Math.floor( Math.random() * 4 )];
cc = GetCell( x + d[ 0 ], y + d[ 1 ] );
if( cc != null && cc[ "o" ] != uid && x + d[ 0 ] != lastAttack[ 0 ] && y + d[ 1 ] != lastAttack[ 1 ] ) {
Attack( x + d[ 0 ], y + d[ 1 ] );
}
}
}
}
}
function GameLoop() {
switch( mode ) {
default:
ExampleAI();
break;
}
}
function Refresh() {
var xhr = new XMLHttpRequest();
xhr.open( "POST", "https://colorfight.herokuapp.com/getgameinfo" );
xhr.setRequestHeader( "Content-Type", "application/json;charset=UTF-8" );
xhr.onreadystatechange = function() {
if( this.readyState == XMLHttpRequest.DONE && this.status == 200 ) {
if( data == null ) {
data = JSON.parse( this.responseText );
} else {
var t = JSON.parse( this.responseText );
data[ "info" ] = t[ "info" ];
for( var i = 0; i < t[ "cells" ].length; i++ ) {
data[ "cells" ][ t[ "cells" ][ i ][ "y" ] * 30 + t[ "cells" ][ i ][ "x" ] ] = t[ "cells" ][ i ];
}
}
lastUpdate = data[ "info" ][ "time" ];
GameLoop();
Refresh();
}
}
if( data == null ) {
xhr.send( JSON.stringify( { "protocol" : 2 } ) );
} else {
xhr.send( JSON.stringify( { "protocol" : 1, "timeAfter" : lastUpdate } ) );
}
}
function JoinGame( name ) {
var xhr = new XMLHttpRequest();
xhr.open( "POST", "https://colorfight.herokuapp.com/joingame", true );
xhr.setRequestHeader( "Content-Type", "application/json;charset=UTF-8" );
xhr.onreadystatechange = function() {
if( this.readyState == XMLHttpRequest.DONE && this.status == 200 ) {
var d = JSON.parse( this.responseText );
token = d[ "token" ];
uid = d[ "uid" ];
Refresh();
}
}
xhr.send( JSON.stringify( { "name" : name } ) );
}
var name_input = document.getElementById( "name_input" ),
join_button = document.getElementById( "join_button" );
join_button.addEventListener( "click", function() {
if( name_input.value.trim() != "" ) {
JoinGame( name_input.value.trim() );
}
} );