projects-jenz/AutismBotIngame/python/ingamefollowct.py

251 lines
8.4 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 settings import mysql_connection_ip, mysql_connection_user, mysql_connection_pw, mysql_connection_database
looptestPath = '/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/cfg/looptest.cfg'
def get_connection():
return mysql.connector.connect(
host=mysql_connection_ip,
user=mysql_connection_user,
passwd=mysql_connection_pw,
database=mysql_connection_database)
def writeCfgInput(Input):
with open(looptestPath, 'w') as f:
f.write(Input)
def resetCfgInputShortWait():
str = "{0}; wait 5;"
with open(looptestPath, 'w') as f:
f.write(str)
time.sleep(0.5)
def exit_handler():
print('reached exithandler')
str = "wait 5;"
with open(looptestPath, 'w') as f:
f.write(str)
def joinTeam():
str = "jointeam 2; wait 2; zspawn; wait 1; {0}; wait 5;"
writeCfgInput(str)
time.sleep(4.5)
print('jointeam func: ')
def checkbotteam():
mysql_check_spectator()
resetCfgInputShortWait()
connectionTimer = Timer(20.0, checkbotteam)
connectionTimer.daemon = True
connectionTimer.start()
def mysql_check_spectator():
cnx = get_connection()
cur = cnx.cursor()
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()
cur.close()
cnx.close()
def mysql_get_player_info(movement_list):
cnx = get_connection()
cur = cnx.cursor()
sql_statement = f"""SELECT * from unloze_css_autism_bot.`bot movement input` ORDER BY entry_time LIMIT 1"""
sql_row_count = "SELECT COUNT(*) FROM unloze_css_autism_bot.`bot movement input`"
cur.execute(sql_statement)
result = cur.fetchall()
cur.execute(sql_row_count)
result_count = cur.fetchone()[0]
if result:
result = result[0]
movement_input = result[0]
client_angles = [result[1], result[2], result[3]]
xyz_difference = [result[4], result[5], result[6]]
client_coordinates = result[7], result[8], result[9]
hunt_or_mimic = result[10]
stuckX = result[11]
stuckY = result[12]
strInput = "-attack; wait 5; -jump; wait 5; -duck; wait 5; +attack; wait 5; cl_minmodels 1; "
print('movement_list[0]: ', movement_list[0], '#false = forward, true = back')
print('movement_list[1]: ', movement_list[1], '#false = left, true = right')
print('result_count: ', result_count)
axis_distance = 105
#print('stuckX: ', stuckX, ' stuckY: ', stuckY)
if stuckX and xyz_difference[0] < -axis_distance or xyz_difference[0] > axis_distance:
movement_list[0] = not movement_list[0]
strInput += " +jump; wait 3; +duck; wait 5;"
if stuckY and xyz_difference[1] < -axis_distance or xyz_difference[1] > axis_distance:
movement_list[1] = not movement_list[1]
strInput += " +jump; wait 3; +duck; wait 5;"
if hunt_or_mimic:
print('hunt_or_mimic enabled')
bool_x_axis_permit = True
bool_y_axis_permit = True
sql_statement = f"""SELECT entry_time FROM unloze_css_autism_bot.`bot movement input` where client_coord_0 = '{client_coordinates[0]}' and client_coord_1 = '{client_coordinates[1]}'
and client_coord_2 = '{client_coordinates[2]}' LIMIT 1"""
cur.execute(sql_statement)
entry_time = cur.fetchall()[0][0]
#once hunt_or_mimic is reached it should remain true until round restart or ct death, once close enough distance bot has to copy its input for rest of round
if not hunt_or_mimic:
if xyz_difference[0] > axis_distance:
movement_list[0] = True
elif xyz_difference[0] < -axis_distance:
movement_list[0] = False
else:
bool_x_axis_permit = False
strInput += " -back; wait 5; -forward; wait 5;"
if xyz_difference[1] < -axis_distance:
movement_list[1] = True
elif xyz_difference[1] > axis_distance:
movement_list[1] = False
else:
bool_y_axis_permit = False
strInput += " -moveright; wait 5; -moveleft; wait 5;"
print('xyz_difference[0]: ', xyz_difference[0])
print('xyz_difference[1]: ', xyz_difference[1])
strInput += f""" setang 0 180 0; wait 5; """
else:
#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};"""
#if xyz_difference[0] > -axis_distance and xyz_difference[0] < axis_distance and xyz_difference[1] > -axis_distance and xyz_difference[1] < axis_distance:
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`bot movement input` where entry_time <= '{entry_time}'"""
cur.execute(sql_statement)
print('Deleted ', cur.rowcount, ' rows before/during entry_time:', entry_time)
cnx.commit()
if xyz_difference[0] > -25 and xyz_difference[0] < 25:
strInput += "-back; wait 5; -forward; wait 5;"
if xyz_difference[1] > -25 and xyz_difference[1] < 25:
strInput += "-moveright; wait 5; -moveleft; wait 5;"
if not hunt_or_mimic:
if bool_x_axis_permit:
if movement_list[0]:
strInput += " -back; wait 5; +forward; wait 5;"
else:
strInput += " -forward; wait 5; +back; wait 5;"
if bool_y_axis_permit:
if movement_list[1]:
strInput += " -moveleft; wait 5; +moveright; wait 5;"
else:
strInput += " -moveright; wait 5; +moveleft; wait 5;"
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`bot movement input` where entry_time <= '{entry_time}'"""
cur.execute(sql_statement)
#print('sql_statement: ', sql_statement)
#print('Deleted ', cur.rowcount, ' rows before/during entry_time:', entry_time)
cnx.commit()
#print('strInput final:', strInput)
writeCfgInput(strInput)
time.sleep(0.10)
writeCfgInput("wait 5;")
cur.close()
cnx.close()
run_delay = 0.05 #0.05
connectionTimer = Timer(run_delay, mysql_get_player_info, args=[movement_list])
connectionTimer.daemon = True
connectionTimer.start()
def mysql_check_messages():
cnx = get_connection()
cur = cnx.cursor()
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)
writeCfgInput(str)
time.sleep(0.2)
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`chatting` WHERE `responsemessage` = '{result}'"""
print('sql_statement: ', sql_statement)
cur.execute(sql_statement)
cnx.commit()
cur.close()
cnx.close()
connectionTimer = Timer(2, mysql_check_messages)
connectionTimer.daemon = True
connectionTimer.start()
def mysql_check_if_connected():
cnx = get_connection()
cur = cnx.cursor()
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:27019/test132;"
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()
cur.close()
cnx.close()
def mysql_reset_input():
cnx = get_connection()
cur = cnx.cursor()
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`bot movement input`"""
cur.execute(sql_statement)
cnx.commit()
cur.close()
cnx.close()
def deadlock():
try:
while True: 42 == 42
except KeyboardInterrupt:
pass
if __name__ == '__main__':
atexit.register(exit_handler)
resetCfgInputShortWait()
mysql_check_if_connected()
checkbotteam()
print('reached mysql _get_player_info')
mysql_reset_input()
mysql_get_player_info([False, True])
mysql_check_messages()
print('reached deadlock')
deadlock()
#/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/
#./steam.sh -applaunch 240 -textmode -textmessagedebug -novid -nosound -noipx -nojoy -noshaderapi +exec looptest.cfg