Skip to content

Commit

Permalink
fix: bad variable naming, inconsistencies, bugs here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
jeenyuhs committed Jul 9, 2024
1 parent b80a074 commit f699cb0
Show file tree
Hide file tree
Showing 19 changed files with 1,008 additions and 1,063 deletions.
326 changes: 137 additions & 189 deletions constants/commands.py

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions constants/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class ScoringType(IntEnum):
SCORE_V2 = 3

@classmethod
def find_value(cls, name: str) -> "ScoringType":
c = cls(0)
def from_name(cls, name: str) -> "ScoringType":
scoring_type = cls(0)

if name == "sv2":
return c.__class__.SCORE_V2
return scoring_type.__class__.SCORE_V2

if name.upper() in c.__class__.__dict__:
return c.__class__.__dict__[name.upper()]
if name.upper() in scoring_type.__class__.__dict__:
return scoring_type.__class__.__dict__[name.upper()]

return c
return scoring_type
2 changes: 1 addition & 1 deletion constants/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ActionStatus(IntEnum):
PAUSED = 10
LOBBY = 11
MULTIPLAYING = 12
OSUDIRECT = 13
OSU_DIRECT = 13


country_codes = {
Expand Down
6 changes: 5 additions & 1 deletion constants/playmode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import unique, IntEnum
from typing import Iterable


@unique
Expand Down Expand Up @@ -61,8 +62,11 @@ class Gamemode(IntEnum):
VANILLA = 0
RELAX = 1

def __iter__(self) -> Iterable:
return iter((self.VANILLA, self.RELAX))

@property
def table(self):
def to_db(self):
return (
"stats"
if self == self.VANILLA
Expand Down
50 changes: 24 additions & 26 deletions events/bancho.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from enum import IntEnum
from enum import IntEnum, unique
import os
import time
import copy
Expand All @@ -15,7 +15,7 @@
from constants import commands as cmd
from rina_pp_pyb import GameMode, Performance, Beatmap as BMap

from constants.match import *
from constants.match import SlotStatus, SlotTeams
from constants.mods import Mods
from objects.player import LoggingType, Player
from objects.channel import Channel
Expand Down Expand Up @@ -350,7 +350,7 @@ async def login(req: Request) -> Response:
###
"status": player.status.name,
"status_text": player.status_text,
"beatmap_id": player.beatmap_id,
"beatmap_id": player.map_id,
},
) # type: ignore

Expand All @@ -377,7 +377,7 @@ async def login(req: Request) -> Response:

target.enqueue(writer.user_presence(player) + writer.update_stats(player))

if target.bot:
if target.is_bot:
response += writer.bot_presence()
else:
response += writer.user_presence(target)
Expand All @@ -404,11 +404,11 @@ async def login(req: Request) -> Response:
@register_event(ClientPackets.CHANGE_ACTION, restricted=True)
async def change_action(player: Player, sr: Reader) -> None:
player.status = ActionStatus(sr.read_byte())
status_text = sr.read_str()
player.beatmap_md5 = sr.read_str()
status_text = sr.read_string()
player.map_md5 = sr.read_string()
player.current_mods = Mods(sr.read_uint32())
player.play_mode = Mode(sr.read_byte())
player.beatmap_id = sr.read_int32()
player.map_id = sr.read_int32()

player.gamemode = (
Gamemode.RELAX if player.current_mods & Mods.RELAX else Gamemode.VANILLA
Expand All @@ -426,7 +426,7 @@ async def change_action(player: Player, sr: Reader) -> None:
###
"status": player.status.name,
"status_text": player.status_text,
"beatmap_id": player.beatmap_id,
"beatmap_id": player.map_id,
},
) # type: ignore

Expand All @@ -444,10 +444,10 @@ async def _handle_command(channel: Channel, msg: str, player: Player):
async def send_public_message(player: Player, sr: Reader) -> None:
# sender; but unused since
# we know who sent it lol
sr.read_str()
sr.read_string()

msg = sr.read_str()
channel_name = sr.read_str()
msg = sr.read_string()
channel_name = sr.read_string()

sr.read_int32() # sender id

Expand Down Expand Up @@ -485,9 +485,7 @@ async def send_public_message(player: Player, sr: Reader) -> None:
# if so, post the 100%, 99%, etc.
# pp for the map.
if now_playing := services.regex["np"].search(msg):
beatmap = await Beatmap._get_beatmap_from_sql(
beatmap_id=int(now_playing.group(1))
)
beatmap = await Beatmap._get_beatmap_from_sql(map_id=int(now_playing.group(1)))

