projects-jenz/AutismBotIngame/python/ingamefollowct.py

188 lines
5.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):
#1 = forward, 2 = back, 3 = left, 4 = right
#use previous movement instead for evaluation
strInput = "-attack; -jump; -duck; +attack; cl_minmodels 1; "
print('movement_list[0]: ', movement_list[0], '#1 = forward, 2 = back, 3 = left, 4 = right')
print('movement_list[1]: ', movement_list[1], '#1 = forward, 2 = back, 3 = left, 4 = right')
if movement_list[0] == 1:
strInput += " -back; wait 5;"
else:
strInput += " -forward; wait 5;"
if movement_list[1] == 4:
strInput += " -moveleft; wait 5;"
else:
strInput += " -moveright; wait 5;"
#print('strInput movement_list:', strInput)
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:
movement_list = []
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]]
lowest_distance = result[10]
print('result_count: ', result_count)
if "+jump;" in movement_input:
strInput += " +jump;"
if "+duck;" in movement_input:
strInput += " +duck;"
#print('xyz_difference: ', xyz_difference)
if xyz_difference[0] > 0:
movement_list.append(1)
strInput += " +forward; wait 5;"
else:
movement_list.append(2)
strInput += " +back; wait 5;"
if xyz_difference[1] < 0:
movement_list.append(4)
strInput += " +moveright; wait 5;"
else:
movement_list.append(3)
strInput += " +moveleft; wait 5;"
strInput += f""" setang 0 180 0; wait 5; """
#print('strInput final:', strInput)
writeCfgInput(strInput)
time.sleep(0.20)
writeCfgInput("wait 5;")
limit = 0
if result_count > 7:
limit = result_count - 2
else:
limit = 1
sql_statement = f"""DELETE FROM unloze_css_autism_bot.`bot movement input` ORDER BY entry_time LIMIT {limit}"""
cur.execute(sql_statement)
cnx.commit()
cur.close()
cnx.close()
connectionTimer = Timer(0.10, mysql_get_player_info, args=[movement_list])
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:27015;"
writeCfgInput(str1)
time.sleep(0.5)
writeCfgInput("wait 500;")
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 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_get_player_info([" +forward;", " +moveright;"])
print('reached deadlock')
deadlock()
#/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/cfg/autoexec.cfg:
#alias loop "exec looptest.cfg; wait 300; loop;"; wait 5; loop;
#-condebug
#cd /home/nonroot/.steam/
#./steam.sh -applaunch 240 -textmode -textmessagedebug -novid -nosound -noipx -nojoy -noshaderapi +exec looptest.cfg