Skip to content

Commit

Permalink
Additional changes for moloch--#515
Browse files Browse the repository at this point in the history
  • Loading branch information
eljeffeg committed Oct 15, 2022
1 parent aa97819 commit f2c5335
Show file tree
Hide file tree
Showing 27 changed files with 191 additions and 92 deletions.
4 changes: 2 additions & 2 deletions alembic/versions/31918b83c372_add_box_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@


# revision identifiers, used by Alembic.
revision = '31918b83c372'
down_revision = '83f862086ff0'
revision = "31918b83c372"
down_revision = "83f862086ff0"
branch_labels = None
depends_on = None

Expand Down
32 changes: 32 additions & 0 deletions alembic/versions/de5d615ae090_add_user_to_penalty_stat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""add user to penalty stat
Revision ID: de5d615ae090
Revises: 31918b83c372
Create Date: 2022-10-14 19:33:02.808038
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "de5d615ae090"
down_revision = "31918b83c372"
branch_labels = None
depends_on = None


def upgrade():
op.add_column("penalty", sa.Column("user_id", sa.INTEGER))
op.create_foreign_key(
"penalty_ibfk_3",
"penalty",
"user",
["user_id"],
["id"],
ondelete="SET NULL",
)


def downgrade():
op.drop_column("penalty", "user_id")
69 changes: 31 additions & 38 deletions handlers/AdminHandlers/AdminGameObjectHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import re
import json

