2022-05-18 23:41:52 +02:00
#!/home/autismbot1/ze_runner/venv/bin/python3
2020-01-23 23:15:26 +01:00
import os
2020-04-22 00:17:28 +02:00
import sys
2020-01-23 23:15:26 +01:00
import subprocess
import atexit
2020-01-26 02:29:34 +01:00
from threading import Timer
2020-01-23 23:15:26 +01:00
import string
import random
2020-08-19 00:12:39 +02:00
import signal
2020-07-03 22:50:21 +02:00
import socket
import codecs
2021-07-26 01:26:16 +02:00
import json
2020-08-30 20:00:02 +02:00
import datetime
2020-09-20 19:52:28 +02:00
import time
2023-10-01 21:05:47 +02:00
import glob
connection_issue_counter = 0 ;
call_bot_connect = True
2020-01-23 23:15:26 +01:00
2021-07-26 01:26:16 +02:00
whoami = subprocess . getoutput ( [ " whoami " ] )
with open ( f ' /home/ { whoami } /ze_runner/config.json ' ) as jsonfile :
2023-10-01 21:05:47 +02:00
data_ports = json . load ( jsonfile )
looptestPath = f " /home/ { whoami } /.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/cfg/looptest.cfg "
2020-05-21 00:19:49 +02:00
chatmsg = " "
2020-02-14 23:27:57 +01:00
2020-09-20 20:19:07 +02:00
def writeCfgInput ( Input_user ) :
2020-09-20 19:52:28 +02:00
with open ( looptestPath , ' w ' ) as f :
2020-09-20 20:19:07 +02:00
f . write ( Input_user )
2021-07-26 01:26:16 +02:00
time . sleep ( 0.1 )
open ( looptestPath , ' w ' ) . close ( )
2020-01-23 23:15:26 +01:00
def exit_handler ( ) :
2020-06-12 02:01:39 +02:00
print ( ' reached exithandler ' )
2020-09-22 00:08:25 +02:00
writeCfgInput ( ' ' )
2021-07-26 01:26:16 +02:00
#securing the looptest.cfg wont be stuck accidently with commands
2022-03-27 12:18:00 +02:00
kill_owned_process ( " pidof hl2_linux " )
kill_owned_process ( " pidof xterm " )
2024-01-06 19:38:47 +01:00
#deleting POSIX shared memory objects, as long as one process has them open they exist. THis is to prevent /dev/shm from being full
#due to steam child processes.
#even with steam turned offline might there be chromium web browsers left over from steam who still hold processes open.
#only kind of potential issues from this is steam cloud being out of sync, which is really fucking irrelevant.
subprocess . getoutput ( [ f " pkill -9 -u { whoami } " ] )
2020-01-26 02:29:34 +01:00
2020-07-15 02:14:00 +02:00
def bot_process_movement ( input_line ) :
2022-07-14 21:23:19 +02:00
dist_target = input_line [ input_line . index ( " dist_target: " ) + len ( " dist_target: " ) : input_line . index ( " enemy_distance " ) ]
2020-09-22 00:08:25 +02:00
enemy_distance = input_line [ input_line . index ( " enemy_distance: " ) + len ( " enemy_distance: " ) : input_line . index ( " targeteam: " ) ]
2022-07-14 21:23:19 +02:00
targeteam = input_line [ input_line . index ( " targeteam: " ) + len ( " targeteam: " ) : input_line . index ( " state: " ) ]
state = input_line [ input_line . index ( " state: " ) + len ( " state: " ) : ]
state = int ( state . strip ( ) )
2020-07-15 02:14:00 +02:00
dist_target = float ( dist_target )
2020-07-21 01:03:06 +02:00
enemy_distance = float ( enemy_distance )
2020-07-24 00:25:03 +02:00
targeteam = int ( targeteam )
2023-01-19 17:46:02 +01:00
#request by bane that bots should simply not infect people shrug
2023-01-30 13:12:48 +01:00
strInput = " -attack; wait 2; -use; wait 5; -attack; wait 5; cl_minmodels 1; wait 2; +use; wait 5; "
2023-01-30 14:20:55 +01:00
if targeteam == 3 :
2023-01-19 17:46:02 +01:00
strInput = " -attack; wait 2; -use; wait 5; +attack; wait 5; cl_minmodels 1; wait 2; +use; wait 5; "
2022-07-14 21:23:19 +02:00
#python has no switches and such
if state in [ 5 , 7 ] :
strInput + = " -forward; wait 2; use weapon_knife; wait 5; "
elif state > = 3 :
strInput + = " use weapon_knife; wait 5; +forward; wait 2; "
elif state == 0 :
strInput + = " use weapon_p90; wait 2; -forward; wait 2; "
elif state == 1 :
strInput + = " wait 2; use weapon_elite; wait 3; +forward; wait 2; "
elif state == 2 :
strInput + = " -forward; wait 2; use weapon_elite; wait 3; "
2022-07-23 21:14:39 +02:00
elif state == 8 :
strInput + = " use weapon_knife; wait 5; +forward; wait 2; "
2022-07-14 21:23:19 +02:00
2023-10-01 21:05:47 +02:00
#print('dist_target: ', dist_target, ' enemy distance: ', enemy_distance, ' targeteam: ', targeteam, ' state:', state)
2022-07-14 21:23:19 +02:00
if state not in [ 0 , 2 , 5 , 7 , 8 ] :
strInput = strinput_append ( strInput , 2 )
2020-07-17 01:44:18 +02:00
#print('strInput final:', strInput)
2020-07-03 22:50:21 +02:00
writeCfgInput ( strInput )
2020-01-23 23:15:26 +01:00
2020-07-27 00:34:15 +02:00
def strinput_append ( strInput , nth ) :
2022-07-14 21:23:19 +02:00
for _ in range ( 3 * nth ) :
2020-07-27 00:34:15 +02:00
boolean_val = random . choice ( [ True , False ] )
if boolean_val :
2022-07-14 21:23:19 +02:00
strInput + = " +moveleft; wait 15; -moveleft; "
2020-07-27 00:34:15 +02:00
else :
2022-07-14 21:23:19 +02:00
strInput + = " +moveright; wait 15; -moveright; "
2020-07-27 00:34:15 +02:00
return strInput
2021-07-26 01:26:16 +02:00
def kill_user_owned_pid ( pid ) :
print ( ' pid: ' , pid , ' killed ' )
pid = int ( pid . strip ( ) )
os . kill ( pid , signal . SIGKILL )
time . sleep ( 10 )
def return_user_owned_pid ( pidof ) :
owner_pid = subprocess . getoutput ( [ f " { pidof } " ] )
if owner_pid :
for pid in owner_pid . split ( " " ) :
2021-04-09 21:17:30 +02:00
username = subprocess . getoutput ( [ f """ ps -o user= -p { pid } """ ] )
2021-07-26 01:26:16 +02:00
if username == whoami :
return pid
return None
def kill_owned_process ( pidof ) :
pid = return_user_owned_pid ( pidof )
while pid :
kill_user_owned_pid ( pid )
pid = return_user_owned_pid ( pidof )
def restart_sdl_and_steam ( ) :
2022-03-25 19:59:42 +01:00
#subprocess.getoutput(["screen -XS XTERM quit"])
2021-07-26 01:26:16 +02:00
kill_owned_process ( " pidof hl2_linux " )
kill_owned_process ( " pidof xterm " )
x2go_session_list = subprocess . getoutput ( [ " x2golistsessions " ] )
2022-03-25 19:59:42 +01:00
print ( ' x2golistsessions: ' , x2go_session_list )
2021-07-26 01:26:16 +02:00
if not x2go_session_list :
2022-03-27 12:18:00 +02:00
print ( ' no session. creating Display to make idle session ' )
subprocess . getoutput ( [ f " screen -d -m -S pyhoca_display ./run_follow.sh " ] )
while not x2go_session_list or len ( x2go_session_list . split ( ' | ' ) [ 0 ] . replace ( " " , " " ) ) == 0 :
time . sleep ( 5 )
x2go_session_list = subprocess . getoutput ( [ " x2golistsessions " ] )
print ( f ' finally found x2go_session_list: { x2go_session_list } ' )
2021-07-26 01:26:16 +02:00
2022-03-25 19:59:42 +01:00
hostname = subprocess . getoutput ( [ ' hostname ' ] )
x2go_session_display = x2go_session_list . split ( f ' | { hostname } ' ) [ 0 ] . rsplit ( ' | ' , 1 ) [ 1 ]
2021-07-26 01:26:16 +02:00
x2go_session_pid = x2go_session_list . split ( ' | ' ) [ 0 ]
x2go_session_id = x2go_session_list . split ( ' | ' ) [ 1 ] . split ( ' | ' ) [ 0 ]
x2go_session_command = ' TERMINAL xterm '
2022-03-25 19:59:42 +01:00
xterm_cmd = f ' screen -d -m -S XTERM x2goruncommand { x2go_session_display } { x2go_session_pid } { x2go_session_id } { x2go_session_command } '
2022-03-27 12:18:00 +02:00
print ( f ' xterm_cmd: { xterm_cmd } ' )
2022-03-25 19:59:42 +01:00
subprocess . getoutput ( [ xterm_cmd ] )
2021-07-26 01:26:16 +02:00
print ( ' reached .bashrc executing steam and variables ' )
2023-10-01 21:05:47 +02:00
time . sleep ( 40 )
2022-03-27 12:18:00 +02:00
#we sleep here to wait for .bashrc to launch steam. It takes some time
2021-07-26 01:26:16 +02:00
2022-07-23 21:14:39 +02:00
def bot_connect ( data ) :
2020-07-03 22:50:21 +02:00
#use whatever ip you want here to connect with
2021-10-17 01:09:18 +02:00
str1 = " "
2022-07-23 21:14:39 +02:00
if " connect to ze " == data :
2021-10-17 01:09:18 +02:00
str1 = f " connect { data_ports [ ' server_ip_port_ze ' ] } "
2022-07-23 21:14:39 +02:00
elif " connect to ze2 " == data :
2023-10-01 21:05:47 +02:00
str1 = f " connect { data_ports [ ' server_ip_port_ze2 ' ] } ; wait 15000; "
2020-07-15 02:14:00 +02:00
writeCfgInput ( str1 )
2023-10-01 21:05:47 +02:00
time . sleep ( 1 )
2022-07-23 21:14:39 +02:00
writeCfgInput ( " " )
2024-01-06 20:22:45 +01:00
t = Timer ( 150 , attempt_bot_connect )
2023-10-01 21:05:47 +02:00
t . start ( )
def attempt_bot_connect ( ) :
global connection_issue_counter
global call_bot_connect
connection_issue_counter + = 1
call_bot_connect = True
print ( ' finished timer callback ' )
2020-06-12 02:01:39 +02:00
2020-01-23 23:15:26 +01:00
if __name__ == ' __main__ ' :
2020-06-12 02:01:39 +02:00
atexit . register ( exit_handler )
2021-07-26 01:26:16 +02:00
local_port = data_ports [ ' udp_port ' ]
external_port_messages = data_ports [ ' chat_external_port ' ]
2020-07-03 22:50:21 +02:00
buffer_size = 4096 #potentially not large enough?
2023-10-01 21:05:47 +02:00
#if downloading maps we give it 10 seconds delay to see if the size changed, if not its probably safe to delete the .bz2 file as no download in progress
bz2_maps = { }
while True :
found = False
for f in glob . glob ( f " /home/ { whoami } /.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps/*.bz2 " ) :
found = True
fsize = subprocess . Popen ( [ " du " , " -sh " , f ] , stdout = subprocess . PIPE ) . communicate ( ) [ 0 ] . decode ( ) . split ( " \t " ) [ 0 ]
#deleting left over bz2 files because they give missing map issue
if f in bz2_maps :
#if the size is still the same it should be abandoned bz2 file that we can delete
if bz2_maps [ f ] == fsize :
subprocess . Popen ( [ " rm " , f ] , stdout = subprocess . PIPE ) . communicate ( ) [ 0 ]
bz2_maps [ f ] = fsize
#no bz2 maps left to be concerned about
if not found :
break
time . sleep ( 10 )
maps_folder_size = subprocess . Popen ( [ " du " , " -sh " , f " /home/ { whoami } /.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps " ] , stdout = subprocess . PIPE ) . communicate ( ) [ 0 ] . decode ( ) . split ( " \t " ) [ 0 ]
2023-02-21 14:21:09 +01:00
#deleting when maps folder larger than 150GB
if maps_folder_size . endswith ( " G " ) :
maps_folder_size = float ( maps_folder_size [ : - 1 ] )
2023-10-01 21:05:47 +02:00
if maps_folder_size > 40.0 :
for f in glob . glob ( f " /home/ { whoami } /.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps/* " ) :
subprocess . Popen ( [ " rm " , f ] , stdout = subprocess . PIPE ) . communicate ( ) [ 0 ]
2023-02-13 21:38:06 +01:00
restart_sdl_and_steam ( )
2022-07-23 21:14:39 +02:00
is_bot_connected_to_ze2 = False
2023-02-13 21:38:06 +01:00
print ( ' preparing to launch game.... ' )
2020-09-20 19:52:28 +02:00
sock = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM )
sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 )
sock . bind ( ( " " , local_port ) )
2021-07-26 01:26:16 +02:00
sock . settimeout ( 5.0 )
2020-09-27 03:16:52 +02:00
messager_name = " "
2023-10-01 21:05:47 +02:00
2020-09-20 19:52:28 +02:00
try :
while True :
2021-07-26 01:26:16 +02:00
try :
data , addr = sock . recvfrom ( buffer_size )
except socket . timeout :
continue
2020-09-20 19:52:28 +02:00
data = codecs . decode ( data , " utf-8 " , " ignore " )
ip = addr [ 0 ]
port = addr [ 1 ]
#print('port: ', port, " ip: ", ip)
2021-07-26 01:26:16 +02:00
#print('data: ', data)
2020-09-20 19:52:28 +02:00
if not data :
continue
2023-10-01 21:05:47 +02:00
#print("ip: ", ip, " port: ", port)
2022-03-25 19:59:42 +01:00
if ip == data_ports [ ' discord_bot_ip ' ] and port == external_port_messages :
2020-09-22 22:10:24 +02:00
if messager_name in data :
messager_name = " "
response_msg = f """ say { messager_name } { data } """
print ( " remote UDP packet response_msg: " , response_msg )
2020-09-20 19:52:28 +02:00
writeCfgInput ( response_msg )
2022-03-25 19:59:42 +01:00
if ip != data_ports [ ' ovh_ip ' ] :
2021-06-17 18:36:23 +02:00
continue
2021-10-17 17:17:34 +02:00
elif data == " rtv " :
response_msg = " say rtv "
writeCfgInput ( response_msg )
2020-09-20 19:52:28 +02:00
elif data == " bot kicked server full " :
print ( ' bot kicked server full: ' , datetime . datetime . now ( ) . time ( ) )
2022-07-23 21:14:39 +02:00
elif " autismo connected to ze " == data :
print ( ' Bot connected to ze! ' )
connection_issue_counter = 0
is_bot_connected_to_ze2 = False
elif " not connected to ze2 " == data :
is_bot_connected_to_ze2 = False
elif " autismo connected to ze2 " == data :
print ( ' Bot connected to ze2! ' )
connection_issue_counter = 0
is_bot_connected_to_ze2 = True
elif " connect to ze " == data or ( " connect to ze2 " == data and not is_bot_connected_to_ze2 ) :
2023-10-01 21:05:47 +02:00
if connection_issue_counter == 5 :
2021-07-26 01:26:16 +02:00
kill_owned_process ( " pidof hl2_linux " )
2022-05-18 23:41:52 +02:00
print ( ' exiting ' )
2024-01-06 19:38:47 +01:00
sock . close ( )
2022-05-18 23:41:52 +02:00
sys . exit ( 1 )
2022-07-23 21:14:39 +02:00
else :
2023-10-01 21:05:47 +02:00
if call_bot_connect :
call_bot_connect = False
print ( ' data: ' , data )
print ( " connection_issue_counter: " , connection_issue_counter )
bot_connect ( data )
2020-09-20 19:52:28 +02:00
elif " clientmessage: " in data :
2020-09-22 22:10:24 +02:00
messager_name = data . split ( " clientmessage: " , 1 ) [ 1 ] . split ( " 72DqZ84 " ) [ 0 ]
databyte_send_message = messager_name + data . split ( " 72DqZ84 " ) [ 1 ]
2022-03-25 19:59:42 +01:00
sock . sendto ( databyte_send_message . encode ( ) , ( data_ports [ " discord_bot_ip " ] , external_port_messages ) )
2023-10-01 21:05:47 +02:00
#print('databyte_send_message: ', databyte_send_message)
2020-09-20 19:52:28 +02:00
elif data . startswith ( " dist_target: " ) :
bot_process_movement ( data )
elif data . startswith ( " surfing: " ) :
bot_process_surf ( data )
2020-10-02 19:05:48 +02:00
elif data . startswith ( " hull info: " ) :
hull_info = data [ data . index ( " hull info: " ) + len ( " hull info: " ) : ]
2020-09-22 00:08:25 +02:00
strInput = " "
2020-10-02 19:05:48 +02:00
if hull_info == " jump " :
strInput + = " +jump; wait 5; -jump; +duck; wait 50; -duck; wait 5; "
elif hull_info == " crouch " :
2020-10-02 22:42:53 +02:00
strInput + = " +duck; wait 50; -duck; wait 5; "
2020-09-22 00:08:25 +02:00
writeCfgInput ( strInput )
2020-09-20 19:52:28 +02:00
finally :
sock . close ( )