Skip to content

Commit

Permalink
#389 #393 Admin panel start/stop/pause/resume
Browse files Browse the repository at this point in the history
  • Loading branch information
eljeffeg committed Aug 27, 2020
1 parent 251e084 commit 6e486c4
Show file tree
Hide file tree
Showing 28 changed files with 215 additions and 168 deletions.
63 changes: 35 additions & 28 deletions handlers/AdminHandlers/AdminGameHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,43 +70,50 @@ class AdminGameHandler(BaseHandler):
@authorized(ADMIN_PERMISSION)
def post(self, *args, **kwargs):
start_game = self.get_argument("start_game", None)
suspend_reg = self.get_argument("suspend_registration", "false")
freeze_score = self.get_argument("freeze_scoreboard", "false")
stop_timer = self.get_argument("stop_timer", "off")
suspend_reg = self.get_argument("suspend_registration", None)
freeze_score = self.get_argument("freeze_scoreboard", None)
stop_timer = self.get_argument("stop_timer", None)

if start_game:
if start_game and start_game != str(self.application.settings["game_started"]).lower():
if self.get_argument("start_game", "") == "true":
self.start_game()
else:
self.stop_game()
if stop_timer == "on":
self.application.settings["stop_timer"] = True
else:
self.application.settings["stop_timer"] = False
if suspend_reg == "true":
self.application.settings["suspend_registration"] = True
elif suspend_reg == "false":
self.application.settings["suspend_registration"] = False
if freeze_score == "false":
self.application.settings["freeze_scoreboard"] = False
if self.application.settings["temp_global_notifications"] is not None:
options.global_notification = self.application.settings[
if stop_timer and self.isOn(stop_timer) != self.application.settings["stop_timer"]:
if self.isOn(stop_timer):
self.application.settings["stop_timer"] = True
else:
self.application.settings["stop_timer"] = False
if suspend_reg and suspend_reg != str(self.application.settings["suspend_registration"]).lower():
if suspend_reg == "true":
self.application.settings["suspend_registration"] = True
elif suspend_reg == "false":
self.application.settings["suspend_registration"] = False
if freeze_score and freeze_score != str(self.application.settings["freeze_scoreboard"]).lower():
if freeze_score == "false":
self.application.settings["freeze_scoreboard"] = False
self.application.settings["stop_timer"] = False
if self.application.settings["temp_global_notifications"] is not None:
options.global_notification = self.application.settings[
"temp_global_notifications"
]
self.application.settings["temp_global_notifications"] = None
self.event_manager.push_scoreboard()
elif freeze_score:
diff = 60 * int(float(freeze_score))
self.application.settings["freeze_scoreboard"] = time.time() + diff
self.application.settings[
"temp_global_notifications"
]
self.application.settings["temp_global_notifications"] = None
self.event_manager.push_scoreboard()

elif freeze_score:
diff = 60 * int(freeze_score)
self.application.settings["freeze_scoreboard"] = time.time() + diff
self.application.settings[
"temp_global_notifications"
] = options.global_notification
options.global_notification = False
self.event_manager.push_scoreboard()
] = options.global_notification
options.global_notification = False
self.event_manager.push_scoreboard()

self.redirect("/user")

def isOn(self, value):
return value == "on"



class AdminMessageHandler(BaseHandler):

Expand Down
12 changes: 1 addition & 11 deletions handlers/BaseHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def timer(self):
if timerdiff <= 0:
timerdiff = 0
if self.application.settings["stop_timer"]:
self.application.settings["stop_timer"] = False
self.stop_game()
timer = str(timerdiff)
return timer
Expand All @@ -255,27 +256,16 @@ def start_game(self):
self.application.settings["history_callback"].start()
if self.config.use_bots:
self.application.settings["score_bots_callback"].start()
self.set_all_users_lock(False)

