├── .editorconfig ├── .github └── workflows │ ├── build-and-draft-release.yml │ └── main.yml ├── .gitignore ├── .pre-commit-config.yaml ├── .sourcery.yaml ├── LICENSE ├── README.md ├── README.zh_Hans.md ├── assets ├── banner.png └── logo.svg ├── pyproject.toml ├── requirements.dev.txt ├── requirements.txt ├── src └── arcaea_offline │ ├── __init__.py │ ├── calculate │ ├── __init__.py │ ├── b30.py │ ├── score.py │ └── world_step.py │ ├── database.py │ ├── external │ ├── __init__.py │ ├── andreal │ │ ├── __init__.py │ │ ├── account.py │ │ └── api_data.py │ ├── arcaea │ │ ├── __init__.py │ │ ├── common.py │ │ ├── online.py │ │ ├── packlist.py │ │ ├── songlist.py │ │ └── st3.py │ ├── arcsong │ │ ├── __init__.py │ │ ├── arcsong_db.py │ │ └── arcsong_json.py │ ├── chart_info_db │ │ ├── __init__.py │ │ └── parser.py │ ├── exports │ │ ├── __init__.py │ │ ├── exporters.py │ │ └── types.py │ └── smartrte │ │ ├── __init__.py │ │ └── b30_csv.py │ ├── models │ ├── __init__.py │ ├── common.py │ ├── config.py │ ├── scores.py │ └── songs.py │ ├── searcher.py │ ├── singleton.py │ └── utils │ ├── __init__.py │ ├── partner.py │ ├── rating.py │ ├── score.py │ └── search_title.py ├── tests ├── calculate │ └── test_world_step.py └── db │ ├── __init__.py │ ├── db.py │ └── models │ ├── __init__.py │ └── test_songs.py └── tox.ini /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*.py] 4 | indent_size = 4 5 | indent_style = space 6 | 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.github/workflows/build-and-draft-release.yml: -------------------------------------------------------------------------------- 1 | name: "Build and draft a release" 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | tags: 7 | - "v[0-9]+.[0-9]+.[0-9]+" 8 | 9 | permissions: 10 | contents: write 11 | discussions: write 12 | 13 | jobs: 14 | build-and-draft-release: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: Checkout 18 | uses: actions/checkout@v4 19 | with: 20 | fetch-depth: 0 21 | 22 | - name: Set up Python environment 23 | uses: actions/setup-python@v5 24 | with: 25 | python-version: "3.x" 26 | 27 | - name: Build package 28 | run: | 29 | pip install build 30 | python -m build 31 | 32 | - name: Remove `v` in tag name 33 | uses: mad9000/actions-find-and-replace-string@5 34 | id: tagNameReplaced 35 | with: 36 | source: ${{ github.ref_name }} 37 | find: "v" 38 | replace: "" 39 | 40 | - name: Draft a release 41 | uses: softprops/action-gh-release@v2 42 | with: 43 | discussion_category_name: New releases 44 | draft: true 45 | generate_release_notes: true 46 | files: | 47 | dist/arcaea_offline-${{ steps.tagNameReplaced.outputs.value }}*.whl 48 | dist/arcaea-offline-${{ steps.tagNameReplaced.outputs.value }}.tar.gz 49 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: test & lint 2 | 3 | on: 4 | push: 5 | branches: 6 | - '*' 7 | pull_request: 8 | types: [opened, reopened] 9 | workflow_dispatch: 10 | 11 | jobs: 12 | pytest: 13 | runs-on: ubuntu-latest 14 | strategy: 15 | matrix: 16 | python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] 17 | 18 | steps: 19 | - uses: actions/checkout@v4 20 | - uses: actions/setup-python@v5 21 | with: 22 | python-version: ${{ matrix.python-version }} 23 | 24 | - name: Install dev dependencies 25 | run: 'pip install .[dev]' 26 | - name: Run tests 27 | run: 'pytest -v' 28 | 29 | ruff: 30 | runs-on: ubuntu-latest 31 | steps: 32 | - uses: actions/checkout@v4 33 | - uses: actions/setup-python@v5 34 | with: 35 | python-version: '3.12' 36 | 37 | - name: Install dev dependencies 38 | run: 'pip install .[dev]' 39 | - name: Run linter 40 | run: 'ruff check' 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __debug* 2 | arcsong.db 3 | arcaea_offline.db 4 | .vscode 5 | 6 | # Byte-compiled / optimized / DLL files 7 | __pycache__/ 8 | *.py[cod] 9 | *$py.class 10 | 11 | # C extensions 12 | *.so 13 | 14 | # Distribution / packaging 15 | .Python 16 | build/ 17 | develop-eggs/ 18 | dist/ 19 | downloads/ 20 | eggs/ 21 | .eggs/ 22 | lib/ 23 | lib64/ 24 | parts/ 25 | sdist/ 26 | var/ 27 | wheels/ 28 | share/python-wheels/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | MANIFEST 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .nox/ 48 | .coverage 49 | .coverage.* 50 | .cache 51 | nosetests.xml 52 | coverage.xml 53 | *.cover 54 | *.py,cover 55 | .hypothesis/ 56 | .pytest_cache/ 57 | cover/ 58 | 59 | # Translations 60 | *.mo 61 | *.pot 62 | 63 | # Django stuff: 64 | *.log 65 | local_settings.py 66 | db.sqlite3 67 | db.sqlite3-journal 68 | 69 | # Flask stuff: 70 | instance/ 71 | .webassets-cache 72 | 73 | # Scrapy stuff: 74 | .scrapy 75 | 76 | # Sphinx documentation 77 | docs/_build/ 78 | 79 | # PyBuilder 80 | .pybuilder/ 81 | target/ 82 | 83 | # Jupyter Notebook 84 | .ipynb_checkpoints 85 | 86 | # IPython 87 | profile_default/ 88 | ipython_config.py 89 | 90 | # pyenv 91 | # For a library or package, you might want to ignore these files since the code is 92 | # intended to run in multiple environments; otherwise, check them in: 93 | # .python-version 94 | 95 | # pipenv 96 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 97 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 98 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 99 | # install all needed dependencies. 100 | #Pipfile.lock 101 | 102 | # poetry 103 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 104 | # This is especially recommended for binary packages to ensure reproducibility, and is more 105 | # commonly ignored for libraries. 106 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 107 | #poetry.lock 108 | 109 | # pdm 110 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 111 | #pdm.lock 112 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 113 | # in version control. 114 | # https://pdm.fming.dev/#use-with-ide 115 | .pdm.toml 116 | 117 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 118 | __pypackages__/ 119 | 120 | # Celery stuff 121 | celerybeat-schedule 122 | celerybeat.pid 123 | 124 | # SageMath parsed files 125 | *.sage.py 126 | 127 | # Environments 128 | .env 129 | .venv 130 | env/ 131 | venv/ 132 | ENV/ 133 | env.bak/ 134 | venv.bak/ 135 | 136 | # Spyder project settings 137 | .spyderproject 138 | .spyproject 139 | 140 | # Rope project settings 141 | .ropeproject 142 | 143 | # mkdocs documentation 144 | /site 145 | 146 | # mypy 147 | .mypy_cache/ 148 | .dmypy.json 149 | dmypy.json 150 | 151 | # Pyre type checker 152 | .pyre/ 153 | 154 | # pytype static type analyzer 155 | .pytype/ 156 | 157 | # Cython debug symbols 158 | cython_debug/ 159 | 160 | # PyCharm 161 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 162 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 163 | # and can be added to the global gitignore or merged into this file. For a more nuclear 164 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 165 | #.idea/ 166 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v4.4.0 4 | hooks: 5 | - id: end-of-file-fixer 6 | - id: trailing-whitespace 7 | 8 | - repo: https://github.com/astral-sh/ruff-pre-commit 9 | rev: v0.4.4 10 | hooks: 11 | - id: ruff 12 | args: ["--fix"] 13 | - id: ruff-format 14 | -------------------------------------------------------------------------------- /.sourcery.yaml: -------------------------------------------------------------------------------- 1 | rule_settings: 2 | python_version: '3.8' 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arcaea Offline 2 | 3 | English | [简体中文](./README.zh_Hans.md) 4 | 5 | 6 | 7 | 8 | 9 | > Accept lrlowiro 10 | 11 | ## WIP 12 | 13 | > [!CAUTION] 14 | > This project is under active development, thus it is unstable and API may change frequently. 15 | 16 | > [!IMPORTANT] 17 | > v0.3.0 is under development, check out [this branch](https://github.com/283375/arcaea-offline/tree/0.3.0-refactor)! 18 | > 19 | > Once v0.3.0 is ready for release, this repository will be transferred to *[ArcaeaOffline](https://github.com/ArcaeaOffline)/core-python* 20 | 21 | ## What is this? 22 | 23 | This is the core library of `Arcaea Offline`, designed to manage player scores, calculate their potential, and provide various useful tools. 24 | 25 | ## How to use this? 26 | 27 | This repository is a python library. 28 | 29 | For general users, if you don't know what is a "library", you may be interested about [this GUI](https://github.com/283375/arcaea-offline-pyside-ui). 30 | 31 | For developers, the documentation is under construction. Check back later! 32 | 33 | ## License 34 | 35 | This file is part of arcaea-offline. 36 | 37 | arcaea-offline is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 38 | 39 | arcaea-offline is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 40 | 41 | You should have received a copy of the GNU General Public License along with arcaea-offline. If not, see . 42 | 43 | ## Credits 44 | 45 | [Arcaea-Infinity/ArcaeaSongDatabase](https://github.com/Arcaea-Infinity/ArcaeaSongDatabase) 46 | -------------------------------------------------------------------------------- /README.zh_Hans.md: -------------------------------------------------------------------------------- 1 | # Arcaea Offline 2 | 3 | 4 | 5 | 6 | 7 | > 接受 lrlowiro 的一切 8 | 9 | ## WIP 10 | 11 | > [!CAUTION] 12 | > 该项目正处于早期开发阶段,不能保证稳定性,且 API 可能随时变动。 13 | 14 | > [!IMPORTANT] 15 | > v0.3.0 正在[此分支](https://github.com/283375/arcaea-offline/tree/0.3.0-refactor)下开发! 16 | > 17 | > 在 v0.3.0 准备好发布后,此存储库将被迁移至 *[ArcaeaOffline](https://github.com/ArcaeaOffline)/core-python*。 18 | 19 | ## 这是什么? 20 | 21 | 这是 `Arcaea Offline` 的核心依赖库,用于维护分数数据库、计算潜力值,并提供一些实用工具。 22 | 23 | ## 这怎么用? 24 | 25 | 该仓库是一个 python 库。 26 | 27 | 对普通用户,如果你不知道“库”是什么,你应该对[这个 GUI](https://github.com/283375/arcaea-offline-pyside-ui) 更感兴趣。 28 | 29 | 对开发者,文档仍在建设当中,敬请期待。 30 | 31 | ## 许可声明 32 | 33 | 本文件是 arcaea-offline 的一部分。 34 | 35 | arcaea-offline 是自由软件:你可以再分发之和/或依照由自由软件基金会发布的 GNU 通用公共许可证修改之,无论是版本 3 许可证,还是(按你的决定)任何以后版都可以。 36 | 37 | 发布 arcaea-offline 是希望它能有用,但是并无保障;甚至连可销售和符合某个特定的目的都不保证。请参看 GNU 通用公共许可证,了解详情。 38 | 39 | 你应该随程序获得一份 GNU 通用公共许可证的复本。如果没有,请看 。 40 | 41 | ## Credits 42 | 43 | [Arcaea-Infinity/ArcaeaSongDatabase](https://github.com/Arcaea-Infinity/ArcaeaSongDatabase) 44 | -------------------------------------------------------------------------------- /assets/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/283375/arcaea-offline/908613306fdeeb584ce34400b4435e114342c190/assets/banner.png -------------------------------------------------------------------------------- /assets/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 15 | 17 | 21 | 25 | 26 | 28 | 32 | 36 | 37 | 45 | 53 | 54 | 56 | 60 | 64 | 68 | 73 | 78 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=61.0"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [project] 6 | name = "arcaea-offline" 7 | version = "0.2.2" 8 | authors = [{ name = "283375", email = "log_283375@163.com" }] 9 | description = "Manage your local Arcaea score database." 10 | readme = "README.md" 11 | requires-python = ">=3.8" 12 | dependencies = [ 13 | "beautifulsoup4==4.12.2", 14 | "SQLAlchemy==2.0.20", 15 | "SQLAlchemy-Utils==0.41.1", 16 | "Whoosh==2.7.4", 17 | ] 18 | classifiers = [ 19 | "Development Status :: 3 - Alpha", 20 | "Programming Language :: Python :: 3", 21 | ] 22 | 23 | [project.optional-dependencies] 24 | dev = ["ruff~=0.4", "pre-commit~=3.3", "pytest~=7.4", "tox~=4.11"] 25 | 26 | [project.urls] 27 | "Homepage" = "https://github.com/283375/arcaea-offline" 28 | "Bug Tracker" = "https://github.com/283375/arcaea-offline/issues" 29 | 30 | [tool.pyright] 31 | ignore = ["build/"] 32 | 33 | [tool.ruff.lint] 34 | # Full list: https://docs.astral.sh/ruff/rules 35 | select = [ 36 | "E", # pycodestyle (Error) 37 | "W", # pycodestyle (Warning) 38 | "F", # pyflakes 39 | "I", # isort 40 | "PL", # pylint 41 | "N", # pep8-naming 42 | "FBT", # flake8-boolean-trap 43 | "A", # flake8-builtins 44 | "DTZ", # flake8-datetimez 45 | "LOG", # flake8-logging 46 | "Q", # flake8-quotes 47 | "G", # flake8-logging-format 48 | "PIE", # flake8-pie 49 | "PT", # flake8-pytest-style 50 | ] 51 | ignore = [ 52 | "E501", # line-too-long 53 | ] 54 | -------------------------------------------------------------------------------- /requirements.dev.txt: -------------------------------------------------------------------------------- 1 | ruff~=0.4 2 | pre-commit~=3.3 3 | pytest~=7.4 4 | tox~=4.11 5 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | beautifulsoup4==4.12.2 2 | SQLAlchemy==2.0.20 3 | SQLAlchemy-Utils==0.41.1 4 | Whoosh==2.7.4 5 | -------------------------------------------------------------------------------- /src/arcaea_offline/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/283375/arcaea-offline/908613306fdeeb584ce34400b4435e114342c190/src/arcaea_offline/__init__.py -------------------------------------------------------------------------------- /src/arcaea_offline/calculate/__init__.py: -------------------------------------------------------------------------------- 1 | from .b30 import calculate_b30, get_b30_calculated_list 2 | from .score import ( 3 | calculate_constants_from_play_rating, 4 | calculate_play_rating, 5 | calculate_score_modifier, 6 | calculate_score_range, 7 | calculate_shiny_pure, 8 | ) 9 | -------------------------------------------------------------------------------- /src/arcaea_offline/calculate/b30.py: -------------------------------------------------------------------------------- 1 | from decimal import Decimal 2 | from typing import Dict, List 3 | 4 | from ..models.scores import ScoreCalculated 5 | 6 | 7 | def get_b30_calculated_list( 8 | calculated_list: List[ScoreCalculated], 9 | ) -> List[ScoreCalculated]: 10 | best_scores: Dict[str, ScoreCalculated] = {} 11 | for calculated in calculated_list: 12 | key = f"{calculated.song_id}_{calculated.rating_class}" 13 | stored = best_scores.get(key) 14 | if stored and stored.score < calculated.score or not stored: 15 | best_scores[key] = calculated 16 | ret_list = list(best_scores.values()) 17 | ret_list = sorted(ret_list, key=lambda c: c.potential, reverse=True)[:30] 18 | return ret_list 19 | 20 | 21 | def calculate_b30(calculated_list: List[ScoreCalculated]) -> Decimal: 22 | ptt_list = [Decimal(c.potential) for c in get_b30_calculated_list(calculated_list)] 23 | sum_ptt_list = sum(ptt_list) 24 | return (sum_ptt_list / len(ptt_list)) if sum_ptt_list else Decimal("0.0") 25 | -------------------------------------------------------------------------------- /src/arcaea_offline/calculate/score.py: -------------------------------------------------------------------------------- 1 | from dataclasses import dataclass 2 | from decimal import Decimal 3 | from math import floor 4 | from typing import Tuple, Union 5 | 6 | 7 | def calculate_score_range(notes: int, pure: int, far: int): 8 | single_note_score = 10000000 / Decimal(notes) 9 | 10 | actual_score = floor( 11 | single_note_score * pure + single_note_score * Decimal(0.5) * far 12 | ) 13 | return (actual_score, actual_score + pure) 14 | 15 | 16 | def calculate_score_modifier(score: int) -> Decimal: 17 | if score >= 10000000: 18 | return Decimal(2) 19 | if score >= 9800000: 20 | return Decimal(1) + (Decimal(score - 9800000) / 200000) 21 | return Decimal(score - 9500000) / 300000 22 | 23 | 24 | def calculate_play_rating(constant: int, score: int) -> Decimal: 25 | score_modifier = calculate_score_modifier(score) 26 | return max(Decimal(0), Decimal(constant) / 10 + score_modifier) 27 | 28 | 29 | def calculate_shiny_pure(notes: int, score: int, pure: int, far: int) -> int: 30 | single_note_score = 10000000 / Decimal(notes) 31 | actual_score = single_note_score * pure + single_note_score * Decimal(0.5) * far 32 | return score - floor(actual_score) 33 | 34 | 35 | @dataclass 36 | class ConstantsFromPlayRatingResult: 37 | # pylint: disable=invalid-name 38 | EXPlus: Tuple[Decimal, Decimal] 39 | EX: Tuple[Decimal, Decimal] 40 | AA: Tuple[Decimal, Decimal] 41 | A: Tuple[Decimal, Decimal] 42 | B: Tuple[Decimal, Decimal] 43 | C: Tuple[Decimal, Decimal] 44 | 45 | 46 | def calculate_constants_from_play_rating(play_rating: Union[Decimal, str, float, int]): 47 | # pylint: disable=no-value-for-parameter 48 | 49 | play_rating = Decimal(play_rating) 50 | 51 | ranges = [] 52 | for upper_score, lower_score in [ 53 | (10000000, 9900000), 54 | (9899999, 9800000), 55 | (9799999, 9500000), 56 | (9499999, 9200000), 57 | (9199999, 8900000), 58 | (8899999, 8600000), 59 | ]: 60 | upper_score_modifier = calculate_score_modifier(upper_score) 61 | lower_score_modifier = calculate_score_modifier(lower_score) 62 | ranges.append( 63 | (play_rating - upper_score_modifier, play_rating - lower_score_modifier) 64 | ) 65 | 66 | return ConstantsFromPlayRatingResult(*ranges) 67 | -------------------------------------------------------------------------------- /src/arcaea_offline/calculate/world_step.py: -------------------------------------------------------------------------------- 1 | from decimal import Decimal 2 | from typing import Literal, Optional, Union 3 | 4 | 5 | class PlayResult: 6 | def __init__( 7 | self, 8 | *, 9 | play_rating: Union[Decimal, str, float, int], 10 | partner_step: Union[Decimal, str, float, int], 11 | ): 12 | self.__play_rating = play_rating 13 | self.__partner_step = partner_step 14 | 15 | @property 16 | def play_rating(self): 17 | return Decimal(self.__play_rating) 18 | 19 | @property 20 | def partner_step(self): 21 | return Decimal(self.__partner_step) 22 | 23 | 24 | class PartnerBonus: 25 | def __init__( 26 | self, 27 | *, 28 | step_bonus: Union[Decimal, str, float, int] = Decimal("0.0"), 29 | final_multiplier: Union[Decimal, str, float, int] = Decimal("1.0"), 30 | ): 31 | self.__step_bonus = step_bonus 32 | self.__final_multiplier = final_multiplier 33 | 34 | @property 35 | def step_bonus(self): 36 | return Decimal(self.__step_bonus) 37 | 38 | @property 39 | def final_multiplier(self): 40 | return Decimal(self.__final_multiplier) 41 | 42 | 43 | AwakenedIlithPartnerBonus = PartnerBonus(step_bonus="6.0") 44 | AwakenedEtoPartnerBonus = PartnerBonus(step_bonus="7.0") 45 | AwakenedLunaPartnerBonus = PartnerBonus(step_bonus="7.0") 46 | 47 | 48 | class AwakenedAyuPartnerBonus(PartnerBonus): 49 | def __init__(self, step_bonus: Union[Decimal, str, float, int]): 50 | super().__init__(step_bonus=step_bonus) 51 | 52 | 53 | AmaneBelowExPartnerBonus = PartnerBonus(final_multiplier="0.5") 54 | 55 | 56 | class MithraTerceraPartnerBonus(PartnerBonus): 57 | def __init__(self, step_bonus: int): 58 | super().__init__(step_bonus=step_bonus) 59 | 60 | 61 | MayaPartnerBonus = PartnerBonus(final_multiplier="2.0") 62 | 63 | 64 | class StepBooster: 65 | def final_value(self) -> Decimal: 66 | raise NotImplementedError() 67 | 68 | 69 | class LegacyMapStepBooster(StepBooster): 70 | def __init__( 71 | self, 72 | stamina: Literal[2, 4, 6], 73 | fragments: Literal[100, 250, 500, None], 74 | ): 75 | self.stamina = stamina 76 | self.fragments = fragments 77 | 78 | @property 79 | def stamina(self): 80 | return self.__stamina 81 | 82 | @stamina.setter 83 | def stamina(self, value: Literal[2, 4, 6]): 84 | if value not in [2, 4, 6]: 85 | raise ValueError("stamina can only be one of [2, 4, 6]") 86 | self.__stamina = value 87 | 88 | @property 89 | def fragments(self): 90 | return self.__fragments 91 | 92 | @fragments.setter 93 | def fragments(self, value: Literal[100, 250, 500, None]): 94 | if value not in [100, 250, 500, None]: 95 | raise ValueError("fragments can only be one of [100, 250, 500, None]") 96 | self.__fragments = value 97 | 98 | def final_value(self) -> Decimal: 99 | stamina_multiplier = Decimal(self.stamina) 100 | fragments_multiplier = Decimal(1) 101 | if self.fragments == 100: 102 | fragments_multiplier = Decimal("1.1") 103 | elif self.fragments == 250: 104 | fragments_multiplier = Decimal("1.25") 105 | elif self.fragments == 500: 106 | fragments_multiplier = Decimal("1.5") 107 | return stamina_multiplier * fragments_multiplier 108 | 109 | 110 | class MemoriesStepBooster(StepBooster): 111 | def final_value(self) -> Decimal: 112 | return Decimal("4.0") 113 | 114 | 115 | def calculate_step_original( 116 | play_result: PlayResult, 117 | *, 118 | partner_bonus: Optional[PartnerBonus] = None, 119 | step_booster: Optional[StepBooster] = None, 120 | ) -> Decimal: 121 | ptt = play_result.play_rating 122 | step = play_result.partner_step 123 | if partner_bonus: 124 | partner_bonus_step = partner_bonus.step_bonus 125 | partner_bonus_multiplier = partner_bonus.final_multiplier 126 | else: 127 | partner_bonus_step = Decimal("0") 128 | partner_bonus_multiplier = Decimal("1.0") 129 | 130 | result = (Decimal("2.45") * ptt.sqrt() + Decimal("2.5")) * (step / 50) 131 | result += partner_bonus_step 132 | result *= partner_bonus_multiplier 133 | if step_booster: 134 | result *= step_booster.final_value() 135 | 136 | return result 137 | 138 | 139 | def calculate_step( 140 | play_result: PlayResult, 141 | *, 142 | partner_bonus: Optional[PartnerBonus] = None, 143 | step_booster: Optional[StepBooster] = None, 144 | ) -> Decimal: 145 | result_original = calculate_step_original( 146 | play_result, partner_bonus=partner_bonus, step_booster=step_booster 147 | ) 148 | 149 | return round(result_original, 1) 150 | 151 | 152 | def calculate_play_rating_from_step( 153 | step: Union[Decimal, str, int, float], 154 | partner_step_value: Union[Decimal, str, int, float], 155 | *, 156 | partner_bonus: Optional[PartnerBonus] = None, 157 | step_booster: Optional[StepBooster] = None, 158 | ): 159 | step = Decimal(step) 160 | partner_step_value = Decimal(partner_step_value) 161 | 162 | # get original play result 163 | if partner_bonus and partner_bonus.final_multiplier: 164 | step /= partner_bonus.final_multiplier 165 | if step_booster: 166 | step /= step_booster.final_value() 167 | 168 | if partner_bonus and partner_bonus.step_bonus: 169 | step -= partner_bonus.step_bonus 170 | 171 | play_rating_sqrt = (Decimal(50) * step - Decimal("2.5") * partner_step_value) / ( 172 | Decimal("2.45") * partner_step_value 173 | ) 174 | return play_rating_sqrt**2 if play_rating_sqrt >= 0 else -(play_rating_sqrt**2) 175 | -------------------------------------------------------------------------------- /src/arcaea_offline/database.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import math 3 | from typing import Iterable, List, Optional, Type, Union 4 | 5 | from sqlalchemy import Engine, func, inspect, select 6 | from sqlalchemy.orm import DeclarativeBase, InstrumentedAttribute, sessionmaker 7 | 8 | from .external.arcsong.arcsong_json import ArcSongJsonBuilder 9 | from .external.exports import ArcaeaOfflineDEFV2_Score, ScoreExport, exporters 10 | from .models.config import ConfigBase, Property 11 | from .models.scores import ( 12 | CalculatedPotential, 13 | Score, 14 | ScoreBest, 15 | ScoreCalculated, 16 | ScoresBase, 17 | ScoresViewBase, 18 | ) 19 | from .models.songs import ( 20 | Chart, 21 | ChartInfo, 22 | Difficulty, 23 | DifficultyLocalized, 24 | Pack, 25 | PackLocalized, 26 | Song, 27 | SongLocalized, 28 | SongsBase, 29 | SongsViewBase, 30 | ) 31 | from .singleton import Singleton 32 | 33 | logger = logging.getLogger(__name__) 34 | 35 | 36 | class Database(metaclass=Singleton): 37 | def __init__(self, engine: Optional[Engine]): 38 | try: 39 | self.__engine 40 | except AttributeError: 41 | self.__engine = None 42 | 43 | if engine is None: 44 | if isinstance(self.engine, Engine): 45 | return 46 | raise ValueError("No sqlalchemy.Engine instance specified before.") 47 | 48 | if not isinstance(engine, Engine): 49 | raise ValueError( 50 | f"A sqlalchemy.Engine instance expected, not {repr(engine)}" 51 | ) 52 | 53 | if isinstance(self.engine, Engine): 54 | logger.warning( 55 | "A sqlalchemy.Engine instance %r has been specified " 56 | "and will be replaced to %r", 57 | self.engine, 58 | engine, 59 | ) 60 | self.engine = engine 61 | 62 | @property 63 | def engine(self) -> Engine: 64 | return self.__engine # type: ignore 65 | 66 | @engine.setter 67 | def engine(self, value: Engine): 68 | if not isinstance(value, Engine): 69 | raise ValueError("Database.engine only accepts sqlalchemy.Engine") 70 | self.__engine = value 71 | self.__sessionmaker = sessionmaker(self.__engine) 72 | 73 | @property 74 | def sessionmaker(self): 75 | return self.__sessionmaker 76 | 77 | # region init 78 | 79 | def init(self, checkfirst: bool = True): 80 | # create tables & views 81 | if checkfirst: 82 | # > https://github.com/kvesteri/sqlalchemy-utils/issues/396 83 | # > view.create_view() causes DuplicateTableError on 84 | # > Base.metadata.create_all(checkfirst=True) 85 | # so if `checkfirst` is True, drop these views before creating 86 | SongsViewBase.metadata.drop_all(self.engine) 87 | ScoresViewBase.metadata.drop_all(self.engine) 88 | 89 | SongsBase.metadata.create_all(self.engine, checkfirst=checkfirst) 90 | SongsViewBase.metadata.create_all(self.engine) 91 | ScoresBase.metadata.create_all(self.engine, checkfirst=checkfirst) 92 | ScoresViewBase.metadata.create_all(self.engine) 93 | ConfigBase.metadata.create_all(self.engine, checkfirst=checkfirst) 94 | 95 | # insert version property 96 | with self.sessionmaker() as session: 97 | stmt = select(Property.value).where(Property.key == "version") 98 | result = session.execute(stmt).fetchone() 99 | if not checkfirst or not result: 100 | session.add(Property(key="version", value="4")) 101 | session.commit() 102 | 103 | def check_init(self) -> bool: 104 | # check table exists 105 | expect_tables = ( 106 | list(SongsBase.metadata.tables.keys()) 107 | + list(ScoresBase.metadata.tables.keys()) 108 | + list(ConfigBase.metadata.tables.keys()) 109 | + [ 110 | Chart.__tablename__, 111 | ScoreCalculated.__tablename__, 112 | ScoreBest.__tablename__, 113 | CalculatedPotential.__tablename__, 114 | ] 115 | ) 116 | return all(inspect(self.engine).has_table(t) for t in expect_tables) 117 | 118 | # endregion 119 | 120 | def version(self) -> Union[int, None]: 121 | stmt = select(Property).where(Property.key == "version") 122 | with self.sessionmaker() as session: 123 | result = session.scalar(stmt) 124 | return None if result is None else int(result.value) 125 | 126 | # region Pack 127 | 128 | def get_packs(self): 129 | stmt = select(Pack) 130 | with self.sessionmaker() as session: 131 | results = list(session.scalars(stmt)) 132 | return results 133 | 134 | def get_pack(self, pack_id: str): 135 | stmt = select(Pack).where(Pack.id == pack_id) 136 | with self.sessionmaker() as session: 137 | result = session.scalar(stmt) 138 | return result 139 | 140 | def get_pack_localized(self, pack_id: str): 141 | stmt = select(PackLocalized).where(PackLocalized.id == pack_id) 142 | with self.sessionmaker() as session: 143 | result = session.scalar(stmt) 144 | return result 145 | 146 | # endregion 147 | 148 | # region Song 149 | 150 | def get_songs(self): 151 | stmt = select(Song) 152 | with self.sessionmaker() as session: 153 | results = list(session.scalars(stmt)) 154 | return results 155 | 156 | def get_songs_by_pack_id(self, pack_id: str): 157 | stmt = select(Song).where(Song.set == pack_id) 158 | with self.sessionmaker() as session: 159 | results = list(session.scalars(stmt)) 160 | return results 161 | 162 | def get_song(self, song_id: str): 163 | stmt = select(Song).where(Song.id == song_id) 164 | with self.sessionmaker() as session: 165 | result = session.scalar(stmt) 166 | return result 167 | 168 | def get_song_localized(self, song_id: str): 169 | stmt = select(SongLocalized).where(SongLocalized.id == song_id) 170 | with self.sessionmaker() as session: 171 | result = session.scalar(stmt) 172 | return result 173 | 174 | # endregion 175 | 176 | # region Difficulty 177 | 178 | def get_difficulties(self): 179 | stmt = select(Difficulty) 180 | with self.sessionmaker() as session: 181 | results = list(session.scalars(stmt)) 182 | return results 183 | 184 | def get_difficulties_by_song_id(self, song_id: str): 185 | stmt = select(Difficulty).where(Difficulty.song_id == song_id) 186 | with self.sessionmaker() as session: 187 | results = list(session.scalars(stmt)) 188 | return results 189 | 190 | def get_difficulties_localized_by_song_id(self, song_id: str): 191 | stmt = select(DifficultyLocalized).where(DifficultyLocalized.song_id == song_id) 192 | with self.sessionmaker() as session: 193 | results = list(session.scalars(stmt)) 194 | return results 195 | 196 | def get_difficulty(self, song_id: str, rating_class: int): 197 | stmt = select(Difficulty).where( 198 | (Difficulty.song_id == song_id) & (Difficulty.rating_class == rating_class) 199 | ) 200 | with self.sessionmaker() as session: 201 | result = session.scalar(stmt) 202 | return result 203 | 204 | def get_difficulty_localized(self, song_id: str, rating_class: int): 205 | stmt = select(DifficultyLocalized).where( 206 | (DifficultyLocalized.song_id == song_id) 207 | & (DifficultyLocalized.rating_class == rating_class) 208 | ) 209 | with self.sessionmaker() as session: 210 | result = session.scalar(stmt) 211 | return result 212 | 213 | # endregion 214 | 215 | # region ChartInfo 216 | 217 | def get_chart_infos(self): 218 | stmt = select(ChartInfo) 219 | with self.sessionmaker() as session: 220 | results = list(session.scalars(stmt)) 221 | return results 222 | 223 | def get_chart_infos_by_song_id(self, song_id: str): 224 | stmt = select(ChartInfo).where(ChartInfo.song_id == song_id) 225 | with self.sessionmaker() as session: 226 | results = list(session.scalars(stmt)) 227 | return results 228 | 229 | def get_chart_info(self, song_id: str, rating_class: int): 230 | stmt = select(ChartInfo).where( 231 | (ChartInfo.song_id == song_id) & (ChartInfo.rating_class == rating_class) 232 | ) 233 | with self.sessionmaker() as session: 234 | result = session.scalar(stmt) 235 | return result 236 | 237 | # endregion 238 | 239 | # region Chart 240 | 241 | def get_charts_by_pack_id(self, pack_id: str): 242 | stmt = select(Chart).where(Chart.set == pack_id) 243 | with self.sessionmaker() as session: 244 | results = list(session.scalars(stmt)) 245 | return results 246 | 247 | def get_charts_by_song_id(self, song_id: str): 248 | stmt = select(Chart).where(Chart.song_id == song_id) 249 | with self.sessionmaker() as session: 250 | results = list(session.scalars(stmt)) 251 | return results 252 | 253 | def get_charts_by_constant(self, constant: int): 254 | stmt = select(Chart).where(Chart.constant == constant) 255 | with self.sessionmaker() as session: 256 | results = list(session.scalars(stmt)) 257 | return results 258 | 259 | def get_chart(self, song_id: str, rating_class: int): 260 | stmt = select(Chart).where( 261 | (Chart.song_id == song_id) & (Chart.rating_class == rating_class) 262 | ) 263 | with self.sessionmaker() as session: 264 | result = session.scalar(stmt) 265 | return result 266 | 267 | # endregion 268 | 269 | # region Score 270 | 271 | def get_scores(self): 272 | stmt = select(Score) 273 | with self.sessionmaker() as session: 274 | results = list(session.scalars(stmt)) 275 | return results 276 | 277 | def get_score(self, score_id: int): 278 | stmt = select(Score).where(Score.id == score_id) 279 | with self.sessionmaker() as session: 280 | result = session.scalar(stmt) 281 | return result 282 | 283 | def get_score_best(self, song_id: str, rating_class: int): 284 | stmt = select(ScoreBest).where( 285 | (ScoreBest.song_id == song_id) & (ScoreBest.rating_class == rating_class) 286 | ) 287 | with self.sessionmaker() as session: 288 | result = session.scalar(stmt) 289 | return result 290 | 291 | def insert_score(self, score: Score): 292 | with self.sessionmaker() as session: 293 | session.add(score) 294 | session.commit() 295 | 296 | def insert_scores(self, scores: Iterable[Score]): 297 | with self.sessionmaker() as session: 298 | session.add_all(scores) 299 | session.commit() 300 | 301 | def update_score(self, score: Score): 302 | if score.id is None: 303 | raise ValueError( 304 | "Cannot determine which score to update, please specify `score.id`" 305 | ) 306 | with self.sessionmaker() as session: 307 | session.merge(score) 308 | session.commit() 309 | 310 | def delete_score(self, score: Score): 311 | with self.sessionmaker() as session: 312 | session.delete(score) 313 | session.commit() 314 | 315 | def recommend_charts(self, play_result: float, bounds: float = 0.1): 316 | base_constant = math.ceil(play_result * 10) 317 | 318 | results = [] 319 | results_id = [] 320 | with self.sessionmaker() as session: 321 | for constant in range(base_constant - 20, base_constant + 1): 322 | # from Pure Memory(EX+) to AA 323 | score_modifier = (play_result * 10 - constant) / 10 324 | if score_modifier >= 2.0: 325 | min_score = 10000000 326 | elif score_modifier >= 1.0: 327 | min_score = 200000 * (score_modifier - 1) + 9800000 328 | else: 329 | min_score = 300000 * score_modifier + 9500000 330 | min_score = int(min_score) 331 | 332 | charts = self.get_charts_by_constant(constant) 333 | for chart in charts: 334 | score_best_stmt = select(ScoreBest).where( 335 | (ScoreBest.song_id == chart.song_id) 336 | & (ScoreBest.rating_class == chart.rating_class) 337 | & (ScoreBest.score >= min_score) 338 | & (play_result - bounds < ScoreBest.potential) 339 | & (ScoreBest.potential < play_result + bounds) 340 | ) 341 | if session.scalar(score_best_stmt): 342 | chart_id = f"{chart.song_id},{chart.rating_class}" 343 | if chart_id not in results_id: 344 | results.append(chart) 345 | results_id.append(chart_id) 346 | 347 | return results 348 | 349 | # endregion 350 | 351 | def get_b30(self): 352 | stmt = select(CalculatedPotential.b30).select_from(CalculatedPotential) 353 | with self.sessionmaker() as session: 354 | result = session.scalar(stmt) 355 | return result 356 | 357 | # region COUNT 358 | 359 | def __count_table(self, base: Type[DeclarativeBase]): 360 | stmt = select(func.count()).select_from(base) 361 | with self.sessionmaker() as session: 362 | result = session.scalar(stmt) 363 | return result or 0 364 | 365 | def __count_column(self, column: InstrumentedAttribute): 366 | stmt = select(func.count(column)) 367 | with self.sessionmaker() as session: 368 | result = session.scalar(stmt) 369 | return result or 0 370 | 371 | def count_packs(self): 372 | return self.__count_column(Pack.id) 373 | 374 | def count_songs(self): 375 | return self.__count_column(Song.id) 376 | 377 | def count_difficulties(self): 378 | return self.__count_table(Difficulty) 379 | 380 | def count_chart_infos(self): 381 | return self.__count_table(ChartInfo) 382 | 383 | def count_complete_chart_infos(self): 384 | stmt = ( 385 | select(func.count()) 386 | .select_from(ChartInfo) 387 | .where((ChartInfo.constant != None) & (ChartInfo.notes != None)) 388 | ) 389 | with self.sessionmaker() as session: 390 | result = session.scalar(stmt) 391 | return result or 0 392 | 393 | def count_charts(self): 394 | return self.__count_table(Chart) 395 | 396 | def count_scores(self): 397 | return self.__count_column(Score.id) 398 | 399 | def count_scores_calculated(self): 400 | return self.__count_table(ScoreCalculated) 401 | 402 | def count_scores_best(self): 403 | return self.__count_table(ScoreBest) 404 | 405 | # endregion 406 | 407 | # region export 408 | 409 | def export_scores(self) -> List[ScoreExport]: 410 | scores = self.get_scores() 411 | return [exporters.score(score) for score in scores] 412 | 413 | def export_scores_def_v2(self) -> ArcaeaOfflineDEFV2_Score: 414 | scores = self.get_scores() 415 | return { 416 | "$schema": "https://arcaeaoffline.sevive.xyz/schemas/def/v2/score.schema.json", 417 | "type": "score", 418 | "version": 2, 419 | "scores": [exporters.score_def_v2(score) for score in scores], 420 | } 421 | 422 | def generate_arcsong(self): 423 | with self.sessionmaker() as session: 424 | arcsong = ArcSongJsonBuilder(session).generate_arcsong_json() 425 | return arcsong 426 | 427 | # endregion 428 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/283375/arcaea-offline/908613306fdeeb584ce34400b4435e114342c190/src/arcaea_offline/external/__init__.py -------------------------------------------------------------------------------- /src/arcaea_offline/external/andreal/__init__.py: -------------------------------------------------------------------------------- 1 | from .api_data import AndrealImageGeneratorApiDataConverter 2 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/andreal/account.py: -------------------------------------------------------------------------------- 1 | class AndrealImageGeneratorAccount: 2 | def __init__( 3 | self, 4 | name: str = "Player", 5 | code: int = 123456789, 6 | rating: int = -1, 7 | character: int = 5, 8 | character_uncapped: bool = False, 9 | ): 10 | self.name = name 11 | self.code = code 12 | self.rating = rating 13 | self.character = character 14 | self.character_uncapped = character_uncapped 15 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/andreal/api_data.py: -------------------------------------------------------------------------------- 1 | from typing import Optional, Union 2 | 3 | from sqlalchemy import select 4 | from sqlalchemy.orm import Session 5 | 6 | from ...models import CalculatedPotential, ScoreBest, ScoreCalculated 7 | from .account import AndrealImageGeneratorAccount 8 | 9 | 10 | class AndrealImageGeneratorApiDataConverter: 11 | def __init__( 12 | self, 13 | session: Session, 14 | account: AndrealImageGeneratorAccount = AndrealImageGeneratorAccount(), 15 | ): 16 | self.session = session 17 | self.account = account 18 | 19 | def account_info(self): 20 | return { 21 | "code": self.account.code, 22 | "name": self.account.name, 23 | "is_char_uncapped": self.account.character_uncapped, 24 | "rating": self.account.rating, 25 | "character": self.account.character, 26 | } 27 | 28 | def score(self, score: Union[ScoreCalculated, ScoreBest]): 29 | return { 30 | "score": score.score, 31 | "health": 75, 32 | "rating": score.potential, 33 | "song_id": score.song_id, 34 | "modifier": score.modifier or 0, 35 | "difficulty": score.rating_class, 36 | "clear_type": score.clear_type or 1, 37 | "best_clear_type": score.clear_type or 1, 38 | "time_played": score.date * 1000 if score.date else 0, 39 | "near_count": score.far, 40 | "miss_count": score.lost, 41 | "perfect_count": score.pure, 42 | "shiny_perfect_count": score.shiny_pure, 43 | } 44 | 45 | def user_info(self, score: Optional[ScoreCalculated] = None): 46 | if not score: 47 | score = self.session.scalar( 48 | select(ScoreCalculated).order_by(ScoreCalculated.date.desc()).limit(1) 49 | ) 50 | if not score: 51 | raise ValueError("No score available.") 52 | 53 | return { 54 | "content": { 55 | "account_info": self.account_info(), 56 | "recent_score": [self.score(score)], 57 | } 58 | } 59 | 60 | def user_best(self, song_id: str, rating_class: int): 61 | score = self.session.scalar( 62 | select(ScoreBest).where( 63 | (ScoreBest.song_id == song_id) 64 | & (ScoreBest.rating_class == rating_class) 65 | ) 66 | ) 67 | if not score: 68 | raise ValueError("No score available.") 69 | 70 | return { 71 | "content": { 72 | "account_info": self.account_info(), 73 | "record": self.score(score), 74 | } 75 | } 76 | 77 | def user_best30(self): 78 | scores = list( 79 | self.session.scalars( 80 | select(ScoreBest).order_by(ScoreBest.potential.desc()).limit(40) 81 | ) 82 | ) 83 | if not scores: 84 | raise ValueError("No score available.") 85 | best30_avg = self.session.scalar(select(CalculatedPotential.b30)) 86 | 87 | best30_overflow = ( 88 | [self.score(score) for score in scores[30:40]] if len(scores) > 30 else [] 89 | ) 90 | 91 | return { 92 | "content": { 93 | "account_info": self.account_info(), 94 | "best30_avg": best30_avg, 95 | "best30_list": [self.score(score) for score in scores[:30]], 96 | "best30_overflow": best30_overflow, 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcaea/__init__.py: -------------------------------------------------------------------------------- 1 | from .online import ArcaeaOnlineParser 2 | from .packlist import PacklistParser 3 | from .songlist import SonglistDifficultiesParser, SonglistParser 4 | from .st3 import St3ScoreParser 5 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcaea/common.py: -------------------------------------------------------------------------------- 1 | import contextlib 2 | import json 3 | import math 4 | import time 5 | from os import PathLike 6 | from typing import Any, List, Optional, Union 7 | 8 | from sqlalchemy.orm import DeclarativeBase, Session 9 | 10 | 11 | def fix_timestamp(timestamp: int) -> Union[int, None]: 12 | """ 13 | Some of the `date` column in st3 are strangely truncated. For example, 14 | a `1670283375` may be truncated to `167028`, even `1`. Yes, a single `1`. 15 | 16 | To properly handle this situation, we check the timestamp's digits. 17 | If `digits < 5`, we treat this timestamp as a `None`. Otherwise, we try to 18 | fix the timestamp. 19 | 20 | :param timestamp: a POSIX timestamp 21 | :return: `None` if the timestamp's digits < 5, otherwise a fixed POSIX timestamp 22 | """ 23 | # find digit length from https://stackoverflow.com/a/2189827/16484891 24 | # CC BY-SA 2.5 25 | # this might give incorrect result when timestamp > 999999999999997, 26 | # see https://stackoverflow.com/a/28883802/16484891 (CC BY-SA 4.0). 27 | # but that's way too later than 9999-12-31 23:59:59, 253402271999, 28 | # I don't think Arcaea would still be an active updated game by then. 29 | # so don't mind those small issues, just use this. 30 | digits = int(math.log10(abs(timestamp))) + 1 if timestamp != 0 else 1 31 | if digits < 5: 32 | return None 33 | timestamp_str = str(timestamp) 34 | current_timestamp_digits = int(math.log10(int(time.time()))) + 1 35 | timestamp_str = timestamp_str.ljust(current_timestamp_digits, "0") 36 | return int(timestamp_str, 10) 37 | 38 | 39 | def to_db_value(val: Any) -> Any: 40 | if not val: 41 | return None 42 | return json.dumps(val, ensure_ascii=False) if isinstance(val, list) else val 43 | 44 | 45 | def is_localized(item: dict, key: str, append_localized: bool = True): 46 | item_key = f"{key}_localized" if append_localized else key 47 | subitem: Optional[dict] = item.get(item_key) 48 | return subitem and ( 49 | subitem.get("ja") 50 | or subitem.get("ko") 51 | or subitem.get("zh-Hant") 52 | or subitem.get("zh-Hans") 53 | ) 54 | 55 | 56 | def set_model_localized_attrs( 57 | model: DeclarativeBase, item: dict, model_key: str, item_key: Optional[str] = None 58 | ): 59 | if item_key is None: 60 | item_key = f"{model_key}_localized" 61 | subitem: dict = item.get(item_key, {}) 62 | if not subitem: 63 | return 64 | setattr(model, f"{model_key}_ja", to_db_value(subitem.get("ja"))) 65 | setattr(model, f"{model_key}_ko", to_db_value(subitem.get("ko"))) 66 | setattr(model, f"{model_key}_zh_hans", to_db_value(subitem.get("zh-Hans"))) 67 | setattr(model, f"{model_key}_zh_hant", to_db_value(subitem.get("zh-Hant"))) 68 | 69 | 70 | class ArcaeaParser: 71 | def __init__(self, filepath: Union[str, bytes, PathLike]): 72 | self.filepath = filepath 73 | 74 | def read_file_text(self): 75 | file_handle = None 76 | 77 | with contextlib.suppress(TypeError): 78 | # original open 79 | file_handle = open(self.filepath, "r", encoding="utf-8") 80 | 81 | if file_handle is None: 82 | try: 83 | # or maybe a `pathlib.Path` subset 84 | # or an `importlib.resources.abc.Traversable` like object 85 | # e.g. `zipfile.Path` 86 | file_handle = self.filepath.open(mode="r", encoding="utf-8") # type: ignore 87 | except Exception as e: 88 | raise ValueError("Invalid `filepath`.") from e 89 | 90 | with file_handle: 91 | return file_handle.read() 92 | 93 | def parse(self) -> List[DeclarativeBase]: 94 | raise NotImplementedError() 95 | 96 | def write_database(self, session: Session): 97 | results = self.parse() 98 | for result in results: 99 | session.merge(result) 100 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcaea/online.py: -------------------------------------------------------------------------------- 1 | import json 2 | import logging 3 | from datetime import datetime 4 | from typing import Dict, List, Literal, Optional, TypedDict 5 | 6 | from ...models import Score 7 | from .common import ArcaeaParser, fix_timestamp 8 | 9 | logger = logging.getLogger(__name__) 10 | 11 | 12 | class TWebApiRatingMeScoreItem(TypedDict): 13 | song_id: str 14 | difficulty: int 15 | modifier: int 16 | rating: float 17 | score: int 18 | perfect_count: int 19 | near_count: int 20 | miss_count: int 21 | clear_type: int 22 | title: Dict[Literal["ja", "en"], str] 23 | artist: str 24 | time_played: int 25 | bg: str 26 | 27 | 28 | class TWebApiRatingMeValue(TypedDict): 29 | best_rated_scores: List[TWebApiRatingMeScoreItem] 30 | recent_rated_scores: List[TWebApiRatingMeScoreItem] 31 | 32 | 33 | class TWebApiRatingMeResult(TypedDict): 34 | success: bool 35 | error_code: Optional[int] 36 | value: Optional[TWebApiRatingMeValue] 37 | 38 | 39 | class ArcaeaOnlineParser(ArcaeaParser): 40 | def parse(self) -> List[Score]: 41 | api_result_root: TWebApiRatingMeResult = json.loads(self.read_file_text()) 42 | 43 | api_result_value = api_result_root.get("value") 44 | if not api_result_value: 45 | error_code = api_result_root.get("error_code") 46 | raise ValueError(f"Cannot parse API result, error code {error_code}") 47 | 48 | best30_score_items = api_result_value.get("best_rated_scores", []) 49 | recent_score_items = api_result_value.get("recent_rated_scores", []) 50 | score_items = best30_score_items + recent_score_items 51 | 52 | date_text = datetime.now().strftime("%Y-%m-%d %H:%M:%S") 53 | 54 | results: List[Score] = [] 55 | for score_item in score_items: 56 | score = Score() 57 | score.song_id = score_item["song_id"] 58 | score.rating_class = score_item["difficulty"] 59 | score.score = score_item["score"] 60 | score.pure = score_item["perfect_count"] 61 | score.far = score_item["near_count"] 62 | score.lost = score_item["miss_count"] 63 | score.date = fix_timestamp(int(score_item["time_played"] / 1000)) 64 | score.modifier = score_item["modifier"] 65 | score.clear_type = score_item["clear_type"] 66 | 67 | if score.lost == 0: 68 | score.max_recall = score.pure + score.far 69 | 70 | score.comment = f"Parsed from web API at {date_text}" 71 | results.append(score) 72 | return results 73 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcaea/packlist.py: -------------------------------------------------------------------------------- 1 | import json 2 | from typing import List, Union 3 | 4 | from ...models.songs import Pack, PackLocalized 5 | from .common import ArcaeaParser, is_localized, set_model_localized_attrs 6 | 7 | 8 | class PacklistParser(ArcaeaParser): 9 | def parse(self) -> List[Union[Pack, PackLocalized]]: 10 | packlist_json_root = json.loads(self.read_file_text()) 11 | 12 | packlist_json = packlist_json_root["packs"] 13 | results: List[Union[Pack, PackLocalized]] = [ 14 | Pack(id="single", name="Memory Archive") 15 | ] 16 | for item in packlist_json: 17 | pack = Pack() 18 | pack.id = item["id"] 19 | pack.name = item["name_localized"]["en"] 20 | pack.description = item["description_localized"]["en"] or None 21 | results.append(pack) 22 | 23 | if is_localized(item, "name") or is_localized(item, "description"): 24 | pack_localized = PackLocalized(id=pack.id) 25 | set_model_localized_attrs(pack_localized, item, "name") 26 | set_model_localized_attrs(pack_localized, item, "description") 27 | results.append(pack_localized) 28 | 29 | return results 30 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcaea/songlist.py: -------------------------------------------------------------------------------- 1 | import json 2 | from typing import List, Union 3 | 4 | from ...models.songs import Difficulty, DifficultyLocalized, Song, SongLocalized 5 | from .common import ArcaeaParser, is_localized, set_model_localized_attrs, to_db_value 6 | 7 | 8 | class SonglistParser(ArcaeaParser): 9 | def parse( 10 | self, 11 | ) -> List[Union[Song, SongLocalized, Difficulty, DifficultyLocalized]]: 12 | songlist_json_root = json.loads(self.read_file_text()) 13 | 14 | songlist_json = songlist_json_root["songs"] 15 | results = [] 16 | for item in songlist_json: 17 | song = Song() 18 | song.idx = item["idx"] 19 | song.id = item["id"] 20 | song.title = item["title_localized"]["en"] 21 | song.artist = item["artist"] 22 | song.bpm = item["bpm"] 23 | song.bpm_base = item["bpm_base"] 24 | song.set = item["set"] 25 | song.audio_preview = item["audioPreview"] 26 | song.audio_preview_end = item["audioPreviewEnd"] 27 | song.side = item["side"] 28 | song.version = item["version"] 29 | song.date = item["date"] 30 | song.bg = to_db_value(item.get("bg")) 31 | song.bg_inverse = to_db_value(item.get("bg_inverse")) 32 | if item.get("bg_daynight"): 33 | song.bg_day = to_db_value(item["bg_daynight"].get("day")) 34 | song.bg_night = to_db_value(item["bg_daynight"].get("night")) 35 | if item.get("source_localized"): 36 | song.source = item["source_localized"]["en"] 37 | song.source_copyright = to_db_value(item.get("source_copyright")) 38 | results.append(song) 39 | 40 | if ( 41 | is_localized(item, "title") 42 | or is_localized(item, "search_title", append_localized=False) 43 | or is_localized(item, "search_artist", append_localized=False) 44 | or is_localized(item, "source") 45 | ): 46 | song_localized = SongLocalized(id=song.id) 47 | set_model_localized_attrs(song_localized, item, "title") 48 | set_model_localized_attrs( 49 | song_localized, item, "search_title", "search_title" 50 | ) 51 | set_model_localized_attrs( 52 | song_localized, item, "search_artist", "search_artist" 53 | ) 54 | set_model_localized_attrs(song_localized, item, "source") 55 | results.append(song_localized) 56 | 57 | return results 58 | 59 | 60 | class SonglistDifficultiesParser(ArcaeaParser): 61 | def parse(self) -> List[Union[Difficulty, DifficultyLocalized]]: 62 | songlist_json_root = json.loads(self.read_file_text()) 63 | 64 | songlist_json = songlist_json_root["songs"] 65 | results = [] 66 | for song_item in songlist_json: 67 | if not song_item.get("difficulties"): 68 | continue 69 | 70 | for item in song_item["difficulties"]: 71 | if item["rating"] == 0: 72 | continue 73 | 74 | chart = Difficulty(song_id=song_item["id"]) 75 | chart.rating_class = item["ratingClass"] 76 | chart.rating = item["rating"] 77 | chart.rating_plus = item.get("ratingPlus") or False 78 | chart.chart_designer = item["chartDesigner"] 79 | chart.jacket_desginer = item.get("jacketDesigner") or None 80 | chart.audio_override = item.get("audioOverride") or False 81 | chart.jacket_override = item.get("jacketOverride") or False 82 | chart.jacket_night = item.get("jacketNight") or None 83 | chart.title = item.get("title_localized", {}).get("en") or None 84 | chart.artist = item.get("artist") or None 85 | chart.bg = item.get("bg") or None 86 | chart.bg_inverse = item.get("bg_inverse") 87 | chart.bpm = item.get("bpm") or None 88 | chart.bpm_base = item.get("bpm_base") or None 89 | chart.version = item.get("version") or None 90 | chart.date = item.get("date") or None 91 | results.append(chart) 92 | 93 | if is_localized(item, "title") or is_localized(item, "artist"): 94 | chart_localized = DifficultyLocalized( 95 | song_id=chart.song_id, rating_class=chart.rating_class 96 | ) 97 | set_model_localized_attrs(chart_localized, item, "title") 98 | set_model_localized_attrs(chart_localized, item, "artist") 99 | results.append(chart_localized) 100 | 101 | return results 102 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcaea/st3.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import sqlite3 3 | from typing import List 4 | 5 | from sqlalchemy import select 6 | from sqlalchemy.orm import Session 7 | 8 | from ...models.scores import Score 9 | from .common import ArcaeaParser, fix_timestamp 10 | 11 | logger = logging.getLogger(__name__) 12 | 13 | 14 | class St3ScoreParser(ArcaeaParser): 15 | def parse(self) -> List[Score]: 16 | items = [] 17 | with sqlite3.connect(self.filepath) as st3_conn: 18 | cursor = st3_conn.cursor() 19 | db_scores = cursor.execute( 20 | "SELECT songId, songDifficulty, score, perfectCount, nearCount, missCount, " 21 | "date, modifier FROM scores" 22 | ).fetchall() 23 | for ( 24 | song_id, 25 | rating_class, 26 | score, 27 | pure, 28 | far, 29 | lost, 30 | date, 31 | modifier, 32 | ) in db_scores: 33 | clear_type = cursor.execute( 34 | "SELECT clearType FROM cleartypes WHERE songId = ? AND songDifficulty = ?", 35 | (song_id, rating_class), 36 | ).fetchone()[0] 37 | 38 | items.append( 39 | Score( 40 | song_id=song_id, 41 | rating_class=rating_class, 42 | score=score, 43 | pure=pure, 44 | far=far, 45 | lost=lost, 46 | date=fix_timestamp(date), 47 | modifier=modifier, 48 | clear_type=clear_type, 49 | comment="Parsed from st3", 50 | ) 51 | ) 52 | 53 | return items 54 | 55 | def write_database(self, session: Session, *, skip_duplicate=True): 56 | parsed_scores = self.parse() 57 | for parsed_score in parsed_scores: 58 | query_score = session.scalar( 59 | select(Score).where( 60 | (Score.song_id == parsed_score.song_id) 61 | & (Score.rating_class == parsed_score.rating_class) 62 | & (Score.score == parsed_score.score) 63 | ) 64 | ) 65 | 66 | if query_score and skip_duplicate: 67 | logger.info( 68 | "%r skipped because potential duplicate item %r found.", 69 | parsed_score, 70 | query_score, 71 | ) 72 | continue 73 | session.add(parsed_score) 74 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcsong/__init__.py: -------------------------------------------------------------------------------- 1 | from .arcsong_db import ArcsongDbParser 2 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcsong/arcsong_db.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | from typing import List 3 | 4 | from sqlalchemy.orm import Session 5 | 6 | from ...models.songs import ChartInfo 7 | 8 | 9 | class ArcsongDbParser: 10 | def __init__(self, filepath): 11 | self.filepath = filepath 12 | 13 | def parse(self) -> List[ChartInfo]: 14 | results = [] 15 | with sqlite3.connect(self.filepath) as conn: 16 | cursor = conn.cursor() 17 | arcsong_db_results = cursor.execute( 18 | "SELECT song_id, rating_class, rating, note FROM charts" 19 | ) 20 | for result in arcsong_db_results: 21 | chart = ChartInfo( 22 | song_id=result[0], 23 | rating_class=result[1], 24 | constant=result[2], 25 | notes=result[3] or None, 26 | ) 27 | results.append(chart) 28 | 29 | return results 30 | 31 | def write_database(self, session: Session): 32 | results = self.parse() 33 | for result in results: 34 | session.merge(result) 35 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/arcsong/arcsong_json.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import re 3 | from typing import List, Optional, TypedDict 4 | 5 | from sqlalchemy import func, select 6 | from sqlalchemy.orm import Session 7 | 8 | from ...models import ( 9 | ChartInfo, 10 | Difficulty, 11 | DifficultyLocalized, 12 | Pack, 13 | Song, 14 | SongLocalized, 15 | ) 16 | 17 | logger = logging.getLogger(__name__) 18 | 19 | 20 | class TArcSongJsonDifficultyItem(TypedDict): 21 | name_en: str 22 | name_jp: str 23 | artist: str 24 | bpm: str 25 | bpm_base: float 26 | set: str 27 | set_friendly: str 28 | time: int 29 | side: int 30 | world_unlock: bool 31 | remote_download: bool 32 | bg: str 33 | date: int 34 | version: str 35 | difficulty: int 36 | rating: int 37 | note: int 38 | chart_designer: str 39 | jacket_designer: str 40 | jacket_override: bool 41 | audio_override: bool 42 | 43 | 44 | class TArcSongJsonSongItem(TypedDict): 45 | song_id: str 46 | difficulties: List[TArcSongJsonDifficultyItem] 47 | alias: List[str] 48 | 49 | 50 | class TArcSongJson(TypedDict): 51 | songs: List[TArcSongJsonSongItem] 52 | 53 | 54 | class ArcSongJsonBuilder: 55 | def __init__(self, session: Session): 56 | self.session = session 57 | 58 | def get_difficulty_item( 59 | self, 60 | difficulty: Difficulty, 61 | song: Song, 62 | pack: Pack, 63 | song_localized: Optional[SongLocalized], 64 | ) -> TArcSongJsonDifficultyItem: 65 | if "_append_" in pack.id: 66 | base_pack = self.session.scalar( 67 | select(Pack).where(Pack.id == re.sub(r"_append_.*$", "", pack.id)) 68 | ) 69 | else: 70 | base_pack = None 71 | 72 | difficulty_localized = self.session.scalar( 73 | select(DifficultyLocalized).where( 74 | (DifficultyLocalized.song_id == difficulty.song_id) 75 | & (DifficultyLocalized.rating_class == difficulty.rating_class) 76 | ) 77 | ) 78 | chart_info = self.session.scalar( 79 | select(ChartInfo).where( 80 | (ChartInfo.song_id == difficulty.song_id) 81 | & (ChartInfo.rating_class == difficulty.rating_class) 82 | ) 83 | ) 84 | 85 | if difficulty_localized: 86 | name_jp = difficulty_localized.title_ja or "" 87 | elif song_localized: 88 | name_jp = song_localized.title_ja or "" 89 | else: 90 | name_jp = "" 91 | 92 | return { 93 | "name_en": difficulty.title or song.title, 94 | "name_jp": name_jp, 95 | "artist": difficulty.artist or song.artist, 96 | "bpm": difficulty.bpm or song.bpm or "", 97 | "bpm_base": difficulty.bpm_base or song.bpm_base or 0.0, 98 | "set": song.set, 99 | "set_friendly": f"{base_pack.name} - {pack.name}" 100 | if base_pack 101 | else pack.name, 102 | "time": 0, 103 | "side": song.side or 0, 104 | "world_unlock": False, 105 | "remote_download": False, 106 | "bg": difficulty.bg or song.bg or "", 107 | "date": difficulty.date or song.date or 0, 108 | "version": difficulty.version or song.version or "", 109 | "difficulty": difficulty.rating * 2 + int(difficulty.rating_plus), 110 | "rating": chart_info.constant or 0 if chart_info else 0, 111 | "note": chart_info.notes or 0 if chart_info else 0, 112 | "chart_designer": difficulty.chart_designer or "", 113 | "jacket_designer": difficulty.jacket_desginer or "", 114 | "jacket_override": difficulty.jacket_override, 115 | "audio_override": difficulty.audio_override, 116 | } 117 | 118 | def get_song_item(self, song: Song) -> TArcSongJsonSongItem: 119 | difficulties = self.session.scalars( 120 | select(Difficulty).where(Difficulty.song_id == song.id) 121 | ) 122 | 123 | pack = self.session.scalar(select(Pack).where(Pack.id == song.set)) 124 | if not pack: 125 | logger.warning( 126 | 'Cannot find pack "%s", using placeholder instead.', song.set 127 | ) 128 | pack = Pack(id="unknown", name="Unknown", description="__PLACEHOLDER__") 129 | song_localized = self.session.scalar( 130 | select(SongLocalized).where(SongLocalized.id == song.id) 131 | ) 132 | 133 | return { 134 | "song_id": song.id, 135 | "difficulties": [ 136 | self.get_difficulty_item(difficulty, song, pack, song_localized) 137 | for difficulty in difficulties 138 | ], 139 | "alias": [], 140 | } 141 | 142 | def generate_arcsong_json(self) -> TArcSongJson: 143 | songs = self.session.scalars(select(Song)) 144 | arcsong_songs = [] 145 | for song in songs: 146 | proceed = self.session.scalar( 147 | select(func.count(Difficulty.rating_class)).where( 148 | Difficulty.song_id == song.id 149 | ) 150 | ) 151 | 152 | if not proceed: 153 | continue 154 | 155 | arcsong_songs.append(self.get_song_item(song)) 156 | 157 | return {"songs": arcsong_songs} 158 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/chart_info_db/__init__.py: -------------------------------------------------------------------------------- 1 | from .parser import ChartInfoDbParser 2 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/chart_info_db/parser.py: -------------------------------------------------------------------------------- 1 | import contextlib 2 | import sqlite3 3 | from typing import List 4 | 5 | from sqlalchemy.orm import Session 6 | 7 | from ...models.songs import ChartInfo 8 | 9 | 10 | class ChartInfoDbParser: 11 | def __init__(self, filepath): 12 | self.filepath = filepath 13 | 14 | def parse(self) -> List[ChartInfo]: 15 | results = [] 16 | with sqlite3.connect(self.filepath) as conn: 17 | with contextlib.closing(conn.cursor()) as cursor: 18 | db_results = cursor.execute( 19 | "SELECT song_id, rating_class, constant, notes FROM charts_info" 20 | ).fetchall() 21 | for result in db_results: 22 | chart = ChartInfo( 23 | song_id=result[0], 24 | rating_class=result[1], 25 | constant=result[2], 26 | notes=result[3] or None, 27 | ) 28 | results.append(chart) 29 | 30 | return results 31 | 32 | def write_database(self, session: Session): 33 | results = self.parse() 34 | for result in results: 35 | session.merge(result) 36 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/exports/__init__.py: -------------------------------------------------------------------------------- 1 | from . import exporters 2 | from .types import ArcaeaOfflineDEFV2_Score, ScoreExport 3 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/exports/exporters.py: -------------------------------------------------------------------------------- 1 | from ...models import Score 2 | from .types import ArcaeaOfflineDEFV2_ScoreItem, ScoreExport 3 | 4 | 5 | def score(score: Score) -> ScoreExport: 6 | return { 7 | "id": score.id, 8 | "song_id": score.song_id, 9 | "rating_class": score.rating_class, 10 | "score": score.score, 11 | "pure": score.pure, 12 | "far": score.far, 13 | "lost": score.lost, 14 | "date": score.date, 15 | "max_recall": score.max_recall, 16 | "modifier": score.modifier, 17 | "clear_type": score.clear_type, 18 | "comment": score.comment, 19 | } 20 | 21 | 22 | def score_def_v2(score: Score) -> ArcaeaOfflineDEFV2_ScoreItem: 23 | return { 24 | "id": score.id, 25 | "songId": score.song_id, 26 | "ratingClass": score.rating_class, 27 | "score": score.score, 28 | "pure": score.pure, 29 | "far": score.far, 30 | "lost": score.lost, 31 | "date": score.date, 32 | "maxRecall": score.max_recall, 33 | "modifier": score.modifier, 34 | "clearType": score.clear_type, 35 | "source": None, 36 | "comment": score.comment, 37 | } 38 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/exports/types.py: -------------------------------------------------------------------------------- 1 | from typing import List, Literal, Optional, TypedDict 2 | 3 | 4 | class ScoreExport(TypedDict): 5 | id: int 6 | song_id: str 7 | rating_class: int 8 | score: int 9 | pure: Optional[int] 10 | far: Optional[int] 11 | lost: Optional[int] 12 | date: Optional[int] 13 | max_recall: Optional[int] 14 | modifier: Optional[int] 15 | clear_type: Optional[int] 16 | comment: Optional[str] 17 | 18 | 19 | class ArcaeaOfflineDEFV2_ScoreItem(TypedDict, total=False): 20 | id: Optional[int] 21 | songId: str 22 | ratingClass: int 23 | score: int 24 | pure: Optional[int] 25 | far: Optional[int] 26 | lost: Optional[int] 27 | date: Optional[int] 28 | maxRecall: Optional[int] 29 | modifier: Optional[int] 30 | clearType: Optional[int] 31 | source: Optional[str] 32 | comment: Optional[str] 33 | 34 | 35 | ArcaeaOfflineDEFV2_Score = TypedDict( 36 | "ArcaeaOfflineDEFV2_Score", 37 | { 38 | "$schema": Literal[ 39 | "https://arcaeaoffline.sevive.xyz/schemas/def/v2/score.schema.json" 40 | ], 41 | "type": Literal["score"], 42 | "version": Literal[2], 43 | "scores": List[ArcaeaOfflineDEFV2_ScoreItem], 44 | }, 45 | ) 46 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/smartrte/__init__.py: -------------------------------------------------------------------------------- 1 | from .b30_csv import SmartRteB30CsvConverter 2 | -------------------------------------------------------------------------------- /src/arcaea_offline/external/smartrte/b30_csv.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy.orm import Session 2 | 3 | from ...models import Chart, ScoreBest 4 | from ...utils.rating import rating_class_to_text 5 | 6 | 7 | class SmartRteB30CsvConverter: 8 | CSV_ROWS = [ 9 | "songname", 10 | "songId", 11 | "Difficulty", 12 | "score", 13 | "Perfect", 14 | "criticalPerfect", 15 | "Far", 16 | "Lost", 17 | "Constant", 18 | "singlePTT", 19 | ] 20 | 21 | def __init__( 22 | self, 23 | session: Session, 24 | ): 25 | self.session = session 26 | 27 | def rows(self) -> list: 28 | csv_rows = [self.CSV_ROWS.copy()] 29 | 30 | with self.session as session: 31 | results = ( 32 | session.query( 33 | Chart.title, 34 | ScoreBest.song_id, 35 | ScoreBest.rating_class, 36 | ScoreBest.score, 37 | ScoreBest.pure, 38 | ScoreBest.shiny_pure, 39 | ScoreBest.far, 40 | ScoreBest.lost, 41 | Chart.constant, 42 | ScoreBest.potential, 43 | ) 44 | .join( 45 | Chart, 46 | (Chart.song_id == ScoreBest.song_id) 47 | & (Chart.rating_class == ScoreBest.rating_class), 48 | ) 49 | .all() 50 | ) 51 | 52 | for result in results: 53 | # replace the comma in song title because the target project 54 | # cannot handle quoted string 55 | result = list(result) 56 | result[0] = result[0].replace(",", "") 57 | result[2] = rating_class_to_text(result[2]) 58 | # divide constant to float 59 | result[-2] = result[-2] / 10 60 | # round potential 61 | result[-1] = round(result[-1], 5) 62 | csv_rows.append(result) 63 | 64 | return csv_rows 65 | -------------------------------------------------------------------------------- /src/arcaea_offline/models/__init__.py: -------------------------------------------------------------------------------- 1 | from .config import ConfigBase, Property 2 | from .scores import ( 3 | CalculatedPotential, 4 | Score, 5 | ScoreBest, 6 | ScoreCalculated, 7 | ScoresBase, 8 | ScoresViewBase, 9 | ) 10 | from .songs import ( 11 | Chart, 12 | ChartInfo, 13 | Difficulty, 14 | DifficultyLocalized, 15 | Pack, 16 | PackLocalized, 17 | Song, 18 | SongLocalized, 19 | SongsBase, 20 | SongsViewBase, 21 | ) 22 | -------------------------------------------------------------------------------- /src/arcaea_offline/models/common.py: -------------------------------------------------------------------------------- 1 | # pylint: disable=too-few-public-methods 2 | 3 | from sqlalchemy.orm import DeclarativeBase 4 | from sqlalchemy.orm.exc import DetachedInstanceError 5 | 6 | 7 | class ReprHelper: 8 | # pylint: disable=no-member 9 | 10 | def _repr(self, **kwargs) -> str: 11 | """ 12 | Helper for __repr__ 13 | 14 | https://stackoverflow.com/a/55749579/16484891 15 | 16 | CC BY-SA 4.0 17 | """ 18 | field_strings = [] 19 | at_least_one_attached_attribute = False 20 | for key, field in kwargs.items(): 21 | try: 22 | field_strings.append(f"{key}={field!r}") 23 | except DetachedInstanceError: 24 | field_strings.append(f"{key}=DetachedInstanceError") 25 | else: 26 | at_least_one_attached_attribute = True 27 | if at_least_one_attached_attribute: 28 | return f"<{self.__class__.__name__}({','.join(field_strings)})>" 29 | return f"<{self.__class__.__name__} {id(self)}>" 30 | 31 | def __repr__(self): 32 | if isinstance(self, DeclarativeBase): 33 | return self._repr( 34 | **{c.key: getattr(self, c.key) for c in self.__table__.columns} 35 | ) 36 | return super().__repr__() 37 | -------------------------------------------------------------------------------- /src/arcaea_offline/models/config.py: -------------------------------------------------------------------------------- 1 | # pylint: disable=too-few-public-methods 2 | 3 | from sqlalchemy import TEXT 4 | from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column 5 | 6 | from .common import ReprHelper 7 | 8 | __all__ = [ 9 | "ConfigBase", 10 | "Property", 11 | ] 12 | 13 | 14 | class ConfigBase(DeclarativeBase, ReprHelper): 15 | pass 16 | 17 | 18 | class Property(ConfigBase): 19 | __tablename__ = "properties" 20 | 21 | key: Mapped[str] = mapped_column(TEXT(), primary_key=True) 22 | value: Mapped[str] = mapped_column(TEXT()) 23 | -------------------------------------------------------------------------------- /src/arcaea_offline/models/scores.py: -------------------------------------------------------------------------------- 1 | # pylint: disable=too-few-public-methods, duplicate-code 2 | 3 | from typing import Optional 4 | 5 | from sqlalchemy import TEXT, case, func, inspect, select, text 6 | from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column 7 | from sqlalchemy_utils import create_view 8 | 9 | from .common import ReprHelper 10 | from .songs import ChartInfo, Difficulty 11 | 12 | __all__ = [ 13 | "ScoresBase", 14 | "Score", 15 | "ScoresViewBase", 16 | "ScoreCalculated", 17 | "ScoreBest", 18 | "CalculatedPotential", 19 | ] 20 | 21 | 22 | class ScoresBase(DeclarativeBase, ReprHelper): 23 | pass 24 | 25 | 26 | class Score(ScoresBase): 27 | __tablename__ = "scores" 28 | 29 | id: Mapped[int] = mapped_column(autoincrement=True, primary_key=True) 30 | song_id: Mapped[str] = mapped_column(TEXT()) 31 | rating_class: Mapped[int] 32 | score: Mapped[int] 33 | pure: Mapped[Optional[int]] 34 | far: Mapped[Optional[int]] 35 | lost: Mapped[Optional[int]] 36 | date: Mapped[Optional[int]] 37 | max_recall: Mapped[Optional[int]] 38 | modifier: Mapped[Optional[int]] = mapped_column( 39 | comment="0: NORMAL, 1: EASY, 2: HARD" 40 | ) 41 | clear_type: Mapped[Optional[int]] = mapped_column( 42 | comment="0: TRACK LOST, 1: NORMAL CLEAR, 2: FULL RECALL, " 43 | "3: PURE MEMORY, 4: EASY CLEAR, 5: HARD CLEAR" 44 | ) 45 | comment: Mapped[Optional[str]] = mapped_column(TEXT()) 46 | 47 | 48 | # How to create an SQL View with SQLAlchemy? 49 | # https://stackoverflow.com/a/53253105/16484891 50 | # CC BY-SA 4.0 51 | 52 | 53 | class ScoresViewBase(DeclarativeBase, ReprHelper): 54 | pass 55 | 56 | 57 | class ScoreCalculated(ScoresViewBase): 58 | __tablename__ = "scores_calculated" 59 | 60 | id: Mapped[int] 61 | song_id: Mapped[str] 62 | rating_class: Mapped[int] 63 | score: Mapped[int] 64 | pure: Mapped[Optional[int]] 65 | shiny_pure: Mapped[Optional[int]] 66 | far: Mapped[Optional[int]] 67 | lost: Mapped[Optional[int]] 68 | date: Mapped[Optional[int]] 69 | max_recall: Mapped[Optional[int]] 70 | modifier: Mapped[Optional[int]] 71 | clear_type: Mapped[Optional[int]] 72 | potential: Mapped[float] 73 | comment: Mapped[Optional[str]] 74 | 75 | __table__ = create_view( 76 | name=__tablename__, 77 | selectable=select( 78 | Score.id, 79 | Difficulty.song_id, 80 | Difficulty.rating_class, 81 | Score.score, 82 | Score.pure, 83 | ( 84 | case( 85 | ( 86 | ( 87 | ChartInfo.notes.is_not(None) 88 | & Score.pure.is_not(None) 89 | & Score.far.is_not(None) 90 | & (ChartInfo.notes != 0) 91 | ), 92 | Score.score 93 | - func.floor( 94 | (Score.pure * 10000000.0 / ChartInfo.notes) 95 | + (Score.far * 0.5 * 10000000.0 / ChartInfo.notes) 96 | ), 97 | ), 98 | else_=text("NULL"), 99 | ) 100 | ).label("shiny_pure"), 101 | Score.far, 102 | Score.lost, 103 | Score.date, 104 | Score.max_recall, 105 | Score.modifier, 106 | Score.clear_type, 107 | case( 108 | (Score.score >= 10000000, ChartInfo.constant / 10.0 + 2), 109 | ( 110 | Score.score >= 9800000, 111 | ChartInfo.constant / 10.0 + 1 + (Score.score - 9800000) / 200000.0, 112 | ), 113 | else_=func.max( 114 | (ChartInfo.constant / 10.0) + (Score.score - 9500000) / 300000.0, 115 | 0, 116 | ), 117 | ).label("potential"), 118 | Score.comment, 119 | ) 120 | .select_from(Difficulty) 121 | .join( 122 | ChartInfo, 123 | (Difficulty.song_id == ChartInfo.song_id) 124 | & (Difficulty.rating_class == ChartInfo.rating_class), 125 | ) 126 | .join( 127 | Score, 128 | (Difficulty.song_id == Score.song_id) 129 | & (Difficulty.rating_class == Score.rating_class), 130 | ), 131 | metadata=ScoresViewBase.metadata, 132 | cascade_on_drop=False, 133 | ) 134 | 135 | 136 | class ScoreBest(ScoresViewBase): 137 | __tablename__ = "scores_best" 138 | 139 | id: Mapped[int] 140 | song_id: Mapped[str] 141 | rating_class: Mapped[int] 142 | score: Mapped[int] 143 | pure: Mapped[Optional[int]] 144 | shiny_pure: Mapped[Optional[int]] 145 | far: Mapped[Optional[int]] 146 | lost: Mapped[Optional[int]] 147 | date: Mapped[Optional[int]] 148 | max_recall: Mapped[Optional[int]] 149 | modifier: Mapped[Optional[int]] 150 | clear_type: Mapped[Optional[int]] 151 | potential: Mapped[float] 152 | comment: Mapped[Optional[str]] 153 | 154 | __table__ = create_view( 155 | name=__tablename__, 156 | selectable=select( 157 | *[ 158 | col 159 | for col in inspect(ScoreCalculated).columns 160 | if col.name != "potential" 161 | ], 162 | func.max(ScoreCalculated.potential).label("potential"), 163 | ) 164 | .select_from(ScoreCalculated) 165 | .group_by(ScoreCalculated.song_id, ScoreCalculated.rating_class) 166 | .order_by(ScoreCalculated.potential.desc()), 167 | metadata=ScoresViewBase.metadata, 168 | cascade_on_drop=False, 169 | ) 170 | 171 | 172 | class CalculatedPotential(ScoresViewBase): 173 | __tablename__ = "calculated_potential" 174 | 175 | b30: Mapped[float] 176 | 177 | _select_bests_subquery = ( 178 | select(ScoreBest.potential.label("b30_sum")) 179 | .order_by(ScoreBest.potential.desc()) 180 | .limit(30) 181 | .subquery() 182 | ) 183 | __table__ = create_view( 184 | name=__tablename__, 185 | selectable=select(func.avg(_select_bests_subquery.c.b30_sum).label("b30")), 186 | metadata=ScoresViewBase.metadata, 187 | cascade_on_drop=False, 188 | ) 189 | -------------------------------------------------------------------------------- /src/arcaea_offline/models/songs.py: -------------------------------------------------------------------------------- 1 | # pylint: disable=too-few-public-methods, duplicate-code 2 | 3 | from typing import Optional 4 | 5 | from sqlalchemy import TEXT, ForeignKey, func, select 6 | from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column 7 | from sqlalchemy_utils import create_view 8 | 9 | from .common import ReprHelper 10 | 11 | __all__ = [ 12 | "SongsBase", 13 | "Pack", 14 | "PackLocalized", 15 | "Song", 16 | "SongLocalized", 17 | "Difficulty", 18 | "DifficultyLocalized", 19 | "ChartInfo", 20 | "SongsViewBase", 21 | "Chart", 22 | ] 23 | 24 | 25 | class SongsBase(DeclarativeBase, ReprHelper): 26 | pass 27 | 28 | 29 | class Pack(SongsBase): 30 | __tablename__ = "packs" 31 | 32 | id: Mapped[str] = mapped_column(TEXT(), primary_key=True) 33 | name: Mapped[str] = mapped_column(TEXT()) 34 | description: Mapped[Optional[str]] = mapped_column(TEXT()) 35 | 36 | 37 | class PackLocalized(SongsBase): 38 | __tablename__ = "packs_localized" 39 | 40 | id: Mapped[str] = mapped_column(ForeignKey("packs.id"), primary_key=True) 41 | name_ja: Mapped[Optional[str]] = mapped_column(TEXT()) 42 | name_ko: Mapped[Optional[str]] = mapped_column(TEXT()) 43 | name_zh_hans: Mapped[Optional[str]] = mapped_column(TEXT()) 44 | name_zh_hant: Mapped[Optional[str]] = mapped_column(TEXT()) 45 | description_ja: Mapped[Optional[str]] = mapped_column(TEXT()) 46 | description_ko: Mapped[Optional[str]] = mapped_column(TEXT()) 47 | description_zh_hans: Mapped[Optional[str]] = mapped_column(TEXT()) 48 | description_zh_hant: Mapped[Optional[str]] = mapped_column(TEXT()) 49 | 50 | 51 | class Song(SongsBase): 52 | __tablename__ = "songs" 53 | 54 | idx: Mapped[int] 55 | id: Mapped[str] = mapped_column(TEXT(), primary_key=True) 56 | title: Mapped[str] = mapped_column(TEXT()) 57 | artist: Mapped[str] = mapped_column(TEXT()) 58 | set: Mapped[str] = mapped_column(TEXT()) 59 | bpm: Mapped[Optional[str]] = mapped_column(TEXT()) 60 | bpm_base: Mapped[Optional[float]] 61 | audio_preview: Mapped[Optional[int]] 62 | audio_preview_end: Mapped[Optional[int]] 63 | side: Mapped[Optional[int]] 64 | version: Mapped[Optional[str]] = mapped_column(TEXT()) 65 | date: Mapped[Optional[int]] 66 | bg: Mapped[Optional[str]] = mapped_column(TEXT()) 67 | bg_inverse: Mapped[Optional[str]] = mapped_column(TEXT()) 68 | bg_day: Mapped[Optional[str]] = mapped_column(TEXT()) 69 | bg_night: Mapped[Optional[str]] = mapped_column(TEXT()) 70 | source: Mapped[Optional[str]] = mapped_column(TEXT()) 71 | source_copyright: Mapped[Optional[str]] = mapped_column(TEXT()) 72 | 73 | 74 | class SongLocalized(SongsBase): 75 | __tablename__ = "songs_localized" 76 | 77 | id: Mapped[str] = mapped_column(ForeignKey("songs.id"), primary_key=True) 78 | title_ja: Mapped[Optional[str]] = mapped_column(TEXT()) 79 | title_ko: Mapped[Optional[str]] = mapped_column(TEXT()) 80 | title_zh_hans: Mapped[Optional[str]] = mapped_column(TEXT()) 81 | title_zh_hant: Mapped[Optional[str]] = mapped_column(TEXT()) 82 | search_title_ja: Mapped[Optional[str]] = mapped_column(TEXT(), comment="JSON array") 83 | search_title_ko: Mapped[Optional[str]] = mapped_column(TEXT(), comment="JSON array") 84 | search_title_zh_hans: Mapped[Optional[str]] = mapped_column( 85 | TEXT(), comment="JSON array" 86 | ) 87 | search_title_zh_hant: Mapped[Optional[str]] = mapped_column( 88 | TEXT(), comment="JSON array" 89 | ) 90 | search_artist_ja: Mapped[Optional[str]] = mapped_column( 91 | TEXT(), comment="JSON array" 92 | ) 93 | search_artist_ko: Mapped[Optional[str]] = mapped_column( 94 | TEXT(), comment="JSON array" 95 | ) 96 | search_artist_zh_hans: Mapped[Optional[str]] = mapped_column( 97 | TEXT(), comment="JSON array" 98 | ) 99 | search_artist_zh_hant: Mapped[Optional[str]] = mapped_column( 100 | TEXT(), comment="JSON array" 101 | ) 102 | source_ja: Mapped[Optional[str]] = mapped_column(TEXT()) 103 | source_ko: Mapped[Optional[str]] = mapped_column(TEXT()) 104 | source_zh_hans: Mapped[Optional[str]] = mapped_column(TEXT()) 105 | source_zh_hant: Mapped[Optional[str]] = mapped_column(TEXT()) 106 | 107 | 108 | class Difficulty(SongsBase): 109 | __tablename__ = "difficulties" 110 | 111 | song_id: Mapped[str] = mapped_column(TEXT(), primary_key=True) 112 | rating_class: Mapped[int] = mapped_column(primary_key=True) 113 | rating: Mapped[int] 114 | rating_plus: Mapped[bool] 115 | chart_designer: Mapped[Optional[str]] = mapped_column(TEXT()) 116 | jacket_desginer: Mapped[Optional[str]] = mapped_column(TEXT()) 117 | audio_override: Mapped[bool] 118 | jacket_override: Mapped[bool] 119 | jacket_night: Mapped[Optional[str]] = mapped_column(TEXT()) 120 | title: Mapped[Optional[str]] = mapped_column(TEXT()) 121 | artist: Mapped[Optional[str]] = mapped_column(TEXT()) 122 | bg: Mapped[Optional[str]] = mapped_column(TEXT()) 123 | bg_inverse: Mapped[Optional[str]] = mapped_column(TEXT()) 124 | bpm: Mapped[Optional[str]] = mapped_column(TEXT()) 125 | bpm_base: Mapped[Optional[float]] 126 | version: Mapped[Optional[str]] = mapped_column(TEXT()) 127 | date: Mapped[Optional[int]] 128 | 129 | 130 | class DifficultyLocalized(SongsBase): 131 | __tablename__ = "difficulties_localized" 132 | 133 | song_id: Mapped[str] = mapped_column( 134 | ForeignKey("difficulties.song_id"), primary_key=True 135 | ) 136 | rating_class: Mapped[str] = mapped_column( 137 | ForeignKey("difficulties.rating_class"), primary_key=True 138 | ) 139 | title_ja: Mapped[Optional[str]] = mapped_column(TEXT()) 140 | title_ko: Mapped[Optional[str]] = mapped_column(TEXT()) 141 | title_zh_hans: Mapped[Optional[str]] = mapped_column(TEXT()) 142 | title_zh_hant: Mapped[Optional[str]] = mapped_column(TEXT()) 143 | artist_ja: Mapped[Optional[str]] = mapped_column(TEXT()) 144 | artist_ko: Mapped[Optional[str]] = mapped_column(TEXT()) 145 | artist_zh_hans: Mapped[Optional[str]] = mapped_column(TEXT()) 146 | artist_zh_hant: Mapped[Optional[str]] = mapped_column(TEXT()) 147 | 148 | 149 | class ChartInfo(SongsBase): 150 | __tablename__ = "charts_info" 151 | 152 | song_id: Mapped[str] = mapped_column( 153 | ForeignKey("difficulties.song_id"), primary_key=True 154 | ) 155 | rating_class: Mapped[str] = mapped_column( 156 | ForeignKey("difficulties.rating_class"), primary_key=True 157 | ) 158 | constant: Mapped[int] = mapped_column( 159 | comment="real_constant * 10. For example, Crimson Throne [FTR] is 10.4, then store 104." 160 | ) 161 | notes: Mapped[Optional[int]] 162 | 163 | 164 | class SongsViewBase(DeclarativeBase, ReprHelper): 165 | pass 166 | 167 | 168 | class Chart(SongsViewBase): 169 | __tablename__ = "charts" 170 | 171 | song_idx: Mapped[int] 172 | song_id: Mapped[str] 173 | rating_class: Mapped[int] 174 | rating: Mapped[int] 175 | rating_plus: Mapped[bool] 176 | title: Mapped[str] 177 | artist: Mapped[str] 178 | set: Mapped[str] 179 | bpm: Mapped[Optional[str]] 180 | bpm_base: Mapped[Optional[float]] 181 | audio_preview: Mapped[Optional[int]] 182 | audio_preview_end: Mapped[Optional[int]] 183 | side: Mapped[Optional[int]] 184 | version: Mapped[Optional[str]] 185 | date: Mapped[Optional[int]] 186 | bg: Mapped[Optional[str]] 187 | bg_inverse: Mapped[Optional[str]] 188 | bg_day: Mapped[Optional[str]] 189 | bg_night: Mapped[Optional[str]] 190 | source: Mapped[Optional[str]] 191 | source_copyright: Mapped[Optional[str]] 192 | chart_designer: Mapped[Optional[str]] 193 | jacket_desginer: Mapped[Optional[str]] 194 | audio_override: Mapped[bool] 195 | jacket_override: Mapped[bool] 196 | jacket_night: Mapped[Optional[str]] 197 | constant: Mapped[int] 198 | notes: Mapped[Optional[int]] 199 | 200 | __table__ = create_view( 201 | name=__tablename__, 202 | selectable=select( 203 | Song.idx.label("song_idx"), 204 | Difficulty.song_id, 205 | Difficulty.rating_class, 206 | Difficulty.rating, 207 | Difficulty.rating_plus, 208 | func.coalesce(Difficulty.title, Song.title).label("title"), 209 | func.coalesce(Difficulty.artist, Song.artist).label("artist"), 210 | Song.set, 211 | func.coalesce(Difficulty.bpm, Song.bpm).label("bpm"), 212 | func.coalesce(Difficulty.bpm_base, Song.bpm_base).label("bpm_base"), 213 | Song.audio_preview, 214 | Song.audio_preview_end, 215 | Song.side, 216 | func.coalesce(Difficulty.version, Song.version).label("version"), 217 | func.coalesce(Difficulty.date, Song.date).label("date"), 218 | func.coalesce(Difficulty.bg, Song.bg).label("bg"), 219 | func.coalesce(Difficulty.bg_inverse, Song.bg_inverse).label("bg_inverse"), 220 | Song.bg_day, 221 | Song.bg_night, 222 | Song.source, 223 | Song.source_copyright, 224 | Difficulty.chart_designer, 225 | Difficulty.jacket_desginer, 226 | Difficulty.audio_override, 227 | Difficulty.jacket_override, 228 | Difficulty.jacket_night, 229 | ChartInfo.constant, 230 | ChartInfo.notes, 231 | ) 232 | .select_from(Difficulty) 233 | .join( 234 | ChartInfo, 235 | (Difficulty.song_id == ChartInfo.song_id) 236 | & (Difficulty.rating_class == ChartInfo.rating_class), 237 | ) 238 | .join(Song, Difficulty.song_id == Song.id), 239 | metadata=SongsViewBase.metadata, 240 | cascade_on_drop=False, 241 | ) 242 | -------------------------------------------------------------------------------- /src/arcaea_offline/searcher.py: -------------------------------------------------------------------------------- 1 | from typing import List, Union 2 | 3 | from sqlalchemy import select 4 | from sqlalchemy.orm import Session 5 | from whoosh.analysis import NgramFilter, StandardAnalyzer 6 | from whoosh.fields import ID, KEYWORD, TEXT, Schema 7 | from whoosh.filedb.filestore import RamStorage 8 | from whoosh.qparser import FuzzyTermPlugin, MultifieldParser, OrGroup 9 | 10 | from .models.songs import Song, SongLocalized 11 | from .utils.search_title import recover_search_title 12 | 13 | 14 | class Searcher: 15 | def __init__(self): 16 | self.text_analyzer = StandardAnalyzer() | NgramFilter(minsize=2, maxsize=5) 17 | self.song_schema = Schema( 18 | song_id=ID(stored=True, unique=True), 19 | title=TEXT(analyzer=self.text_analyzer, spelling=True), 20 | artist=TEXT(analyzer=self.text_analyzer, spelling=True), 21 | source=TEXT(analyzer=self.text_analyzer, spelling=True), 22 | keywords=KEYWORD(lowercase=True, stored=True, scorable=True), 23 | ) 24 | self.storage = RamStorage() 25 | self.index = self.storage.create_index(self.song_schema) 26 | 27 | self.default_query_parser = MultifieldParser( 28 | ["song_id", "title", "artist", "source", "keywords"], 29 | self.song_schema, 30 | group=OrGroup, 31 | ) 32 | self.default_query_parser.add_plugin(FuzzyTermPlugin()) 33 | 34 | def import_songs(self, session: Session): 35 | writer = self.index.writer() 36 | songs = list(session.scalars(select(Song))) 37 | song_localize_stmt = select(SongLocalized) 38 | for song in songs: 39 | stmt = song_localize_stmt.where(SongLocalized.id == song.id) 40 | sl = session.scalar(stmt) 41 | song_id = song.id 42 | possible_titles: List[Union[str, None]] = [song.title] 43 | possible_artists: List[Union[str, None]] = [song.artist] 44 | possible_sources: List[Union[str, None]] = [song.source] 45 | if sl: 46 | possible_titles.extend( 47 | [sl.title_ja, sl.title_ko, sl.title_zh_hans, sl.title_zh_hant] 48 | ) 49 | possible_titles.extend( 50 | recover_search_title(sl.search_title_ja) 51 | + recover_search_title(sl.search_title_ko) 52 | + recover_search_title(sl.search_title_zh_hans) 53 | + recover_search_title(sl.search_title_zh_hant) 54 | ) 55 | possible_artists.extend( 56 | recover_search_title(sl.search_artist_ja) 57 | + recover_search_title(sl.search_artist_ko) 58 | + recover_search_title(sl.search_artist_zh_hans) 59 | + recover_search_title(sl.search_artist_zh_hant) 60 | ) 61 | possible_sources.extend( 62 | [ 63 | sl.source_ja, 64 | sl.source_ko, 65 | sl.source_zh_hans, 66 | sl.source_zh_hant, 67 | ] 68 | ) 69 | 70 | # remove empty items in list 71 | titles = [t for t in possible_titles if t != "" and t is not None] 72 | artists = [t for t in possible_artists if t != "" and t is not None] 73 | sources = [t for t in possible_sources if t != "" and t is not None] 74 | 75 | writer.update_document( 76 | song_id=song_id, 77 | title=" ".join(titles), 78 | artist=" ".join(artists), 79 | source=" ".join(sources), 80 | keywords=" ".join([song_id] + titles + artists + sources), 81 | ) 82 | 83 | writer.commit() 84 | 85 | def did_you_mean(self, string: str): 86 | results = set() 87 | 88 | with self.index.searcher() as searcher: 89 | corrector_keywords = searcher.corrector("keywords") # type: ignore 90 | corrector_song_id = searcher.corrector("song_id") # type: ignore 91 | corrector_title = searcher.corrector("title") # type: ignore 92 | corrector_artist = searcher.corrector("artist") # type: ignore 93 | corrector_source = searcher.corrector("source") # type: ignore 94 | 95 | results.update(corrector_keywords.suggest(string)) 96 | results.update(corrector_song_id.suggest(string)) 97 | results.update(corrector_title.suggest(string)) 98 | results.update(corrector_artist.suggest(string)) 99 | results.update(corrector_source.suggest(string)) 100 | 101 | if string in results: 102 | results.remove(string) 103 | 104 | return list(results) 105 | 106 | def search(self, string: str, *, limit: int = 10): 107 | query_string = f"{string}" 108 | query = self.default_query_parser.parse(query_string) 109 | with self.index.searcher() as searcher: 110 | results = searcher.search(query, limit=limit) 111 | return [result.get("song_id") for result in results] 112 | -------------------------------------------------------------------------------- /src/arcaea_offline/singleton.py: -------------------------------------------------------------------------------- 1 | from typing import Generic, TypeVar 2 | 3 | T = TypeVar("T") 4 | 5 | 6 | class Singleton(type, Generic[T]): 7 | _instance = None 8 | 9 | def __call__(cls, *args, **kwargs) -> T: 10 | if cls._instance is None: 11 | cls._instance = super().__call__(*args, **kwargs) 12 | return cls._instance 13 | -------------------------------------------------------------------------------- /src/arcaea_offline/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/283375/arcaea-offline/908613306fdeeb584ce34400b4435e114342c190/src/arcaea_offline/utils/__init__.py -------------------------------------------------------------------------------- /src/arcaea_offline/utils/partner.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from enum import IntEnum 3 | 4 | 5 | class KanaeDayNight(IntEnum): 6 | Day = 0 7 | Night = 1 8 | 9 | 10 | def kanae_day_night(timestamp: int) -> KanaeDayNight: 11 | """ 12 | :param timestamp: POSIX timestamp, which is passed to `datetime.fromtimestamp(timestamp)`. 13 | """ 14 | dt = datetime.fromtimestamp(timestamp) 15 | return KanaeDayNight.Day if 6 <= dt.hour <= 19 else KanaeDayNight.Night 16 | -------------------------------------------------------------------------------- /src/arcaea_offline/utils/rating.py: -------------------------------------------------------------------------------- 1 | from typing import Optional 2 | 3 | RATING_CLASS_TEXT_MAP = { 4 | 0: "Past", 5 | 1: "Present", 6 | 2: "Future", 7 | 3: "Beyond", 8 | 4: "Eternal", 9 | } 10 | 11 | RATING_CLASS_SHORT_TEXT_MAP = { 12 | 0: "PST", 13 | 1: "PRS", 14 | 2: "FTR", 15 | 3: "BYD", 16 | 4: "ETR", 17 | } 18 | 19 | 20 | def rating_class_to_text(rating_class: int) -> Optional[str]: 21 | return RATING_CLASS_TEXT_MAP.get(rating_class) 22 | 23 | 24 | def rating_class_to_short_text(rating_class: int) -> Optional[str]: 25 | return RATING_CLASS_SHORT_TEXT_MAP.get(rating_class) 26 | -------------------------------------------------------------------------------- /src/arcaea_offline/utils/score.py: -------------------------------------------------------------------------------- 1 | from typing import Any, Sequence 2 | 3 | SCORE_GRADE_FLOOR = [9900000, 9800000, 9500000, 9200000, 8900000, 8600000, 0] 4 | SCORE_GRADE_TEXTS = ["EX+", "EX", "AA", "A", "B", "C", "D"] 5 | MODIFIER_TEXTS = ["NORMAL", "EASY", "HARD"] 6 | CLEAR_TYPE_TEXTS = [ 7 | "TRACK LOST", 8 | "NORMAL CLEAR", 9 | "FULL RECALL", 10 | "PURE MEMORY", 11 | "EASY CLEAR", 12 | "HARD CLEAR", 13 | ] 14 | 15 | 16 | def zip_score_grade(score: int, __seq: Sequence, default: Any = "__PRESERVE__"): 17 | """ 18 | zip_score_grade is a simple wrapper that equals to: 19 | ```py 20 | for score_floor, val in zip(SCORE_GRADE_FLOOR, __seq): 21 | if score >= score_floor: 22 | return val 23 | return seq[-1] if default == "__PRESERVE__" else default 24 | ``` 25 | Could be useful in specific cases. 26 | """ 27 | return next( 28 | ( 29 | val 30 | for score_floor, val in zip(SCORE_GRADE_FLOOR, __seq) 31 | if score >= score_floor 32 | ), 33 | __seq[-1] if default == "__PRESERVE__" else default, 34 | ) 35 | 36 | 37 | def score_to_grade_text(score: int) -> str: 38 | return zip_score_grade(score, SCORE_GRADE_TEXTS) 39 | 40 | 41 | def modifier_to_text(modifier: int) -> str: 42 | return MODIFIER_TEXTS[modifier] 43 | 44 | 45 | def clear_type_to_text(clear_type: int) -> str: 46 | return CLEAR_TYPE_TEXTS[clear_type] 47 | -------------------------------------------------------------------------------- /src/arcaea_offline/utils/search_title.py: -------------------------------------------------------------------------------- 1 | import json 2 | from typing import List, Optional 3 | 4 | 5 | def recover_search_title(db_value: Optional[str]) -> List[str]: 6 | return json.loads(db_value) if db_value else [] 7 | -------------------------------------------------------------------------------- /tests/calculate/test_world_step.py: -------------------------------------------------------------------------------- 1 | from decimal import Decimal 2 | 3 | from arcaea_offline.calculate.world_step import ( 4 | AwakenedAyuPartnerBonus, 5 | LegacyMapStepBooster, 6 | PlayResult, 7 | calculate_step_original, 8 | ) 9 | 10 | 11 | def test_world_step(): 12 | # the result was copied from https://arcaea.fandom.com/wiki/World_Mode_Mechanics#Calculation 13 | # CC BY-SA 3.0 14 | 15 | booster = LegacyMapStepBooster(6, 250) 16 | partner_bonus = AwakenedAyuPartnerBonus("+3.6") 17 | play_result = PlayResult(play_rating=Decimal("11.299"), partner_step=92) 18 | result = calculate_step_original( 19 | play_result, partner_bonus=partner_bonus, step_booster=booster 20 | ) 21 | 22 | assert result.quantize(Decimal("0.000")) == Decimal("175.149") 23 | -------------------------------------------------------------------------------- /tests/db/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/283375/arcaea-offline/908613306fdeeb584ce34400b4435e114342c190/tests/db/__init__.py -------------------------------------------------------------------------------- /tests/db/db.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy import Engine, create_engine, inspect 2 | 3 | 4 | def create_engine_in_memory(): 5 | return create_engine("sqlite:///:memory:") 6 | -------------------------------------------------------------------------------- /tests/db/models/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/283375/arcaea-offline/908613306fdeeb584ce34400b4435e114342c190/tests/db/models/__init__.py -------------------------------------------------------------------------------- /tests/db/models/test_songs.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy import Engine 2 | from sqlalchemy.orm import Session 3 | 4 | from arcaea_offline.models.songs import ( 5 | Chart, 6 | ChartInfo, 7 | Difficulty, 8 | Pack, 9 | Song, 10 | SongsBase, 11 | SongsViewBase, 12 | ) 13 | 14 | from ..db import create_engine_in_memory 15 | 16 | 17 | def _song(**kw): 18 | defaults = {"artist": "test"} 19 | defaults.update(kw) 20 | return Song(**defaults) 21 | 22 | 23 | def _difficulty(**kw): 24 | defaults = {"rating_plus": False, "audio_override": False, "jacket_override": False} 25 | defaults.update(kw) 26 | return Difficulty(**defaults) 27 | 28 | 29 | class Test_Chart: 30 | def init_db(self, engine: Engine): 31 | SongsBase.metadata.create_all(engine) 32 | SongsViewBase.metadata.create_all(engine) 33 | 34 | def db(self): 35 | db = create_engine_in_memory() 36 | self.init_db(db) 37 | return db 38 | 39 | def test_chart_info(self): 40 | pre_entites = [ 41 | Pack(id="test", name="Test Pack"), 42 | _song(idx=0, id="song0", set="test", title="Full Chart Info"), 43 | _song(idx=1, id="song1", set="test", title="Partial Chart Info"), 44 | _song(idx=2, id="song2", set="test", title="No Chart Info"), 45 | _difficulty(song_id="song0", rating_class=2, rating=9), 46 | _difficulty(song_id="song1", rating_class=2, rating=9), 47 | _difficulty(song_id="song2", rating_class=2, rating=9), 48 | ChartInfo(song_id="song0", rating_class=2, constant=90, notes=1234), 49 | ChartInfo(song_id="song1", rating_class=2, constant=90), 50 | ] 51 | 52 | db = self.db() 53 | with Session(db) as session: 54 | session.add_all(pre_entites) 55 | session.commit() 56 | 57 | chart_song0_ratingclass2 = ( 58 | session.query(Chart) 59 | .where((Chart.song_id == "song0") & (Chart.rating_class == 2)) 60 | .one() 61 | ) 62 | 63 | assert chart_song0_ratingclass2.constant == 90 64 | assert chart_song0_ratingclass2.notes == 1234 65 | 66 | chart_song1_ratingclass2 = ( 67 | session.query(Chart) 68 | .where((Chart.song_id == "song1") & (Chart.rating_class == 2)) 69 | .one() 70 | ) 71 | 72 | assert chart_song1_ratingclass2.constant == 90 73 | assert chart_song1_ratingclass2.notes is None 74 | 75 | chart_song2_ratingclass2 = ( 76 | session.query(Chart) 77 | .where((Chart.song_id == "song2") & (Chart.rating_class == 2)) 78 | .first() 79 | ) 80 | 81 | assert chart_song2_ratingclass2 is None 82 | 83 | def test_difficulty_title_override(self): 84 | pre_entites = [ 85 | Pack(id="test", name="Test Pack"), 86 | _song(idx=0, id="test", set="test", title="Test"), 87 | _difficulty(song_id="test", rating_class=0, rating=2), 88 | _difficulty(song_id="test", rating_class=1, rating=5), 89 | _difficulty(song_id="test", rating_class=2, rating=8), 90 | _difficulty( 91 | song_id="test", rating_class=3, rating=10, title="TEST ~REVIVE~" 92 | ), 93 | ChartInfo(song_id="test", rating_class=0, constant=10), 94 | ChartInfo(song_id="test", rating_class=1, constant=10), 95 | ChartInfo(song_id="test", rating_class=2, constant=10), 96 | ChartInfo(song_id="test", rating_class=3, constant=10), 97 | ] 98 | 99 | db = self.db() 100 | with Session(db) as session: 101 | session.add_all(pre_entites) 102 | session.commit() 103 | 104 | charts_original_title = ( 105 | session.query(Chart) 106 | .where((Chart.song_id == "test") & (Chart.rating_class in [0, 1, 2])) 107 | .all() 108 | ) 109 | 110 | assert all(chart.title == "Test" for chart in charts_original_title) 111 | 112 | chart_overrided_title = ( 113 | session.query(Chart) 114 | .where((Chart.song_id == "test") & (Chart.rating_class == 3)) 115 | .one() 116 | ) 117 | 118 | assert chart_overrided_title.title == "TEST ~REVIVE~" 119 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | env_list = 3 | py311 4 | py310 5 | py39 6 | py38 7 | minversion = 4.11.3 8 | 9 | [testenv] 10 | description = run the tests with pytest 11 | package = wheel 12 | wheel_build_env = .pkg 13 | deps = 14 | pytest==7.4.3 15 | commands = 16 | pytest {tty:--color=yes} {posargs} 17 | --------------------------------------------------------------------------------