if not beatmap:
player.shout(
Expand Down Expand Up @@ -594,22 +592,22 @@ async def unable_to_spec(player: Player, sr: Reader) -> None:
async def send_private_message(player: Player, sr: Reader) -> None:
# sender - but unused, since we already know
# who the sender is lol
sr.read_str()
sr.read_string()

msg = sr.read_str()
recipent_id = sr.read_str()
msg = sr.read_string()
recipent_id = sr.read_string()

sr.read_int32() # sender id

if not (recipent := services.players.get(recipent_id)):
player.shout("The player you're trying to reach is currently offline.")
return

if not recipent.bot:
if not recipent.is_bot:
player.send(msg, recipent)
else:
if now_playing := services.regex["np"].search(msg):
beatmap = await Beatmap.get_beatmap(beatmap_id=int(now_playing.group(1)))
beatmap = await Beatmap.get(map_id=int(now_playing.group(1)))

if not beatmap:
return
Expand Down Expand Up @@ -659,7 +657,7 @@ async def mp_join(player: Player, sr: Reader) -> None:
match_id = sr.read_int32()
match = services.matches.get(match_id)

match_password = sr.read_str()
match_password = sr.read_string()

if player.match or not match:
player.enqueue(writer.match_fail())
Expand Down Expand Up @@ -847,7 +845,7 @@ async def mp_score_update(player: Player, sr: Reader) -> None:
raw_sr = copy.copy(sr)

raw = raw_sr.read_raw()
score_frame = sr.read_scoreframe()
score_frame = sr.read_score_frame()

if match.pp_win_condition and match.map is not None:
if os.path.isfile(f".data/beatmaps/{match.map.map_id}.osu"):
Expand Down Expand Up @@ -1097,7 +1095,7 @@ async def skip_request(player: Player, sr: Reader) -> None:
# id: 63
@register_event(ClientPackets.CHANNEL_JOIN, restricted=True)
async def join_channel(player: Player, sr: Reader) -> None:
channel_name = sr.read_str()
channel_name = sr.read_string()
channel = services.channels.get(channel_name)

if not channel:
Expand Down Expand Up @@ -1172,7 +1170,7 @@ async def mp_change_team(player: Player, sr: Reader) -> None:
# id: 78
@register_event(ClientPackets.CHANNEL_PART, restricted=True)
async def part_channel(player: Player, sr: Reader) -> None:
channel_name = sr.read_str()
channel_name = sr.read_string()

# if the channel_name doesn't start with "#",
# it means, they're parting from DM, which is
Expand All @@ -1193,7 +1191,7 @@ async def part_channel(player: Player, sr: Reader) -> None:
@register_event(ClientPackets.USER_STATS_REQUEST, restricted=True)
async def request_stats(player: Player, sr: Reader) -> None:
# people id's that current online rn
user_ids = sr.read_i32_list()
user_ids = sr.read_int32_list()

if len(user_ids) > 32:
return
Expand Down Expand Up @@ -1259,7 +1257,7 @@ async def change_pass(player: Player, sr: Reader) -> None:
@register_event(ClientPackets.USER_PRESENCE_REQUEST, restricted=True)
async def request_presence(player: Player, sr: Reader) -> None:
# people id's that current online rn
user_ids = sr.read_i32_list()
user_ids = sr.read_int32_list()

if len(user_ids) > 256:
return
Expand All @@ -1271,7 +1269,7 @@ async def request_presence(player: Player, sr: Reader) -> None:
if not (target := services.players.get(user_id)):
continue

if target.bot:
if target.is_bot:
player.enqueue(writer.bot_presence())
else:
player.enqueue(writer.user_presence(target))
Expand Down
85 changes: 42 additions & 43 deletions events/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@osu.route("/web/osu-osz2-bmsubmit-getid.php")
@check_auth("u", "h", b"5\nAuthentication failure. Please check your login details.")
async def get_last_id(req: Request, p: Player) -> Response:
async def get_last_id(request: Request, player: Player) -> Response:
# arguments:
# s = BeatmapSetId (if available)
# b = BeatmapIds (comma separated list)
Expand All @@ -37,23 +37,22 @@ async def get_last_id(req: Request, p: Player) -> Response:
everything else - Unknown error occured
"""

if p.is_restricted:
if player.is_restricted:
return Response(content=b"5\nYour account is currently restricted.")

BASE_ID_INCREMENT = 100_000_000

if p.id not in (1000, 1106):
if player.id not in (1000, 1106):
return Response(content=b"6\nNo permission to upload (yet)")

await p.update_latest_activity()
await player.update_latest_activity()

set_id = int(req.query_params["s"])
set_id = int(request.query_params["s"])
services.logger.debug(set_id)
map_ids = req.query_params["b"].split(",")
old_osz2_hash = req.query_params["z"]
map_ids = request.query_params["b"].split(",")
old_osz2_hash = request.query_params["z"]

new_submit = old_osz2_hash == ""
services.logger.debug(new_submit)
osz2_available = False

if set_id < BASE_ID_INCREMENT and set_id != -1:
Expand All @@ -64,19 +63,19 @@ async def get_last_id(req: Request, p: Player) -> Response:
# check if penis map exist in database
# also check if the set_id is below base_id_increment
# (that would mean it's not from rina)
beatmap = await services.database.fetch_one(
map = await services.database.fetch_one(
"SELECT server, creator_id FROM beatmaps " "WHERE set_id = :set_id LIMIT 1",
{"set_id": set_id},
)

if beatmap:
if map:
# if this beatmap is already in the system/existed
if beatmap["server"] == "bancho":
if map["server"] == "bancho":
return Response(
content=b"7\nYou're not allowed to update bancho maps. (error 2)"
)

if beatmap["creator_id"] != p.id:
if map["creator_id"] != player.id:
return Response(
content=b"1\nThe beatmap you're trying to submit isn't yours!"
)
Expand Down Expand Up @@ -162,65 +161,65 @@ async def beatmap_submission(req: Request, p: Player) -> Response:
return Response(content=b"error while parsing osz2")

set_id = form["s"]
beatmaps = osz2_data.extract_osu_files()
maps = osz2_data.extract_osu_files()
metadata = osz2_data.metadata

for beatmap in beatmaps:
# TODO: .osu specifically made for this, instead of using external libraries
attributes = BMap(bytes=beatmap.raw_data)
for child_map in maps:
# TODO: .osu parser specifically made for this, instead of using external libraries
attributes = BMap(bytes=child_map.raw_data)
difficulty = Performance().calculate(attributes).difficulty

bmap = await services.beatmaps.get_by_map_id(beatmap._map_id) or Beatmap()
map = await services.beatmaps.get_by_map_id(child_map._map_id) or Beatmap()

bmap.server = "rina"
bmap.title = metadata.title
bmap.artist = metadata.artist
map.server = "rina"
map.title = metadata.title
map.artist = metadata.artist

osu_file_regex = services.regex[".osu"].search(beatmap.name)
osu_file_regex = services.regex[".osu"].search(child_map.name)

if not osu_file_regex:
return Response(content=b"Unexpected error.")

bmap.artist = osu_file_regex.group(1)
bmap.artist_unicode = metadata.artist_unicode
map.artist = osu_file_regex.group(1)
map.artist_unicode = metadata.artist_unicode

bmap.title = osu_file_regex.group(2)
bmap.title_unicode = metadata.title_unicode
map.title = osu_file_regex.group(2)
map.title_unicode = metadata.title_unicode

bmap.version = osu_file_regex.group(4)
map.version = osu_file_regex.group(4)

bmap.creator = p.username
bmap.creator_id = p.id
map.creator = p.username
map.creator_id = p.id

bmap.set_id = metadata.set_id
bmap.map_id = beatmap._map_id
map.set_id = metadata.set_id
map.map_id = child_map._map_id

bmap.ar = attributes.ar
bmap.od = attributes.od
bmap.hp = attributes.hp
bmap.cs = attributes.cs
bmap.bpm = attributes.bpm
bmap.mode = attributes.mode.value
map.ar = attributes.ar
map.od = attributes.od
map.hp = attributes.hp
map.cs = attributes.cs
map.bpm = attributes.bpm
map.mode = attributes.mode.value

bmap.stars = difficulty.stars
bmap.max_combo = difficulty.max_combo
bmap.map_md5 = hashlib.md5(beatmap.raw_data).digest().hex()
map.stars = difficulty.stars
map.max_combo = difficulty.max_combo
map.map_md5 = hashlib.md5(child_map.raw_data).digest().hex()

# TODO: proper beatmap update
if old_map := await services.database.fetch_one(
"SELECT map_md5 FROM beatmaps WHERE map_id = :map_id",
{"map_id": bmap.map_id},
{"map_id": map.map_id},
):
await services.database.execute(
"DELETE FROM beatmaps WHERE map_md5 = :map_md5",
{"map_md5": old_map["map_md5"]},
)

await bmap.add_to_db()
await map.add_to_db()

# save .osu file in .data/beatmaps
with open(f".data/beatmaps/{bmap.map_id}.osu", "wb+") as beatmap_file:
beatmap_file.write(beatmap.raw_data)
with open(f".data/beatmaps/{map.map_id}.osu", "wb+") as osu_file:
osu_file.write(child_map.raw_data)

# response with "0" if everything went right, okay
return Response(content=b"0")
Loading

0 comments on commit f699cb0

Please sign in to comment.