2014-05-03 21:57:38 +02:00
< ? php
namespace MCTeam ;
use FML\Controls\Frame ;
use FML\Controls\Gauge ;
use FML\Controls\Label ;
use FML\Controls\Quad ;
use FML\ManiaLink ;
use ManiaControl\Callbacks\CallbackListener ;
use ManiaControl\Callbacks\CallbackManager ;
use ManiaControl\Callbacks\Callbacks ;
use ManiaControl\Callbacks\TimerListener ;
2017-03-26 20:21:23 +02:00
use ManiaControl\Files\AsyncHttpRequest ;
2014-08-05 01:49:13 +02:00
use ManiaControl\Logger ;
2014-05-03 21:57:38 +02:00
use ManiaControl\ManiaControl ;
use ManiaControl\Maps\Map ;
use ManiaControl\Players\Player ;
use ManiaControl\Players\PlayerManager ;
use ManiaControl\Plugins\Plugin ;
2014-05-09 10:48:51 +02:00
use ManiaControl\Plugins\PluginMenu ;
2014-05-13 14:15:00 +02:00
use ManiaControl\Settings\Setting ;
2014-05-03 21:57:38 +02:00
use ManiaControl\Settings\SettingManager ;
2014-05-13 16:40:05 +02:00
use ManiaControl\Utils\ColorUtil ;
2014-05-03 21:57:38 +02:00
/**
* ManiaControl Karma Plugin
*
* @ author ManiaControl Team < mail @ maniacontrol . com >
2017-02-04 11:49:23 +01:00
* @ copyright 2014 - 2017 ManiaControl Team
2014-05-03 21:57:38 +02:00
* @ license http :// www . gnu . org / licenses / GNU General Public License , Version 3
*/
class KarmaPlugin implements CallbackListener , TimerListener , Plugin {
/*
* Constants
*/
const ID = 2 ;
const VERSION = 0.1 ;
2014-05-03 23:49:58 +02:00
const NAME = 'Karma Plugin' ;
const AUTHOR = 'MCTeam' ;
2014-05-03 21:57:38 +02:00
const MLID_KARMA = 'KarmaPlugin.MLID' ;
const TABLE_KARMA = 'mc_karma' ;
const CB_KARMA_CHANGED = 'KarmaPlugin.Changed' ;
const CB_KARMA_MXUPDATED = 'KarmaPlugin.MXUpdated' ;
const SETTING_AVAILABLE_VOTES = 'Available Votes (X-Y: Comma separated)' ;
const SETTING_WIDGET_ENABLE = 'Enable Karma Widget' ;
const SETTING_WIDGET_TITLE = 'Widget-Title' ;
const SETTING_WIDGET_POSX = 'Widget-Position: X' ;
const SETTING_WIDGET_POSY = 'Widget-Position: Y' ;
const SETTING_WIDGET_WIDTH = 'Widget-Size: Width' ;
const SETTING_WIDGET_HEIGHT = 'Widget-Size: Height' ;
const SETTING_NEWKARMA = 'Enable "new karma" (percentage), disable = RASP karma' ;
const STAT_PLAYER_MAPVOTES = 'Voted Maps' ;
/*
* Constants MX Karma
*/
const SETTING_WIDGET_DISPLAY_MX = 'Display MX-Karma in Widget' ;
const SETTING_MX_KARMA_ACTIVATED = 'Activate MX-Karma' ;
2014-05-20 15:42:10 +02:00
const SETTING_MX_KARMA_IMPORTING = 'Import old MX-Karma' ;
2014-05-03 21:57:38 +02:00
const MX_IMPORT_TABLE = 'mc_karma_mximport' ;
2015-11-16 22:07:06 +01:00
const MX_KARMA_URL = 'https://karma.mania-exchange.com/api2/' ;
2014-06-17 23:35:56 +02:00
const MX_KARMA_START_SESSION = 'startSession' ;
const MX_KARMA_ACTIVATE_SESSION = 'activateSession' ;
const MX_KARMA_SAVE_VOTES = 'saveVotes' ;
const MX_KARMA_GET_MAP_RATING = 'getMapRating' ;
2014-05-03 21:57:38 +02:00
/*
2014-08-02 22:31:46 +02:00
* Private properties
2014-05-03 21:57:38 +02:00
*/
2014-05-03 23:49:58 +02:00
/** @var ManiaControl $maniaControl */
2017-03-26 13:58:23 +02:00
private $maniaControl = null ;
2014-05-03 21:57:38 +02:00
private $updateManialink = false ;
2014-05-03 23:49:58 +02:00
/** @var ManiaLink $manialink */
2014-05-03 21:57:38 +02:00
private $manialink = null ;
2014-06-29 17:24:01 +02:00
// TODO: use some sort of model class instead of various array keys that you can't remember
2014-05-03 21:57:38 +02:00
private $mxKarma = array ();
/**
2014-05-03 23:49:58 +02:00
* @ see \ManiaControl\Plugins\Plugin :: prepare ()
2014-05-03 21:57:38 +02:00
*/
public static function prepare ( ManiaControl $maniaControl ) {
2014-05-13 14:15:00 +02:00
$thisClass = get_class ();
2014-08-13 11:14:29 +02:00
$maniaControl -> getSettingManager () -> initSetting ( $thisClass , self :: SETTING_MX_KARMA_ACTIVATED , true );
$maniaControl -> getSettingManager () -> initSetting ( $thisClass , self :: SETTING_MX_KARMA_IMPORTING , true );
$maniaControl -> getSettingManager () -> initSetting ( $thisClass , self :: SETTING_WIDGET_DISPLAY_MX , true );
$servers = $maniaControl -> getServer () -> getAllServers ();
2014-05-03 21:57:38 +02:00
foreach ( $servers as $server ) {
2014-05-13 14:15:00 +02:00
$settingName = self :: buildKarmaSettingName ( $server -> login );
2014-08-13 11:14:29 +02:00
$maniaControl -> getSettingManager () -> initSetting ( $thisClass , $settingName , '' );
2014-05-03 21:57:38 +02:00
}
}
2014-05-13 14:15:00 +02:00
/**
* Build the Karma Setting Name for the given Server Login
*
* @ param string $serverLogin
* @ return string
*/
private static function buildKarmaSettingName ( $serverLogin ) {
2017-03-16 22:13:47 +01:00
return '$l[https://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l' ;
2014-05-13 14:15:00 +02:00
}
2014-05-03 21:57:38 +02:00
/**
* @ see \ManiaControl\Plugins\Plugin :: getId ()
*/
public static function getId () {
return self :: ID ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getName ()
*/
public static function getName () {
2014-05-03 23:49:58 +02:00
return self :: NAME ;
2014-05-03 21:57:38 +02:00
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getVersion ()
*/
public static function getVersion () {
return self :: VERSION ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getAuthor ()
*/
public static function getAuthor () {
2014-05-03 23:49:58 +02:00
return self :: AUTHOR ;
2014-05-03 21:57:38 +02:00
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getDescription ()
*/
public static function getDescription () {
return 'Plugin offering Karma Voting for Maps.' ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: load ()
*/
public function load ( ManiaControl $maniaControl ) {
$this -> maniaControl = $maniaControl ;
// Init database
$this -> initTables ();
2014-08-03 01:34:18 +02:00
// Settings
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_AVAILABLE_VOTES , '-2,2' );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_ENABLE , true );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_TITLE , 'Map-Karma' );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_POSX , 160 - 27.5 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_POSY , 90 - 10 - 6 );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_WIDTH , 25. );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_WIDGET_HEIGHT , 12. );
$this -> maniaControl -> getSettingManager () -> initSetting ( $this , self :: SETTING_NEWKARMA , true );
2014-08-03 01:34:18 +02:00
// Callbacks
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getTimerManager () -> registerTimerListening ( $this , 'handle1Second' , 1000 );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: BEGINMAP , $this , 'handleBeginMap' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: BEGINMAP , $this , 'importMxKarmaVotes' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( Callbacks :: ENDMAP , $this , 'sendMxKarmaVotes' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( PlayerManager :: CB_PLAYERCONNECT , $this , 'handlePlayerConnect' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( CallbackManager :: CB_MP_PLAYERCHAT , $this , 'handlePlayerChat' );
$this -> maniaControl -> getCallbackManager () -> registerCallbackListener ( SettingManager :: CB_SETTING_CHANGED , $this , 'updateSettings' );
2014-05-03 21:57:38 +02:00
// Define player stats
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getStatisticManager () -> defineStatMetaData ( self :: STAT_PLAYER_MAPVOTES );
2014-05-03 21:57:38 +02:00
// Register Stat in Simple StatsList
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getStatisticManager () -> getSimpleStatsList () -> registerStat ( self :: STAT_PLAYER_MAPVOTES , 100 , " VM " );
2014-05-03 21:57:38 +02:00
$this -> updateManialink = true ;
// Open MX-Karma Session
$this -> mxKarmaOpenSession ();
$this -> mxKarma [ 'startTime' ] = time ();
2014-06-12 15:46:24 +02:00
//Check if Karma Code got specified, and inform admin that it would be good to specify one
2014-08-03 01:34:18 +02:00
$serverLogin = $this -> maniaControl -> getServer () -> login ;
2014-05-13 14:15:00 +02:00
$karmaSettingName = self :: buildKarmaSettingName ( $serverLogin );
2014-08-13 11:14:29 +02:00
$mxKarmaCode = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , $karmaSettingName );
2014-05-09 10:48:51 +02:00
2014-05-13 16:03:26 +02:00
if ( ! $mxKarmaCode ) {
2014-08-13 11:14:29 +02:00
$permission = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this -> maniaControl -> getAuthenticationManager (), PluginMenu :: SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS );
$this -> maniaControl -> getChat () -> sendErrorToAdmins ( " Please specify a Mania-Exchange Karma Key in the Karma-Plugin settings! " , $permission );
2014-05-09 10:48:51 +02:00
}
2014-05-03 21:57:38 +02:00
return true ;
}
/**
* Create necessary database tables
*/
private function initTables () {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-05-03 21:57:38 +02:00
// Create local table
$query = " CREATE TABLE IF NOT EXISTS ` " . self :: TABLE_KARMA . " ` (
`index` int ( 11 ) NOT NULL AUTO_INCREMENT ,
`mapIndex` int ( 11 ) NOT NULL ,
`playerIndex` int ( 11 ) NOT NULL ,
`vote` float NOT NULL DEFAULT '-1' ,
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY ( `index` ),
UNIQUE KEY `player_map_vote` ( `mapIndex` , `playerIndex` )
) ENGINE = MyISAM DEFAULT CHARSET = utf8 COMMENT = 'Save players map votes' AUTO_INCREMENT = 1 ; " ;
$mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error , E_USER_ERROR );
}
// Create mx table
$query = " CREATE TABLE IF NOT EXISTS ` " . self :: MX_IMPORT_TABLE . " ` (
`index` int ( 11 ) NOT NULL AUTO_INCREMENT ,
`mapIndex` int ( 11 ) NOT NULL ,
`mapImported` tinyint ( 1 ) NOT NULL ,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY ( `index` ),
UNIQUE KEY `mapIndex` ( `mapIndex` )
) ENGINE = MyISAM DEFAULT CHARSET = utf8 COMMENT = 'MX Karma Import Table' AUTO_INCREMENT = 1 ; " ;
$mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error , E_USER_ERROR );
}
}
/**
* Open a Mx Karma Session
*/
private function mxKarmaOpenSession () {
2017-03-26 13:58:23 +02:00
if ( ! $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
2014-05-03 21:57:38 +02:00
return ;
}
2014-08-03 01:34:18 +02:00
$serverLogin = $this -> maniaControl -> getServer () -> login ;
2014-05-13 14:15:00 +02:00
$karmaSettingName = self :: buildKarmaSettingName ( $serverLogin );
2014-08-13 11:14:29 +02:00
$mxKarmaCode = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , $karmaSettingName );
2014-05-03 21:57:38 +02:00
2014-06-14 14:32:29 +02:00
if ( ! $mxKarmaCode ) {
2014-05-03 21:57:38 +02:00
return ;
}
2014-07-28 14:39:12 +02:00
$appIdentifier = 'ManiaControl v' . ManiaControl :: VERSION ;
$testMode = 'true' ;
2014-05-03 21:57:38 +02:00
2014-06-17 23:35:56 +02:00
$query = self :: MX_KARMA_URL . self :: MX_KARMA_START_SESSION ;
2014-05-03 21:57:38 +02:00
$query .= '?serverLogin=' . $serverLogin ;
2014-07-28 14:39:12 +02:00
$query .= '&applicationIdentifier=' . urlencode ( $appIdentifier );
2014-05-03 21:57:38 +02:00
$query .= '&testMode=' . $testMode ;
$this -> mxKarma [ 'connectionInProgress' ] = true ;
2017-03-30 20:27:57 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , $query );
$asyncHttpRequest -> setContentType ( AsyncHttpRequest :: CONTENT_TYPE_JSON );
$asyncHttpRequest -> setCallable ( function ( $json , $error ) use ( $mxKarmaCode ) {
2017-03-26 13:58:23 +02:00
$this -> mxKarma [ 'connectionInProgress' ] = false ;
if ( $error ) {
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'mx karma error: ' . $error );
return ;
}
$data = json_decode ( $json );
if ( ! $data ) {
2017-03-30 20:27:57 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'auth error' . $json );
2017-03-26 13:58:23 +02:00
return ;
}
if ( $data -> success ) {
$this -> mxKarma [ 'session' ] = $data -> data ;
$this -> activateSession ( $mxKarmaCode );
} else {
Logger :: logError ( " Error while authenticating on Mania-Exchange Karma " );
2017-06-28 14:33:05 +02:00
if ( $data -> data -> message == " invalid server " ){
Logger :: log ( " You need to get a Karma Key from MX with registering your server " );
} else {
// TODO remove temp trigger
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'auth error ' . json_encode ( $data -> data -> message ));
}
2017-03-26 13:58:23 +02:00
$this -> mxKarma [ 'connectionInProgress' ] = false ;
}
2017-03-30 20:27:57 +02:00
});
$asyncHttpRequest -> getData ( 1000 );
2014-05-03 21:57:38 +02:00
}
/**
* Activates the MX - Karma Session
*
2014-05-13 16:40:05 +02:00
* @ param string $mxKarmaCode
2014-05-03 21:57:38 +02:00
*/
private function activateSession ( $mxKarmaCode ) {
$hash = $this -> buildActivationHash ( $this -> mxKarma [ 'session' ] -> sessionSeed , $mxKarmaCode );
2014-06-17 23:35:56 +02:00
$query = self :: MX_KARMA_URL . self :: MX_KARMA_ACTIVATE_SESSION ;
2014-05-03 21:57:38 +02:00
$query .= '?sessionKey=' . urlencode ( $this -> mxKarma [ 'session' ] -> sessionKey );
$query .= '&activationHash=' . urlencode ( $hash );
2017-03-30 20:27:57 +02:00
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , $query );
$asyncHttpRequest -> setContentType ( AsyncHttpRequest :: CONTENT_TYPE_JSON );
$asyncHttpRequest -> setCallable ( function ( $json , $error ) use ( $query ) {
2017-03-26 13:58:23 +02:00
$this -> mxKarma [ 'connectionInProgress' ] = false ;
if ( $error ) {
2017-03-30 20:27:57 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'mx karma error' . $error );
2017-03-26 13:58:23 +02:00
return ;
}
$data = json_decode ( $json );
if ( ! $data ) {
2017-03-30 20:27:57 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'parse error' . $json );
2017-03-26 13:58:23 +02:00
return ;
}
if ( $data -> success && $data -> data -> activated ) {
Logger :: log ( 'Successfully authenticated on Mania-Exchange Karma' );
// Fetch the Mx Karma Votes
$this -> getMxKarmaVotes ();
} else {
if ( $data -> data -> message === 'invalid hash' ) {
$permission = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this -> maniaControl -> getAuthenticationManager (), PluginMenu :: SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS );
$this -> maniaControl -> getChat () -> sendErrorToAdmins ( " Invalid Mania-Exchange Karma code in Karma Plugin specified! " , $permission );
} else {
2017-03-30 20:27:57 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'auth error' . $data -> data -> message . $query );
2017-03-26 13:58:23 +02:00
}
Logger :: logError ( " Error while activating Mania-Exchange Karma Session: " . $data -> data -> message );
unset ( $this -> mxKarma [ 'session' ]);
}
2017-03-30 20:27:57 +02:00
});
$asyncHttpRequest -> getData ( 1000 );
2014-05-03 21:57:38 +02:00
}
/**
* Builds a sha512 activation Hash for the MX - Karma
*
2014-05-13 16:40:05 +02:00
* @ param string $sessionSeed
* @ param string $mxKey
2014-05-03 21:57:38 +02:00
* @ return string
*/
private function buildActivationHash ( $sessionSeed , $mxKey ) {
return hash ( 'sha512' , $mxKey . $sessionSeed );
}
/**
* Fetch the mxKarmaVotes for the current map
*/
public function getMxKarmaVotes ( Player $player = null ) {
2017-03-26 13:58:23 +02:00
if ( ! $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
2014-05-03 21:57:38 +02:00
return ;
}
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
return ;
}
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-03 21:57:38 +02:00
$properties = array ();
2014-08-13 11:14:29 +02:00
$gameMode = $this -> maniaControl -> getServer () -> getGameMode ( true );
2014-06-14 14:32:29 +02:00
if ( $gameMode === 'Script' ) {
2014-08-13 11:14:29 +02:00
$scriptName = $this -> maniaControl -> getClient () -> getScriptName ();
2014-07-28 14:39:12 +02:00
$properties [ 'gamemode' ] = $scriptName [ 'CurrentValue' ];
2014-05-03 21:57:38 +02:00
} else {
$properties [ 'gamemode' ] = $gameMode ;
}
2014-08-03 01:34:18 +02:00
$properties [ 'titleid' ] = $this -> maniaControl -> getServer () -> titleId ;
2014-05-03 21:57:38 +02:00
$properties [ 'mapuid' ] = $map -> uid ;
if ( ! $player ) {
$properties [ 'getvotesonly' ] = false ;
$properties [ 'playerlogins' ] = array ();
2014-08-13 11:14:29 +02:00
foreach ( $this -> maniaControl -> getPlayerManager () -> getPlayers () as $loopPlayer ) {
2014-05-13 18:26:38 +02:00
$properties [ 'playerlogins' ][] = $loopPlayer -> login ;
2014-05-03 21:57:38 +02:00
}
} else {
$properties [ 'getvotesonly' ] = true ;
$properties [ 'playerlogins' ] = array ( $player -> login );
}
$content = json_encode ( $properties );
2017-03-26 20:21:23 +02:00
$url = self :: MX_KARMA_URL . self :: MX_KARMA_GET_MAP_RATING . '?sessionKey=' . urlencode ( $this -> mxKarma [ 'session' ] -> sessionKey );
$asyncHttpRequest = new AsyncHttpRequest ( $this -> maniaControl , $url );
$asyncHttpRequest -> setContent ( $content );
$asyncHttpRequest -> setContentType ( $asyncHttpRequest :: CONTENT_TYPE_JSON );
$asyncHttpRequest -> setCallable ( function ( $json , $error ) use (
2017-03-26 13:58:23 +02:00
& $player
) {
if ( $error ) {
2017-03-26 20:21:23 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'mx karma error' . $error );
2017-03-26 13:58:23 +02:00
return ;
}
$data = json_decode ( $json );
if ( ! $data ) {
2017-03-26 20:21:23 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'parse error' . $json );
2017-03-26 13:58:23 +02:00
return ;
}
if ( $data -> success ) {
// Fetch averages if it's for the whole server
if ( ! $player ) {
$this -> mxKarma [ 'voteCount' ] = $data -> data -> votecount ;
$this -> mxKarma [ 'voteAverage' ] = $data -> data -> voteaverage ;
$this -> mxKarma [ 'modeVoteCount' ] = $data -> data -> modevotecount ;
$this -> mxKarma [ 'modeVoteAverage' ] = $data -> data -> modevoteaverage ;
}
foreach ( $data -> data -> votes as $votes ) {
$this -> mxKarma [ 'votes' ][ $votes -> login ] = $votes -> vote ;
}
$this -> updateManialink = true ;
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_KARMA_MXUPDATED , $this -> mxKarma );
Logger :: logInfo ( 'MX-Karma Votes successfully fetched!' );
} else {
// Problem occurred
Logger :: logError ( 'Error while fetching votes: ' . $data -> data -> message );
if ( $data -> data -> message === 'invalid session' ) {
unset ( $this -> mxKarma [ 'session' ]);
} else {
2017-06-22 17:04:58 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'fetch error ' . json_encode ( $data -> data -> message . self :: MX_KARMA_GET_MAP_RATING ));
2017-03-26 13:58:23 +02:00
}
}
2017-03-26 20:21:23 +02:00
});
$asyncHttpRequest -> postData ();
2014-05-03 21:57:38 +02:00
}
/**
* @ see \ManiaControl\Plugins\Plugin :: unload ()
*/
public function unload () {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getManialinkManager () -> hideManialink ( self :: MLID_KARMA );
2014-05-03 21:57:38 +02:00
}
/**
2014-06-14 15:48:27 +02:00
* Handle Begin Map Callback
2014-05-03 21:57:38 +02:00
*/
2014-06-14 15:48:27 +02:00
public function handleBeginMap () {
2014-05-03 21:57:38 +02:00
// send Map Karma to MX from previous Map
if ( isset ( $this -> mxKarma [ 'map' ])) {
$votes = array ();
foreach ( $this -> mxKarma [ 'votes' ] as $login => $value ) {
2014-08-13 11:14:29 +02:00
$player = $this -> maniaControl -> getPlayerManager () -> getPlayer ( $login );
2014-07-28 14:39:12 +02:00
array_push ( $votes , array ( 'login' => $login , 'nickname' => $player -> rawNickname , 'vote' => $value ));
2014-05-03 21:57:38 +02:00
}
$this -> postKarmaVotes ( $this -> mxKarma [ 'map' ], $votes );
unset ( $this -> mxKarma [ 'map' ]);
}
unset ( $this -> mxKarma [ 'votes' ]);
$this -> mxKarma [ 'startTime' ] = time ();
$this -> updateManialink = true ;
// Get Karma votes at begin of map
$this -> getMxKarmaVotes ();
}
/**
* Post the Karma votes to MX - Karma
*
* @ param Map $map
* @ param array $votes
* @ param bool $import
*/
private function postKarmaVotes ( Map $map , array $votes , $import = false ) {
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
return ;
}
2014-08-13 11:14:29 +02:00
$gameMode = $this -> maniaControl -> getServer () -> getGameMode ( true );
2014-05-03 21:57:38 +02:00
2014-06-14 14:32:29 +02:00
if ( empty ( $votes )) {
2014-05-03 21:57:38 +02:00
return ;
}
$properties = array ();
2014-06-14 14:32:29 +02:00
if ( $gameMode === 'Script' ) {
2014-08-13 11:14:29 +02:00
$scriptName = $this -> maniaControl -> getClient () -> getScriptName ();
2014-07-28 14:39:12 +02:00
$properties [ 'gamemode' ] = $scriptName [ 'CurrentValue' ];
2014-05-03 21:57:38 +02:00
} else {
$properties [ 'gamemode' ] = $gameMode ;
}
if ( $import ) {
$properties [ 'maptime' ] = 0 ;
} else {
$properties [ 'maptime' ] = time () - $this -> mxKarma [ 'startTime' ];
}
$properties [ 'votes' ] = $votes ;
2014-08-03 01:34:18 +02:00
$properties [ 'titleid' ] = $this -> maniaControl -> getServer () -> titleId ;
2014-05-03 21:57:38 +02:00
$properties [ 'mapname' ] = $map -> rawName ;
$properties [ 'mapuid' ] = $map -> uid ;
$properties [ 'mapauthor' ] = $map -> authorLogin ;
$properties [ 'isimport' ] = $import ;
$content = json_encode ( $properties );
2017-03-26 20:21:23 +02:00
$url = self :: MX_KARMA_URL . self :: MX_KARMA_SAVE_VOTES . " ?sessionKey= " . urlencode ( $this -> mxKarma [ 'session' ] -> sessionKey );
$asyncRequest = new AsyncHttpRequest ( $this -> maniaControl , $url );
$asyncRequest -> setContent ( $content );
$asyncRequest -> setContentType ( $asyncRequest :: CONTENT_TYPE_JSON );
$asyncRequest -> setCallable ( function ( $json , $error ) {
2017-03-26 13:58:23 +02:00
if ( $error ) {
2017-03-26 20:21:23 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'mx karma error ' . $error );
2017-03-26 13:58:23 +02:00
return ;
}
$data = json_decode ( $json );
if ( ! $data ) {
2017-03-26 20:21:23 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'parse error ' . $json );
2017-03-26 13:58:23 +02:00
return ;
}
if ( $data -> success ) {
Logger :: logInfo ( 'Votes successfully submitted!' );
} else {
// Problem occurred
Logger :: logError ( " Error while updating votes: ' { $data -> data -> message } ' " );
if ( $data -> data -> message === " invalid session " ) {
unset ( $this -> mxKarma [ 'session' ]);
} else {
2017-06-22 17:04:58 +02:00
$this -> maniaControl -> getErrorHandler () -> triggerDebugNotice ( 'saving error ' . json_encode ( $data -> data -> message . self :: MX_KARMA_SAVE_VOTES ));
2017-03-26 13:58:23 +02:00
}
}
2017-03-26 20:21:23 +02:00
});
$asyncRequest -> postData ();
2014-05-03 21:57:38 +02:00
}
/**
* Handle PlayerConnect callback
*
2014-07-28 14:39:12 +02:00
* @ param Player $player
2014-05-03 21:57:38 +02:00
*/
public function handlePlayerConnect ( Player $player ) {
if ( ! $player ) {
return ;
}
$this -> queryManialinkUpdateFor ( $player );
// Get Mx Karma Vote for Player
$this -> getMxKarmaVotes ( $player );
}
/**
* Query the player to update the manialink
*
* @ param Player $player
*/
private function queryManialinkUpdateFor ( Player $player ) {
if ( $this -> updateManialink === true ) {
return ;
}
if ( ! is_array ( $this -> updateManialink )) {
$this -> updateManialink = array ();
}
$this -> updateManialink [ $player -> login ] = $player ;
}
/**
* Handle PlayerChat callback
*
* @ param array $chatCallback
*/
public function handlePlayerChat ( array $chatCallback ) {
$login = $chatCallback [ 1 ][ 1 ];
2014-08-13 11:14:29 +02:00
$player = $this -> maniaControl -> getPlayerManager () -> getPlayer ( $login );
2014-05-03 21:57:38 +02:00
if ( ! $player ) {
return ;
}
$message = $chatCallback [ 1 ][ 2 ];
if ( $chatCallback [ 1 ][ 3 ]) {
$message = substr ( $message , 1 );
}
if ( preg_match ( '/[^+-]/' , $message )) {
return ;
}
$countPositive = substr_count ( $message , '+' );
$countNegative = substr_count ( $message , '-' );
if ( $countPositive <= 0 && $countNegative <= 0 ) {
return ;
}
$vote = $countPositive - $countNegative ;
$success = $this -> handleVote ( $player , $vote );
if ( ! $success ) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getChat () -> sendError ( 'Error occurred.' , $player -> login );
2014-05-03 21:57:38 +02:00
return ;
}
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getChat () -> sendSuccess ( 'Vote updated!' , $player -> login );
2014-05-03 21:57:38 +02:00
}
/**
* Handle a vote done by a player
*
* @ param Player $player
* @ param int $vote
* @ return bool
*/
private function handleVote ( Player $player , $vote ) {
// Check vote
2014-08-13 11:14:29 +02:00
$votesSetting = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_AVAILABLE_VOTES );
2014-05-03 21:57:38 +02:00
$votes = explode ( ',' , $votesSetting );
$voteLow = intval ( $votes [ 0 ]);
$voteHigh = $voteLow + 2 ;
if ( isset ( $votes [ 1 ])) {
$voteHigh = intval ( $votes [ 1 ]);
}
if ( $vote < $voteLow || $vote > $voteHigh ) {
return false ;
}
// Calculate actual voting
2017-03-26 13:58:23 +02:00
$vote -= $voteLow ;
2014-05-03 21:57:38 +02:00
$voteHigh -= $voteLow ;
2017-03-26 13:58:23 +02:00
$vote /= $voteHigh ;
2014-05-03 21:57:38 +02:00
// Save vote
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-03 21:57:38 +02:00
// Update vote in MX karma array
2014-08-13 11:14:29 +02:00
if ( $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MX_KARMA_ACTIVATED )
2014-08-05 02:17:41 +02:00
&& isset ( $this -> mxKarma [ 'session' ])
) {
2014-06-29 17:24:01 +02:00
if ( ! isset ( $this -> mxKarma [ 'votes' ][ $player -> login ])) {
if ( ! isset ( $this -> mxKarma [ 'voteCount' ])) {
$this -> mxKarma [ 'voteCount' ] = 0 ;
}
if ( ! isset ( $this -> mxKarma [ 'voteAverage' ])) {
$this -> mxKarma [ 'voteAverage' ] = 0. ;
}
$sum = $this -> mxKarma [ 'voteCount' ] * $this -> mxKarma [ 'voteAverage' ] + $vote * 100 ;
$this -> mxKarma [ 'voteCount' ] ++ ;
2014-05-03 21:57:38 +02:00
2014-06-29 17:24:01 +02:00
if ( ! isset ( $this -> mxKarma [ 'modeVoteCount' ])) {
$this -> mxKarma [ 'modeVoteCount' ] = 0 ;
}
if ( ! isset ( $this -> mxKarma [ 'modeVoteAverage' ])) {
$this -> mxKarma [ 'modeVoteAverage' ] = 0 ;
}
$modeSum = $this -> mxKarma [ 'modeVoteCount' ] * $this -> mxKarma [ 'modeVoteAverage' ] + $vote * 100 ;
$this -> mxKarma [ 'modeVoteCount' ] ++ ;
2014-05-03 21:57:38 +02:00
} else {
2014-06-29 17:24:01 +02:00
$oldVote = $this -> mxKarma [ 'votes' ][ $player -> login ];
$sum = $this -> mxKarma [ 'voteCount' ] * $this -> mxKarma [ 'voteAverage' ] - $oldVote + $vote * 100 ;
$modeSum = $this -> mxKarma [ 'modeVoteCount' ] * $this -> mxKarma [ 'modeVoteAverage' ] - $oldVote + $vote * 100 ;
2014-05-03 21:57:38 +02:00
}
2014-05-10 10:12:48 +02:00
//FIXME, how can here ever be division by zero?, a voting just happened before, and a vote of a player is set
//edit problem is if someone votes on one server (on a map which has no votes yet, joins another server than where same map is running and votes again)
2014-06-29 17:24:01 +02:00
$this -> mxKarma [ 'voteAverage' ] = $sum / $this -> mxKarma [ 'voteCount' ];
$this -> mxKarma [ 'modeVoteAverage' ] = $modeSum / $this -> mxKarma [ 'modeVoteCount' ];
$this -> mxKarma [ 'votes' ][ $player -> login ] = $vote * 100 ;
2014-05-03 21:57:38 +02:00
}
$voted = $this -> getPlayerVote ( $player , $map );
if ( ! $voted ) {
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getStatisticManager () -> incrementStat ( self :: STAT_PLAYER_MAPVOTES , $player , $this -> maniaControl -> getServer () -> index );
2014-05-03 21:57:38 +02:00
}
$success = $this -> savePlayerVote ( $player , $map , $vote );
if ( ! $success ) {
return false ;
}
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getCallbackManager () -> triggerCallback ( self :: CB_KARMA_CHANGED );
2014-05-03 21:57:38 +02:00
$this -> updateManialink = true ;
return true ;
}
/**
* Get the current vote of the player for the map
*
* @ param Player $player
* @ param Map $map
* @ return int
*/
public function getPlayerVote ( Player $player , Map $map ) {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-05-03 21:57:38 +02:00
$query = " SELECT * FROM ` " . self :: TABLE_KARMA . " `
WHERE `playerIndex` = { $player -> index }
AND `mapIndex` = { $map -> index }
AND `vote` >= 0 ; " ;
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return false ;
}
if ( $result -> num_rows <= 0 ) {
$result -> free ();
return false ;
}
$item = $result -> fetch_object ();
$result -> free ();
$vote = $item -> vote ;
return floatval ( $vote );
}
/**
* Save the vote of the player for the map
*
* @ param Player $player
* @ param Map $map
* @ param float $vote
* @ return bool
*/
private function savePlayerVote ( Player $player , Map $map , $vote ) {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-05-03 21:57:38 +02:00
$query = " INSERT INTO ` " . self :: TABLE_KARMA . " ` (
`mapIndex` ,
`playerIndex` ,
`vote`
) VALUES (
{ $map -> index },
{ $player -> index },
{ $vote }
) ON DUPLICATE KEY UPDATE
`vote` = VALUES ( `vote` ); " ;
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return false ;
}
return $result ;
}
/**
* Get all players votes
*
* @ param Map $map
* @ return array
*/
public function getMapPlayerVotes ( Map $map ) {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-05-03 21:57:38 +02:00
$query = " SELECT * FROM ` " . self :: TABLE_KARMA . " `
WHERE `mapIndex` = { $map -> index }
2014-08-03 01:34:18 +02:00
AND `vote` >= 0 ; " ;
2014-05-03 21:57:38 +02:00
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return false ;
}
$votes = array ();
while ( $vote = $result -> fetch_object ()) {
2014-08-13 11:14:29 +02:00
$player = $this -> maniaControl -> getPlayerManager () -> getPlayerByIndex ( $vote -> playerIndex );
2014-06-14 15:48:27 +02:00
$karma = $vote -> vote ;
$voteArray = array ( 'player' => $player , 'karma' => $karma );
array_push ( $votes , $voteArray );
2014-05-03 21:57:38 +02:00
}
2014-06-14 15:48:27 +02:00
usort ( $votes , function ( $paramA , $paramB ) {
return $paramA [ 'karma' ] - $paramB [ 'karma' ];
2014-05-03 21:57:38 +02:00
});
$votes = array_reverse ( $votes );
return $votes ;
}
/**
2014-05-13 14:15:00 +02:00
* Update Setting
2014-05-03 21:57:38 +02:00
*
2014-05-13 14:15:00 +02:00
* @ param Setting $setting
2014-05-03 21:57:38 +02:00
*/
2014-05-13 14:15:00 +02:00
public function updateSettings ( Setting $setting ) {
if ( ! $setting -> belongsToClass ( $this )) {
2014-05-03 21:57:38 +02:00
return ;
}
2014-08-03 01:34:18 +02:00
$serverLogin = $this -> maniaControl -> getServer () -> login ;
2014-05-13 14:15:00 +02:00
$karmaSettingName = self :: buildKarmaSettingName ( $serverLogin );
2014-05-03 21:57:38 +02:00
2014-05-13 14:15:00 +02:00
switch ( $setting -> setting ) {
2017-03-26 13:58:23 +02:00
case $karmaSettingName : {
2014-05-13 14:15:00 +02:00
$this -> mxKarmaOpenSession ();
break ;
}
2017-03-26 13:58:23 +02:00
case self :: SETTING_WIDGET_ENABLE : {
2014-05-13 14:15:00 +02:00
if ( $setting -> value ) {
$this -> updateManialink = true ;
2017-05-12 20:15:37 +02:00
$this -> handle1Second ();
2014-05-13 14:15:00 +02:00
} else {
$this -> updateManialink = false ;
2014-08-13 11:14:29 +02:00
$this -> maniaControl -> getManialinkManager () -> hideManialink ( self :: MLID_KARMA );
2014-05-13 14:15:00 +02:00
}
break ;
}
2014-05-03 21:57:38 +02:00
}
}
/**
2014-05-13 16:40:05 +02:00
* Handle ManiaControl 1 Second Callback
2014-05-03 21:57:38 +02:00
*/
2014-05-13 16:40:05 +02:00
public function handle1Second () {
2014-05-03 21:57:38 +02:00
if ( ! $this -> updateManialink ) {
return ;
}
2014-08-13 11:14:29 +02:00
$displayMxKarma = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_DISPLAY_MX );
2014-05-03 21:57:38 +02:00
// Get players
$players = $this -> updateManialink ;
if ( $players === true ) {
2014-08-13 11:14:29 +02:00
$players = $this -> maniaControl -> getPlayerManager () -> getPlayers ();
2014-05-03 21:57:38 +02:00
}
$this -> updateManialink = false ;
// Get map karma
2014-08-13 11:14:29 +02:00
$map = $this -> maniaControl -> getMapManager () -> getCurrentMap ();
2014-05-03 21:57:38 +02:00
2014-05-13 16:03:26 +02:00
// Display the mx Karma if the setting is chosen and the MX session is available
2014-05-03 21:57:38 +02:00
if ( $displayMxKarma && isset ( $this -> mxKarma [ 'session' ]) && isset ( $this -> mxKarma [ 'voteCount' ])) {
$karma = $this -> mxKarma [ 'modeVoteAverage' ] / 100 ;
$voteCount = $this -> mxKarma [ 'modeVoteCount' ];
} else {
$karma = $this -> getMapKarma ( $map );
$votes = $this -> getMapVotes ( $map );
$voteCount = $votes [ 'count' ];
}
2017-03-26 13:58:23 +02:00
if ( $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_ENABLE )) {
2014-05-03 21:57:38 +02:00
// Build karma manialink
$this -> buildManialink ();
// Update karma gauge & label
/**
* @ var Gauge $karmaGauge
*/
$karmaGauge = $this -> manialink -> karmaGauge ;
/**
* @ var Label $karmaLabel
*/
$karmaLabel = $this -> manialink -> karmaLabel ;
if ( is_numeric ( $karma ) && $voteCount > 0 ) {
$karma = floatval ( $karma );
$karmaGauge -> setRatio ( $karma + 0.15 - $karma * 0.15 );
$karmaColor = ColorUtil :: floatToStatusColor ( $karma );
$karmaGauge -> setColor ( $karmaColor . '7' );
$karmaLabel -> setText ( ' ' . round ( $karma * 100. ) . '% (' . $voteCount . ')' );
} else {
$karmaGauge -> setRatio ( 0. );
$karmaGauge -> setColor ( '00fb' );
$karmaLabel -> setText ( '-' );
}
2017-05-09 13:15:16 +02:00
$this -> maniaControl -> getManialinkManager () -> sendManialink ( $this -> manialink , $players );
// TODO: show the player his own vote in some way
// $vote = $this->getPlayerVote($player, $map);
// $votesFrame = $this->manialink->votesFrame;
// $votesFrame->removeChildren();
2014-05-03 21:57:38 +02:00
}
}
/**
* Get the current karma of the map
*
* @ param Map $map
* @ return float | bool
*/
public function getMapKarma ( Map $map ) {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-05-03 21:57:38 +02:00
$query = " SELECT AVG(`vote`) AS `karma` FROM ` " . self :: TABLE_KARMA . " `
WHERE `mapIndex` = { $map -> index }
AND `vote` >= 0 ; " ;
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return false ;
}
if ( $result -> num_rows <= 0 ) {
$result -> free ();
return false ;
}
$item = $result -> fetch_object ();
$result -> free ();
$karma = $item -> karma ;
if ( $karma === null ) {
return false ;
}
return floatval ( $karma );
}
/**
* Get the current Votes for the Map
*
* @ param Map $map
* @ return array
*/
public function getMapVotes ( Map $map ) {
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-05-03 21:57:38 +02:00
$query = " SELECT `vote`, COUNT(`vote`) AS `count` FROM ` " . self :: TABLE_KARMA . " `
WHERE `mapIndex` = { $map -> index }
AND `vote` >= 0
GROUP BY `vote` ; " ;
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return false ;
}
$votes = array ();
$count = 0 ;
while ( $vote = $result -> fetch_object ()) {
$votes [ $vote -> vote ] = $vote ;
2017-03-26 13:58:23 +02:00
$count += $vote -> count ;
2014-05-03 21:57:38 +02:00
}
$votes [ 'count' ] = $count ;
$result -> free ();
return $votes ;
}
/**
2014-06-14 15:48:27 +02:00
* Build Karma Voting Manialink if necessary
2014-05-03 21:57:38 +02:00
*
* @ param bool $forceBuild
*/
private function buildManialink ( $forceBuild = false ) {
if ( is_object ( $this -> manialink ) && ! $forceBuild ) {
return ;
}
2014-08-13 11:14:29 +02:00
$title = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_TITLE );
$posX = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_POSX );
$posY = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_POSY );
$width = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_WIDTH );
$height = $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_WIDGET_HEIGHT );
$labelStyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultLabelStyle ();
$quadStyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultQuadStyle ();
$quadSubstyle = $this -> maniaControl -> getManialinkManager () -> getStyleManager () -> getDefaultQuadSubstyle ();
2014-05-03 21:57:38 +02:00
$manialink = new ManiaLink ( self :: MLID_KARMA );
$frame = new Frame ();
2017-03-25 19:15:50 +01:00
$manialink -> addChild ( $frame );
2014-06-14 15:48:27 +02:00
$frame -> setPosition ( $posX , $posY );
2014-05-03 21:57:38 +02:00
$backgroundQuad = new Quad ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $backgroundQuad );
2014-05-03 21:57:38 +02:00
$backgroundQuad -> setY ( $height * 0.15 );
$backgroundQuad -> setSize ( $width , $height );
$backgroundQuad -> setStyles ( $quadStyle , $quadSubstyle );
$titleLabel = new Label ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $titleLabel );
2014-05-03 21:57:38 +02:00
$titleLabel -> setY ( $height * 0.36 );
$titleLabel -> setWidth ( $width * 0.85 );
$titleLabel -> setStyle ( $labelStyle );
$titleLabel -> setTranslate ( true );
$titleLabel -> setTextSize ( 1 );
$titleLabel -> setScale ( 0.90 );
$titleLabel -> setText ( $title );
$karmaGauge = new Gauge ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $karmaGauge );
2014-05-03 21:57:38 +02:00
$karmaGauge -> setSize ( $width * 0.95 , $height * 0.92 );
2017-03-25 19:15:50 +01:00
$karmaGauge -> setDrawBackground ( false );
2014-05-03 21:57:38 +02:00
$manialink -> karmaGauge = $karmaGauge ;
$karmaLabel = new Label ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $karmaLabel );
2014-05-03 21:57:38 +02:00
$karmaLabel -> setPosition ( 0 , - 0.4 , 1 );
$karmaLabel -> setSize ( $width * 0.9 , $height * 0.9 );
$karmaLabel -> setStyle ( $labelStyle );
$karmaLabel -> setTextSize ( 1 );
$manialink -> karmaLabel = $karmaLabel ;
$votesFrame = new Frame ();
2017-03-25 19:15:50 +01:00
$frame -> addChild ( $votesFrame );
2014-05-03 21:57:38 +02:00
$manialink -> votesFrame = $votesFrame ;
$this -> manialink = $manialink ;
}
/**
* Import old Karma votes to Mania - Exchange Karma
*
* @ param Map $map
*/
public function importMxKarmaVotes ( Map $map ) {
2017-03-26 13:58:23 +02:00
if ( ! $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
2014-05-03 21:57:38 +02:00
return ;
}
2017-03-26 13:58:23 +02:00
if ( ! $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MX_KARMA_IMPORTING )) {
2014-05-03 21:57:38 +02:00
return ;
}
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
return ;
}
2014-08-13 11:14:29 +02:00
$mysqli = $this -> maniaControl -> getDatabase () -> getMysqli ();
2014-07-28 14:39:12 +02:00
$query = " SELECT `mapImported` FROM ` " . self :: MX_IMPORT_TABLE . " `
WHERE `mapIndex` = { $map -> index }; " ;
2014-05-03 21:57:38 +02:00
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return ;
}
$vote = $result -> fetch_object ();
2014-06-14 14:32:29 +02:00
if ( ! $result -> field_count || ! $vote ) {
2014-07-28 14:39:12 +02:00
$query = " SELECT `vote`, `login`, `nickname`
FROM `" . self::TABLE_KARMA . "` k
LEFT JOIN `" . PlayerManager::TABLE_PLAYERS . "` p
ON k . playerIndex = p . index
WHERE `mapIndex` = { $map -> index }; " ;
2014-05-03 21:57:38 +02:00
$result2 = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return ;
}
$votes = array ();
while ( $row = $result2 -> fetch_object ()) {
2014-07-28 14:39:12 +02:00
array_push ( $votes , array ( 'login' => $row -> login , 'nickname' => $row -> nickname , 'vote' => $row -> vote * 100 ));
2014-05-03 21:57:38 +02:00
}
$this -> postKarmaVotes ( $map , $votes , true );
// Flag Map as Imported in database if it is a import
2014-07-28 14:39:12 +02:00
$query = " INSERT INTO ` " . self :: MX_IMPORT_TABLE . " ` (
`mapIndex` ,
`mapImported`
) VALUES (
{ $map -> index },
1
) ON DUPLICATE KEY UPDATE
`mapImported` = 1 ; " ;
2014-05-03 21:57:38 +02:00
$mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
}
$result2 -> free ();
}
2014-07-28 14:39:12 +02:00
$result -> free ();
2014-05-03 21:57:38 +02:00
return ;
}
/**
2014-05-13 18:26:38 +02:00
* Save Mx Karma Votes at MapEnd
2014-05-03 21:57:38 +02:00
*/
public function sendMxKarmaVotes ( Map $map ) {
2017-03-26 13:58:23 +02:00
if ( ! $this -> maniaControl -> getSettingManager () -> getSettingValue ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
2014-05-03 21:57:38 +02:00
return ;
}
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
return ;
}
2014-06-14 14:32:29 +02:00
if ( ! isset ( $this -> mxKarma [ 'votes' ]) || empty ( $this -> mxKarma [ 'votes' ])) {
2014-05-03 21:57:38 +02:00
return ;
}
$this -> mxKarma [ 'map' ] = $map ;
}
}