Upload files to "core"
This commit is contained in:
210
core/discord.py
Normal file
210
core/discord.py
Normal file
@@ -0,0 +1,210 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
from core.config import discord as _discord_cfg, base_url as _BASE_URL
|
||||
|
||||
_WEBHOOK_PUBLIC = _discord_cfg.get("webhook_public", "")
|
||||
_WEBHOOK_ADMIN = _discord_cfg.get("webhook_admin", "")
|
||||
_WEBHOOK_ANTICHEAT = _discord_cfg.get("webhook_anticheat", "")
|
||||
|
||||
_throttle: dict[str, float] = {}
|
||||
_throttle_lock = threading.Lock()
|
||||
|
||||
_PUBLIC_GLOBAL_COOLDOWN = 8
|
||||
|
||||
_CONCURRENT_MILESTONES = {5, 10, 20, 50, 100}
|
||||
_concurrent_peak = 0
|
||||
_concurrent_peak_lock = threading.Lock()
|
||||
|
||||
_FIRST_LOGIN_FILE = os.path.join(
|
||||
os.path.dirname(os.path.dirname(__file__)), "tracking", "discord_first_login.json"
|
||||
)
|
||||
_first_login_notified: set[int] = set()
|
||||
_first_login_lock = threading.Lock()
|
||||
|
||||
_signup_times: list[float] = []
|
||||
_signup_lock = threading.Lock()
|
||||
_SIGNUP_BURST_WINDOW = 60
|
||||
_SIGNUP_BURST_THRESHOLD = 5
|
||||
|
||||
|
||||
def _load_state() -> None:
|
||||
global _first_login_notified, _concurrent_peak
|
||||
try:
|
||||
with open(_FIRST_LOGIN_FILE) as f:
|
||||
data = json.load(f)
|
||||
with _first_login_lock:
|
||||
_first_login_notified = set(data.get("notified", []))
|
||||
with _concurrent_peak_lock:
|
||||
_concurrent_peak = int(data.get("peak", 0))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _save_state() -> None:
|
||||
try:
|
||||
os.makedirs(os.path.dirname(_FIRST_LOGIN_FILE), exist_ok=True)
|
||||
with _first_login_lock:
|
||||
notified = list(_first_login_notified)
|
||||
with _concurrent_peak_lock:
|
||||
peak = _concurrent_peak
|
||||
with open(_FIRST_LOGIN_FILE, "w") as f:
|
||||
json.dump({"notified": notified, "peak": peak}, f)
|
||||
except Exception as e:
|
||||
print(f"[Discord] Failed to save state: {e}")
|
||||
|
||||
|
||||
def mark_first_login(pid: int) -> bool:
|
||||
with _first_login_lock:
|
||||
if pid in _first_login_notified:
|
||||
return False
|
||||
_first_login_notified.add(pid)
|
||||
threading.Thread(target=_save_state, daemon=True).start()
|
||||
return True
|
||||
|
||||
|
||||
def record_signup() -> bool:
|
||||
now = time.monotonic()
|
||||
with _signup_lock:
|
||||
_signup_times.append(now)
|
||||
cutoff = now - _SIGNUP_BURST_WINDOW
|
||||
while _signup_times and _signup_times[0] < cutoff:
|
||||
_signup_times.pop(0)
|
||||
count = len(_signup_times)
|
||||
return count >= _SIGNUP_BURST_THRESHOLD and count % _SIGNUP_BURST_THRESHOLD == 0
|
||||
|
||||
|
||||
def _can_fire(key: str, cooldown: float) -> bool:
|
||||
now = time.monotonic()
|
||||
with _throttle_lock:
|
||||
if now - _throttle.get(key, 0) < cooldown:
|
||||
return False
|
||||
_throttle[key] = now
|
||||
return True
|
||||
|
||||
|
||||
def _send(url: str, payload: dict) -> None:
|
||||
try:
|
||||
data = json.dumps(payload).encode()
|
||||
req = urllib.request.Request(
|
||||
url, data=data,
|
||||
headers={"Content-Type": "application/json", "User-Agent": "KittyRecBot/1.0"},
|
||||
method="POST",
|
||||
)
|
||||
urllib.request.urlopen(req, timeout=5)
|
||||
except Exception as e:
|
||||
print(f"[Discord] Webhook failed: {e}")
|
||||
|
||||
|
||||
def _fire(url: str, payload: dict) -> None:
|
||||
threading.Thread(target=_send, args=(url, payload), daemon=True).start()
|
||||
|
||||
|
||||
def send_public(
|
||||
title: str,
|
||||
color: int,
|
||||
fields: list,
|
||||
description: str = None,
|
||||
thumbnail_url: str = None,
|
||||
throttle_key: str = "global",
|
||||
throttle_secs: float = None,
|
||||
) -> None:
|
||||
if throttle_secs is None:
|
||||
throttle_secs = _PUBLIC_GLOBAL_COOLDOWN
|
||||
if not _can_fire(f"pub_{throttle_key}", throttle_secs):
|
||||
return
|
||||
embed = {"title": title, "color": color, "fields": fields}
|
||||
if description:
|
||||
embed["description"] = description
|
||||
if thumbnail_url:
|
||||
embed["thumbnail"] = {"url": thumbnail_url}
|
||||
_fire(_WEBHOOK_PUBLIC, {"embeds": [embed]})
|
||||
|
||||
|
||||
def send_admin(title: str, color: int, fields: list, description: str = None) -> None:
|
||||
embed = {"title": title, "color": color, "fields": fields}
|
||||
if description:
|
||||
embed["description"] = description
|
||||
_fire(_WEBHOOK_ADMIN, {"embeds": [embed]})
|
||||
|
||||
|
||||
def send_security(title: str, color: int, fields: list, description: str = None, throttle_key: str = None, throttle_secs: float = 30) -> None:
|
||||
if throttle_key and not _can_fire(f"sec_{throttle_key}", throttle_secs):
|
||||
return
|
||||
url = _WEBHOOK_ANTICHEAT or _WEBHOOK_ADMIN
|
||||
if not url:
|
||||
return
|
||||
embed = {"title": f":warning: {title}", "color": color, "fields": fields}
|
||||
if description:
|
||||
embed["description"] = description
|
||||
_fire(url, {"embeds": [embed]})
|
||||
|
||||
|
||||
def avatar_url(profile_image_name) -> str | None:
|
||||
if not profile_image_name:
|
||||
return None
|
||||
return f"{_BASE_URL}/alt/{profile_image_name}"
|
||||
|
||||
|
||||
def check_concurrent_milestone(count: int) -> None:
|
||||
global _concurrent_peak
|
||||
with _concurrent_peak_lock:
|
||||
if count <= _concurrent_peak:
|
||||
return
|
||||
crossed = sorted(m for m in _CONCURRENT_MILESTONES if _concurrent_peak < m <= count)
|
||||
_concurrent_peak = count
|
||||
if not crossed:
|
||||
threading.Thread(target=_save_state, daemon=True).start()
|
||||
return
|
||||
threading.Thread(target=_save_state, daemon=True).start()
|
||||
for milestone in crossed:
|
||||
send_public(
|
||||
title="Server Milestone!",
|
||||
color=0x2ECC71,
|
||||
fields=[
|
||||
{"name": "Players Online", "value": str(milestone), "inline": True},
|
||||
{"name": "All-Time Peak", "value": str(count), "inline": True},
|
||||
],
|
||||
description=f"KittyRec just hit **{milestone} players online** at the same time!",
|
||||
throttle_key=f"milestone_{milestone}",
|
||||
throttle_secs=0,
|
||||
)
|
||||
|
||||
|
||||
def notify_player_login(username: str, pid: int) -> None:
|
||||
if not _WEBHOOK_PUBLIC:
|
||||
return
|
||||
send_public(
|
||||
title="Player Online",
|
||||
color=0x2ECC71,
|
||||
fields=[
|
||||
{"name": "Username", "value": username, "inline": True},
|
||||
{"name": "Player ID", "value": str(pid), "inline": True},
|
||||
],
|
||||
description=f"**{username}** just logged in to KittyRec!",
|
||||
throttle_key=f"login_{pid}",
|
||||
throttle_secs=300,
|
||||
)
|
||||
|
||||
|
||||
def notify_player_logout(username: str, pid: int) -> None:
|
||||
if not _WEBHOOK_PUBLIC:
|
||||
return
|
||||
send_public(
|
||||
title="Player Offline",
|
||||
color=0x95A5A6,
|
||||
fields=[
|
||||
{"name": "Username", "value": username, "inline": True},
|
||||
{"name": "Player ID", "value": str(pid), "inline": True},
|
||||
],
|
||||
description=f"**{username}** logged out of KittyRec.",
|
||||
throttle_key=f"logout_{pid}",
|
||||
throttle_secs=300,
|
||||
)
|
||||
|
||||
|
||||
_load_state()
|
||||
27
core/gift_dispatch.py
Normal file
27
core/gift_dispatch.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import datetime as _dt
|
||||
from core.state import db
|
||||
|
||||
|
||||
def dispatch_gift(
|
||||
pid: int,
|
||||
activity_guid: str | None,
|
||||
gift_context: int = 2,
|
||||
session_id: int | None = None,
|
||||
) -> dict | None:
|
||||
from routes.avatar import _get_gift_gen_lock, _generate_gift_for_player_locked
|
||||
from routes.sessions import session_gift_count, session_gift_increment
|
||||
|
||||
if activity_guid and session_gift_count(pid, activity_guid) > 0:
|
||||
return None
|
||||
|
||||
date_str = _dt.date.today().isoformat()
|
||||
if not db.try_record_daily_gift(pid, date_str, activity_guid or "", max_count=20):
|
||||
return None
|
||||
|
||||
with _get_gift_gen_lock(pid):
|
||||
gift = _generate_gift_for_player_locked(pid, activity_guid=activity_guid, gift_context=gift_context)
|
||||
|
||||
if activity_guid:
|
||||
session_gift_increment(pid, activity_guid)
|
||||
|
||||
return gift
|
||||
20
core/limiter.py
Normal file
20
core/limiter.py
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
from flask import request as _req
|
||||
from flask_limiter import Limiter
|
||||
|
||||
|
||||
def _get_client_ip():
|
||||
cf_ip = _req.headers.get("Cf-Connecting-Ip", "").strip()
|
||||
if cf_ip:
|
||||
return cf_ip
|
||||
xff = _req.headers.get("X-Forwarded-For", "").strip()
|
||||
if xff:
|
||||
return xff.split(",")[0].strip()
|
||||
return _req.remote_addr or "127.0.0.1"
|
||||
|
||||
|
||||
limiter = Limiter(
|
||||
_get_client_ip,
|
||||
default_limits=[],
|
||||
storage_uri="memory://",
|
||||
)
|
||||
111
core/logging_setup.py
Normal file
111
core/logging_setup.py
Normal file
@@ -0,0 +1,111 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
import traceback
|
||||
import uuid
|
||||
from logging.handlers import TimedRotatingFileHandler
|
||||
|
||||
|
||||
_INSTANCE_ID = uuid.uuid4().hex[:8]
|
||||
|
||||
|
||||
def _make_rotating_handler(path, fmt):
|
||||
h = TimedRotatingFileHandler(
|
||||
path, when="midnight", backupCount=3, encoding="utf-8", utc=True,
|
||||
)
|
||||
h.suffix = "%Y-%m-%d"
|
||||
h.setFormatter(logging.Formatter(fmt, datefmt="%Y-%m-%d %H:%M:%S"))
|
||||
return h
|
||||
|
||||
|
||||
class _Tee:
|
||||
def __init__(self, stream, file_log, err_log, sec_log, level=logging.INFO):
|
||||
self._stream = stream
|
||||
self._file_log = file_log
|
||||
self._err_log = err_log
|
||||
self._sec_log = sec_log
|
||||
self._level = level
|
||||
self._buf = ""
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def _flush_line(self, line):
|
||||
if not line.strip():
|
||||
return
|
||||
self._file_log.log(self._level, line)
|
||||
if self._level >= logging.WARNING:
|
||||
self._err_log.log(self._level, line)
|
||||
if "[SECURITY]" in line:
|
||||
self._sec_log.log(logging.WARNING, line)
|
||||
|
||||
def write(self, data):
|
||||
self._stream.write(data)
|
||||
try:
|
||||
with self._lock:
|
||||
self._buf += data
|
||||
while "\n" in self._buf:
|
||||
line, self._buf = self._buf.split("\n", 1)
|
||||
self._flush_line(line)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def flush(self):
|
||||
self._stream.flush()
|
||||
|
||||
def fileno(self):
|
||||
return self._stream.fileno()
|
||||
|
||||
def isatty(self):
|
||||
return False
|
||||
|
||||
|
||||
def setup_logging(base_dir):
|
||||
log_dir = os.path.join(base_dir, "logs")
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
log_path = os.path.join(log_dir, "server.log")
|
||||
err_path = os.path.join(log_dir, "errors.log")
|
||||
|
||||
file_log = logging.getLogger("kittyrec.out")
|
||||
file_log.setLevel(logging.DEBUG)
|
||||
file_log.propagate = False
|
||||
file_log.addHandler(_make_rotating_handler(log_path, "%(asctime)s %(message)s"))
|
||||
|
||||
err_log = logging.getLogger("kittyrec.err")
|
||||
err_log.setLevel(logging.WARNING)
|
||||
err_log.propagate = False
|
||||
err_log.addHandler(_make_rotating_handler(err_path, "%(asctime)s [%(levelname)s] %(message)s"))
|
||||
|
||||
sec_log = logging.getLogger("kittyrec.sec")
|
||||
sec_log.setLevel(logging.WARNING)
|
||||
sec_log.propagate = False
|
||||
sec_log.addHandler(_make_rotating_handler(os.path.join(log_dir, "security.log"), "%(asctime)s %(message)s"))
|
||||
|
||||
access_log = logging.getLogger("kittyrec.access")
|
||||
access_log.setLevel(logging.DEBUG)
|
||||
access_log.propagate = False
|
||||
access_log.addHandler(_make_rotating_handler(os.path.join(log_dir, "access.log"), "%(asctime)s %(message)s"))
|
||||
|
||||
stdout_orig = sys.__stdout__
|
||||
stderr_orig = sys.__stderr__
|
||||
sys.stdout = _Tee(stdout_orig, file_log, err_log, sec_log, logging.INFO)
|
||||
sys.stderr = _Tee(stderr_orig, file_log, err_log, sec_log, logging.WARNING)
|
||||
|
||||
orig_excepthook = threading.excepthook
|
||||
|
||||
def _thread_excepthook(args):
|
||||
print(f"[THREAD ERROR] Uncaught exception in thread '{args.thread.name if args.thread else '?'}':")
|
||||
traceback.print_exception(args.exc_type, args.exc_value, args.exc_traceback)
|
||||
if orig_excepthook:
|
||||
orig_excepthook(args)
|
||||
|
||||
threading.excepthook = _thread_excepthook
|
||||
|
||||
logging.basicConfig(
|
||||
stream=stderr_orig,
|
||||
level=logging.WARNING,
|
||||
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
logging.getLogger("werkzeug").setLevel(logging.ERROR)
|
||||
|
||||
return file_log, err_log, access_log, _INSTANCE_ID
|
||||
120
core/models.py
Normal file
120
core/models.py
Normal file
@@ -0,0 +1,120 @@
|
||||
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
sql_db = SQLAlchemy()
|
||||
|
||||
|
||||
class Profile(sql_db.Model):
|
||||
__tablename__ = "profiles"
|
||||
id = sql_db.Column(sql_db.Integer, primary_key=True)
|
||||
platform = sql_db.Column(sql_db.String(50), nullable=False, default="")
|
||||
platform_id = sql_db.Column(sql_db.String(100), nullable=False, default="")
|
||||
username = sql_db.Column(sql_db.String(100), nullable=False)
|
||||
display_name = sql_db.Column(sql_db.String(100), nullable=False)
|
||||
xp = sql_db.Column(sql_db.Integer, default=0)
|
||||
level = sql_db.Column(sql_db.Integer, default=1)
|
||||
reputation = sql_db.Column(sql_db.Integer, default=0)
|
||||
developer = sql_db.Column(sql_db.Boolean, default=False)
|
||||
banned = sql_db.Column(sql_db.Boolean, default=False)
|
||||
ban_reason = sql_db.Column(sql_db.String(256), default="")
|
||||
banned_until = sql_db.Column(sql_db.String(50))
|
||||
banned_by = sql_db.Column(sql_db.String(100), default="")
|
||||
tokens = sql_db.Column(sql_db.Integer, default=500)
|
||||
bio = sql_db.Column(sql_db.Text, default="")
|
||||
created_at = sql_db.Column(sql_db.String(50))
|
||||
|
||||
gifts = sql_db.relationship("Gift", backref="profile", lazy="dynamic")
|
||||
ban_logs = sql_db.relationship("BanLog", backref="profile", lazy="dynamic")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Profile {self.username}>"
|
||||
|
||||
def __str__(self):
|
||||
return self.username
|
||||
|
||||
|
||||
class Gift(sql_db.Model):
|
||||
__tablename__ = "gifts"
|
||||
id = sql_db.Column(sql_db.Integer, primary_key=True)
|
||||
profile_id = sql_db.Column(sql_db.Integer, sql_db.ForeignKey("profiles.id"), nullable=False)
|
||||
avatar_item_desc = sql_db.Column(sql_db.Text, default="")
|
||||
consumable_item_desc = sql_db.Column(sql_db.Text, default="")
|
||||
equipment_prefab_name = sql_db.Column(sql_db.Text, default="")
|
||||
equipment_modification_guid = sql_db.Column(sql_db.Text, default="")
|
||||
xp = sql_db.Column(sql_db.Integer, default=0)
|
||||
gift_rarity = sql_db.Column(sql_db.Integer, default=0)
|
||||
message = sql_db.Column(sql_db.Text, default="")
|
||||
gift_context = sql_db.Column(sql_db.Integer, default=0)
|
||||
consumed = sql_db.Column(sql_db.Boolean, default=False)
|
||||
created_at = sql_db.Column(sql_db.String(50))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Gift #{self.id}>"
|
||||
|
||||
|
||||
class BanLog(sql_db.Model):
|
||||
__tablename__ = "ban_log"
|
||||
id = sql_db.Column(sql_db.Integer, primary_key=True)
|
||||
profile_id = sql_db.Column(sql_db.Integer, sql_db.ForeignKey("profiles.id"), nullable=False)
|
||||
action = sql_db.Column(sql_db.String(20), nullable=False)
|
||||
reason = sql_db.Column(sql_db.Text, default="")
|
||||
duration_hours = sql_db.Column(sql_db.Integer, default=0)
|
||||
banned_until = sql_db.Column(sql_db.String(50))
|
||||
admin_name = sql_db.Column(sql_db.String(100), default="")
|
||||
created_at = sql_db.Column(sql_db.String(50))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<BanLog #{self.id} {self.action}>"
|
||||
|
||||
|
||||
class Room(sql_db.Model):
|
||||
__tablename__ = "rooms"
|
||||
id = sql_db.Column(sql_db.Integer, primary_key=True)
|
||||
creator_id = sql_db.Column(sql_db.Integer, nullable=False)
|
||||
name = sql_db.Column(sql_db.String(200), default="")
|
||||
description = sql_db.Column(sql_db.Text, default="")
|
||||
activity_level_id = sql_db.Column(sql_db.String(100), default="")
|
||||
accessibility = sql_db.Column(sql_db.Integer, default=0)
|
||||
is_sandbox = sql_db.Column(sql_db.Boolean, default=False)
|
||||
instanced = sql_db.Column(sql_db.Boolean, default=True)
|
||||
max_players = sql_db.Column(sql_db.Integer, default=20)
|
||||
special_gamemode = sql_db.Column(sql_db.Integer, default=0)
|
||||
state = sql_db.Column(sql_db.Integer, default=0)
|
||||
created_at = sql_db.Column(sql_db.String(50))
|
||||
modified_at = sql_db.Column(sql_db.String(50))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Room {self.name}>"
|
||||
|
||||
|
||||
class AdminTodo(sql_db.Model):
|
||||
__tablename__ = "admin_todos"
|
||||
id = sql_db.Column(sql_db.Integer, primary_key=True)
|
||||
title = sql_db.Column(sql_db.String(256), nullable=False)
|
||||
description = sql_db.Column(sql_db.Text, default="")
|
||||
priority = sql_db.Column(sql_db.Integer, default=1)
|
||||
status = sql_db.Column(sql_db.String(20), default="open")
|
||||
assigned_to = sql_db.Column(sql_db.String(100), default="")
|
||||
created_by = sql_db.Column(sql_db.String(100), default="")
|
||||
created_at = sql_db.Column(sql_db.String(50))
|
||||
completed_at = sql_db.Column(sql_db.String(50))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Todo #{self.id} {self.title[:30]}>"
|
||||
|
||||
|
||||
class AdminIssue(sql_db.Model):
|
||||
__tablename__ = "admin_issues"
|
||||
id = sql_db.Column(sql_db.Integer, primary_key=True)
|
||||
title = sql_db.Column(sql_db.String(256), nullable=False)
|
||||
description = sql_db.Column(sql_db.Text, default="")
|
||||
category = sql_db.Column(sql_db.String(50), default="bug")
|
||||
severity = sql_db.Column(sql_db.String(20), default="medium")
|
||||
status = sql_db.Column(sql_db.String(20), default="open")
|
||||
reported_by = sql_db.Column(sql_db.String(100), default="")
|
||||
assigned_to = sql_db.Column(sql_db.String(100), default="")
|
||||
created_at = sql_db.Column(sql_db.String(50))
|
||||
resolved_at = sql_db.Column(sql_db.String(50))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Issue #{self.id} {self.title[:30]}>"
|
||||
Reference in New Issue
Block a user