server/ui/js/arena.js

72 lines
2.0 KiB
JavaScript

$(document).ready(function(){
$('#starter,#watch').prop('disabled', 'disabled');
$("#error").hide();
editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
function resizeAce() {
return $('#editor').height($(window).height() - $("#editor").offset().top - 100);
};
$(window).resize(resizeAce);
resizeAce();
var gorobots = gorobots_init({}, true);
$("#robot_picker").change(function(e){
$.ajax({
url:"/api/v1/bot/" + $("#robot_picker").val(),
success: function(data){
editor.setValue(data.code);
set_current_bot($("#robot_picker").val());
}
});
});
$('#starter').click(function() {
$('#tabs a[href="#arena"]').tab('show');
var url = "ws://{{ server.hostname }}:8666/ws/";
gorobots.set_spectator(false);
gorobots.set_server(
url,
"{{ game }}"
);
});
$('#watch').click(function() {
$('#tabs a[href="#arena"]').tab('show');
var url = "ws://{{ server.hostname }}:8666/ws/";
gorobots.set_spectator(true);
gorobots.set_server(
url,
"{{ game }}"
);
});
$('#tabs a[href="#code"]').tab('show');
$("#fetch-url").keydown(function(e){
if(e.keyCode == 13 || e.keyCode == 32){
loadCode();
}
});
$("#fetch-button").click(loadCode);
});
function loadCode() {
var url = $("#fetch-url").val();
$.get(url).success(function(e) {
$("#error").val(e).hide();
editor.setValue(e);
$("#editor").show();
$("#fetch-area").hide();
$('#starter,#watch').prop('disabled', function(i, v) { return !v; });
}).fail(function(e){
var msg = "got a useless error message; is the remote server down?";
if (e.statusText != "error") {
msg = e.responseText;
}
$("#error").text(msg).show();
});
}