Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Refactor #3

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions magicked_admin/chatbot/chatbot.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
from server.chat.listener import Listener
from web_admin.data_logger import Listener
#from server.chat.listener import Listener
from chatbot.commands.command_map import CommandMap
from chatbot.commands.event_commands import CommandGreeter

from os import path
from utils.logger import logger


class Chatbot(Listener):
<<<<<<< HEAD

def __init__(self, chat, name, command_map=None):
Listener.__init__(self)

self.name = name
self.chat = chat
self.command_map = command_map
=======
"""
responsible for sending chat to the WebAdmin.
"""

def __init__(self, server, greeter_enabled=True):
self.server = server
self.chat = server.chat
>>>>>>> master
# The in-game chat can fit 21 Ws horizontally
self.word_wrap = 21
self.max_lines = 7

<<<<<<< HEAD
self.silent = False

if path.exists(self.name + ".init"):
self.execute_script(self.name + ".init")

print("INFO: Bot on server " + self.name + " initialised")

# Look at structure of this closer
def set_commands(self, command_map):
self.command_map = command_map

def receive_message(self, username, message, admin=False):
=======
self.commands = CommandMap(server, self)
self.silent = False
self.greeter_enabled = True
Expand All @@ -27,9 +53,6 @@ def __init__(self, server, greeter_enabled=True):
logger.debug("Bot on server " + server.name + " initialised")

def receive_message(self, username, message, admin=False):
"""

"""
if message[0] == '!':
# Drop the '!' because its no longer relevant
args = message[1:].split(' ')
Expand All @@ -39,14 +62,26 @@ def command_handler(self, username, args, admin=False):
if args is None or len(args) == 0:
return

<<<<<<< HEAD
print("DEBUG: " + str(args))

if args[0] in self.command_map:
command = self.command_map[args[0]]

response = command.execute(username, args, admin)
if not self.silent:
self.chat.submit_message(response)
elif username != "%internal%" and not self.silent:
self.chat.submit_message("Sorry, I didn't understand that request.")
=======
if args[0].lower() in self.commands.command_map:
command = self.commands.command_map[args[0].lower()]
if not self.greeter_enabled and isinstance(command, CommandGreeter):
return
response = command.execute(username, args, admin)
if not self.silent:
self.chat.submit_message(response)

>>>>>>> master
def execute_script(self, file_name):
logger.debug("Executing script: " + file_name)
print("Executing script: " + file_name)
Expand Down
29 changes: 25 additions & 4 deletions magicked_admin/chatbot/commands/command.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
<<<<<<< HEAD

class Command:
def __init__(self, operator_list, admin=True):
"""
Abstract for creating commands. By default contains the functions (XYZ)
"""
self.admin = admin
self.operator_list = operator_list
self.not_auth_message = "You're not authorised to use that command."

def authorise(self, admin, steam_id=None):
if self.admin and admin:
# Return true if player is logged in as admin regardless of op list
return True

if self.admin and not admin:
# Return true if player is an op even if not logged in as admin
return steam_id in self.operator_list

# If command doesn't require any permission return true
return True

=======
class Command:
"""
Abstract for creating commands. By default contains the functions (XYZ)
"""
def __init__(self, server, admin_only=True):
self.server = server
self.admin_only = admin_only
self.not_auth_message = "You're not authorised to use that command."

def authorise(self, admin):
if admin and self.admin_only:
Expand All @@ -15,5 +35,6 @@ def authorise(self, admin):
else:
return True

>>>>>>> master
def execute(self, username, args, admin):
raise NotImplementedError("Command.execute() not implemented")
61 changes: 60 additions & 1 deletion magicked_admin/chatbot/commands/command_map.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
from chatbot.commands.player_commands import *
from chatbot.commands.info_commands import *
from chatbot.commands.server_commands import *
from chatbot.commands.event_commands import *
#from chatbot.commands.event_commands import *

<<<<<<< HEAD

def build_command_map(server, chatbot, operator_commands):
#wave_event_manager = CommandOnWaveManager(server, chatbot)
#trader_event_manager = CommandOnTraderManager(server, chatbot)
#time_event_manager = CommandOnTimeManager(server, chatbot)

