-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
215 lines (208 loc) · 6.5 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Battle for the Front-End</title>
<meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=0 minimal-ui" />
<script src="jquery-2.0.3.min.js" type="text/javascript"></script>
<script src="phaser.2.6.2.min.js" type="text/javascript"></script>
<script src="blob.js" type="text/javascript"></script>
<script src="canvas-to-blob.js" type="text/javascript"></script>
<script src="filesaver.js" type="text/javascript"></script>
<script src="embed.js" type="text/javascript"></script>
<link href="https://fonts.googleapis.com/css?family=Audiowide" rel="stylesheet">
<style>
body {
margin: 0px;
padding: 0px;
font-family: 'Audiowide', cursive;
font-size: 30px;
background-color: #070222;
color: #fff;
text-align: center;
}
#phaser-example > canvas {
display: inline-block !important;
border: 7px solid #80808040;
border-radius: 20px;
width: 1571px;
height: 610px;
}
.enemy_1{
display: none;
}
.container {
min-height: 400px;
display: flex;
display: -webkit-flex;
flex-wrap: nowrap;
flex-direction: row;
justify-content: center;
align-items: center;
align-content: center;
}
.container:after {
display: block;
flex: 999 999 auto;
}
.item, canvas {
flex: 0 0 auto;
margin: 5px;
}
.docs{
font-size: 12px;
color: white;
margin: 0;
padding: 0;
text-align: left;
font-family: Arial;
}
.docs > a{
color: white;
}
#editor{
border: 7px solid #80808040;
color: white;
font-size: 24px;
width: 500px;
}
#editor,.enemy_0{
height: 600px;
}
.player_name{
border: 7px solid #80808040;
background-color: #80808040;
width: 150px;
font-size: 14px;
color: white;
vertical-align: middle;
margin-top: -15px;
}
</style>
</head>
<body>
<h1>Battle for the Front-End</h1>
<div class="container">
<div class="item">
<div id="editor" class="enemy_0"></div>
<p class="docs"><a href="docs.html" target="_blank">Read the docs!</a></p>
</div>
<div class="code item">
<div class="player_editor">
<div class="player container" style="min-height: auto;justify-content: end;">
<div id="avatar zero" class="item">
<img src="green-tank.png" alt="">
</div>
<div class="item">
<input class="player_name" oninput="resetGame(this);" id="player_name" type="text" name="" value="" placeholder="Robot name" style="height: 40px;">
</div>
<div id="avatar zero" class="item">
<img src="red-tank.png" alt="">
</div>
<div class="item">
<select class="player_name" id="enemy_selected" name="">
<option value="dummy-robot.js" selected>Dummy robot</option>
<option value="advanced-robot.js">Advanced robot</option>
</select>
</div>
<div class="item">
<button type="button" class="player_name" id="save_tank" name="button">Save</button>
</div>
</div>
<div id="phaser-example" ></div>
</div>
<div class="enemy_1">
<textarea name="name" rows="8" cols="80"></textarea>
</div>
</div>
</div>
<script type="text/javascript">
const IDE_HOOK = false;
</script>
<script src="editor/ace.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="game.js"></script>
<script type="text/javascript" src="launch.js"></script>
<script>
var BOARD_SIZE_WIDTH = 650;
var BOARD_SIZE_HEIGHT = 500;
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
document.getElementById('editor').style.fontSize='13px';
let firstLaunch = true;
editor.session.on('change', resetGame);
var input = document.querySelector('#enemy_selected');
const enemyCode = document.querySelector('.enemy_1 > textarea');
input.addEventListener('input', function() {
destroyGame();
if(input.value === 'advanced-robot.js') {
enemyCode.value = robots[2].asStr;
window.launchGame([robots[0], robots[2]]);
} else {
enemyCode.value = robots[1].asStr;
window.launchGame([robots[0], robots[1]]);
}
});
function getTankIndex() {
if(input.value === 'advanced-robot.js') {
return 2;
}
return 1;
}
function resetGame() {
if (firstLaunch) {
firstLaunch = false;
return;
};
destroyGame();
const enemyCode = document.querySelector('.enemy_1 > textarea').value;
modifyRobot(0, { code: editor.getValue(), name: getPlayerName() });
modifyRobot(getTankIndex(), { code: enemyCode, name: getEnemyName() });
window.launchGame([robots[0], robots[getTankIndex()]]);
}
var save = document.querySelector('#save_tank');
save.addEventListener('click', () => {
const url = "/save";
const playerName = getPlayerName();
if (!playerName || playerName === 'Your robot') return;
if( /[^a-zA-Z0-9\-\/]/.test(playerName) ) {
alert('Invalid name');
return false;
}
fetch(url, {
method : "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
code: editor.getValue(),
name: playerName
})
}).then(
response => response.text()
).then(
response => {
save.innerText = 'Saved!'
setTimeout(() => (save.innerText = 'Save'), 1000);
console.log('Robot saved', response);
}
);
});
getRobot('robot.js', (robotCode) => {
const robotName = getPlayerName();
addRobot({ code: robotCode, name: robotName });
var editor = ace.edit('editor');
editor.setValue(robotCode);
getRobot('dummy-robot.js', (robotCode) => {
document.querySelector('.enemy_1 > textarea').value = robotCode;
addRobot({ code: robotCode, name: getEnemyName() });
getRobot('advanced-robot.js', (robotCode) => {
addRobot({ code: robotCode, name: 'Advanced robot' });
window.launchGame([robots[0], robots[1]]);
});
});
});
</script>
</body>
</html>