from past.utils import old_div
from handlers.BaseHandlers import BaseHandler
from models.Box import Box, FlagsSubmissionType
from models.Corporation import Corporation
Expand Down Expand Up @@ -408,8 +407,10 @@ def post(self, *args, **kwargs):
}
flag_uuid = self.get_argument("flag_uuid", "")
team_uuid = self.get_argument("team_uuid", "")
user_uuid = self.get_argument("user_uuid", "")
flag = Flag.by_uuid(flag_uuid)
team = Team.by_uuid(team_uuid)
user = User.by_uuid(user_uuid)
errors = []
success = []
if flag:
Expand All @@ -418,20 +419,23 @@ def post(self, *args, **kwargs):
answer_token = self.get_argument("answer_token", "")
if point_restore == "on" and team:
if options.penalize_flag_value:
value = int(
flag.dynamic_value(team) * (options.flag_penalty_cost * 0.01)
)
team.money += value
penalty = Penalty.by_team_token(flag, team, answer_token)
if penalty:
value = penalty.cost()
if value > 0:
team.money += value
if user:
user.money += value
self.dbsession.add(user)
self.dbsession.add(team)
self.event_manager.admin_score_update(
team,
"%s penalty reversed - score has been updated."
% team.name,
value,
)
self.dbsession.delete(penalty)
self.dbsession.add(team)
self.dbsession.commit()
self.event_manager.admin_score_update(
team,
"%s penalty reversed - score has been updated." % team.name,
value,
)
self.dbsession.commit()
if flag not in team.flags:
flag_value = flag.dynamic_value(team)
if (
Expand All @@ -445,6 +449,10 @@ def post(self, *args, **kwargs):
self.dbsession.add(tm)
self.event_manager.flag_decayed(tm, flag)
team.money += flag_value
if user:
user.money += flag_value
user.flags.append(flag)
self.dbsession.add(user)
team.flags.append(flag)
self.dbsession.add(team)
self.dbsession.commit()
Expand Down Expand Up @@ -689,8 +697,7 @@ def edit_boxes(self):
order = self.get_argument("order", None)
if order and int(order) != box.order:
logging.info(
"Updated %s's box order %s -> %s"
% (box.name, box.order, order)
"Updated %s's box order %s -> %s" % (box.name, box.order, order)
)
box.order = order
# Avatar
Expand Down Expand Up @@ -1215,42 +1222,28 @@ def post(self, *args, **kwargs):

def attempts(self, flag):
attempts = []
teamcount = {}
for item in Penalty.by_flag_id(flag.id):
team = Team.by_id(item.team_id)
if team.id in teamcount:
teamcount.update({team.id: teamcount[team.id] + 1})
else:
teamcount.update({team.id: 1})
user_uuid = ""
if item.user_id:
user = User.by_id(item.user_id)
if user:
user_uuid = user.uuid
if team:
if team.id in teamcount:
teamcount.update({team.id: teamcount[team.id] + 1})
else:
teamcount.update({team.id: 1})
entries = {
"name": team.name,
"token": item.token,
"flag": flag.uuid,
"team": team.uuid,
"user": user_uuid,
"type": flag.type,
}
if options.penalize_flag_value:
penalty = "-"
penalty = item.cost()
prefix = "-" if penalty > 0 else ""
if options.banking:
penalty += "$"
if (
teamcount[team.id] < options.flag_start_penalty
or teamcount[team.id] > options.flag_stop_penalty
):
penalty += "0"
else:
penalty += str(
int(
flag.dynamic_value(team)
* (options.flag_penalty_cost * 0.01)
)
)
entries.update({"penalty": penalty})
prefix = prefix + "$"
entries.update({"penalty": prefix + str(penalty)})
attempts.append(entries)
return attempts

Expand Down
2 changes: 1 addition & 1 deletion handlers/MissionsHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def failed_capture(self, flag, submission):
if submission is not None and flag not in user.team.flags:
if flag.is_file:
submission = Flag.digest(submission)
Penalty.create_attempt(team=user.team, flag=flag, submission=submission)
Penalty.create_attempt(user=user, flag=flag, submission=submission)
if not self.config.penalize_flag_value:
return False
attempts = Penalty.by_count(flag, user.team)
Expand Down
6 changes: 3 additions & 3 deletions handlers/PublicHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,9 @@ def create_validate_message(self, user, token):
account = urlsafe_b64encode(account)
token = urlsafe_b64encode(token)
if options.ssl:
origin = options.origin.replace(
"wss://", "https://"
).replace("ws://", "https://")
origin = options.origin.replace("wss://", "https://").replace(
"ws://", "https://"
)
else:
origin = options.origin.replace("ws://", "http://")
validate_url = "%s/registration/token?u=%s&t=%s" % (origin, account, token)
Expand Down
2 changes: 1 addition & 1 deletion handlers/UpgradeHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def purchase_code(self, box):
def render_page(self, errors=None):
"""Adds extra params to render()"""
user = self.get_current_user()
boxes = [box for box in Box.all() if box.source_code is not None]
boxes = [box for box in sorted(Box.all()) if box.source_code is not None]
self.render(
"upgrades/source_code_market.html", user=user, boxes=boxes, errors=errors
)
Expand Down
6 changes: 3 additions & 3 deletions handlers/UserHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ def get(self, *args, **kwargs):
if uuid is None and user.is_admin():
self.timer()
self.render(
"admin/home.html",
"admin/home.html",
user=user,
boxcount=len(Box.all()),
teamcount=len(gamestate),
teamcount=len(gamestate),
usercount=len(User.all_users()),
activeconnections=activeconnections
activeconnections=activeconnections,
)
else:
game_started = self.application.settings["game_started"] or user.is_admin()
Expand Down
6 changes: 3 additions & 3 deletions libs/Scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def now(self, app):
@classmethod
def update_gamestate(self, app, background=False):
if background:
t = Thread(target=self._update_gamestate, args = (self, app), daemon=True)
t = Thread(target=self._update_gamestate, args=(self, app), daemon=True)
t.start()
else:
self._update_gamestate(self, app)
Expand Down Expand Up @@ -117,7 +117,7 @@ def _update_gamestate(self, app):
"lvl_count": len(team.level_flags(level.number)),
"lvl_unlock": level in team.game_levels,
}
for box in level.boxes:
for box in sorted(level.boxes):
game_state["levels"][level.name]["boxes"][box.uuid] = {
"name": box.name,
"locked": box.locked,
Expand All @@ -129,7 +129,7 @@ def _update_gamestate(self, app):
game_state["levels"][level.name]["boxes"][box.uuid]["teams"][
team.name
] = {"box_count": len(team.box_flags(box))}
for flag in box.flags:
for flag in sorted(box.flags):
game_state["levels"][level.name]["boxes"][box.uuid]["flags"][
flag.uuid
] = {"name": flag.name}
Expand Down
22 changes: 15 additions & 7 deletions models/Box.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ def flaglist(self, box_id=None):
flaglist[flag.uuid] = flag.name
return flaglist

@property
def corporation(self):
return Corporation.by_id(self.corporation_id)

@property
def game_level(self):
return GameLevel.by_id(self.game_level_id)

@property
def category(self):
return Category.by_id(self.category_id)

@property
def flags(self):
flags = []
Expand Down Expand Up @@ -203,7 +215,6 @@ def order(self, value):
value = int(value)
if value == self.order:
return
print("Box %s setting to %d" % (self.name, value))
i = 1
boxes = self.all()
for box in boxes:
Expand All @@ -214,7 +225,6 @@ def order(self, value):
else:
box._order = i
i += 1
print("Box %s set to %d" % (box.name, box.order))

@property
def operating_system(self):
Expand Down Expand Up @@ -421,23 +431,21 @@ def to_xml(self, parent):

def to_dict(self):
"""Returns editable data as a dictionary"""
corp = Corporation.by_id(self.corporation_id)
game_level = GameLevel.by_id(self.game_level_id)
cat = Category.by_id(self.category_id)
cat = self.category
if cat:
category = cat.uuid
else:
category = ""
return {
"name": self.name,
"uuid": self.uuid,
"corporation": corp.uuid,
"corporation": self.corporation.uuid,
"category": category,
"operating_system": self.operating_system,
"description": self._description,
"capture_message": self.capture_message,
"difficulty": self.difficulty,
"game_level": game_level.uuid,
"game_level": self.game_level.uuid,
"flag_submission_type": self.flag_submission_type,
"flaglist": self.flaglist(self.id),
"value": str(self.value),
Expand Down
31 changes: 31 additions & 0 deletions models/Flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,34 @@ def to_dict(self):

def __repr__(self):
return "<Flag - name:%s, type:%s >" % (self.name, str(self._type))

def __str__(self):
return self.name

def __cmp__(self, other):
"""Compare based on the order"""
this, that = self.order, other.order
if this > that:
return 1
elif this == that:
return 0
else:
return -1

def __eq__(self, other):
return self.id == other.id

def __ne__(self, other):
return not self.__eq__(other)

def __gt__(self, other):
return self.__cmp__(other) > 0

def __lt__(self, other):
return self.__cmp__(other) < 0

def __ge__(self, other):
return self.__cmp__(other) >= 0

def __le__(self, other):
return self.__cmp__(other) <= 0
2 changes: 1 addition & 1 deletion models/GameLevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def flags(self):
"""Return all flags for the level"""
_flags = []
for box in self.boxes:
_flags += box.flags
_flags += sorted(box.flags)
return _flags

def to_xml(self, parent):
Expand Down
Loading

0 comments on commit f2c5335

Please sign in to comment.