command_map = {
'''''stop_wc': wave_event_manager,
'start_wc': wave_event_manager,
'new_wave': wave_event_manager,
'start_tc': time_event_manager,
'stop_tc': time_event_manager,
'start_trc': trader_event_manager,
'stop_trc': trader_event_manager,
't_close': trader_event_manager,
't_open': trader_event_manager,'''

'say': CommandSay(server.operators,
admin='say' in operator_commands),
'restart': CommandRestart(server.operators, server.web_admin,
admin='restart' in operator_commands),
'toggle_pass': CommandTogglePassword(server.operators, server.web_admin,
admin=
'toggle_pass' in operator_commands),
'silent': CommandSilent(server.operators, chatbot,
admin='silent' in operator_commands),
'length': CommandLength(server.operators, server.web_admin,
admin='length' in operator_commands),
'difficulty': CommandDifficulty(server.operators, server.web_admin,
admin='difficulty' in operator_commands),
'players': CommandPlayers(server.operators, server.data_logger,
admin='players' in operator_commands),
'game': CommandGame(server.operators, server.data_logger,
admin='game' in operator_commands),
'help': CommandHelp(server.operators, server.help_text,
admin='help' in operator_commands),
'info': CommandInfo(server.operators,
admin='info' in operator_commands),
'kills': CommandKills(server.operators, server.data_logger,
admin='kills' in operator_commands),
'dosh': CommandDosh(server.operators, server.data_logger,
admin='dosh' in operator_commands),
'top_kills': CommandTopKills(server.operators, server.data_logger,
server.database_queries,
admin='top_kills' in operator_commands),
'top_dosh': CommandTopDosh(server.operators, server.data_logger,
server.database_queries,
admin='top_dosh' in operator_commands),
'me': CommandMe(server.operators, server.data_logger,
admin='me' in operator_commands),
'stats': CommandStats(server.operators, server.data_logger,
admin='stats' in operator_commands)
}

return command_map

=======

class CommandMap:
def __init__(self, server, chatbot):
Expand Down Expand Up @@ -59,3 +117,4 @@ def generate_map(self):
}

return command_map
>>>>>>> master
35 changes: 33 additions & 2 deletions magicked_admin/chatbot/commands/event_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def run(self):
return
while not self.exit_flag.wait(self.time_interval):
self.chatbot.command_handler("server", self.args, admin=True)
<<<<<<< HEAD


class CommandOnTimeManager(Command):

def __init__(self, operator_list, chatbot, admin=True):
self.command_threads = []
self.chatbot = chatbot

Command.__init__(self, operator_list, admin)

=======


class CommandOnTimeManager(Command):
Expand All @@ -111,6 +123,7 @@ def __init__(self, server, chatbot, admin_only = True):
self.chatbot = chatbot
Command.__init__(self, server, admin_only)

>>>>>>> master
def execute(self, username, args, admin):
if not self.authorise(admin):
return self.not_auth_message
Expand Down Expand Up @@ -142,12 +155,21 @@ def terminate_all(self):
else:
return "Nothing is running."


class CommandOnWaveManager(Command):
<<<<<<< HEAD
def __init__(self, server, chatbot, admin=True):
self.commands = []
self.chatbot = chatbot
Command.__init__(self, server, admin)

=======
def __init__(self, server, chatbot, admin_only = True):
self.commands = []
self.chatbot = chatbot
Command.__init__(self, server, admin_only)

>>>>>>> master
def execute(self, username, args, admin):
if not self.authorise(admin):
return self.not_auth_message
Expand All @@ -172,9 +194,9 @@ def terminate_all(self):
def start_command(self, args):
if len(args) < 2:
return "Missing argument (command)."

game_length = int(self.server.game.length)

try:
wc = CommandOnWave(args[1:], int(args[0]), game_length, self.chatbot)
except ValueError:
Expand All @@ -185,12 +207,21 @@ def start_command(self, args):


class CommandOnTraderManager(Command):
<<<<<<< HEAD
def __init__(self, server, chatbot, admin=True):
self.commands = []
self.chatbot = chatbot

Command.__init__(self, server, admin)

=======
def __init__(self, server, chatbot, admin_only = True):
self.commands = []
self.chatbot = chatbot

Command.__init__(self, server, admin_only)

>>>>>>> master
def execute(self, username, args, admin):
if not self.authorise(admin):
return self.not_auth_message
Expand Down
Loading