Commit 8c73cf73 authored by blueblack's avatar blueblack

Initial commit

parents
Pipeline #17318 canceled with stages
# Server Status
Statistics for servers of Twinstar Gaming project
## Backend preview
![Admin widget Screenshot](/screenshots/backend.png)
## Frontend preview
![Live preview Screenshot](/screenshots/frontend.png)
\ No newline at end of file
<?php
/**
* Fetch stored data
*/
class Data {
/**
* Stats for defined realm
*
* @var object
*/
public $data;
/**
* Stats for authentication server
*
* @var object
*/
public $authentication_data;
/**
* Constructor for fetching data
*
* @param int $realm_id ID of realm for our widget
* @param string $server_ip IP of the server where are stored data
* @param int $server_port Port of the server where are stored data
*/
function __construct( int $realm_id, string $server_ip, int $server_port ) {
/* Check if class Memcache or Memcached exist */
if ( class_exists( 'Memcache' ) || class_exists( 'Memcached' ) ) {
$mc = new Memcached(); // Init Memcached class
$mc->addServer( $server_ip, $server_port ); // Add server with its IP where are stored data
}
$this->data = $mc->get( 'realm-stats-' . $realm_id ); // Fetch realm data from server
$this->authentication_data = $mc->get( 'realm-stats-auth'); // Fetch authentication data from server
}
}
<?php
/**
* Functionality for Frontend of the widget
*/
class Frontend {
/**
* Stats for defined realm
*
* @var string
*/
public static $status_range = 185;
/**
* Check if realm is online and by that return its HTML code
*
* @param bool $status
*
* @return string
*/
public static function online( $status ) {
if( $status ) {
return '<span class="online">ONLiNE</span>';
} else {
return '<span class="offline">OFFLiNE</span>';
}
}
/**
* HTML code for Factions status bar
*
* @return string
*/
public static function factions( $data ) {
/* Check if realm is online, otherwise return nothing */
if ( $data[ 'online' ] ) {
$playerCntAlliance = $data[ 'playerCountAliance' ]; // Data for Alliance faction
$playerCntHorde = $data[ 'playerCountHorde' ]; // Data for Horde faction
$playerCnt = $playerCntAlliance + $playerCntHorde+1; // Total of factions
$horde_length = Utilities::getLength( $playerCnt, $playerCntHorde, self::$status_range ); // Get length of Horde faction for width of its bar
$alliance_length = self::$status_range - $horde_length; // Length of Alliance faction for width of its bar
$horde_percent = floor( $playerCntHorde / ( $playerCnt / 100 ) ); // Calculate percentage of Horde faction
$alliance_percent = 100 - $horde_percent; // Calculate percentage of Alliance faction
/* Print HTML code */
print '
<div class="center-range cf">
<div class="range">
<div class="red" style="width:' . $horde_length . 'px">
<span>' . $horde_percent . ' %</span>
</div>
<div class="blue" style="width:' . $alliance_length . 'px;">
<span>' . $alliance_percent . ' %</span>
</div>
</div>
</div>';
}
}
}
\ No newline at end of file
<?php
/**
* Utilities for the widget
*/
class Utilities {
/**
* Convert seconds to readable time
*
* @param int $seconds
*
* @return string
*/
public static function secondsToReadableTime( int $seconds ) {
$readable = "";
/* Months */
$period = 30.42 * 24 * 60 * 60;
if ( $seconds >= $period ) {
$time = floor( $seconds / $period );
$seconds -= $time * $period;
$readable .= $time . ( ( $time < 2 ) ? " month " : " months " );
}
/* Days */
$period = 24 * 60 * 60;
if ( $seconds >= $period ) {
$time = floor( $seconds / $period );
$seconds -= $time * $period;
$readable .= $time . ( ( $time < 2 ) ? " day " : " days " );
}
/* Hours */
$period = 60 * 60;
if ( $seconds >= $period ) {
$time = floor( $seconds / $period );
$seconds -= $time * $period;
$readable .= $time . ( ( $time < 2 ) ? " hour " : " hours " );
}
/* Minutes */
$period = 60;
if ( $seconds >= $period ) {
$time = floor( $seconds / $period );
$seconds -= $time * $period;
$readable .= $time . ( ( $time < 2 ) ? " min " : " mins " );
}
return $readable;
}
/**
* Calculate length of one part of the object
*
* @param int $total Total value of all parts
* @param int $sub Number of one part
* @param int $range Length of the object
*
* @return int
*/
public static function getLength( int $total, int $sub, int $range ) {
$one = $total / 100;
if( $one ) {
$sub_perc = $sub / $one;
}
return floor( ( $range / 100 ) * $sub_perc );
}
}
\ No newline at end of file
<?php
/**
* Functionality for Server Status Widget
*/
class Widget extends \WP_Widget {
/**
* Name of text domain for translation in the Wordpress
*
* @var string
*/
const TEXT_DOMAIN = 'server_status';
/**
* Wordpress Constructor for the Widget
*
* @param string $widget_id Optional Base ID for the widget, lowercase and unique. If left empty,
* a portion of the widget's class name will be used Has to be unique.
* @param string $name Name for the widget displayed on the configuration page.
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for information
* on accepted arguments. Default empty array.
* @param array $control_options Optional. Widget control options. See wp_register_widget_control() for
*/
public function __construct() {
parent::__construct(
'server_status_widget',
__( 'Server Status', self::TEXT_DOMAIN ),
array( 'description' => __( 'Statistics for servers of Twinstar Gaming project', self::TEXT_DOMAIN ), 'customize_selective_refresh' => true, )
);
}
/**
* Create Widget form for backend with Wordpress function
*
* @return string
*/
public function form( $instance ) {
/* Set widget defaults */
$defaults = array(
'title' => 'Server Status',
'server_name' => '',
'realm_id' => '1',
'factions' => '1',
'uptime' => '1',
'realmlist' => '',
);
/* Parse current settings with defaults */
extract( wp_parse_args( ( array ) $instance, $defaults ) ); ?>
<?php /* Input for Widget Title */ ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', self::TEXT_DOMAIN ); ?></label>
<input class="widefat"
v-model="message"
id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
type="text"
value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php /* Input for Realm ID */ ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'realm_id' ) ); ?>"><?php _e( 'Realm ID:', self::TEXT_DOMAIN ); ?></label>
<input class="tiny-text"
v-model="message"
id="<?php echo esc_attr( $this->get_field_id( 'realm_id' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'realm_id' ) ); ?>"
type="number"
value="<?php echo esc_attr( $realm_id ); ?>"
size="3" />
</p>
<?php /* Checkbox for Show Factions status */ ?>
<p>
<input id="<?php echo esc_attr( $this->get_field_id( 'factions' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'factions' ) ); ?>"
type="checkbox"
value="1" <?php checked( '1', $factions ); ?> />
<label for="<?php echo esc_attr( $this->get_field_id( 'factions' ) ); ?>"><?php _e( 'Show Factions status', self::TEXT_DOMAIN ); ?></label>
</p>
<?php /* Checkbox for Show Uptime status */ ?>
<p>
<input id="<?php echo esc_attr( $this->get_field_id( 'uptime' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'uptime' ) ); ?>"
type="checkbox"
value="1" <?php checked( '1', $uptime ); ?> />
<label for="<?php echo esc_attr( $this->get_field_id( 'uptime' ) ); ?>"><?php _e( 'Show Uptime status', self::TEXT_DOMAIN ); ?></label>
</p>
<?php /* Input for Realmlist */ ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'realmlist' ) ); ?>"><?php _e( 'Realmlist:', self::TEXT_DOMAIN ); ?></label>
<input class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'realmlist' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'realmlist' ) ); ?>"
type="text"
value="<?php echo esc_attr( $realmlist ); ?>" />
</p>
<p class="howto">
For next line use &lt;br&gt;.<br>(set realmlist login.kronos-wow.com<strong>&lt;br&gt;</strong>set realmlist north.kronos-wow.com)
</p>
<?php
}
/* Update widget settings */
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
$instance['realm_id'] = isset( $new_instance['realm_id'] ) ? wp_strip_all_tags( $new_instance['realm_id'] ) : '';
$instance['factions'] = isset( $new_instance['factions'] ) ? 1 : false;
$instance['uptime'] = isset( $new_instance['uptime'] ) ? 1 : false;
$instance['realmlist'] = isset( $new_instance['realmlist'] ) ? $new_instance['realmlist'] : '';
return $instance;
}
/* Display the widget */
public function widget( $args, $instance ) {
extract( $args );
/* Check the widget options */
$title = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : '';
$realm_id = isset( $instance['realm_id'] ) ? apply_filters( 'realm_id', $instance['realm_id'] ) : '';
$factions = ! empty( $instance['factions'] ) ? $instance['factions'] : false;
$uptime = ! empty( $instance['uptime'] ) ? $instance['uptime'] : false;
$realmlist = isset( $instance['realmlist'] ) ? $instance['realmlist'] : '';
/* Load Data */
$realm = new Data( $realm_id, 'localhost', 11211 );
/* WordPress core before_widget hook */
echo $before_widget;
/* Frontend of the widget */
echo '<div id="server-status">';
echo '<h4>' . $title . '</h4>';
echo '<p class="status">';
echo '</p>' . "\n";
echo '<div class="server-status">';
/* Check if there are any data */
if ( $realm->data ) {
echo '<p class="status">';
echo '<strong>Login: </strong>' . Frontend::online( $realm->authentication_data[ 'online' ] );
echo '<span class="bull"></span><strong>Server: </strong>';
echo Frontend::online( $realm->data[ 'online' ] );
echo '</p>' . "\n";
if ( $factions ) {
echo '</p>' . "\n";
Frontend::factions( $realm->data );
echo "\n" . '<p class="status2">';
}
if ( $uptime ) {
echo ' <strong>Uptime: </strong>' . Utilities::secondsToReadableTime( $realm->data[ 'uptime' ] );
}
if ( $realmlist ) {
echo ' <br><strong>Realmlist: </strong>' . $realmlist;
}
echo '</p>' . "\n";
} else {
echo '<p class="status">';
echo 'No data for realm with ID ' . $realm_id . '.';
echo '</p>' . "\n";
}
echo '</div>';
echo '</div>';
/* WordPress core after_widget hook) */
echo $after_widget;
}
}
<?php
/**
* Plugin Name: Server Status
* Description: Statistics for servers of Twinstar Gaming project
* Version: 1.0.1
* Author: František 'Fery14' Briš
* Author URI: https://fery14.com
* License: GPL2
*/
$ss_dir = dirname( __FILE__ );
require( "$ss_dir/includes/utilities.php" );
require( "$ss_dir/includes/data.php" );
require( "$ss_dir/includes/frontend.php" );
require( "$ss_dir/includes/widget.php" );
/* Register the widget */
function register_server_status_widget() {
register_widget( 'Widget' );
}
add_action( 'widgets_init', 'register_server_status_widget' );
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment