| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- /**
- * @Name minecraft_server
- * @Unique Name
- * @Unique Domain, Port
- */
- class MinecraftServer extends Entity {
- /**
- * @DB-Column
- * @Name ID
- * @Type INT
- * @Primary
- * @AutoIncrement
- * @NotNull
- */
- public $ID;
- /**
- * @DB-Column
- * @Name Name
- * @Type VARCHAR(256)
- * @NotNull
- */
- public $Name;
- /**
- * @DB-Column
- * @Name Domain
- * @Type VARCHAR(256)
- */
- public $Domain = null;
- /**
- * @DB-Column
- * @Name Port
- * @Type INT(4)
- */
- public $Port = null;
- /**
- * @DB-Column
- * @Name Password
- * @Type VARCHAR(256)
- */
- public $Password = null;
- /**
- * @DB-Column
- * @Name ServerPath
- * @Type VARCHAR(256)
- */
- public $ServerPath = null;
- /**
- * @DB-Column
- * @Name PerMonthPrice
- * @Type INT(4)
- */
- public $PerMonthPrice = null;
- /**
- * @DB-Column
- * @Name MaxPerMonthPrice
- * @Type INT(4)
- */
- public $MaxPerMonthPrice = null;
- /**
- * @DB-Relation
- * @Field ID
- * @FieldName SubscriptionID
- * @OneToMany
- * @OnUpdate CASCADE
- * @OnDelete CASCADE
- * @var MinecraftServer[]
- */
- public $Subscriptions;
-
- public function IsUnlocked( MinecraftUser $user ): bool {
- return in_array( strtolower( $user->GetUsername() ), $this->GetWhitelistedNames() );
- }
-
- private $RCon = null;
-
- private function GetRCon() {
- if ( $this->RCon == null ) $this->RCon = new Rcon( $this->GetDomain(), $this->GetPort(), $this->GetPassword(), 1500 );
- return $this->RCon;
- }
-
- public function Unlock( MinecraftUser $user, bool $keepOpen = false ): bool {
- $rcon = $this->GetRCon();
- if ( !$rcon->isConnected() ) $rcon->connect();
- $rcon->sendCommand( 'whitelist add ' . $user->GetUsername() );
- $res = $rcon->getResponse();
- if ( !$keepOpen ) $rcon->disconnect();
- return $this->IsUnlocked( $user );
- }
-
- public function Lock( MinecraftUser $user, bool $keepOpen = false ): bool {
- $rcon = $this->GetRCon();
- if ( !$rcon->isConnected() ) $rcon->connect();
- $rcon->sendCommand( 'whitelist remove ' . $user->GetUsername() );
- $res = $rcon->getResponse();
- if ( !$keepOpen ) $rcon->disconnect();
- return !$this->IsUnlocked( $user );
- }
-
- public function GetWhitelistedNames( bool $keepOpen = false ) {
- // There are 1 whitelisted players: DragonSkills99
- $rcon = $this->GetRCon();
- if ( !$rcon->isConnected() ) $rcon->connect();
- $rcon->sendCommand( 'whitelist list' );
- $res = $rcon->getResponse();
- if ( !$keepOpen ) $rcon->disconnect();
- if ( nocase_compare( 'There are no whitelisted players', trim( $res ) ) ) return [];
- preg_match( '/^There are [0-9]+ whitelisted players: (.*)/', trim( $res ), $m );
- if ( !isset( $m[ 1 ] ) ) return [];
- $players = explode( ', ', strtolower( $m[ 1 ] ) );
- if ( trim( $players[ 0 ] ) == '' ) return [];
- else return $players;
- }
-
- public function ClearWhitelist( int $tries = 3 ) {
- $rcon = $this->GetRCon();
- $players = $this->GetWhitelistedNames( true );
- if ( !count( $players ) ) { $rcon->disconnect(); return true; }
-
- foreach( $players as $player ) {
- $rcon->sendCommand( "whitelist remove $player" );
- }
-
- $nplayers = $this->GetWhitelistedNames( true );
- if ( count( $nplayers ) ) {
- if ( $tries > 0 ) $this->ClearWhitelist( $tries - 1 );
- else { $rcon->disconnect(); return false; }
- }
- $rcon->disconnect();
- return true;
- }
- }
|