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-02-14 23:27:57 +01:00
import time
2020-04-22 00:17:28 +02:00
import mysql . connector
from mysql . connector import Error
2020-06-12 02:01:39 +02:00
from contextlib import closing
2020-04-22 00:17:28 +02:00
from settings import mysql_connection_ip , mysql_connection_user , mysql_connection_pw , mysql_connection_database
2020-01-23 23:15:26 +01:00
2020-06-12 02:01:39 +02:00
looptestPath = ' /home/gameservers/.steam/steam/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-01-23 23:15:26 +01:00
def writeCfgInput ( Input ) :
with open ( looptestPath , ' w ' ) as f :
f . write ( Input )
2020-06-12 02:01:39 +02:00
2020-01-23 23:15:26 +01:00
def resetCfgInputShortWait ( ) :
2020-06-12 02:01:39 +02:00
str = " wait 5; "
2020-01-23 23:15:26 +01:00
with open ( looptestPath , ' w ' ) as f :
f . write ( str )
2020-04-22 00:17:28 +02:00
time . sleep ( 0.5 )
2020-01-23 23:15:26 +01:00
def exit_handler ( ) :
2020-06-12 02:01:39 +02:00
print ( ' reached exithandler ' )
resetCfgInputShortWait ( )
2020-01-26 02:29:34 +01:00
2020-01-23 23:15:26 +01:00
def joinTeam ( ) :
2020-04-22 00:17:28 +02:00
str = " jointeam 2; wait 2; zspawn; wait 1; {0} ; wait 5; "
2020-01-23 23:15:26 +01:00
writeCfgInput ( str )
2020-04-22 00:17:28 +02:00
time . sleep ( 4.5 )
print ( ' jointeam func: ' )
2020-01-23 23:15:26 +01:00
def checkbotteam ( ) :
2020-06-12 02:01:39 +02:00
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 ( )
2020-04-22 00:17:28 +02:00
2020-06-12 02:01:39 +02:00
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 ' )
2020-05-21 00:19:49 +02:00
cnx . commit ( )
2020-06-12 02:01:39 +02:00
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 ]
2020-05-21 00:19:49 +02:00
if not hunt_or_mimic :
2020-06-12 02:01:39 +02:00
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
2020-05-21 00:19:49 +02:00
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 :
2020-06-12 02:01:39 +02:00
strInput + = " -moveleft; wait 5; +moveright; wait 5; "
2020-05-21 00:19:49 +02:00
elif movement_list [ 1 ] == 0 :
strInput + = " -moveright; wait 5; +moveleft; wait 5; "
2020-06-12 02:01:39 +02:00
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 """
2020-05-21 00:19:49 +02:00
cur . execute ( sql_statement )
2020-06-12 02:01:39 +02:00
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; " )
2020-05-05 23:52:01 +02:00
def mysql_check_messages ( ) :
2020-06-12 02:01:39 +02:00
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 ( )
2020-02-09 00:26:05 +01:00
2020-04-22 00:17:28 +02:00
def mysql_check_if_connected ( ) :
2020-06-12 02:01:39 +02:00
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 ( )
2020-05-05 23:52:01 +02:00
2020-02-09 00:26:05 +01:00
def deadlock ( ) :
try :
while True : 42 == 42
except KeyboardInterrupt :
pass
2020-01-23 23:15:26 +01:00
2020-06-12 02:01:39 +02:00
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 ( )
2020-01-23 23:15:26 +01:00
if __name__ == ' __main__ ' :
2020-06-12 02:01:39 +02:00
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 ( )
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-04-22 00:17:28 +02:00
#/home/nonroot/.steam/steam/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;
2020-04-22 00:17:28 +02:00
#-condebug
#cd /home/nonroot/.steam/
2020-06-12 02:01:39 +02:00
#cd /home/gameservers/.steam/debian-installation/
2020-05-21 00:19:49 +02:00
#./steam.sh -applaunch 240 -textmode -textmessagedebug -novid -nosound -noipx -nojoy -noshaderapi +exec looptest.cfg
2020-06-12 02:01:39 +02:00
#cd /home/gameservers/ze_runner_files
#./play.sh
#screen -A -d -m -S ze_runner ./play.sh