Archived
0

MCSM: отображение реального списка серверов

This commit is contained in:
2017-05-01 14:42:16 +03:00
parent 26352ef17e
commit c8fc952ffc
5 changed files with 58 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys'
version = '0.8-SNAPSHOT'
version = '0.8.1-SNAPSHOT'
apply plugin: 'osgi'

View File

@@ -31,7 +31,7 @@ public class Activator implements BundleActivator, ServiceListener {
Manager manager = new Manager();
module = new MCSM_WebModule();
module = new MCSM_WebModule(manager);
logger.debug("Get service: {}", Webinterface.class);
serviceTracker = new ServiceTracker<>(context, Webinterface.class, null);

View File

@@ -5,6 +5,8 @@
package asys.mcsmanager;
import asys.webinterface.api.WebModule;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
@@ -15,8 +17,15 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MCSM_WebModule extends WebModule {
private static final Gson GSON = new Gson();
private Manager manager;
private final String MODULE_NAME = "mcsmanager";
private final Pattern URL_PATTERN_JS = Pattern.compile("/"+MODULE_NAME+"/(\\w+)\\.js");
private final String MODULE_URL = "/"+MODULE_NAME;
private final Pattern URL_PATTERN_JS = Pattern.compile(MODULE_URL+"/(\\w+)\\.js");
MCSM_WebModule(Manager manager) {
this.manager = manager;
}
@Override
public String getName() {
@@ -38,18 +47,31 @@ public class MCSM_WebModule extends WebModule {
@Override
public boolean handle(HttpExchange httpExchange) throws IOException {
String urlPath = httpExchange.getRequestURI().getPath();
Matcher matcher = URL_PATTERN_JS.matcher(urlPath);
if (matcher.find()) {
InputStream stream = getClass().getResourceAsStream("/"+matcher.group(1)+".js");
if (stream == null) {
this.sendHttpCode(httpExchange, 404, "not found");
if (urlPath.equals(MODULE_URL+"/servers.json")) {
return handleServersJson(httpExchange);
} else {
Matcher matcher = URL_PATTERN_JS.matcher(urlPath);
if (matcher.find()) {
InputStream stream = getClass().getResourceAsStream("/" + matcher.group(1) + ".js");
if (stream == null) {
this.sendHttpCode(httpExchange, 404, "not found");
return true;
}
httpExchange.getResponseHeaders().add("Content-Type", "text/javascript;charset=utf-8");
this.sendContent(httpExchange, 0, stream);
return true;
}
httpExchange.getResponseHeaders().add("Content-Type", "text/javascript;charset=utf-8");
this.sendContent(httpExchange, 0, stream);
return true;
}
return false;
}
private boolean handleServersJson(HttpExchange httpExchange) throws IOException {
this.sendJson(httpExchange, serverList());
return true;
}
private JsonElement serverList() {
return GSON.toJsonTree(manager.getServerList());
}
}

View File

@@ -55,21 +55,13 @@ var NvLineChart = React.createClass({
var ServerListItem = React.createClass({
render: function(){
var status;
if (this.props.stopped) {
status = ce('span', {className: 'label label-danger'}, 'остановлен');
} else {
status = ce('span', {className: 'label label-primary'}, this.props.players);
}
return(
ce('a', {className: 'list-group-item clearfix' + (this.props.active ? ' active' : ''), href: '#'},
ce('span', {style: {'padding-top': '15px'}},
ce('span', {className: 'glyphicon glyphicon-tasks'}),
nbsp,
this.props.title
),
ce('span', {className: 'pull-right'}, status)
)
)
)
}
@@ -77,12 +69,6 @@ var ServerListItem = React.createClass({
var ServerList = React.createClass({
render: function(){return(
ce('div', {className: 'list-group'},
ce(ServerListItem, {title: 'Server 1 [hub-1]', players: 42}),
ce(ServerListItem, {title: 'Server 2 [skywars-1]', players: 6, active: true}),
ce(ServerListItem, {title: 'Server 3 [bedwars-3]', players: 23}),
ce(ServerListItem, {title: 'Server 4', stopped: true}),
ce(ServerListItem, {title: 'Server 5', stopped: true})
)
ce('div', {className: 'list-group'}, this.props.children)
)}
});

View File

@@ -14,8 +14,22 @@ var ContentModule = React.createClass({
style.onerror = fnError;
document.body.appendChild(style);
},
requestServerList: function() {
var _this = this;
fetch('/mcsmanager/servers.json')
.then(function(response) {
response.json().then(function(data) {
console.debug(data);
_this.setState({serverList: data})
});
})
.catch(function(err){
console.error(err);
});
},
getInitialState: function(){return{
nvScriptReady: 0
nvScriptReady: 0,
serverList: []
}},
componentWillMount: function(){
var _this = this;
@@ -36,12 +50,19 @@ var ContentModule = React.createClass({
},
function(){ _this.setState({nvScriptReady: -5}); console.error('d3 - error'); }
);
this.requestServerList();
},
render: function(){
var element;
if (this.state.nvScriptReady === 3) {
var serverListItems = [];
this.state.serverList.forEach(function(item){
serverListItems.push(ce(ServerListItem, {title: item}));
});
element = ce('div', {className: 'row'},
ce('div', {className: 'col-md-4'}, ce(ServerList)),
ce('div', {className: 'col-md-4'}, ce(ServerList, null, serverListItems)),
ce('div', {className: 'col-md-8'},
ce('div', {className: 'pull-right'},
ce('div', {className: 'btn-group'},