server/ui/js/arena.js

77 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-08-27 22:19:53 -07:00
$(document).ready(function(){
2015-08-30 02:16:44 -07:00
$('#starter').prop('disabled', 'disabled');
2015-08-27 22:19:53 -07:00
$("#error").hide();
editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
function resizeAce() {
2015-08-30 00:58:18 -07:00
return $('#editor').height($(window).height() - $("#editor").offset().top - 100);
2015-08-27 22:19:53 -07:00
};
$(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');
gorobots.set_spectator(false);
2015-08-30 02:16:44 -07:00
gorobots.set_server(wsurl(), getGameID());
2015-08-27 22:19:53 -07:00
});
$('#watch').click(function() {
$('#tabs a[href="#arena"]').tab('show');
gorobots.set_spectator(true);
2015-08-30 02:16:44 -07:00
gorobots.set_server(wsurl(), getGameID());
2015-08-27 22:19:53 -07:00
});
$('#tabs a[href="#code"]').tab('show');
2015-08-30 00:58:18 -07:00
$("#fetch-url").keydown(function(e){
if(e.keyCode == 13 || e.keyCode == 32){
loadCode();
}
});
$("#fetch-button").click(loadCode);
2015-08-27 22:19:53 -07:00
});
2015-08-30 00:58:18 -07:00
function loadCode() {
var url = $("#fetch-url").val();
$.get(url).success(function(e) {
$("#error").val(e).hide();
editor.setValue(e);
$("#editor").show();
$("#fetch-area").hide();
2015-08-30 02:16:44 -07:00
$('#starter').prop('disabled', '');
2015-08-30 00:58:18 -07:00
}).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();
});
}
2015-08-30 02:16:44 -07:00
function wsurl() {
var url = "ws://";
if(window.location.protocol == "https:") {
url = "wss://";
}
url = url + window.location.host + "/ws/";
return url;
2015-08-30 02:16:44 -07:00
}
function getGameID() {
return window.location.search.split("=")[1];
}