projects-jenz/AutismBotIngame/python/ingamefollowct.py

268 lines
11 KiB
Python

import os
import sys
import subprocess
import atexit
from threading import Timer
import string
import random
import time
import mysql.connector
from mysql.connector import Error
from contextlib import closing
from settings import mysql_connection_ip, mysql_connection_user, mysql_connection_pw, mysql_connection_database
looptestPath = '/home/gameservers/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/cfg/looptest.cfg'
chatmsg = ""
def writeCfgInput(Input):
with open(looptestPath, 'w') as f:
f.write(Input)
def resetCfgInputShortWait():
str = "wait 5; "
with open(looptestPath, 'w') as f:
f.write(str)
time.sleep(0.5)
def exit_handler():
print('reached exithandler')
resetCfgInputShortWait()
def joinTeam():
str = "jointeam 2; wait 2; zspawn; wait 1; {0}; wait 5;"
writeCfgInput(str)
time.sleep(4.5)
print('jointeam func: ')
def checkbotteam():
with closing(mysql.connector.connect(host=mysql_connection_ip, database=mysql_connection_database, user=mysql_connection_user, password=mysql_connection_pw,
auth_plugin='mysql_native_password')) as cnx:
with closing(cnx.cursor()) as cur:
mysql_check_spectator(cur)
resetCfgInputShortWait()
connectionTimer = Timer(20.0, checkbotteam)
connectionTimer.daemon = True
connectionTimer.start()
def mysql_check_spectator(cur):
sql_statement = f"""SELECT spectate from unloze_css_autism_bot.`bot status`"""
cur.execute(sql_statement)
result = cur.fetchall()[0]
spectate = result[0]
print('spectate: ', spectate)
if spectate != 0:
joinTeam()
def organize_player_info(movement_list):
strInput = "-attack; wait 5; -jump; wait 5; -duck; wait 5; +attack; wait 5; cl_minmodels 1; "
writeCfgInput(strInput)
time.sleep(0.2)
writeCfgInput("wait 5;")
mysql_get_player_info(movement_list)
mysql_delete_rows()
post_chatmsg()
run_delay = 0.2 #0.05
connectionTimer = Timer(run_delay, organize_player_info, args=[movement_list])
connectionTimer.daemon = True
connectionTimer.start()
def post_chatmsg():
global chatmsg
strInput = f"""{chatmsg}"""
chatmsg = ""
writeCfgInput(strInput)
time.sleep(0.5)
writeCfgInput("wait 5;")
def mysql_delete_rows():
with closing(mysql.connector.connect(host=mysql_connection_ip, database=mysql_connection_database, user=mysql_connection_user, password=mysql_connection_pw,
auth_plugin='mysql_native_password')) as cnx:
with closing(cnx.cursor()) as cur:
sql_statement = f"""SELECT hunt_or_mimic from unloze_css_autism_bot.`bot movement input` ORDER BY entry_time desc LIMIT 1"""
results = cur.execute(sql_statement, multi=True)
if results:
for result in results:
if result.with_rows:
resultfetch = result.fetchone()
break
if resultfetch:
hunt_or_mimic = resultfetch[0]
print('hunt_or_mimic delete: ', hunt_or_mimic)
if hunt_or_mimic:
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`bot movement input` ORDER BY entry_time asc LIMIT 10"""
cur.execute(sql_statement)
print('Deleted ', cur.rowcount, ' rows')
cnx.commit()
def mysql_get_player_info(movement_list):
with closing(mysql.connector.connect(host=mysql_connection_ip, database=mysql_connection_database, user=mysql_connection_user, password=mysql_connection_pw,
auth_plugin='mysql_native_password')) as cnx:
with closing(cnx.cursor()) as cur:
sql_statement = f"""SELECT StuckX, StuckY, xyz_0, xyz_1, xyz_2, hunt_or_mimic from unloze_css_autism_bot.`bot movement input` bmi ORDER BY entry_time desc LIMIT 1"""
cur.execute(sql_statement)
huntfetch = cur.fetchone()
if huntfetch:
hunt_or_mimic = huntfetch[5]
if not hunt_or_mimic:
stuckX = huntfetch[0]
stuckY = huntfetch[1]
xyz_difference = [huntfetch[2], huntfetch[3], huntfetch[4]]
strInput = ""
axis_distance = 50
if stuckX:
print('stuckX enabled')
if movement_list[0] == 0:
movement_list[0] = 1
elif movement_list[0] == 1:
movement_list[0] = 0
strInput += " +jump; wait 3; +duck; wait 5;"
if stuckY:
print('stuckY enabled')
if movement_list[1] == 0:
movement_list[1] = 1
elif movement_list[1] == 1:
movement_list[1] = 0
strInput += " +jump; wait 3; +duck; wait 5;"
print('hunt mode enabled')
if not stuckX and not stuckY:
if xyz_difference[0] > axis_distance:
movement_list[0] = 1
elif xyz_difference[0] < -axis_distance:
movement_list[0] = 0
if xyz_difference[1] < -axis_distance:
movement_list[1] = 1
elif xyz_difference[1] > axis_distance:
movement_list[1] = 0
if movement_list[0] == 1:
strInput += " -back; wait 5; +forward; wait 5;"
elif movement_list[0] == 0:
strInput += " -forward; wait 5; +back; wait 5;"
if movement_list[1] == 1:
strInput += " -moveleft; wait 5; +moveright; wait 5;"
elif movement_list[1] == 0:
strInput += " -moveright; wait 5; +moveleft; wait 5;"
print('xyz_difference[0]: ', xyz_difference[0])
print('xyz_difference[1]: ', xyz_difference[1])
print('movement_list[0]: ', movement_list[0], '0 = back. 1 = forward.')
print('movement_list[1]: ', movement_list[1], '0 = moveleft. 1 = moveright')
strInput += f""" setang 0 180 0; wait 5; """
writeCfgInput(strInput)
time.sleep(0.4)
writeCfgInput("wait 5;")
else:
sql_statement = f"""SELECT * from unloze_css_autism_bot.`bot movement input` ORDER BY entry_time asc LIMIT 10"""
cur.execute(sql_statement)
resultfetch = cur.fetchall()
#print('resultfetch: ', resultfetch)
for result in resultfetch:
#print('result: ', result)
movement_input = result[0]
client_angles = [result[1], result[2], result[3]]
strInput = ""
#print('client_angles: ', client_angles)
print('movement_input: ', movement_input)
strInput += f"""setang {client_angles[0]} {client_angles[1]} {client_angles[2]}; wait 5; {movement_input}; wait 5; """
#print('strInput final:', strInput)
writeCfgInput(strInput)
time.sleep(0.04)
writeCfgInput("wait 5;")
def mysql_check_messages():
with closing(mysql.connector.connect(host=mysql_connection_ip, database=mysql_connection_database, user=mysql_connection_user, password=mysql_connection_pw,
auth_plugin='mysql_native_password')) as cnx:
with closing(cnx.cursor()) as cur:
sql_statement = """SELECT responsemessage FROM unloze_css_autism_bot.chatting WHERE responsemessage != '' LIMIT 1"""
cur.execute(sql_statement)
result = cur.fetchall()
if result:
result = result[0][0]
str = f"""say {result}"""
print('str: ', str)
global chatmsg
chatmsg = str
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`chatting` WHERE `responsemessage` = '{result}'"""
#print('sql_statement: ', sql_statement)
cur.execute(sql_statement)
cnx.commit()
connectionTimer = Timer(0.9, mysql_check_messages)
connectionTimer.daemon = True
connectionTimer.start()
def mysql_check_if_connected():
with closing(mysql.connector.connect(host=mysql_connection_ip, database=mysql_connection_database, user=mysql_connection_user, password=mysql_connection_pw,
auth_plugin='mysql_native_password')) as cnx:
with closing(cnx.cursor()) as cur:
sql_statement = """SELECT connected, playercount from unloze_css_autism_bot.`bot status`"""
cur.execute(sql_statement)
result = cur.fetchall()[0]
#print('result: ', result, ' result[0]: ', result[0])
connected = result[0]
playercount = result[1]
#playercount might not be updated ingame and auto leave?
print('connected: ', connected)
if connected == 0:
str1 = "connect 151.80.230.149:27015;"
writeCfgInput(str1)
time.sleep(0.2)
writeCfgInput("wait 5;")
time.sleep(15.50)
print('not yet connected')
mysql_check_if_connected()
obsolete = """
elif playercount > 60:
str1 = "connect 151.80.230.149:27016;"
writeCfgInput(str1)
time.sleep(0.5)
writeCfgInput("wait 500;")
time.sleep(15.50)
print('not yet connected')
mysql_check_if_connected() """
connectionTimer = Timer(60.0 * 2, mysql_check_if_connected)
connectionTimer.daemon = True
connectionTimer.start()
def deadlock():
try:
while True: 42 == 42
except KeyboardInterrupt:
pass
def mysql_reset_input():
with closing(mysql.connector.connect(host=mysql_connection_ip, database=mysql_connection_database, user=mysql_connection_user, password=mysql_connection_pw,
auth_plugin='mysql_native_password')) as cnx:
with closing(cnx.cursor()) as cur:
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`bot movement input`"""
cur.execute(sql_statement)
cnx.commit()
if __name__ == '__main__':
atexit.register(exit_handler)
resetCfgInputShortWait()
mysql_check_if_connected()
checkbotteam()
print('reached mysql _get_player_info')
mysql_reset_input()
organize_player_info([False, True])
mysql_check_messages()
print('reached deadlock')
deadlock()
#/home/gameservers/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/cfg/autoexec.cfg:
#/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/cfg/autoexec.cfg:
#alias loop "exec looptest.cfg; wait 5; loop;"; wait 5; loop;
#-condebug
#cd /home/nonroot/.steam/
#cd /home/gameservers/.steam/debian-installation/
#./steam.sh -applaunch 240 -textmode -textmessagedebug -novid -nosound -noipx -nojoy -noshaderapi +exec looptest.cfg
#cd /home/gameservers/ze_runner_files
#./play.sh
#screen -A -d -m -S ze_runner ./play.sh