o
    sg                     @   s   U d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	 e  ede
ddZe dZe d	ZeeeZe	ed
< dZdZdZdd Zeddd Zdd Zdd Zdd ZdS )    N)load_dotenv)	Blueprintrender_template)create_clientClient	dashboardz
/dashboard)
url_prefixSUPABASE_URLSUPABASE_KEYsupabasez	127.0.0.1ic  z	init1234!c              
   C   s   z(d}| dt  dt dt d|  	}td|  tj|dtjdd }|W S  tjyH } z|j	r9|j	 nt
|}td	| d
}~ww )u   RCON 명령 실행z/usr/local/bin/mcrconz -H z -P z -p  zExecuting RCON command: T)shellstderrtextz Failed to execute RCON command: N)	RCON_HOST	RCON_PORTRCON_PASSWORDprint
subprocesscheck_outputSTDOUTstripCalledProcessErroroutputstr	Exception)commandmcrcon_pathfull_commandresulteerror_message r"   //var/www/html/minecraft/app/routes/dashboard.pyrun_rcon_command   s    r$   /c                  C   s*   t  rdnd} t }t }td| ||dS )u   대시보드 렌더링.u	   온라인u   오프라인zdashboard.htmlserver_statusplayer_countmod_request_count)check_server_statusget_online_playersget_mod_request_countr   r&   r"   r"   r#   r   !   s   c               
   C   sN   zt d} | r
dW S dW S  ty& } ztd|  W Y d}~dS d}~ww )u   서버 상태 확인.listTFzRCON connection failed: N)r$   r   r   responser    r"   r"   r#   r*   /   s   r*   c               
   C   s   z6t d} td|   | r1d| v r4| dtd }| d}| ||   d }t|W S W dS W dS  tyP } ztd|  W Y d}~dS d}~w tyi } ztd|  W Y d}~dS d}~ww )	u-   현재 접속 중인 플레이어 수 반환.r-   zRCON response for 'list': zplayers onlinez	There arer   zFailed to parse player count: Nz!Failed to retrieve player count: )	r$   r   findlenr   splitint
ValueErrorr   )r/   start_index	end_indexplayers_onlinever    r"   r"   r#   r+   8   s*   



r+   c               
   C   s`   zt ddddd } t| jW S  ty/ } ztd|  W Y d}~dS d}~ww )u@   Supabase에서 승인 대기 중인 모드 요청 수를 반환.mod_requestsidstatuspendingz1Failed to fetch mod request count from Supabase: Nr   )	r   tableselecteqexecuter1   datar   r   r.   r"   r"   r#   r,   L   s   r,   )osr   dotenvr   flaskr   r   r   r   r   __name__bpgetenvr	   r
   __annotations__r   r   r   r$   router   r*   r+   r,   r"   r"   r"   r#   <module>   s&   
 


	