-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnextmap.py
More file actions
115 lines (96 loc) · 3.5 KB
/
nextmap.py
File metadata and controls
115 lines (96 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
from colors import *
from commands.typed import TypedSayCommand, TypedClientCommand
from events import Event
from engines.server import engine_server
from engines.server import global_vars
from entities.helpers import index_from_edict
from listeners.tick import GameThread
from players.helpers import index_from_userid
from filters.players import PlayerIter, Player
from messages.hooks import HookUserMessage
from messages import SayText2
from listeners.tick import Delay
import paths
from listeners import OnConVarChanged
from listeners import OnPlayerRunCommand
from listeners import OnPlayerPostRunCommand
# Core Imports
import os
import threading
import json
import socket
import requests
# 3rd party imports
import requests
import pymysql
import vdf
MAP_CHECK_LOCK = threading.Lock()
def load():
SayText2('NextMap plugin has been loaded successfully!').send()
def unload():
SayText2('NextMap plugin has been unloaded successfully!').send()
# =============================================================================
# >> Utils
# =============================================================================
def threaded(fn):
def wrapper(*args, **kwargs):
thread = GameThread(target=fn, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return wrapper
#this works
@OnConVarChanged
def on_convar_changed(convar, old_value):
check_map_list(convar)
@threaded
def check_map_list(map_name):
global MAP_CHECK_LOCK
with MAP_CHECK_LOCK:
player_count = len([player for player in PlayerIter('human')])
server_hostname = socket.gethostname()
#check if map is pending
#get discord ID
#say map is next
map_data = get_pending_map_data(map_name)
if map_data:
discord_user_id = map_data['discord_user_id']
send_discord_message(map_name, player_count, discord_user_id, f"https://bot.tf2maps.net/connect/?server={server_hostname}:27015")
def send_discord_message(map_name, player_count, discord_user_id, server_url):
filepath = os.path.join(paths.GAME_PATH, "addons/source-python/plugins/pingdiscord/config.json")
with open(filepath) as file:
config = json.load(file)
data = {
"content" : f"<@{discord_user_id}> {map_name} is set as the **next map** on {server_url} with {player_count} players.",
"username" : "Mecha Engineer"
}
result = requests.post(config['webhook_url'], json=data)
result.raise_for_status()
def get_pending_map_data(map_name):
connection = get_db_connection()
with connection.cursor() as cursor:
query = (
"SELECT * "
"FROM maps "
"WHERE map=(%s) AND status='pending' LIMIT 1 "
)
cursor.execute(query, (map_name))
result = cursor.fetchone()
return result
def get_db_connection():
filepath = os.path.join(paths.GAME_PATH, "addons/sourcemod/configs/databases.cfg")
with open(filepath) as file:
databases = vdf.load(file)
database = databases['Databases']['maplist']
connection = pymysql.connect(
host=database['host'],
user=database['user'],
port=int(database['port']),
password=database['pass'],
database=database['database'],
cursorclass=pymysql.cursors.DictCursor
)
return connection