Added a couple of useful services to rconservice

This commit is contained in:
Garry Newman
2016-02-29 13:03:23 +00:00
parent a5a5e107fe
commit 1387e4cb3f
13 changed files with 400 additions and 273 deletions
+149
View File
@@ -0,0 +1,149 @@
var app = angular.module( 'RconApp', ['ngMaterial', 'ngRoute'] );
app.service( 'rconService', [RconService] );
app.config( function ( $mdThemingProvider, $routeProvider )
{
$mdThemingProvider.theme( 'default' ).primaryPalette( 'blue',
{
'default': '700'
}).accentPalette( 'blue' );
$routeProvider.when( "/console", { Title: "Console", templateUrl: "html/console.html", Nav: true } )
$routeProvider.when( "/playerlist", { Title: "Player List", templateUrl: "html/playerlist.html", Nav: true } )
$routeProvider.when( "/player/:userid", { Title: "Player Info", templateUrl: "html/playerInfo.html" } )
$routeProvider.when( "/player/:userid/xp", { Title: "Player Xp Stats", templateUrl: "html/playerXp.html" } )
.otherwise( { redirectTo: '/console' } );
} );
app.controller( 'RconController', RconController );
function RconController( $scope, $rootScope, rconService, $mdSidenav, $timeout, $mdDialog, $route )
{
$scope.$route = $route;
$scope.pages = $.map( $route.routes, function ( value, index )
{
return [value];
} );
console.log( $scope.pages );
$scope.OpenLeftMenu = function ()
{
$mdSidenav( 'left' ).toggle();
};
$scope.IsConnected = function ()
{
return rconService.IsConnected();
}
$rootScope.$on( '$stateChangeStart', function ( next, current )
{
console.log( next );
} );
rconService.OnOpen = function ()
{
$scope.Connected = true;
$scope.$broadcast( "OnConnected" );
$scope.$digest();
}
rconService.OnClose = function ( ev )
{
$scope.$broadcast( "OnDisconnected", ev );
$scope.$digest();
}
rconService.OnError = function ( ev )
{
$scope.$broadcast( "OnConnectionError", ev );
$scope.$digest();
}
rconService.OnMessage = function ( msg )
{
$scope.$apply( function ()
{
$scope.$broadcast( "OnMessage", msg );
} );
}
}
app.controller( 'ConnectionController', ConnectionController );
function ConnectionController( $scope, rconService )
{
$scope.Address = "";
$scope.Password = "";
$scope.SaveConnection = true;
if ( localStorage.previousConnections != null )
$scope.PreviousConnects = angular.fromJson( localStorage.previousConnections );
if ( $scope.PreviousConnects == null )
$scope.PreviousConnects = new Array();
console.log( $scope.PreviousConnects );
$scope.Connect = function ()
{
$scope.LastErrorMessage = null;
rconService.Connect( $scope.Address, $scope.Password );
}
$scope.ConnectTo = function ( c )
{
$scope.SaveConnection = false;
$scope.LastErrorMessage = null;
rconService.Connect( c.Address, c.Password );
}
$scope.$on( "OnDisconnected", function ( x, ev )
{
console.log( ev );
$scope.LastErrorMessage = "Connection was closed - Error " + ev.code;
$scope.$digest();
} );
$scope.$on( "OnConnected", function ( x, ev )
{
if ( $scope.SaveConnection )
{
$scope.PreviousConnects.push( { Address: $scope.Address, Password: $scope.Password } );
localStorage.previousConnections = angular.toJson( $scope.PreviousConnects );
}
} );
}
app.filter( 'SecondsToDuration', [SecondsToDuration] );
function SecondsToDuration()
{
return function ( input )
{
input = parseInt( input );
var out = "";
var hours = Math.floor( input / 3600 );
if ( input > 3600 ) out += hours + "h ";
var minutes = Math.floor( input / 60 ) % 60;
if ( input > 60 ) out += minutes + "m ";
var seconds = input % 60;
out += seconds + "s";
return out;
}
}
+28
View File
@@ -0,0 +1,28 @@
app.controller( 'ConsoleController', ['$scope', 'rconService', ConsoleController] );
function ConsoleController( $scope, rconService )
{
$scope.Output = rconService.Output;
$scope.SubmitCommand = function ()
{
rconService.Command( $scope.Command, 0 );
$scope.Command = "";
}
$scope.$on( "OnMessage", function ( event, msg )
{
if ( msg.Message.startsWith( "[CHAT] " ) )
msg.Class = "Chat";
if ( msg.Identifier == 1 )
msg.Class = "Requested";
if ( msg.Identifier > 1 )
return;
$( "#ConsoleController .Output" ).scrollTop( $( "#ConsoleController .Output" )[0].scrollHeight );
} );
}
+7
View File
@@ -0,0 +1,7 @@
app.controller( 'PlayerInfoController', PlayerInfoController );
function PlayerInfoController( $scope, rconService, $routeParams )
{
$scope.userid = $routeParams.userid;
}
+7
View File
@@ -0,0 +1,7 @@
app.controller( 'PlayerXpController', PlayerXpController );
function PlayerXpController( $scope, rconService, $routeParams )
{
$scope.userid = $routeParams.userid;
}
+17
View File
@@ -0,0 +1,17 @@
app.controller( 'PlayerListController', PlayerListController );
function PlayerListController( $scope, rconService )
{
$scope.Output = new Array();
$scope.Refresh = function ()
{
rconService.Request( "playerlist", $scope, function ( msg )
{
$scope.Players = JSON.parse( msg.Message );
});
}
rconService.InstallService( $scope, $scope.Refresh )
}
+112
View File
@@ -0,0 +1,112 @@
function RconService()
{
var s = {};
s.Callbacks = {};
// A list of untargetted ouput from the server
// ie - the console history
s.Output = new Array();
s.Connect = function ( addr, pass )
{
s.Socket = new WebSocket( "ws://" + addr + "/" + pass );
s.Socket.onmessage = function ( e )
{
var data = angular.fromJson( e.data );
//
// This is a targetted message, it has an identifier
// So feed it back to the right callback.
//
if ( data.Identifier != 0 )
{
var cb = s.Callbacks[data.Identifier];
if ( cb != null )
{
cb.scope.$apply( function ()
{
cb.callback( data );
} );
}
s.Callbacks[data.Identifier] = null;
return;
}
//
// Generic console message, let OnMessage catch it
//
if ( s.OnMessage != null )
{
s.Output.push( data );
s.OnMessage( data );
}
};
s.Socket.onopen = s.OnOpen;
s.Socket.onclose = s.OnClose;
s.Socket.onerror = s.OnError;
console.log( s.Socket );
}
s.Command = function ( msg, identifier )
{
if ( identifier == null )
identifier = -1;
var packet =
{
Identifier: identifier,
Message: msg,
Name: "WebRcon"
}
s.Socket.send( JSON.stringify( packet ) );
};
var LastIndex = 1001;
//
// Make a request, call this function when it returns
//
s.Request = function ( msg, scope, callback )
{
LastIndex++;
s.Callbacks[LastIndex] = { scope: scope, callback: callback };
s.Command( msg, LastIndex );
}
//
// Returns true if websocket is connected
//
s.IsConnected = function ()
{
if ( s.Socket == null ) return false;
return s.Socket.readyState == 1;
}
//
// Helper for installing connectivity logic
//
// Basically if not connected, call this function when we are
// And if we are - then call it right now.
//
s.InstallService = function( scope, func )
{
scope.$on( "OnConnected", function ()
{
func();
});
if ( s.IsConnected() )
{
func();
}
}
return s;
}