def stop_game(self):
""" Stop the game and all callbacks """
if self.application.settings["game_started"]:
logging.info("The game is stopping ...")
self.application.settings["game_started"] = False
self.application.settings["suspend_registration"] = False
self.application.settings["freeze_scoreboard"] = False
if self.application.settings["history_callback"]._running:
self.application.settings["history_callback"].stop()
if self.application.settings["score_bots_callback"]._running:
self.application.settings["score_bots_callback"].stop()
self.set_all_users_lock(True)

def set_all_users_lock(self, lock):
""" Set the lock attribute on all accounts """
for user in User.all_users():
user.locked = lock
self.dbsession.add(user)
self.dbsession.commit()

def get_user_locale(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions handlers/ErrorHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ def get(self, *args, **kwargs):
self.render("public/403.html", locked=locked, xsrf=False)


class StopHandler(BaseHandler):
def get(self, *args, **kwargs):
""" Renders the Game Stopped page """
self.render(
"public/stopped.html", errors=None, info=["The game is currently stopped."]
)


class NoobHandler(BaseHandler):
def get(self, *args, **kwargs):
""" Renders the noob page """
Expand Down
4 changes: 3 additions & 1 deletion handlers/MarketHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
from .BaseHandlers import BaseHandler
from models.MarketItem import MarketItem
from models.Team import Team
from libs.SecurityDecorators import authenticated, use_black_market
from libs.SecurityDecorators import authenticated, use_black_market, game_started


class MarketViewHandler(BaseHandler):

""" Renders views of items in the market """

@authenticated
@game_started
@use_black_market
def get(self, *args, **kwargs):
""" Renders the main table """
user = self.get_current_user()
self.render("market/view.html", user=user, errors=None)

@authenticated
@game_started
@use_black_market
def post(self, *args, **kwargs):
""" Called to purchase an item """
Expand Down
43 changes: 25 additions & 18 deletions handlers/MaterialsHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,31 @@
class MaterialsHandler(BaseHandler):
@authenticated
def get(self, *args, **kwargs):
subdir = ""
if len(args) == 1:
subdir = "/" + args[0] + "/"
if self.show_materials():
subdir = ""
if len(args) == 1:
subdir = "/" + args[0] + "/"

self.render("file_upload/material_files.html", errors=None, subdir=subdir)
self.render("file_upload/material_files.html", errors=None, subdir=subdir)
else:
self.redirect("/gamestatus")

@authenticated
def post(self, *args, **kwargs):
d = options.game_materials_dir
if len(args) == 1:
tmp = os.path.join(os.path.abspath(d), args[0])
if is_directory_traversal(tmp):
logging.warn(
"%s attempted to use a directory traversal" % self.request.remote_ip
)
self.redirect(self.application.settings["forbidden_url"])
return
d = os.path.join(d, args[0])
self.write(json.dumps(self.path_to_dict(d)))
if self.show_materials():
d = options.game_materials_dir
if len(args) == 1:
tmp = os.path.join(os.path.abspath(d), args[0])
if is_directory_traversal(tmp):
logging.warn(
"%s attempted to use a directory traversal" % self.request.remote_ip
)
self.redirect(self.application.settings["forbidden_url"])
return
d = os.path.join(d, args[0])
self.write(json.dumps(self.path_to_dict(d)))
else:
self.redirect("/gamestatus")

def path_to_dict(self, path):
d = {"text": os.path.basename(path)}
Expand Down Expand Up @@ -81,14 +87,16 @@ def path_to_dict(self, path):
d["icon"] = "file ext_%s" % e
return d

def show_materials(self):
return self.application.settings["game_started"] or options.game_materials_on_stop


def is_directory_traversal(file_name):
materials_root_directory = os.path.abspath(options.game_materials_dir)
requested_path = os.path.abspath(file_name)
common_prefix = os.path.commonprefix([requested_path, materials_root_directory])
return common_prefix != materials_root_directory


def has_materials():
d = options.game_materials_dir
i = 0
Expand All @@ -97,8 +105,7 @@ def has_materials():
continue
else:
i += 1
return i > 0

return i > 0

def has_box_materials(box):
if not options.use_box_materials_dir:
Expand Down
Loading

0 comments on commit 6e486c4

Please sign in to comment.