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
|
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:
|
|
|
|
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
|
|
|
#print(Input_user)
|
|
|
|
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
|
2020-01-26 02:29:34 +01:00
|
|
|
|
2020-07-15 02:14:00 +02:00
|
|
|
def bot_process_movement(input_line):
|
|
|
|
dist_target = input_line[input_line.index("dist_target:") + len("dist_target:"):input_line.index("targethuman:")]
|
2020-09-27 03:16:52 +02:00
|
|
|
targethuman = input_line[input_line.index("targethuman:") + len("targethuman:"):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:")]
|
2020-08-30 20:00:02 +02:00
|
|
|
targeteam = input_line[input_line.index("targeteam:") + len("targeteam:"):input_line.index("target_enemy:")]
|
2020-09-27 03:16:52 +02:00
|
|
|
target_enemy = input_line[input_line.index("target_enemy:") + len("target_enemy:"):]
|
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)
|
2021-07-26 01:26:16 +02:00
|
|
|
min_distance_target_human = 1.0
|
2020-09-27 03:16:52 +02:00
|
|
|
strInput = "-attack; wait 2; -use; wait 5; +attack; wait 5; cl_minmodels 1; wait 2; +use; +forward; wait 2; "
|
2021-07-26 01:26:16 +02:00
|
|
|
if dist_target > min_distance_target_human:
|
2020-09-27 03:16:52 +02:00
|
|
|
#print('dist_target: ', dist_target)
|
2020-07-24 00:25:03 +02:00
|
|
|
strInput += "use weapon_elite; wait 3; "
|
|
|
|
elif targeteam == 3:
|
|
|
|
strInput += "use weapon_p90; wait 3; "
|
|
|
|
elif targeteam == 2:
|
|
|
|
strInput += "use weapon_knife; wait 5; "
|
2020-10-02 22:42:53 +02:00
|
|
|
print('date: ', datetime.datetime.now().time(), ' target_enemy: ', target_enemy, ' enemy distance: ', enemy_distance, ' target human: ', targethuman,
|
|
|
|
' dist_target: ', dist_target)
|
2020-08-19 00:12:39 +02:00
|
|
|
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):
|
|
|
|
for _ in range(10 * nth):
|
|
|
|
boolean_val = random.choice([True, False])
|
|
|
|
if boolean_val:
|
|
|
|
strInput += "+moveleft; wait 15; -moveleft; "
|
|
|
|
else:
|
|
|
|
strInput += "+moveright; wait 15; -moveright; "
|
|
|
|
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():
|
|
|
|
#ending screen
|
|
|
|
subprocess.getoutput(["screen -XS XTERM quit"])
|
|
|
|
kill_owned_process("pidof hl2_linux")
|
|
|
|
kill_owned_process("pidof xterm")
|
|
|
|
|
|
|
|
x2go_session_list = subprocess.getoutput(["x2golistsessions"])
|
|
|
|
print('x2go_session_list: ', x2go_session_list)
|
|
|
|
if not x2go_session_list:
|
|
|
|
print('no session available. establish the session manually again')
|
|
|
|
raise Exception('')
|
|
|
|
|
|
|
|
x2go_session_display = x2go_session_list.split('|unloze.com')[0].rsplit('|', 1)[1]
|
|
|
|
x2go_session_pid = x2go_session_list.split('|')[0]
|
|
|
|
x2go_session_id = x2go_session_list.split('|')[1].split('|')[0]
|
|
|
|
x2go_session_command = 'TERMINAL xterm'
|
|
|
|
|
|
|
|
subprocess.getoutput([f"screen -d -m -S XTERM x2goruncommand {x2go_session_display} {x2go_session_pid} {x2go_session_id} {x2go_session_command}"])
|
|
|
|
print('reached .bashrc executing steam and variables')
|
2021-10-17 01:09:18 +02:00
|
|
|
time.sleep(60)
|
2021-07-26 01:26:16 +02:00
|
|
|
#we sleep here to wait for .bashrc to launch steam. It takes some minutes
|
|
|
|
|
|
|
|
def launch_css_process():
|
2020-08-19 00:12:39 +02:00
|
|
|
print('preparing to launch game....')
|
2021-07-26 01:26:16 +02:00
|
|
|
#deleting maps
|
|
|
|
subprocess.getoutput([f"rm '/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps/'*"])
|
|
|
|
|
|
|
|
#launching game
|
|
|
|
subprocess.getoutput([f"/home/{whoami}/.steam/debian-installation/steam.sh -applaunch 240 -textmode -novid -nosound -noipx -nojoy -noshaderapi -port {data_ports['steam_port']}"])
|
2020-08-19 00:12:39 +02:00
|
|
|
print('finished starting game')
|
|
|
|
|
2021-10-17 01:09:18 +02:00
|
|
|
def bot_connect(server, connected_to_other):
|
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 = ""
|
2021-12-09 22:49:52 +01:00
|
|
|
if "connect to ze" == server:
|
2021-10-17 01:09:18 +02:00
|
|
|
str1 = f"connect {data_ports['server_ip_port_ze']}"
|
|
|
|
connected_to_other = False
|
2021-12-09 22:49:52 +01:00
|
|
|
elif "connect to ze2" == server and not connected_to_other:
|
|
|
|
str1 = f"connect {data_ports['server_ip_port_ze2']}"
|
2020-07-15 02:14:00 +02:00
|
|
|
writeCfgInput(str1)
|
2020-07-03 22:50:21 +02:00
|
|
|
print('not yet connected')
|
2020-06-12 02:01:39 +02:00
|
|
|
|
2020-09-27 03:16:52 +02:00
|
|
|
def pairwise(it):
|
|
|
|
it = iter(it)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
yield next(it), next(it)
|
|
|
|
except StopIteration:
|
|
|
|
# no more elements in the iterator
|
|
|
|
return
|
|
|
|
|
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
|
|
|
ovh_ip = "135.125.188.157"
|
|
|
|
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?
|
2020-08-19 00:12:39 +02:00
|
|
|
connection_issue_counter = 0;
|
2021-07-26 01:26:16 +02:00
|
|
|
pid = return_user_owned_pid("pidof hl2_linux")
|
|
|
|
if not pid:
|
|
|
|
restart_sdl_and_steam()
|
|
|
|
else:
|
|
|
|
kill_owned_process("pidof hl2_linux")
|
2021-10-17 01:09:18 +02:00
|
|
|
connected_to_other = False
|
2021-07-26 01:26:16 +02:00
|
|
|
launch_css_process()
|
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 = ""
|
2020-09-20 19:52:28 +02:00
|
|
|
print('reached deadlock')
|
|
|
|
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
|
2021-07-26 01:26:16 +02:00
|
|
|
if ip == ovh_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)
|
2021-07-26 01:26:16 +02:00
|
|
|
if ip != ovh_ip:
|
2021-06-17 18:36:23 +02:00
|
|
|
continue
|
2021-10-17 17:17:34 +02:00
|
|
|
#print("data: ", data)
|
2020-09-20 19:52:28 +02:00
|
|
|
if data == "autismo connected":
|
|
|
|
print('Bot connected!')
|
|
|
|
connection_issue_counter = 0
|
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())
|
2021-10-17 01:09:18 +02:00
|
|
|
elif "connected to" in data:
|
2021-10-17 23:01:05 +02:00
|
|
|
connected_to_other = True
|
2021-10-17 19:07:12 +02:00
|
|
|
elif "not connected" in data:
|
|
|
|
connected_to_other = False
|
2021-10-17 01:09:18 +02:00
|
|
|
elif "connect to" in data:
|
2021-07-26 01:26:16 +02:00
|
|
|
if connection_issue_counter == 20:
|
|
|
|
kill_owned_process("pidof hl2_linux")
|
|
|
|
launch_css_process()
|
2021-10-17 01:09:18 +02:00
|
|
|
connected_to_other = False
|
2021-07-26 01:26:16 +02:00
|
|
|
elif connection_issue_counter == 50:
|
|
|
|
restart_sdl_and_steam()
|
|
|
|
launch_css_process()
|
2021-10-17 01:09:18 +02:00
|
|
|
connected_to_other = False
|
2021-07-26 01:26:16 +02:00
|
|
|
connection_issue_counter = -10
|
2021-12-09 22:49:52 +01:00
|
|
|
if not connected_to_other or "connect to ze" == data:
|
|
|
|
print('data: ', data)
|
2021-10-17 15:00:04 +02:00
|
|
|
connection_issue_counter += 1
|
|
|
|
print('connection_issue_counter: ', connection_issue_counter)
|
|
|
|
bot_connect(data, connected_to_other)
|
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]
|
2021-07-26 01:26:16 +02:00
|
|
|
sock.sendto(databyte_send_message.encode(), (ovh_ip, external_port_messages))
|
2020-09-22 22:10:24 +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()
|
2020-02-09 00:26:05 +01:00
|
|
|
|
2020-06-12 02:01:39 +02:00
|
|
|
#/home/gameservers/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/cfg/autoexec.cfg:
|
2020-05-05 23:52:01 +02:00
|
|
|
#alias loop "exec looptest.cfg; wait 5; loop;"; wait 5; loop;
|
2021-07-26 01:26:16 +02:00
|
|
|
#create looptest.cfg in /cfg/ folder
|
|
|
|
#X2GO_NXAGENT_DEFAULT_OPTIONS="-options nx/nx,sleep=0" /etc/x2go/x2goagent.options
|
|
|
|
#keeps session as R instead of S which is needed for interaction with graphical components such as steam and CS:S
|
2020-06-12 02:01:39 +02:00
|
|
|
|
|
|
|
#cd /home/gameservers/ze_runner_files
|
2020-10-03 02:28:36 +02:00
|
|
|
#python3 ingamefollowct.py
|
|
|
|
#screen -d -m -S ze_runner python3 ingamefollowct.py
|
2020-08-21 02:30:29 +02:00
|
|
|
|
|
|
|
#before steam login: export SDL_VIDEO_X11_VISUALID=0x074
|
|
|
|
#to find correct SDL_VIDEO_X11_VISUALID use glxinfo in X2GO
|
2021-07-26 01:26:16 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
.bashrc requires:
|
|
|
|
if [[ $DISPLAY ]]; then
|
|
|
|
echo 'SDL_VIDEO_X11_VISUALID: ' $SDL_VIDEO_X11_VISUALID
|
|
|
|
export SDL_VIDEO_X11_VISUALID=0x205
|
|
|
|
echo 'SDL_VIDEO_X11_VISUALID: ' $SDL_VIDEO_X11_VISUALID
|
2022-01-16 00:21:22 +01:00
|
|
|
#native libraries being prefered over runtime here
|
|
|
|
STEAM_RUNTIME=0 steam no-browser -console
|
2021-07-26 01:26:16 +02:00
|
|
|
fi
|
|
|
|
"""
|