2013-11-28 01:12:52 +01:00
< ? php
2014-01-03 19:31:30 +01:00
use FML\Controls\Frame ;
use FML\Controls\Gauge ;
use FML\Controls\Label ;
use FML\Controls\Quad ;
use FML\ManiaLink ;
2013-11-28 01:12:52 +01:00
use ManiaControl\Callbacks\CallbackListener ;
use ManiaControl\Callbacks\CallbackManager ;
2014-01-31 00:04:40 +01:00
use ManiaControl\Callbacks\TimerListener ;
2014-01-03 19:31:30 +01:00
use ManiaControl\ColorUtil ;
use ManiaControl\ManiaControl ;
2013-11-28 01:12:52 +01:00
use ManiaControl\Maps\Map ;
2014-02-23 23:27:30 +01:00
use ManiaControl\Maps\MapManager ;
2013-11-28 01:12:52 +01:00
use ManiaControl\Players\Player ;
2014-02-24 21:54:43 +01:00
use ManiaControl\Players\PlayerManager ;
2013-11-28 01:12:52 +01:00
use ManiaControl\Plugins\Plugin ;
2014-03-12 13:53:21 +01:00
use ManiaControl\Settings\SettingManager ;
2013-11-28 01:12:52 +01:00
/**
* ManiaControl Karma Plugin
*
2014-04-13 14:51:18 +02:00
* @ author kremsy and steeffeen
2014-04-13 12:00:08 +02:00
* @ copyright ManiaControl Copyright © 2014 ManiaControl Team
2014-04-13 14:51:18 +02:00
* @ license http :// www . gnu . org / licenses / GNU General Public License , Version 3
2013-11-28 01:12:52 +01:00
*/
2014-01-31 00:04:40 +01:00
class KarmaPlugin implements CallbackListener , TimerListener , Plugin {
2013-11-28 01:12:52 +01:00
/**
* Constants
*/
2014-02-25 18:53:43 +01:00
const ID = 2 ;
2014-01-03 19:31:30 +01:00
const VERSION = 0.1 ;
const MLID_KARMA = 'KarmaPlugin.MLID' ;
const TABLE_KARMA = 'mc_karma' ;
2014-04-19 15:41:54 +02:00
const CB_KARMA_CHANGED = 'KarmaPlugin.Changed' ;
2014-04-19 16:54:30 +02:00
const CB_KARMA_MXUPDATED = 'KarmaPlugin.MXUpdated' ;
2013-11-28 01:12:52 +01:00
const SETTING_AVAILABLE_VOTES = 'Available Votes (X-Y: Comma separated)' ;
2014-04-19 15:41:54 +02:00
const SETTING_WIDGET_ENABLE = 'Enable Karma Widget' ;
2014-01-03 19:31:30 +01:00
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 STAT_PLAYER_MAPVOTES = 'Voted Maps' ;
2014-02-23 23:27:30 +01:00
/**
* Constants MX Karma
*/
2014-04-13 14:51:18 +02:00
const SETTING_WIDGET_DISPLAY_MX = 'Display MX-Karma in Widget' ;
2014-02-24 22:07:51 +01:00
const SETTING_MX_KARMA_ACTIVATED = 'Activate MX-Karma' ;
2014-02-24 21:54:43 +01:00
const SETTING_MX_KARMA_IMPORTING = 'Import old MX-Karmas' ;
2014-03-07 18:28:53 +01:00
const MX_IMPORT_TABLE = 'mc_karma_mximport' ;
const MX_KARMA_URL = 'http://karma.mania-exchange.com/api2/' ;
const MX_KARMA_STARTSESSION = 'startSession' ;
const MX_KARMA_ACTIVATESESSION = 'activateSession' ;
const MX_KARMA_SAVEVOTES = 'saveVotes' ;
2014-04-13 14:51:18 +02:00
const MX_KARMA_GETMAPRATING = 'getMapRating' ;
2014-02-23 23:27:30 +01:00
2013-11-28 01:12:52 +01:00
/**
* Private properties
*/
2013-12-31 11:53:10 +01:00
/**
* @ var maniaControl $maniaControl
*/
2013-12-14 23:29:17 +01:00
private $maniaControl = null ;
2013-11-28 01:12:52 +01:00
private $updateManialink = false ;
2014-03-02 18:42:45 +01:00
/** @var ManiaLink $manialink */
2013-11-28 01:12:52 +01:00
private $manialink = null ;
2014-02-23 23:27:30 +01:00
private $mxKarma = array ();
2014-01-27 20:39:10 +01:00
/**
* Prepares the Plugin
*
* @ param ManiaControl $maniaControl
* @ return mixed
*/
public static function prepare ( ManiaControl $maniaControl ) {
2014-02-23 23:31:28 +01:00
$maniaControl -> settingManager -> initSetting ( get_class (), self :: SETTING_MX_KARMA_ACTIVATED , true );
2014-02-24 21:54:43 +01:00
$maniaControl -> settingManager -> initSetting ( get_class (), self :: SETTING_MX_KARMA_IMPORTING , true );
2014-04-13 14:51:18 +02:00
$maniaControl -> settingManager -> initSetting ( get_class (), self :: SETTING_WIDGET_DISPLAY_MX , true );
2014-02-23 23:27:30 +01:00
$servers = $maniaControl -> server -> getAllServers ();
foreach ( $servers as $server ) {
2014-02-24 11:08:15 +01:00
$maniaControl -> settingManager -> initSetting ( get_class (), '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $server -> login . ']MX Karma Code for ' . $server -> login . '$l' , '' );
2014-02-23 23:27:30 +01:00
}
2014-01-27 20:39:10 +01:00
}
2013-11-28 01:12:52 +01:00
/**
2013-12-14 23:29:17 +01:00
* @ see \ManiaControl\Plugins\Plugin :: load ()
2013-11-28 01:12:52 +01:00
*/
2013-12-14 23:29:17 +01:00
public function load ( ManiaControl $maniaControl ) {
2013-11-28 01:12:52 +01:00
$this -> maniaControl = $maniaControl ;
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
// Init database
$this -> initTables ();
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
// Init settings
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_AVAILABLE_VOTES , '-2,2' );
2014-04-19 15:41:54 +02:00
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_ENABLE , true );
2013-11-28 01:12:52 +01:00
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_TITLE , 'Map-Karma' );
2014-03-02 10:41:51 +01:00
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_POSX , 160 - 27.5 );
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_POSY , 90 - 10 - 6 );
2013-11-28 01:12:52 +01:00
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_WIDTH , 25. );
2014-03-02 10:41:51 +01:00
$this -> maniaControl -> settingManager -> initSetting ( $this , self :: SETTING_WIDGET_HEIGHT , 12. );
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
// Register for callbacks
2014-01-31 00:04:40 +01:00
$this -> maniaControl -> timerManager -> registerTimerListening ( $this , 'handle1Second' , 1000 );
2014-02-19 16:27:56 +01:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( MapManager :: CB_BEGINMAP , $this , 'handleBeginMap' );
2014-02-24 21:54:43 +01:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( MapManager :: CB_BEGINMAP , $this , 'importMxKarmaVotes' );
$this -> maniaControl -> callbackManager -> registerCallbackListener ( MapManager :: CB_ENDMAP , $this , 'sendMxKarmaVotes' );
2014-03-02 11:25:47 +01:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( PlayerManager :: CB_PLAYERCONNECT , $this , 'handlePlayerConnect' );
2013-11-28 01:12:52 +01:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( CallbackManager :: CB_MP_PLAYERCHAT , $this , 'handlePlayerChat' );
2014-03-12 13:53:21 +01:00
$this -> maniaControl -> callbackManager -> registerCallbackListener ( SettingManager :: CB_SETTINGS_CHANGED , $this , 'updateSettings' );
2014-01-03 19:31:30 +01:00
// Define player stats
$this -> maniaControl -> statisticManager -> defineStatMetaData ( self :: STAT_PLAYER_MAPVOTES );
2014-01-29 20:51:53 +01:00
// Register Stat in Simple StatsList
$this -> maniaControl -> statisticManager -> simpleStatsList -> registerStat ( self :: STAT_PLAYER_MAPVOTES , 100 , " VM " );
2014-01-18 10:26:32 +01:00
$this -> updateManialink = true ;
2014-02-23 23:27:30 +01:00
2014-04-13 14:51:18 +02:00
//Open MX-Karma Session
2014-02-23 23:27:30 +01:00
$this -> mxKarmaOpenSession ();
2014-03-02 18:42:45 +01:00
$this -> mxKarma [ 'startTime' ] = time ();
2013-12-14 23:29:17 +01:00
return true ;
2013-11-28 01:12:52 +01:00
}
2013-12-14 23:29:17 +01:00
/**
* @ see \ManiaControl\Plugins\Plugin :: unload ()
*/
public function unload () {
2014-01-18 10:32:33 +01:00
$emptyManialink = new ManiaLink ( self :: MLID_KARMA );
2014-03-02 13:00:23 +01:00
$this -> maniaControl -> manialinkManager -> sendManialink ( $emptyManialink );
2013-12-14 23:29:17 +01:00
$this -> maniaControl -> callbackManager -> unregisterCallbackListener ( $this );
2014-02-01 14:50:24 +01:00
$this -> maniaControl -> timerManager -> unregisterTimerListenings ( $this );
2013-12-14 23:29:17 +01:00
unset ( $this -> maniaControl );
}
2013-12-09 10:04:22 +01:00
/**
* @ see \ManiaControl\Plugins\Plugin :: getId ()
*/
public static function getId () {
return self :: ID ;
}
2013-12-14 23:29:17 +01:00
2013-12-03 22:21:17 +01:00
/**
* @ see \ManiaControl\Plugins\Plugin :: getName ()
*/
public static function getName () {
return 'Karma Plugin' ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getVersion ()
*/
public static function getVersion () {
return self :: VERSION ;
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getAuthor ()
*/
public static function getAuthor () {
2014-02-24 09:11:48 +01:00
return 'steeffeen and kremsy' ;
2013-12-03 22:21:17 +01:00
}
/**
* @ see \ManiaControl\Plugins\Plugin :: getDescription ()
*/
public static function getDescription () {
return 'Plugin offering Karma Voting for Maps.' ;
}
2013-11-28 01:12:52 +01:00
/**
* Handle ManiaControl 1 Second callback
*
2014-01-31 00:04:40 +01:00
* @ param $time
2013-11-28 01:12:52 +01:00
*/
2014-01-31 00:04:40 +01:00
public function handle1Second ( $time ) {
2014-02-23 23:27:30 +01:00
if ( ! $this -> updateManialink ) {
2014-01-03 19:31:30 +01:00
return ;
}
2014-04-13 14:51:18 +02:00
$displayMxKarma = $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_WIDGET_DISPLAY_MX );
2013-11-28 01:12:52 +01:00
// Get players
$players = $this -> updateManialink ;
2014-02-23 23:27:30 +01:00
if ( $players === true ) {
2013-11-28 01:12:52 +01:00
$players = $this -> maniaControl -> playerManager -> getPlayers ();
}
$this -> updateManialink = false ;
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
// Get map karma
2014-04-13 14:51:18 +02:00
$map = $this -> maniaControl -> mapManager -> getCurrentMap ();
//Display the mx Karma if the setting is choosen and the MX session is available
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' ];
}
2014-01-03 19:31:30 +01:00
2014-04-19 15:41:54 +02:00
if ( $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_WIDGET_ENABLE )) {
// Build karma manialink
$this -> buildManialink ();
2014-01-03 19:31:30 +01:00
2014-04-19 15:41:54 +02:00
// Update karma gauge & label
$karmaGauge = $this -> manialink -> karmaGauge ;
$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 {
$karma = 0. ;
$karmaGauge -> setRatio ( 0. );
$karmaGauge -> setColor ( '00fb' );
$karmaLabel -> setText ( '-' );
}
2014-01-03 19:31:30 +01:00
2014-04-19 15:41:54 +02:00
// Loop players
foreach ( $players as $login => $player ) {
// Get player vote
//$vote = $this->getPlayerVote($player, $map); //TODO what is this for, vote nowhere used?
2014-01-03 19:31:30 +01:00
2014-04-19 15:41:54 +02:00
// Adjust manialink for player's vote
$votesFrame = $this -> manialink -> votesFrame ;
$votesFrame -> removeChildren ();
2014-01-03 19:31:30 +01:00
2014-04-19 15:41:54 +02:00
// Send manialink
$this -> maniaControl -> manialinkManager -> sendManialink ( $this -> manialink , $login );
}
2013-11-28 01:12:52 +01:00
}
}
/**
* Handle BeginMap ManiaControl callback
*
2014-02-19 16:27:56 +01:00
* @ param Map $map
2013-11-28 01:12:52 +01:00
*/
2014-02-19 16:27:56 +01:00
public function handleBeginMap ( Map $map ) {
2014-02-26 13:27:42 +01:00
//send Map Karma to MX from previous Map
2014-03-07 18:28:53 +01:00
if ( isset ( $this -> mxKarma [ 'map' ])) {
2014-02-26 13:27:42 +01:00
$votes = array ();
foreach ( $this -> mxKarma [ 'votes' ] as $login => $value ) {
$player = $this -> maniaControl -> playerManager -> getPlayer ( $login );
array_push ( $votes , array ( " login " => $login , " nickname " => $player -> rawNickname , " vote " => $value ));
}
$this -> postKarmaVotes ( $this -> mxKarma [ 'map' ], $votes );
unset ( $this -> mxKarma [ 'map' ]);
}
2014-02-23 23:49:30 +01:00
unset ( $this -> mxKarma [ 'votes' ]);
2014-03-02 18:42:45 +01:00
$this -> mxKarma [ 'startTime' ] = time ();
2014-03-07 18:28:53 +01:00
$this -> updateManialink = true ;
2014-04-13 14:51:18 +02:00
//Get Karma votes at begin of map
$this -> getMxKarmaVotes ();
2013-11-28 01:12:52 +01:00
}
/**
* Handle PlayerConnect callback
*
2014-03-02 11:25:47 +01:00
* @ param \ManiaControl\Players\Player $player
2013-11-28 01:12:52 +01:00
*/
2014-03-02 11:25:47 +01:00
public function handlePlayerConnect ( Player $player ) {
2014-02-23 23:27:30 +01:00
if ( ! $player ) {
2013-11-28 01:12:52 +01:00
return ;
}
$this -> queryManialinkUpdateFor ( $player );
2014-04-13 14:51:18 +02:00
//Get Mx Karma Vote for Player
$this -> getMxKarmaVotes ( $player );
2013-11-28 01:12:52 +01:00
}
/**
* Handle PlayerChat callback
*
2013-12-31 11:53:10 +01:00
* @ param array $chatCallback
2013-11-28 01:12:52 +01:00
*/
public function handlePlayerChat ( array $chatCallback ) {
2014-01-03 19:31:30 +01:00
$login = $chatCallback [ 1 ][ 1 ];
2013-11-28 01:12:52 +01:00
$player = $this -> maniaControl -> playerManager -> getPlayer ( $login );
2014-02-23 23:27:30 +01:00
if ( ! $player ) {
2013-11-28 01:12:52 +01:00
return ;
}
$message = $chatCallback [ 1 ][ 2 ];
2014-02-23 23:27:30 +01:00
if ( $chatCallback [ 1 ][ 3 ]) {
2013-11-28 01:12:52 +01:00
$message = substr ( $message , 1 );
}
2014-02-23 23:27:30 +01:00
if ( preg_match ( '/[^+-]/' , $message )) {
2013-11-28 01:12:52 +01:00
return ;
}
2014-01-10 18:19:11 +01:00
$countPositive = substr_count ( $message , '+' );
$countNegative = substr_count ( $message , '-' );
2014-02-23 23:27:30 +01:00
if ( $countPositive <= 0 && $countNegative <= 0 ) {
2014-01-10 18:19:11 +01:00
return ;
2014-01-18 10:26:32 +01:00
}
$vote = $countPositive - $countNegative ;
2013-11-28 01:12:52 +01:00
$success = $this -> handleVote ( $player , $vote );
2014-02-23 23:27:30 +01:00
if ( ! $success ) {
2013-12-09 13:45:58 +01:00
$this -> maniaControl -> chat -> sendError ( 'Error occurred.' , $player -> login );
2013-11-28 01:12:52 +01:00
return ;
}
$this -> maniaControl -> chat -> sendSuccess ( 'Vote updated!' , $player -> login );
}
/**
* Handle a vote done by a player
*
2013-12-31 11:53:10 +01:00
* @ param Player $player
2014-01-03 19:31:30 +01:00
* @ param int $vote
2013-11-28 01:12:52 +01:00
* @ return bool
*/
private function handleVote ( Player $player , $vote ) {
// Check vote
$votesSetting = $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_AVAILABLE_VOTES );
2014-01-03 19:31:30 +01:00
$votes = explode ( ',' , $votesSetting );
$voteLow = intval ( $votes [ 0 ]);
$voteHigh = $voteLow + 2 ;
2014-02-23 23:27:30 +01:00
if ( isset ( $votes [ 1 ])) {
2013-11-28 01:12:52 +01:00
$voteHigh = intval ( $votes [ 1 ]);
}
2014-02-23 23:27:30 +01:00
if ( $vote < $voteLow || $vote > $voteHigh ) {
2013-11-28 01:12:52 +01:00
return false ;
}
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
// Calculate actual voting
$vote -= $voteLow ;
$voteHigh -= $voteLow ;
$vote /= $voteHigh ;
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
// Save vote
2014-01-18 10:26:32 +01:00
$map = $this -> maniaControl -> mapManager -> getCurrentMap ();
2014-01-03 19:31:30 +01:00
2014-04-13 14:51:18 +02:00
//Update vote in MX karma array
if ( $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_MX_KARMA_ACTIVATED ) && isset ( $this -> mxKarma [ " session " ])) {
if ( ! isset ( $this -> mxKarma [ " votes " ][ $player -> login ])) {
$sum = $this -> mxKarma [ " voteCount " ] * $this -> mxKarma [ " voteAverage " ] + $vote * 100 ;
$this -> mxKarma [ " voteCount " ] ++ ;
$modeSum = $this -> mxKarma [ " modeVoteCount " ] * $this -> mxKarma [ " modeVoteAverage " ] + $vote * 100 ;
$this -> mxKarma [ " modeVoteCount " ] ++ ;
} else {
$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 ;
}
$this -> mxKarma [ " voteAverage " ] = $sum / $this -> mxKarma [ " voteCount " ];
$this -> mxKarma [ " modeVoteAverage " ] = $modeSum / $this -> mxKarma [ " modeVoteCount " ];
$this -> mxKarma [ " votes " ][ $player -> login ] = $vote * 100 ;
}
2014-01-03 19:31:30 +01:00
$voted = $this -> getPlayerVote ( $player , $map );
2014-02-23 23:27:30 +01:00
if ( ! $voted ) {
2014-01-06 16:14:49 +01:00
$this -> maniaControl -> statisticManager -> incrementStat ( self :: STAT_PLAYER_MAPVOTES , $player , $this -> maniaControl -> server -> index );
2014-01-03 19:31:30 +01:00
}
2013-11-28 01:12:52 +01:00
$success = $this -> savePlayerVote ( $player , $map , $vote );
2014-02-23 23:27:30 +01:00
if ( ! $success ) {
2013-11-28 01:12:52 +01:00
return false ;
}
2014-04-19 15:41:54 +02:00
$this -> maniaControl -> callbackManager -> triggerCallback ( self :: CB_KARMA_CHANGED );
2013-11-28 01:12:52 +01:00
$this -> updateManialink = true ;
return true ;
}
/**
* Query the player to update the manialink
*
2013-12-31 11:53:10 +01:00
* @ param Player $player
2013-11-28 01:12:52 +01:00
*/
private function queryManialinkUpdateFor ( Player $player ) {
2014-02-23 23:27:30 +01:00
if ( $this -> updateManialink === true ) {
2013-11-28 01:12:52 +01:00
return ;
}
2014-02-23 23:27:30 +01:00
if ( ! is_array ( $this -> updateManialink )) {
2013-11-28 01:12:52 +01:00
$this -> updateManialink = array ();
}
$this -> updateManialink [ $player -> login ] = $player ;
}
/**
* Create necessary database tables
*/
private function initTables () {
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-01-03 19:31:30 +01:00
$query = " CREATE TABLE IF NOT EXISTS ` " . self :: TABLE_KARMA . " ` (
2013-11-28 01:12:52 +01:00
`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 ; " ;
2013-12-09 13:45:58 +01:00
$mysqli -> query ( $query );
2014-02-23 23:27:30 +01:00
if ( $mysqli -> error ) {
2013-11-28 01:12:52 +01:00
trigger_error ( $mysqli -> error , E_USER_ERROR );
}
2014-02-24 20:20:17 +01:00
if ( ! $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
return ;
}
2014-02-24 21:54:43 +01:00
$query = " CREATE TABLE IF NOT EXISTS ` " . self :: MX_IMPORT_TABLE . " ` (
2014-02-24 20:20:17 +01:00
`index` int ( 11 ) NOT NULL AUTO_INCREMENT ,
`mapIndex` int ( 11 ) NOT NULL ,
2014-02-24 21:54:43 +01:00
`mapImported` tinyint ( 1 ) NOT NULL ,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
2014-02-24 20:20:17 +01:00
PRIMARY KEY ( `index` ),
2014-02-24 21:54:43 +01:00
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 );
}
2013-11-28 01:12:52 +01:00
}
/**
* Save the vote of the player for the map
*
2013-12-31 11:53:10 +01:00
* @ param Player $player
2014-01-03 19:31:30 +01:00
* @ param Map $map
* @ param float $vote
2013-11-28 01:12:52 +01:00
* @ return bool
*/
private function savePlayerVote ( Player $player , Map $map , $vote ) {
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-01-03 19:31:30 +01:00
$query = " INSERT INTO ` " . self :: TABLE_KARMA . " ` (
2013-11-28 01:12:52 +01:00
`mapIndex` ,
`playerIndex` ,
`vote`
) VALUES (
{ $map -> index },
{ $player -> index },
{ $vote }
) ON DUPLICATE KEY UPDATE
`vote` = VALUES ( `vote` ); " ;
$result = $mysqli -> query ( $query );
2014-02-23 23:27:30 +01:00
if ( $mysqli -> error ) {
2013-11-28 01:12:52 +01:00
trigger_error ( $mysqli -> error );
return false ;
}
2014-02-23 23:27:30 +01:00
2013-11-28 01:12:52 +01:00
return $result ;
}
/**
* Get the current vote of the player for the map
*
2013-12-31 11:53:10 +01:00
* @ param Player $player
2014-01-03 19:31:30 +01:00
* @ param Map $map
2013-11-28 01:12:52 +01:00
* @ return int
*/
2014-04-19 15:41:54 +02:00
public function getPlayerVote ( Player $player , Map $map ) {
2013-11-28 01:12:52 +01:00
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-01-03 19:31:30 +01:00
$query = " SELECT * FROM ` " . self :: TABLE_KARMA . " `
2013-11-28 01:12:52 +01:00
WHERE `playerIndex` = { $player -> index }
AND `mapIndex` = { $map -> index }
AND `vote` >= 0 ; " ;
$result = $mysqli -> query ( $query );
2014-02-23 23:27:30 +01:00
if ( $mysqli -> error ) {
2013-11-28 01:12:52 +01:00
trigger_error ( $mysqli -> error );
return false ;
}
2014-02-23 23:27:30 +01:00
if ( $result -> num_rows <= 0 ) {
2013-11-28 01:12:52 +01:00
$result -> free ();
return false ;
}
$item = $result -> fetch_object ();
$result -> free ();
$vote = $item -> vote ;
return floatval ( $vote );
}
/**
* Get the current karma of the map
*
2013-12-31 11:53:10 +01:00
* @ param Map $map
2013-12-14 23:29:17 +01:00
* @ return float | bool
2013-11-28 01:12:52 +01:00
*/
2013-12-29 19:21:29 +01:00
public function getMapKarma ( Map $map ) {
2013-11-28 01:12:52 +01:00
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-01-03 19:31:30 +01:00
$query = " SELECT AVG(`vote`) AS `karma` FROM ` " . self :: TABLE_KARMA . " `
2013-11-28 01:12:52 +01:00
WHERE `mapIndex` = { $map -> index }
AND `vote` >= 0 ; " ;
$result = $mysqli -> query ( $query );
2014-02-23 23:27:30 +01:00
if ( $mysqli -> error ) {
2013-11-28 01:12:52 +01:00
trigger_error ( $mysqli -> error );
return false ;
}
2014-02-23 23:27:30 +01:00
if ( $result -> num_rows <= 0 ) {
2013-11-28 01:12:52 +01:00
$result -> free ();
return false ;
}
$item = $result -> fetch_object ();
$result -> free ();
$karma = $item -> karma ;
2014-02-23 23:27:30 +01:00
if ( $karma === null ) {
2013-11-28 01:12:52 +01:00
return false ;
}
return floatval ( $karma );
}
/**
2013-12-09 13:45:58 +01:00
* Get the current Votes for the Map
2013-11-28 01:12:52 +01:00
*
2013-12-31 11:53:10 +01:00
* @ param Map $map
2013-12-14 23:29:17 +01:00
* @ return array
2013-11-28 01:12:52 +01:00
*/
2013-12-29 19:21:29 +01:00
public function getMapVotes ( Map $map ) {
2013-11-28 01:12:52 +01:00
$mysqli = $this -> maniaControl -> database -> mysqli ;
2014-01-03 19:31:30 +01:00
$query = " SELECT `vote`, COUNT(`vote`) AS `count` FROM ` " . self :: TABLE_KARMA . " `
2013-11-28 01:12:52 +01:00
WHERE `mapIndex` = { $map -> index }
AND `vote` >= 0
GROUP BY `vote` ; " ;
$result = $mysqli -> query ( $query );
2014-02-23 23:27:30 +01:00
if ( $mysqli -> error ) {
2013-11-28 01:12:52 +01:00
trigger_error ( $mysqli -> error );
return false ;
}
$votes = array ();
2013-12-05 00:53:09 +01:00
$count = 0 ;
2014-01-03 19:31:30 +01:00
while ( $vote = $result -> fetch_object ()) {
2013-11-28 01:12:52 +01:00
$votes [ $vote -> vote ] = $vote ;
2013-12-05 00:53:09 +01:00
$count += $vote -> count ;
2013-11-28 01:12:52 +01:00
}
2013-12-05 00:53:09 +01:00
$votes [ 'count' ] = $count ;
2013-11-28 01:12:52 +01:00
$result -> free ();
return $votes ;
}
/**
* Build karma voting manialink if necessary
*
2013-12-31 11:53:10 +01:00
* @ param bool $forceBuild
2013-11-28 01:12:52 +01:00
*/
private function buildManialink ( $forceBuild = false ) {
2014-02-23 23:27:30 +01:00
if ( is_object ( $this -> manialink ) && ! $forceBuild ) {
2014-01-03 19:31:30 +01:00
return ;
}
$title = $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_WIDGET_TITLE );
$pos_x = $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_WIDGET_POSX );
$pos_y = $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_WIDGET_POSY );
$width = $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_WIDGET_WIDTH );
$height = $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_WIDGET_HEIGHT );
$labelStyle = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultLabelStyle ();
$quadStyle = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultQuadStyle ();
2013-11-28 03:47:08 +01:00
$quadSubstyle = $this -> maniaControl -> manialinkManager -> styleManager -> getDefaultQuadSubstyle ();
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
$manialink = new ManiaLink ( self :: MLID_KARMA );
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
$frame = new Frame ();
$manialink -> add ( $frame );
$frame -> setPosition ( $pos_x , $pos_y );
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
$backgroundQuad = new Quad ();
$frame -> add ( $backgroundQuad );
$backgroundQuad -> setY ( $height * 0.15 );
$backgroundQuad -> setSize ( $width , $height );
2013-11-28 03:47:08 +01:00
$backgroundQuad -> setStyles ( $quadStyle , $quadSubstyle );
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
$titleLabel = new Label ();
$frame -> add ( $titleLabel );
$titleLabel -> setY ( $height * 0.36 );
$titleLabel -> setWidth ( $width * 0.85 );
2013-11-28 03:47:08 +01:00
$titleLabel -> setStyle ( $labelStyle );
2013-11-28 01:12:52 +01:00
$titleLabel -> setTranslate ( true );
$titleLabel -> setTextSize ( 1 );
2013-12-29 23:39:30 +01:00
$titleLabel -> setScale ( 0.90 );
2013-11-28 01:12:52 +01:00
$titleLabel -> setText ( $title );
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
$karmaGauge = new Gauge ();
$frame -> add ( $karmaGauge );
$karmaGauge -> setSize ( $width * 0.95 , $height * 0.92 );
$karmaGauge -> setDrawBg ( false );
$manialink -> karmaGauge = $karmaGauge ;
2014-01-03 19:31:30 +01:00
2013-12-05 00:53:09 +01:00
$karmaLabel = new Label ();
$frame -> add ( $karmaLabel );
$karmaLabel -> setPosition ( 0 , - 0.4 , 1 );
$karmaLabel -> setSize ( $width * 0.9 , $height * 0.9 );
$karmaLabel -> setStyle ( $labelStyle );
$karmaLabel -> setTextSize ( 1 );
$manialink -> karmaLabel = $karmaLabel ;
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
$votesFrame = new Frame ();
$frame -> add ( $votesFrame );
$manialink -> votesFrame = $votesFrame ;
2014-01-03 19:31:30 +01:00
2013-11-28 01:12:52 +01:00
$this -> manialink = $manialink ;
}
2014-02-23 23:27:30 +01:00
2014-03-12 13:53:21 +01:00
/**
* Update Settings
2014-04-09 11:11:22 +02:00
*
2014-03-12 13:53:21 +01:00
* @ param $class
* @ param $settingName
* @ param $value
*/
2014-04-09 11:11:22 +02:00
public function updateSettings ( $class , $settingName , $value ) {
if ( ! $class = get_class ()) {
2014-03-12 13:53:21 +01:00
return ;
2014-04-09 11:11:22 +02:00
}
2014-03-12 13:53:21 +01:00
$serverLogin = $this -> maniaControl -> server -> login ;
2014-04-09 11:11:22 +02:00
if ( $settingName == '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l' ) {
2014-03-12 13:53:21 +01:00
$this -> mxKarmaOpenSession ();
}
2014-04-19 15:41:54 +02:00
if ( $settingName == 'Enable Karma Widget' && $value == true ) {
$this -> updateManialink = true ;
$this -> handle1Second ( time ());
} elseif ( $settingName == 'Enable Karma Widget' && $value == false ) {
$this -> updateManialink = false ;
$ml = new ManiaLink ( self :: MLID_KARMA );
$mltext = $ml -> render () -> saveXML ();
$this -> maniaControl -> manialinkManager -> sendManialink ( $mltext );
}
2014-03-12 13:53:21 +01:00
}
2014-02-23 23:27:30 +01:00
/**
* Open a Mx Karma Session
*/
private function mxKarmaOpenSession () {
2014-02-23 23:31:28 +01:00
if ( ! $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
2014-02-23 23:27:30 +01:00
return ;
}
$serverLogin = $this -> maniaControl -> server -> login ;
2014-02-24 11:08:15 +01:00
$mxKarmaCode = $this -> maniaControl -> settingManager -> getSetting ( $this , '$l[http://karma.mania-exchange.com/auth/getapikey?server=' . $serverLogin . ']MX Karma Code for ' . $serverLogin . '$l' );
2014-03-12 13:53:21 +01:00
2014-02-23 23:27:30 +01:00
if ( $mxKarmaCode == '' ) {
return ;
}
$applicationIdentifier = 'ManiaControl v' . ManiaControl :: VERSION ;
$testMode = 'true' ;
$query = self :: MX_KARMA_URL . self :: MX_KARMA_STARTSESSION ;
$query .= '?serverLogin=' . $serverLogin ;
$query .= '&applicationIdentifier=' . urlencode ( $applicationIdentifier );
$query .= '&testMode=' . $testMode ;
2014-03-07 18:28:53 +01:00
$this -> mxKarma [ 'connectionInProgress' ] = true ;
2014-02-23 23:27:30 +01:00
2014-04-20 14:52:26 +02:00
$self = $this ;
$this -> maniaControl -> fileReader -> loadFile ( $query , function ( $data , $error ) use ( & $self , $mxKarmaCode ) {
2014-02-23 23:27:30 +01:00
if ( ! $error ) {
$data = json_decode ( $data );
if ( $data -> success ) {
2014-04-20 14:52:26 +02:00
$self -> mxKarma [ 'session' ] = $data -> data ;
$self -> activateSession ( $mxKarmaCode );
2014-02-23 23:27:30 +01:00
} else {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( " Error while authenticating on Mania-Exchange Karma " );
2014-02-24 21:54:43 +01:00
//TODO remove temp trigger
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> errorHandler -> triggerDebugNotice ( " Error while authenticating on Mania-Exchange Karma " . $data -> data -> message );
$self -> mxKarma [ 'connectionInProgress' ] = false ;
2014-02-23 23:27:30 +01:00
}
} else {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( $error );
2014-03-01 13:33:21 +01:00
//TODO remove temp trigger
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> errorHandler -> triggerDebugNotice ( " Error while authenticating on Mania-Exchange Karma " . $error );
$self -> mxKarma [ 'connectionInProgress' ] = false ;
2014-02-23 23:27:30 +01:00
}
}, " application/json " , 1000 );
}
/**
* Activates the MX - Karma Session
2014-02-23 23:31:28 +01:00
*
2014-02-23 23:27:30 +01:00
* @ param $mxKarmaCode
*/
private function activateSession ( $mxKarmaCode ) {
$hash = $this -> buildActivationHash ( $this -> mxKarma [ 'session' ] -> sessionSeed , $mxKarmaCode );
$query = self :: MX_KARMA_URL . self :: MX_KARMA_ACTIVATESESSION ;
$query .= '?sessionKey=' . urlencode ( $this -> mxKarma [ 'session' ] -> sessionKey );
$query .= '&activationHash=' . urlencode ( $hash );
2014-04-20 14:52:26 +02:00
$self = $this ;
$this -> maniaControl -> fileReader -> loadFile ( $query , function ( $data , $error ) use ( & $self , $query ) {
2014-02-23 23:27:30 +01:00
if ( ! $error ) {
$data = json_decode ( $data );
if ( $data -> success && $data -> data -> activated ) {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( " Successfully authenticated on Mania-Exchange Karma " );
$self -> mxKarma [ 'connectionInProgress' ] = false ;
2014-04-13 14:51:18 +02:00
//Fetch the Mx Karma Votes
2014-04-20 14:52:26 +02:00
$self -> getMxKarmaVotes ();
2014-02-23 23:27:30 +01:00
} else {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( " Error while authenticating on Mania-Exchange Karma " . $data -> data -> message );
2014-02-24 21:54:43 +01:00
//TODO remove temp trigger
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> errorHandler -> triggerDebugNotice ( " Error while authenticating on Mania-Exchange Karma " . $data -> data -> message . " url Query " . $query );
$self -> mxKarma [ 'connectionInProgress' ] = false ;
2014-02-23 23:27:30 +01:00
}
} else {
2014-03-01 13:33:21 +01:00
//TODO remove temp trigger
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> errorHandler -> triggerDebugNotice ( " Error while authenticating on Mania-Exchange Karma " . $error );
$self -> maniaControl -> log ( $error );
$self -> mxKarma [ 'connectionInProgress' ] = false ;
2014-02-23 23:27:30 +01:00
}
}, " application/json " , 1000 );
}
2014-04-13 14:51:18 +02:00
/**
* Fetch the mxKarmaVotes for the current map
*/
public function getMxKarmaVotes ( Player $player = null ) {
if ( ! $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
return ;
}
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
return ;
}
$map = $this -> maniaControl -> mapManager -> getCurrentMap ();
$properties = array ();
$gameMode = $this -> maniaControl -> server -> getGameMode ( true );
if ( $gameMode == 'Script' ) {
$scriptName = $this -> maniaControl -> client -> getScriptName ();
$properties [ 'gamemode' ] = $scriptName [ " CurrentValue " ];
} else {
$properties [ 'gamemode' ] = $gameMode ;
}
$properties [ 'titleid' ] = $this -> maniaControl -> server -> titleId ;
$properties [ 'mapuid' ] = $map -> uid ;
if ( ! $player ) {
$properties [ 'getvotesonly' ] = false ;
$properties [ 'playerlogins' ] = array ();
foreach ( $this -> maniaControl -> playerManager -> getPlayers () as $plyr ) {
/** @var Player $player */
$properties [ 'playerlogins' ][] = $plyr -> login ;
}
} else {
$properties [ 'getvotesonly' ] = true ;
$properties [ 'playerlogins' ] = array ( $player -> login );
}
$content = json_encode ( $properties );
2014-04-20 14:52:26 +02:00
$self = $this ;
$this -> maniaControl -> fileReader -> postData ( self :: MX_KARMA_URL . self :: MX_KARMA_GETMAPRATING . " ?sessionKey= " . urlencode ( $this -> mxKarma [ 'session' ] -> sessionKey ), function ( $data , $error ) use ( & $self , & $player ) {
2014-04-13 14:51:18 +02:00
if ( ! $error ) {
$data = json_decode ( $data );
if ( $data -> success ) {
//Fetch averages if its for the whole server
if ( ! $player ) {
2014-04-20 14:52:26 +02:00
$self -> mxKarma [ " voteCount " ] = $data -> data -> votecount ;
$self -> mxKarma [ " voteAverage " ] = $data -> data -> voteaverage ;
$self -> mxKarma [ " modeVoteCount " ] = $data -> data -> modevotecount ;
$self -> mxKarma [ " modeVoteAverage " ] = $data -> data -> modevoteaverage ;
2014-04-13 14:51:18 +02:00
}
foreach ( $data -> data -> votes as $votes ) {
2014-04-20 14:52:26 +02:00
$self -> mxKarma [ " votes " ][ $votes -> login ] = $votes -> vote ;
2014-04-13 14:51:18 +02:00
}
2014-04-20 14:52:26 +02:00
$self -> updateManialink = true ;
$self -> maniaControl -> callbackManager -> triggerCallback ( $self :: CB_KARMA_MXUPDATED , $self -> mxKarma );
$self -> maniaControl -> log ( " MX-Karma Votes successfully fetched " );
2014-04-13 14:51:18 +02:00
} else {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( " Error while fetching votes: " . $data -> data -> message );
2014-04-13 14:51:18 +02:00
//TODO remove temp trigger
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> errorHandler -> triggerDebugNotice ( " Error while fetching votes: " . $data -> data -> message . " " . KarmaPlugin :: MX_KARMA_URL . KarmaPlugin :: MX_KARMA_SAVEVOTES . " ?sessionKey= " . urlencode ( $self -> mxKarma [ 'session' ] -> sessionKey ));
2014-04-13 14:51:18 +02:00
}
} else {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( $error );
2014-04-13 14:51:18 +02:00
}
}, $content , false , 'application/json' );
}
2014-02-24 21:54:43 +01:00
/**
* Import old Karma votes to Mania - Exchange Karma
*
* @ param Map $map
*/
public function importMxKarmaVotes ( Map $map ) {
2014-02-24 20:20:17 +01:00
if ( ! $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
return ;
}
2014-02-24 21:54:43 +01:00
if ( ! $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_MX_KARMA_IMPORTING )) {
return ;
}
2014-04-13 14:51:18 +02:00
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
2014-02-24 21:54:43 +01:00
return ;
}
$mysqli = $this -> maniaControl -> database -> mysqli ;
$query = " SELECT mapImported FROM ` " . self :: MX_IMPORT_TABLE . " ` WHERE `mapIndex` = { $map -> index } ; " ;
$result = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return ;
}
$vote = $result -> fetch_object ();
if ( $result -> field_count == 0 || ! $vote ) {
$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 } " ;
$result2 = $mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
return ;
}
$votes = array ();
while ( $row = $result2 -> fetch_object ()) {
array_push ( $votes , array ( " login " => $row -> login , " nickname " => $row -> nickname , " vote " => $row -> vote * 100 ));
}
$this -> postKarmaVotes ( $map , $votes , true );
//Flag Map as Imported in database if it is a import
2014-03-07 18:28:53 +01:00
$query = " INSERT INTO ` " . self :: MX_IMPORT_TABLE . " ` (`mapIndex`,`mapImported`) VALUES ( { $map -> index } ,true) ON DUPLICATE KEY UPDATE `mapImported` = true; " ;
2014-02-24 21:54:43 +01:00
$mysqli -> query ( $query );
if ( $mysqli -> error ) {
trigger_error ( $mysqli -> error );
}
$result2 -> free ();
}
$result -> free_result ();
return ;
2014-02-24 20:20:17 +01:00
}
2014-02-23 23:27:30 +01:00
/**
2014-02-23 23:31:28 +01:00
* Save Mx Karma Votes at Mapend
2014-02-23 23:27:30 +01:00
*/
2014-02-24 21:54:43 +01:00
public function sendMxKarmaVotes ( Map $map ) {
2014-02-23 23:31:28 +01:00
if ( ! $this -> maniaControl -> settingManager -> getSetting ( $this , self :: SETTING_MX_KARMA_ACTIVATED )) {
2014-02-23 23:27:30 +01:00
return ;
}
2014-04-13 14:51:18 +02:00
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
2014-02-23 23:27:30 +01:00
return ;
}
2014-02-24 09:11:48 +01:00
if ( ! isset ( $this -> mxKarma [ 'votes' ]) || count ( $this -> mxKarma [ 'votes' ]) == 0 ) {
2014-02-23 23:30:19 +01:00
return ;
}
2014-02-26 13:27:42 +01:00
$this -> mxKarma [ 'map' ] = $map ;
2014-02-24 21:54:43 +01:00
}
/**
* 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 ) {
2014-04-13 14:51:18 +02:00
if ( ! isset ( $this -> mxKarma [ 'session' ])) {
if ( ! isset ( $this -> mxKarma [ 'connectionInProgress' ]) || ! $this -> mxKarma [ 'connectionInProgress' ]) {
$this -> mxKarmaOpenSession ();
}
2014-04-09 11:11:22 +02:00
return ;
}
2014-02-23 23:27:30 +01:00
$gameMode = $this -> maniaControl -> server -> getGameMode ( true );
2014-03-07 18:28:53 +01:00
if ( count ( $votes ) == 0 ) {
2014-02-24 21:54:43 +01:00
return ;
2014-03-07 18:28:53 +01:00
}
2014-02-24 21:54:43 +01:00
2014-02-23 23:27:30 +01:00
$properties = array ();
2014-02-23 23:31:28 +01:00
if ( $gameMode == 'Script' ) {
$scriptName = $this -> maniaControl -> client -> getScriptName ();
2014-02-23 23:27:30 +01:00
$properties [ 'gamemode' ] = $scriptName [ " CurrentValue " ];
2014-02-23 23:31:28 +01:00
} else {
2014-02-23 23:27:30 +01:00
$properties [ 'gamemode' ] = $gameMode ;
}
2014-03-07 18:28:53 +01:00
if ( $import ) {
2014-03-02 18:42:45 +01:00
$properties [ 'maptime' ] = 0 ;
2014-03-07 18:28:53 +01:00
} else {
2014-03-02 18:42:45 +01:00
$properties [ 'maptime' ] = time () - $this -> mxKarma [ 'startTime' ];
}
2014-02-24 21:54:43 +01:00
$properties [ 'votes' ] = $votes ;
$properties [ 'titleid' ] = $this -> maniaControl -> server -> titleId ;
2014-02-24 20:20:17 +01:00
$properties [ 'mapname' ] = $map -> rawName ;
2014-02-23 23:31:28 +01:00
$properties [ 'mapuid' ] = $map -> uid ;
2014-02-23 23:27:30 +01:00
$properties [ 'mapauthor' ] = $map -> authorLogin ;
2014-02-24 21:54:43 +01:00
$properties [ 'isimport' ] = $import ;
2014-03-02 18:42:45 +01:00
2014-03-07 18:28:53 +01:00
$content = json_encode ( $properties );
2014-04-20 14:52:26 +02:00
$self = $this ;
$this -> maniaControl -> fileReader -> postData ( self :: MX_KARMA_URL . self :: MX_KARMA_SAVEVOTES . " ?sessionKey= " . urlencode ( $this -> mxKarma [ 'session' ] -> sessionKey ), function ( $data , $error ) use ( & $self ){
2014-02-24 20:20:17 +01:00
if ( ! $error ) {
$data = json_decode ( $data );
if ( $data -> success ) {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( " Votes successfully permitted " );
2014-02-23 23:27:30 +01:00
} else {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( " Error while updating votes: " . $data -> data -> message );
2014-02-24 21:54:43 +01:00
//TODO remove temp trigger
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> errorHandler -> triggerDebugNotice ( " Error while updating votes: " . $data -> data -> message . " " . KarmaPlugin :: MX_KARMA_URL . $self :: MX_KARMA_SAVEVOTES . " ?sessionKey= " . urlencode ( $self -> mxKarma [ 'session' ] -> sessionKey ));
2014-02-23 23:27:30 +01:00
}
2014-02-24 20:20:17 +01:00
} else {
2014-04-20 14:52:26 +02:00
$self -> maniaControl -> log ( $error );
2014-02-24 20:20:17 +01:00
}
}, $content , false , 'application/json' );
2014-02-23 23:27:30 +01:00
}
2014-02-23 23:31:28 +01:00
2014-02-23 23:27:30 +01:00
/**
* Builds a sha512 activation Hash for the MX - Karma
2014-02-23 23:31:28 +01:00
*
2014-02-23 23:27:30 +01:00
* @ param $sessionSeed
* @ param $mxKey
* @ return string
*/
private function buildActivationHash ( $sessionSeed , $mxKey ) {
return hash ( 'sha512' , $mxKey . $sessionSeed );
}
2013-11-28 01:12:52 +01:00
}