diff --git a/src/login/controller.js b/src/login/controller.js index 086256f..251bb97 100644 --- a/src/login/controller.js +++ b/src/login/controller.js @@ -1,8 +1,19 @@ 'use strict'; // @ngInject -function LoginController(CardshifterServerAPI, $scope, $location, $rootScope) { +function LoginController(CardshifterServerAPI, $scope, $location, $rootScope, $timeout) { var SUCCESS = 200; + var UPDATE_DELAY = 10000; + var REFRESH_DELAY = 3000; + + $scope.refreshing = false; + + $scope.servers = [ + new ServerInfo("Local Host", "ws://127.0.0.1:4243"), + new ServerInfo("Dwarf Towers", "ws://dwarftowers.com:4243"), + new ServerInfo("Zomis.net", "ws://stats.zomis.net:4243"), + new ServerInfo("Other...", "other") + ]; $scope.login = function() { $scope.loggedIn = true; @@ -48,6 +59,97 @@ function LoginController(CardshifterServerAPI, $scope, $location, $rootScope) { $scope.$apply(); }); } + + $scope.refreshServers = function() { + $scope.refreshing = true; + $timeout(function() { + $scope.refreshing = false; + }, REFRESH_DELAY); + + /** + * This is a recursive function that is called + * once the Websocket has successfully connected + * with the server. Once it is connected, the + * Websocket is obliterated. + * + * This is used in place of the loop because + * API.init is a mostly async method, so the + * loop would rapidly terminate, and the sockets + * will be destroyed at the incorrect points in + * order for the "blank users"(#92) to be prevented. + */ + var i = 0; + (function getServerInfo() { + var thisServer = $scope.servers[i]; + + if(thisServer.name === "Other...") { + return; + } + + var now = Date.now(); + + CardshifterServerAPI.init(thisServer.address, false, function() { + thisServer.latency = Date.now() - now; + thisServer.isOnline = true; + + /* This must be created here because this is run after init is don't, so command is set properly */ + var getUsers = new CardshifterServerAPI.messageTypes.ServerQueryMessage("STATUS", ""); + + CardshifterServerAPI.sendMessage(getUsers); + CardshifterServerAPI.setMessageListener(function(message) { + + /* For some reason, local host always said 1 user online, but dwarftowers did not. */ + thisServer.userCount = message.users; + thisServer.availableMods = message.mods.length; + thisServer.gamesRunning = message.games; + thisServer.ais = message.ais; + + // Should these (^^) be dynamically loaded? + + CardshifterServerAPI.socket.close(); + CardshifterServerAPI.socket = null; + + i++; + if($scope.servers[i]) { + getServerInfo(); + } + }, ["status"]); + }, function() { + thisServer.latency = 0; + thisServer.isOnline = false; + thisServer.userCount = 0; + + i++; + if($scope.servers[i]) { + getServerInfo(); + } + }) + })(); + }; + + $scope.refreshServers(); // call it on startup + + /** + * The ServerInfo class. + * @constructor + * + * @param name:string -- The name of the server + * @param address:string -- The address of the server + * + * This class is used for displaying the various + * available servers at the top of the screen + * in the server status table. + */ + function ServerInfo(name, address) { + this.name = name; + this.address = address; + this.isOnline = false; + this.userCount = 0; + this.latency = 0; + this.availableMods = 0; + this.gamesRunning = 0; + this.ais = 0; + } }; module.exports = LoginController; diff --git a/src/login/login.html b/src/login/login.html index 412eb73..62d87cb 100644 --- a/src/login/login.html +++ b/src/login/login.html @@ -6,14 +6,38 @@
| Server | +Online | +Users | +Mods | +Games | +AIs | +Latency | +
|---|---|---|---|---|---|---|
| {{server.name}} | +{{server.isOnline}} | +{{server.userCount}} users | +{{server.availableMods}} mods | +{{server.gamesRunning}} games | +{{server.ais}} AIs | +{{server.latency}} ms | +
Refreshing...
+