projects-jenz/AutismBotIngame/python/ingamefollowct.py

254 lines
9.6 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 pooling
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'
chatmsg = ""
mysql_connection_pool = mysql.connector.pooling.MySQLConnectionPool(pool_name="mypool", host=mysql_connection_ip, database=mysql_connection_database, user=mysql_connection_user, passwd=mysql_connection_pw)
def get_connection_obsolete():
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 = mysql_connection_pool.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 = mysql_connection_pool.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], '#0 = forward, 1 = back, 2 = none')
print('movement_list[1]: ', movement_list[1], '#0 = left, 1 = right, 2 = none')
print('result_count: ', result_count)
axis_distance = 105
print('stuckX: ', stuckX, ' stuckY: ', stuckY)
if stuckX:
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:
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;"
if hunt_or_mimic:
print('hunt_or_mimic enabled')
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]
if not hunt_or_mimic:
if xyz_difference[0] > axis_distance:
movement_list[0] = 1
elif xyz_difference[0] < -axis_distance:
movement_list[0] = 0
else:
movement_list[0] = 2 #strInput += " -back; wait 5; -forward; wait 5;"
if xyz_difference[1] < -axis_distance:
movement_list[1] = 1
elif xyz_difference[1] > axis_distance:
movement_list[1] = 0
else:
movement_list[1] = 2 #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}; wait 5;"""
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 not hunt_or_mimic:
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;"
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)
global chatmsg
strInput += f" {chatmsg}"
chatmsg = ""
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 = mysql_connection_pool.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)
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()
cur.close()
cnx.close()
connectionTimer = Timer(0.5, mysql_check_messages)
connectionTimer.daemon = True
connectionTimer.start()
def mysql_check_if_connected():
cnx = mysql_connection_pool.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: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()
cur.close()
cnx.close()
def mysql_reset_input():
cnx = mysql_connection_pool.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