diff --git a/.github/workflows/autodeps.yml b/.github/workflows/autodeps.yml index 757f6c8..2974d9b 100644 --- a/.github/workflows/autodeps.yml +++ b/.github/workflows/autodeps.yml @@ -20,6 +20,8 @@ jobs: steps: - name: Checkout + with: + persist-credentials: true # credentials are needed to push commits uses: actions/checkout@v4 - name: Setup python uses: actions/setup-python@v5 @@ -28,21 +30,17 @@ jobs: - name: Bump dependencies run: | - python -m pip install -U pip pre-commit - python -m pip install -r test-requirements.txt - uv pip compile --universal --python-version=3.10 --upgrade test-requirements.in -o test-requirements.txt - pre-commit autoupdate --jobs 0 + python -m pip install -U uv + uv lock --upgrade + uv tool install pre-commit + uv run pre-commit autoupdate --jobs 0 - name: Install new requirements - run: python -m pip install -r test-requirements.txt + run: uv sync # apply newer versions' formatting - - name: Black - run: black src/azul - - - name: uv - run: | - uv pip compile --universal --python-version=3.10 test-requirements.in -o test-requirements.txt + - name: Pre-commit updates + run: uv run pre-commit run -a || true - name: Commit changes and create automerge PR env: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fc9b89d..bdc7d48 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,7 @@ name: CI +permissions: {} + on: push: branches-ignore: @@ -11,52 +13,52 @@ concurrency: cancel-in-progress: true jobs: -## Windows: -## name: 'Windows (${{ matrix.python }}, ${{ matrix.arch }}${{ matrix.extra_name }})' -## timeout-minutes: 20 -## runs-on: 'windows-latest' -## strategy: -## fail-fast: false -## matrix: -## python: ['3.10', '3.11', '3.12'] -## arch: ['x86', 'x64'] -## continue-on-error: >- -## ${{ -## ( -## endsWith(matrix.python, '-dev') -## || endsWith(matrix.python, '-nightly') -## ) -## && true -## || false -## }} -## steps: -## - name: Checkout -## uses: actions/checkout@v4 -## - name: Setup python -## uses: actions/setup-python@v5 -## with: -## # This allows the matrix to specify just the major.minor version while still -## # expanding it to get the latest patch version including alpha releases. -## # This avoids the need to update for each new alpha, beta, release candidate, -## # and then finally an actual release version. actions/setup-python doesn't -## # support this for PyPy presently so we get no help there. -## # -## # 'CPython' -> '3.9.0-alpha - 3.9.X' -## # 'PyPy' -> 'pypy-3.9' -## python-version: ${{ fromJSON(format('["{0}", "{1}"]', format('{0}.0-alpha - {0}.X', matrix.python), matrix.python))[startsWith(matrix.python, 'pypy')] }} -## architecture: '${{ matrix.arch }}' -## cache: pip -## cache-dependency-path: test-requirements.txt -## - name: Run tests -## run: ./ci.sh -## shell: bash + Windows: + name: 'Windows (${{ matrix.python }}, ${{ matrix.arch }}${{ matrix.extra_name }})' + timeout-minutes: 20 + runs-on: 'windows-latest' + strategy: + fail-fast: false + matrix: + python: ['3.10', '3.11', '3.12', '3.13'] + arch: ['x86', 'x64'] + continue-on-error: >- + ${{ + ( + endsWith(matrix.python, '-dev') + || endsWith(matrix.python, '-nightly') + ) + && true + || false + }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + persist-credentials: false + - name: Setup python + uses: actions/setup-python@v5 + with: + # This allows the matrix to specify just the major.minor version while still + # expanding it to get the latest patch version including alpha releases. + # This avoids the need to update for each new alpha, beta, release candidate, + # and then finally an actual release version. actions/setup-python doesn't + # support this for PyPy presently so we get no help there. + # + # 'CPython' -> '3.9.0-alpha - 3.9.X' + # 'PyPy' -> 'pypy-3.9' + python-version: ${{ fromJSON(format('["{0}", "{1}"]', format('{0}.0-alpha - {0}.X', matrix.python), matrix.python))[startsWith(matrix.python, 'pypy')] }} + architecture: '${{ matrix.arch }}' + cache: pip + cache-dependency-path: test-requirements.txt + - name: Run tests + run: ./ci.sh + shell: bash Ubuntu: name: 'Ubuntu (${{ matrix.python }}${{ matrix.extra_name }})' timeout-minutes: 10 runs-on: 'ubuntu-latest' - # Only run for PRs or pushes to main - if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main') strategy: fail-fast: false matrix: @@ -78,6 +80,8 @@ jobs: }} steps: - name: Checkout + with: + persist-credentials: false uses: actions/checkout@v4 - name: Setup python uses: actions/setup-python@v5 @@ -96,49 +100,51 @@ jobs: env: CHECK_FORMATTING: '${{ matrix.check_formatting }}' -## macOS: -## name: 'macOS (${{ matrix.python }})' -## timeout-minutes: 15 -## runs-on: 'macos-latest' -## strategy: -## fail-fast: false -## matrix: -## python: ['3.10', '3.11', '3.12'] -## continue-on-error: >- -## ${{ -## ( -## endsWith(matrix.python, '-dev') -## || endsWith(matrix.python, '-nightly') -## ) -## && true -## || false -## }} -## steps: -## - name: Checkout -## uses: actions/checkout@v4 -## - name: Setup python -## uses: actions/setup-python@v5 -## with: -## python-version: ${{ fromJSON(format('["{0}", "{1}"]', format('{0}.0-alpha - {0}.X', matrix.python), matrix.python))[startsWith(matrix.python, 'pypy')] }} -## cache: pip -## cache-dependency-path: test-requirements.txt -## - name: Run tests -## run: ./ci.sh + macOS: + name: 'macOS (${{ matrix.python }})' + timeout-minutes: 15 + runs-on: 'macos-latest' + strategy: + fail-fast: false + matrix: + python: ['3.10', '3.11', '3.12', '3.13'] + continue-on-error: >- + ${{ + ( + endsWith(matrix.python, '-dev') + || endsWith(matrix.python, '-nightly') + ) + && true + || false + }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + persist-credentials: false + - name: Setup python + uses: actions/setup-python@v5 + with: + python-version: ${{ fromJSON(format('["{0}", "{1}"]', format('{0}.0-alpha - {0}.X', matrix.python), matrix.python))[startsWith(matrix.python, 'pypy')] }} + cache: pip + cache-dependency-path: test-requirements.txt + - name: Run tests + run: ./ci.sh + + # https://github.com/marketplace/actions/alls-green#why + check: # This job does nothing and is only used for the branch protection + + if: always() + + needs: + - Windows + - Ubuntu + - macOS -## # https://github.com/marketplace/actions/alls-green#why -## check: # This job does nothing and is only used for the branch protection -## -## if: always() -## -## needs: -## - Windows -## - Ubuntu -## - macOS -## -## runs-on: ubuntu-latest -## -## steps: -## - name: Decide whether the needed jobs succeeded or failed -## uses: re-actors/alls-green@release/v1 -## with: -## jobs: ${{ toJSON(needs) }} + runs-on: ubuntu-latest + + steps: + - name: Decide whether the needed jobs succeeded or failed + uses: re-actors/alls-green@release/v1 + with: + jobs: ${{ toJSON(needs) }} diff --git a/.gitignore b/.gitignore index 8d514aa..a55ab68 100644 --- a/.gitignore +++ b/.gitignore @@ -86,6 +86,7 @@ instance/ docs/_build/ # PyBuilder +.pybuilder/ target/ # Jupyter Notebook @@ -96,6 +97,8 @@ profile_default/ ipython_config.py # pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: .python-version # pipenv @@ -105,6 +108,21 @@ ipython_config.py # install all needed dependencies. #Pipfile.lock +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + # PEP 582; used by e.g. github.com/David-OConnor/pyflow __pypackages__/ @@ -139,11 +157,15 @@ venv.bak/ .dmypy.json dmypy.json -# Pyre type checker +# Pyre static type analyzer .pyre/ -# Sphinx documentation -doc/_build/ +# Cython debug symbols +cython_debug/ # PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ca61cf5..98c205f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ ci: autofix_prs: true autoupdate_schedule: quarterly submodules: false - skip: [badgie, project-requirements] + skip: [badgie] repos: - repo: https://github.com/pre-commit/pre-commit-hooks @@ -16,6 +16,7 @@ repos: - id: check-merge-conflict - id: mixed-line-ending - id: check-case-conflict + - id: check-added-large-files - id: sort-simple-yaml files: .pre-commit-config.yaml - repo: https://github.com/psf/black-pre-commit-mirror @@ -28,6 +29,7 @@ repos: - id: ruff types: [file] types_or: [python, pyi, toml] + args: ["--show-fixes"] - repo: https://github.com/CoolCat467/badgie rev: v0.9.6 hooks: @@ -36,11 +38,13 @@ repos: rev: v2.4.1 hooks: - id: codespell - - repo: local + additional_dependencies: + - tomli + - repo: https://github.com/adhtruong/mirrors-typos + rev: v1.34.0 hooks: - - id: project-requirements - name: regenerate requirements.in - language: system - entry: python tools/project_requirements.py - pass_filenames: false - files: ^(test-requirements.in)|(pyproject.toml)$ + - id: typos + - repo: https://github.com/woodruffw/zizmor-pre-commit + rev: v1.11.0 + hooks: + - id: zizmor diff --git a/LICENSE-THIRD-PARTY b/LICENSE-THIRD-PARTY index c7343f4..e1a17ae 100644 --- a/LICENSE-THIRD-PARTY +++ b/LICENSE-THIRD-PARTY @@ -1,395 +1,3 @@ ---------------------------------------------------------------------------------------------------- - GNU LESSER GENERAL PUBLIC LICENSE -Applies to: - - Copyright (c) 2023, ItsDrike - All rights reserved. - - src/azul/base_io.py: Entire file - - src/azul/buffer.py: Entire file - - src/azul/utils.py: Entire file - - src/azul/encryption.py: Almost entire file (see details below) - - src/azul/encrypted_event.py: Almost entire file (see details below) - - tests/helpers.py: Entire file - - tests/protocol_helpers.py: Entire file - - tests/test_base_io.py: Entire file - - tests/test_buffer.py: Entire file - - tests/test_encryption.py: Entire file - - tests/test_utils.py: Entire file ---------------------------------------------------------------------------------------------------- - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. - - 0. Additional Definitions. - - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. - - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. - - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. ---------------------------------------------------------------------------------------------------- - Apache License version 2.0 -Applies to: - - Copyright (c) 2012 Ammar Askar - All rights reserved. - - src/azul/encryption.py: encrypt_token_and_secret, generate_shared_secret functions - - src/azul/encrypted_event.py: read, write functions ---------------------------------------------------------------------------------------------------- - - - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --------------------------------------------------------------------------------------------------- CREATIVE COMMONS 0 Applies to: diff --git a/check.sh b/check.sh index fb0c125..c382508 100755 --- a/check.sh +++ b/check.sh @@ -73,15 +73,15 @@ fi # Check pip compile is consistent echo "::group::Pip Compile - Tests" -uv pip compile --universal --python-version=3.10 test-requirements.in -o test-requirements.txt +uv lock echo "::endgroup::" -if git status --porcelain | grep -q "requirements.txt"; then - echo "::error::requirements.txt changed." - echo "::group::requirements.txt changed" - echo "* requirements.txt changed" >> "$GITHUB_STEP_SUMMARY" +if git status --porcelain | grep -q "uv.lock"; then + echo "::error::uv.lock changed." + echo "::group::uv.lock changed" + echo "* uv.lock changed" >> "$GITHUB_STEP_SUMMARY" git status --porcelain - git --no-pager diff --color ./*requirements.txt + git --no-pager diff --color ./*uv.lock EXIT_STATUS=1 echo "::endgroup::" fi @@ -97,9 +97,10 @@ if [ $EXIT_STATUS -ne 0 ]; then Problems were found by static analysis (listed above). To fix formatting and see remaining errors, run - uv pip install -r test-requirements.txt + uv sync --extra tools black src/$PROJECT ruff check src/$PROJECT + mypy ./check.sh in your local checkout. diff --git a/ci.sh b/ci.sh index 81cc25a..02ea341 100755 --- a/ci.sh +++ b/ci.sh @@ -19,18 +19,34 @@ python -c "import sys, struct; print('python:', sys.version); print('version_inf echo "::endgroup::" echo "::group::Install dependencies" -python -m pip install -U pip uv -c test-requirements.txt +python -m pip install -U pip tomli python -m pip --version +UV_VERSION=$(python -c 'import tomli; from pathlib import Path; print({p["name"]:p for p in tomli.loads(Path("uv.lock").read_text())["package"]}["uv"]["version"])') +python -m pip install uv==$UV_VERSION python -m uv --version -python -m uv pip install build - -python -m build -wheel_package=$(ls dist/*.whl) -python -m uv pip install "$PROJECT @ $wheel_package" -c test-requirements.txt +UV_VENV_SEED="pip" +python -m uv venv --seed --allow-existing + +# Determine platform and activate virtual environment accordingly +case "$OSTYPE" in + linux-gnu*|linux-musl*|darwin*) + source .venv/bin/activate + ;; + cygwin*|msys*) + source .venv/Scripts/activate + ;; + *) + echo "::error:: Unknown OS. Please add an activation method for '$OSTYPE'." + exit 1 + ;; +esac + +# Install uv in virtual environment +python -m pip install uv==$UV_VERSION if [ "$CHECK_FORMATTING" = "1" ]; then - python -m uv pip install -r test-requirements.txt exceptiongroup + python -m uv sync --locked --extra tests --extra tools echo "::endgroup::" source check.sh else @@ -38,10 +54,11 @@ else # expands to 0 != 1 if NO_TEST_REQUIREMENTS is not set, if set the `-0` has no effect # https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02 if [ "${NO_TEST_REQUIREMENTS-0}" == 1 ]; then - python -m uv pip install pytest coverage -c test-requirements.txt - flags="--skip-optional-imports" + python -m uv sync --locked --extra tests + flags="" + #"--skip-optional-imports" else - python -m uv pip install -r test-requirements.txt + python -m uv sync --locked --extra tests --extra tools flags="" fi @@ -61,9 +78,6 @@ else # get mypy tests a nice cache MYPYPATH=".." mypy --config-file= --cache-dir=./.mypy_cache -c "import $PROJECT" >/dev/null 2>/dev/null || true - # support subprocess spawning with coverage.py - # echo "import coverage; coverage.process_startup()" | tee -a "$INSTALLDIR/../sitecustomize.py" - echo "::endgroup::" echo "::group:: Run Tests" if coverage run --rcfile=../pyproject.toml -m pytest -ra --junitxml=../test-results.xml ../tests --verbose --durations=10 $flags; then @@ -71,10 +85,14 @@ else else PASSED=false fi + PREV_DIR="$PWD" + cd "$INSTALLDIR" + rm pyproject.toml + cd "$PREV_DIR" echo "::endgroup::" echo "::group::Coverage" - #coverage combine --rcfile ../pyproject.toml + coverage combine --rcfile ../pyproject.toml coverage report -m --rcfile ../pyproject.toml coverage xml --rcfile ../pyproject.toml diff --git a/computer_players/MiniMax_AI.py b/computer_players/MiniMax_AI.py new file mode 100755 index 0000000..2e290a8 --- /dev/null +++ b/computer_players/MiniMax_AI.py @@ -0,0 +1,197 @@ +#!/usr/bin/env python3 +# AI that plays checkers. + +"""Minimax Checkers AI.""" + +from __future__ import annotations + +# Programmed by CoolCat467 + +__title__ = "Minimax AI" +__author__ = "CoolCat467" +__version__ = "0.0.0" + +from math import inf as infinity +from typing import TYPE_CHECKING, TypeAlias, TypeVar + +from machine_client import RemoteState, run_clients_in_local_servers_sync +from minimax import Minimax, MinimaxResult, Player +from mypy_extensions import u8 + +from azul.state import ( + Phase, + SelectableDestinationTiles, + SelectableSourceTiles, + State, +) + +if TYPE_CHECKING: + from collections.abc import Iterable + + from typing_extensions import Self + +T = TypeVar("T") +Action: TypeAlias = ( + tuple[SelectableDestinationTiles, ...] + | tuple[SelectableSourceTiles, tuple[SelectableDestinationTiles, ...]] +) + +# Player: +# 0 = False = Person = MIN = 0, 2 +# 1 = True = AI (Us) = MAX = 1, 3 + + +class AutoWallState(State): + """Azul State with automatic wall tiling in regular play mode.""" + + __slots__ = () + + def _factory_offer_maybe_next_turn(self) -> Self: + """Return either current state or new state if player's turn is over.""" + new_state = super()._factory_offer_maybe_next_turn() + + if ( + new_state.current_phase == Phase.wall_tiling + and not new_state.variant_play + ): + return new_state.apply_auto_wall_tiling() + return new_state + + +class AzulMinimax(Minimax[tuple[AutoWallState, u8], Action]): + """Minimax Algorithm for Checkers.""" + + __slots__ = () + + @staticmethod + def value(state: tuple[AutoWallState, u8]) -> int | float: + """Return value of given game state.""" + # Real + real_state, max_player = state + if AzulMinimax.terminal(state): + winner, _score = real_state.get_win_order()[0] + if winner == max_player: + return 10 + return -10 + # Heuristic + min_ = 0 + max_ = 0 + for player_id, player_data in real_state.player_data.items(): + score = player_data.get_end_of_game_score() + score += player_data.get_floor_line_scoring() + if player_id == max_player: + max_ += score + else: + min_ += score + # More max will make score higher, + # more min will make score lower + # Plus one in divisor makes so never / 0 + return (max_ - min_) / (abs(max_) + abs(min_) + 1) + + @staticmethod + def terminal(state: tuple[AutoWallState, u8]) -> bool: + """Return if game state is terminal.""" + real_state, _max_player = state + return real_state.current_phase == Phase.end + + @staticmethod + def player(state: tuple[AutoWallState, u8]) -> Player: + """Return Player enum from current state's turn.""" + real_state, max_player = state + return ( + Player.MAX if real_state.current_turn == max_player else Player.MIN + ) + + @staticmethod + def actions(state: tuple[AutoWallState, u8]) -> Iterable[Action]: + """Return all actions that are able to be performed for the current player in the given state.""" + real_state, _max_player = state + return tuple(real_state.yield_actions()) + ## print(f'{len(actions) = }') + + @staticmethod + def result( + state: tuple[AutoWallState, u8], + action: Action, + ) -> tuple[AutoWallState, u8]: + """Return new state after performing given action on given current state.""" + real_state, max_player = state + return (real_state.preform_action(action), max_player) + + @classmethod + def adaptive_depth_minimax( + cls, + state: tuple[AutoWallState, u8], + ) -> MinimaxResult[Action]: + """Adaptive depth minimax.""" + # TODO + depth = 1 + return cls.alphabeta(state, depth) + + @classmethod + def alphabeta( + cls, + state: tuple[AutoWallState, u8], + depth: int | None = 5, + a: int | float = -infinity, + b: int | float = infinity, + ) -> MinimaxResult[ + tuple[SelectableDestinationTiles, ...] + | tuple[SelectableSourceTiles, tuple[SelectableDestinationTiles, ...]] + ]: + """Return minimax alphabeta pruning result best action for given current state.""" + new_state, player = state + if ( + new_state.current_phase == Phase.wall_tiling + and not new_state.variant_play + ): + new_state = new_state.apply_auto_wall_tiling() + return super().alphabeta((new_state, player), depth, a, b) + + +class MinimaxPlayer(RemoteState): + """Minimax Player.""" + + __slots__ = () + + def __init__(self) -> None: + """Initialize remote minmax player state.""" + super().__init__(state_class=AutoWallState) + + async def preform_turn(self) -> Action: + """Perform turn.""" + print("preform_turn") + ##value, action = CheckersMinimax.adaptive_depth_minimax( + ## self.state, 4, 5 + ##) + ##value, action = CheckersMinimax.minimax(self.state, 4) + assert isinstance(self.state, AutoWallState) + value, action = AzulMinimax.alphabeta((self.state, self.playing_as), 2) + ## value, action = AzulMinimax.alphabeta((self.state, self.playing_as), 4) + if action is None: + raise ValueError("action is None") + print(f"{value = }") + return action + + +def run() -> None: + """Run MinimaxPlayer clients in local server.""" + ## import random + ## + ## random.seed(0) + ## + ## state = (AutoWallState.new_game(2), 0) + ## + ## while not AzulMinimax.terminal(state): + ## action = AzulMinimax.adaptive_depth_minimax(state) + ## print(f"{action = }") + ## state = AzulMinimax.result(state, action.action) + ## print(f"{state = }") + ## print(state) + + run_clients_in_local_servers_sync(MinimaxPlayer) + + +if __name__ == "__main__": + print(f"{__title__} v{__version__}\nProgrammed by {__author__}.\n") + run() diff --git a/computer_players/machine_client.py b/computer_players/machine_client.py new file mode 100644 index 0000000..69fa89d --- /dev/null +++ b/computer_players/machine_client.py @@ -0,0 +1,508 @@ +"""Machine Client - Checkers game client that can be controlled mechanically.""" + +from __future__ import annotations + +__title__ = "Machine Client" +__author__ = "CoolCat467" +__version__ = "0.0.0" + +import sys +from abc import ABCMeta, abstractmethod +from contextlib import asynccontextmanager +from typing import TYPE_CHECKING, TypeAlias, cast + +import trio +from libcomponent.component import ( + Component, + ComponentManager, + Event, + ExternalRaiseManager, +) + +from azul.client import GameClient, read_advertisements +from azul.state import ( + PatternLine, + Phase, + SelectableDestination, + SelectableDestinationTiles, + SelectableSource, + SelectableSourceTiles, + State, + Tile, + factory_displays_deepcopy, + player_data_deepcopy, +) +from azul.vector import Vector2 + +if TYPE_CHECKING: + from collections import Counter + from collections.abc import AsyncGenerator + + from mypy_extensions import u8 + from numpy import int8 + from numpy.typing import NDArray + + +if sys.version_info < (3, 11): + from exceptiongroup import BaseExceptionGroup + +# Player: +# 0 = False = Person = MIN = 0, 2 +# 1 = True = AI (Us) = MAX = 1, 3 + +##Action: TypeAlias = tuple[SelectableSourceTiles, SelectableDestinationTiles] +Action: TypeAlias = ( + tuple[SelectableDestinationTiles, ...] + | tuple[SelectableSourceTiles, tuple[SelectableDestinationTiles, ...]] +) + + +class RemoteState(Component, metaclass=ABCMeta): + """Remote State. + + Keeps track of game state and call preform_action when it's this clients + turn. + """ + + __slots__ = ( + "can_made_play", + "has_initial", + "moves", + "playing_as", + "playing_lock", + "state", + ) + + def __init__(self, state_class: type[State] = State) -> None: + """Initialize remote state.""" + super().__init__("remote_state") + + ## print(f'[RemoteState] {state_class = }') + self.state = state_class.blank() + self.has_initial = False + + self.playing_as: u8 = 1 + self.moves = 0 + + self.playing_lock = trio.Lock() + self.can_made_play = True + + def bind_handlers(self) -> None: + """Register game event handlers.""" + self.register_handlers( + { + "game_winner": self.handle_game_over, + "game_initial_config": self.handle_initial_config, + "game_playing_as": self.handle_playing_as, + "game_board_data": self.handle_board_data, + "game_pattern_data": self.handle_pattern_data, + "game_factory_data": self.handle_factory_data, + "game_cursor_data": self.handle_cursor_data, + "game_table_data": self.handle_table_data, + # "game_cursor_set_movement_mode": + "game_pattern_current_turn_change": self.handle_pattern_current_turn_change, + # "game_cursor_set_destination": + "game_floor_data": self.handle_floor_data, + }, + ) + + async def apply_select_source( + self, + selection: SelectableSourceTiles, + ) -> None: + """Select source.""" + ## print(f"select {selection = }") + color = selection.tiles + if selection.source == SelectableSource.table_center: + await self.raise_event(Event("game_table_clicked", color)) + elif selection.source == SelectableSource.factory: + assert selection.source_id is not None + await self.raise_event( + Event("game_factory_clicked", (selection.source_id, color)), + ) + else: + raise NotImplementedError(selection.source) + + async def apply_select_destination( + self, + selection: SelectableDestinationTiles, + ) -> None: + """Select destination.""" + assert self.state.current_phase == Phase.factory_offer + ##assert not self.state.is_cursor_empty() + ## print(f'dest {selection = }') + + if selection.destination == SelectableDestination.floor_line: + await self.raise_event( + Event( + "game_floor_clicked", + (self.playing_as, selection.place_count), + ), + ) + elif selection.destination == SelectableDestination.pattern_line: + assert selection.destination_id is not None + line_id = selection.destination_id + currently_placed = self.state.get_player_line_current_place_count( + line_id, + ) + await self.raise_event( + Event( + "game_pattern_row_clicked", + ( + self.playing_as, + Vector2( + 5 - selection.place_count - currently_placed, + line_id, + ), + ), + ), + ) + else: + raise NotImplementedError(selection.destination) + + async def preform_action(self, action: Action) -> None: + """Raise events to perform game action.""" + await self.raise_event( + Event( + "game_cursor_location_transmit", + Vector2(0.5, 0.5), + ), + ) + source: SelectableSourceTiles | None = None + dest: tuple[SelectableDestinationTiles, ...] + if len(action) == 2: + raw_source, raw_dest = action + if isinstance(raw_source, SelectableSourceTiles): + source = raw_source + dest = cast("tuple[SelectableDestinationTiles, ...]", raw_dest) + else: + dest = cast("tuple[SelectableDestinationTiles, ...]", action) + else: + dest = action + + async with self.playing_lock: + self.can_made_play = False + if source is not None: + await self.apply_select_source(source) + for destination in dest: + ## print(f'{destination = }') + assert isinstance(destination, SelectableDestinationTiles) + await self.apply_select_destination(destination) + self.can_made_play = True + + ## raise NotImplementedError(f"{source = } {dest = }") + + @abstractmethod + async def preform_turn(self) -> Action: + """Perform turn, return action to perform.""" + + async def base_preform_turn(self) -> None: + """Perform turn.""" + ## async with self.playing_lock: + if not self.can_made_play: + print("Skipping making move because of flag.") + await trio.lowlevel.checkpoint() + return + self.can_made_play = False + self.moves += 1 + ## winner = self.state.check_for_win() + ## if winner is not None: + if self.state.current_phase == Phase.end: + print("Terminal state, not performing turn") + ##value = ("Lost", "Won")[winner == self.playing_as] + value = "" + print(f"{value} after {self.moves}") + await trio.lowlevel.checkpoint() + return + print(f"\nMove {self.moves}...") + action = await self.preform_turn() + await self.preform_action(action) + print("Action complete.") + + async def handle_playing_as(self, event: Event[u8]) -> None: + """Handle client playing as specified player event.""" + print("handle_playing_as") + self.playing_as = event.data + + if self.state.current_turn == self.playing_as: + await self.base_preform_turn() + return + await trio.lowlevel.checkpoint() + + async def handle_initial_config( + self, + event: Event[tuple[u8, u8, u8, u8, NDArray[int8]]], + ) -> None: + """Set up initial game state.""" + ## print("handle_initial_config") + ( + variant_play, + player_count, + factory_count, + current_turn, + floor_line_data, + ) = event.data + ## print(f'[RemoteState] {variant_play = }') + self.state = State.new_game(player_count, bool(variant_play)) + self.state = self.state._replace(current_turn=current_turn) + self.has_initial = True + ##if current_turn == self.playing_as: + ## await self.base_preform_turn() + + async def handle_game_over(self, event: Event[u8]) -> None: + """Raise network_stop event so we disconnect from server.""" + ## print("handle_game_over") + self.has_initial = False + await self.raise_event(Event("network_stop", None)) + + async def handle_board_data( + self, + event: Event[tuple[u8, NDArray[int8]]], + ) -> None: + """Handle player board data update.""" + ## print("handle_board_data") + player_id, board_data = event.data + + current_player_data = self.state.player_data[player_id] + + new_player_data = current_player_data._replace(wall=board_data) + + player_data = player_data_deepcopy(self.state.player_data) + player_data[player_id] = new_player_data + + self.state = self.state._replace( + player_data=player_data, + ) + await trio.lowlevel.checkpoint() + + async def handle_pattern_data( + self, + event: Event[tuple[u8, u8, tuple[u8, u8]]], + ) -> None: + """Handle player pattern line data update.""" + ## print("handle_pattern_data") + player_id, row_id, (tile_color, tile_count) = event.data + + current_player_data = self.state.player_data[player_id] + + new_player_data = current_player_data._replace( + lines=current_player_data.replace_pattern_line( + current_player_data.lines, + row_id, + PatternLine(Tile(tile_color), int(tile_count)), + ), + ) + + player_data = player_data_deepcopy(self.state.player_data) + player_data[player_id] = new_player_data + + self.state = self.state._replace( + player_data=player_data, + ) + await trio.lowlevel.checkpoint() + + async def handle_factory_data( + self, + event: Event[tuple[u8, Counter[u8]]], + ) -> None: + """Handle factory data update.""" + ## print("handle_factory_data") + factory_id, tiles = event.data + + factory_displays = factory_displays_deepcopy( + self.state.factory_displays, + ) + factory_displays[factory_id] = tiles + + self.state = self.state._replace( + factory_displays=factory_displays, + ) + + ##if self.state.current_turn == self.playing_as: + ## await self.base_preform_turn() + ## return + await trio.lowlevel.checkpoint() + + async def handle_cursor_data( + self, + event: Event[Counter[u8]], + ) -> None: + """Handle cursor data update.""" + ## print("handle_cursor_data") + cursor_contents = event.data + + self.state = self.state._replace( + cursor_contents=cursor_contents, + ) + + ## if self.state.current_turn == self.playing_as and not self.state.is_cursor_empty(): + ## await self.base_preform_turn() + ## return + await trio.lowlevel.checkpoint() + + async def handle_table_data(self, event: Event[Counter[u8]]) -> None: + """Handle table center tile data update.""" + ## print("handle_table_data") + table_center = event.data + + self.state = self.state._replace( + table_center=table_center, + ) + await trio.lowlevel.checkpoint() + + async def handle_pattern_current_turn_change( + self, + event: Event[u8], + ) -> None: + """Handle change of current turn.""" + print("handle_pattern_current_turn_change") + pattern_id = event.data + + self.state = self.state._replace( + current_turn=pattern_id, + ) + + if self.state.current_turn == self.playing_as: + await self.base_preform_turn() + return + await trio.lowlevel.checkpoint() + + async def handle_floor_data( + self, + event: Event[tuple[u8, Counter[u8]]], + ) -> None: + """Handle floor data event.""" + ## print("handle_floor_data") + floor_id, floor_line = event.data + + current_player_data = self.state.player_data[floor_id] + + new_player_data = current_player_data._replace(floor=floor_line) + + player_data = player_data_deepcopy(self.state.player_data) + player_data[floor_id] = new_player_data + + self.state = self.state._replace( + player_data=player_data, + ) + await trio.lowlevel.checkpoint() + + +class MachineClient(ComponentManager): + """Manager that runs until client_disconnected event fires.""" + + __slots__ = ("running",) + + def __init__(self, remote_state_class: type[RemoteState]) -> None: + """Initialize machine client.""" + super().__init__("machine_client") + + self.running = True + + self.add_component(remote_state_class()) + + @asynccontextmanager + async def client_with_block(self) -> AsyncGenerator[GameClient, None]: + """Add client temporarily with `with` block, ensuring closure.""" + async with GameClient("game_client") as client: + with self.temporary_component(client): + yield client + + def bind_handlers(self) -> None: + """Register client event handlers.""" + self.register_handlers( + { + "client_disconnected": self.handle_client_disconnected, + "client_connection_closed": self.handle_client_disconnected, + }, + ) + + ## async def raise_event(self, event: Event) -> None: + ## """Raise event but also log it if not tick.""" + ## if event.name not in {"tick"}: + ## print(f'{event = }') + ## return await super().raise_event(event) + + async def handle_client_disconnected(self, event: Event[None]) -> None: + """Set self.running to false on network disconnect.""" + self.running = False + + +async def run_client( + host: str, + port: int, + remote_state_class: type[RemoteState], + connected: set[tuple[str, int]], +) -> None: + """Run machine client and raise tick events.""" + async with trio.open_nursery() as main_nursery: + event_manager = ExternalRaiseManager( + "checkers", + main_nursery, + "client", + ) + client = MachineClient(remote_state_class) + with event_manager.temporary_component(client): + async with client.client_with_block(): + await event_manager.raise_event( + Event("client_connect", (host, port)), + ) + print(f"Connected to server {host}:{port}") + try: + while client.running: # noqa: ASYNC110 + # Wait so backlog things happen + await trio.sleep(1) + except KeyboardInterrupt: + print("Shutting down client from keyboard interrupt.") + await event_manager.raise_event( + Event("network_stop", None), + ) + print(f"Disconnected from server {host}:{port}") + client.unbind_components() + connected.remove((host, port)) + + +def run_client_sync( + host: str, + port: int, + remote_state_class: type[RemoteState], +) -> None: + """Run client and connect to server at host:port.""" + trio.run(run_client, host, port, remote_state_class, set()) + + +async def run_clients_in_local_servers( + remote_state_class: type[RemoteState], +) -> None: + """Run clients in local servers.""" + connected: set[tuple[str, int]] = set() + print("Watching for advertisements...\n(CTRL + C to quit)") + try: + async with trio.open_nursery(strict_exception_groups=True) as nursery: + while True: + advertisements = set(await read_advertisements()) + servers = {server for _motd, server in advertisements} + servers -= connected + for server in servers: + connected.add(server) + nursery.start_soon( + run_client, + *server, + remote_state_class, + connected, + ) + await trio.sleep(1) + except BaseExceptionGroup as exc: + for ex in exc.exceptions: + if isinstance(ex, KeyboardInterrupt): + print("Shutting down from keyboard interrupt.") + break + else: + raise + + +def run_clients_in_local_servers_sync( + remote_state_class: type[RemoteState], +) -> None: + """Run clients in local servers.""" + trio.run(run_clients_in_local_servers, remote_state_class) diff --git a/computer_players/minimax.py b/computer_players/minimax.py new file mode 100644 index 0000000..4c46356 --- /dev/null +++ b/computer_players/minimax.py @@ -0,0 +1,235 @@ +"""Minimax - Boilerplate code for Minimax AIs.""" + +from __future__ import annotations + +# Programmed by CoolCat467 + +__title__ = "Minimax" +__author__ = "CoolCat467" +__version__ = "0.0.0" + +import operator +import random +from abc import ABC, abstractmethod +from enum import IntEnum, auto +from math import inf as infinity +from typing import TYPE_CHECKING, Generic, NamedTuple, TypeVar, cast + +if TYPE_CHECKING: + from collections.abc import Callable, Iterable + + +class Player(IntEnum): + """Enum for player status.""" + + __slots__ = () + MIN = auto() + MAX = auto() + CHANCE = auto() + + +State = TypeVar("State") +Action = TypeVar("Action") + + +class MinimaxResult(NamedTuple, Generic[Action]): + """Minimax Result.""" + + value: int | float + action: Action | None + + +class Minimax(ABC, Generic[State, Action]): + """Base class for Minimax AIs.""" + + __slots__ = () + + LOWEST = -1 + HIGHEST = 1 + + @classmethod + @abstractmethod + def value(cls, state: State) -> int | float: + """Return the value of a given game state. + + Should be in range [cls.LOWEST, cls.HIGHEST]. + """ + + @classmethod + @abstractmethod + def terminal(cls, state: State) -> bool: + """Return if given game state is terminal.""" + + @classmethod + @abstractmethod + def player(cls, state: State) -> Player: + """Return player status given the state of the game. + + Must return either Player.MIN or Player.MAX, or Player.CHANCE + if there is a random action. + """ + + @classmethod + @abstractmethod + def actions(cls, state: State) -> Iterable[Action]: + """Return a collection of all possible actions in a given game state.""" + + @classmethod + @abstractmethod + def result(cls, state: State, action: Action) -> State: + """Return new game state after performing action on given state.""" + + @classmethod + def probability(cls, action: Action) -> float: + """Return probability that given chance node action will happen. + + Should be in range [0.0, 1.0] for 0% and 100% chance respectively. + """ + raise NotImplementedError() + + @classmethod + def minimax( + cls, + state: State, + depth: int | None = 5, + ) -> MinimaxResult[Action]: + """Return minimax result best action for a given state for the current player.""" + if cls.terminal(state): + return MinimaxResult(cls.value(state), None) + if depth is not None and depth <= 0: + # Choose a random action + # No need for cryptographic secure random + return MinimaxResult( + cls.value(state), + random.choice(tuple(cls.actions(state))), # noqa: S311 + ) + next_down = None if depth is None else depth - 1 + + current_player = cls.player(state) + value: int | float + best: Callable[[float, float], float] + if current_player == Player.MAX: + value = -infinity + best = max + elif current_player == Player.MIN: + value = infinity + best = min + elif current_player == Player.CHANCE: + value = 0 + best = cast("Callable[[float, float], float]", sum) + else: + raise ValueError(f"Unexpected player type {current_player!r}") + + best_action: Action | None = None + for action in cls.actions(state): + result = cls.minimax(cls.result(state, action), next_down) + result_value = result.value + if current_player == Player.CHANCE: + # Probability[action] + result_value *= cls.probability(action) + new_value = best(value, result_value) + if new_value != value and current_player != Player.CHANCE: + best_action = action + value = new_value + return MinimaxResult(value, best_action) + + @classmethod + def alphabeta( + cls, + state: State, + depth: int | None = 5, + a: int | float = -infinity, + b: int | float = infinity, + ) -> MinimaxResult[Action]: + """Return minimax alphabeta pruning result best action for given current state.""" + # print(f'alphabeta {depth = } {a = } {b = }') + + if cls.terminal(state): + return MinimaxResult(cls.value(state), None) + if depth is not None and depth <= 0: + # Choose a random action + # No need for cryptographic secure random + return MinimaxResult( + cls.value(state), + random.choice(tuple(cls.actions(state))), # noqa: S311 + ) + next_down = None if depth is None else depth - 1 + + current_player = cls.player(state) + value: int | float + best: Callable[[float, float], float] + compare = operator.gt + set_idx = 0 + if current_player == Player.MAX: + value = -infinity + best = max + compare = operator.gt # greater than (>) + set_idx = 0 + elif current_player == Player.MIN: + value = infinity + best = min + compare = operator.lt # less than (<) + set_idx = 1 + elif current_player == Player.CHANCE: + value = 0 + best = cast("Callable[[float, float], float]", sum) + else: + raise ValueError(f"Unexpected player type {current_player!r}") + + actions = tuple(cls.actions(state)) + successors = len(actions) + expect_a = successors * (a - cls.HIGHEST) + cls.HIGHEST + expect_b = successors * (b - cls.LOWEST) + cls.LOWEST + + best_action: Action | None = None + for action in actions: + if current_player == Player.CHANCE: + # Limit child a, b to a valid range + ax = max(expect_a, cls.LOWEST) + bx = min(expect_b, cls.HIGHEST) + # Search the child with new cutoff values + result = cls.alphabeta( + cls.result(state, action), + next_down, + ax, + bx, + ) + score = result.value + # Check for a, b cutoff conditions + if score <= expect_a: + return MinimaxResult(a, None) + if score >= expect_b: + return MinimaxResult(b, None) + value += score + # Adjust a, b for the next child + expect_a += cls.HIGHEST - score + expect_b += cls.LOWEST - score + continue + + result = cls.alphabeta(cls.result(state, action), next_down, a, b) + new_value = best(value, result.value) + + if new_value != value: + best_action = action + value = new_value + + if compare(new_value, (a, b)[set_idx ^ 1]): + # print("cutoff") + break # cutoff + + alpha_beta_value = (a, b)[set_idx] + new_alpha_beta_value = best(alpha_beta_value, value) + + if new_alpha_beta_value != alpha_beta_value: + # Set new best + alpha_beta_list = [a, b] + alpha_beta_list[set_idx] = new_alpha_beta_value + a, b = alpha_beta_list + if current_player == Player.CHANCE: + # No cutoff occurred, return score + return MinimaxResult(value / successors, None) + return MinimaxResult(value, best_action) + + +if __name__ == "__main__": + print(f"{__title__}\nProgrammed by {__author__}.\n") diff --git a/pyproject.toml b/pyproject.toml index 1a0cedd..42327a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,13 +36,13 @@ keywords = [ "ai", "multi-player", "azul", "ai-support", "networked-game" ] dependencies = [ - "pygame~=2.6.0", - "typing_extensions>=4.12.2", + "exceptiongroup>=1.2.2; python_version < '3.11'", + "libcomponent~=0.0.4", "mypy_extensions>=1.0.0", - "trio~=0.27.0", - "cryptography>=43.0.0", - "exceptiongroup; python_version < '3.11'", "numpy~=2.1.3", + "orjson>=3.10,<4", + "pygame~=2.6.0", + "trio~=0.30.0", ] [tool.setuptools.dynamic] @@ -52,31 +52,43 @@ version = {attr = "azul.game.__version__"} "Source" = "https://github.com/CoolCat467/Azul" "Bug Tracker" = "https://github.com/CoolCat467/Azul/issues" -[project.scripts] +[project.gui-scripts] azul_game = "azul.game:cli_run" +[project.optional-dependencies] +tests = [ + "pytest>=5.0", + "pytest-cov>=6.0.0", + "pytest-trio>=0.8.0", + "coverage>=7.2.5", + "uv>=0.5.21", + "mypy>=1.14.1", +] +tools = [ + 'black>=24.10.0; implementation_name == "cpython"', + "ruff>=0.9.2", + "codespell>=2.3.0", +] + [tool.setuptools.package-data] -azul = ["py.typed", "data/*"] +azul = ["py.typed", "data/*", "lang/*", "fonts/*"] + +[tool.uv] +package = true [tool.mypy] -files = ["src/azul/",] -check_untyped_defs = true -disallow_any_generics = true -disallow_untyped_calls = true -disallow_untyped_defs = true -ignore_missing_imports = true -no_implicit_optional = true -no_implicit_reexport = true +files = ["src/azul/", "computer_players"] +enable_error_code = ["truthy-bool", "mutable-override"] show_column_numbers = true show_error_codes = true show_traceback = true +disallow_any_decorated = true +disallow_any_unimported = true +ignore_missing_imports = true +local_partial_types = true +no_implicit_optional = true strict = true -strict_equality = true -warn_redundant_casts = true -warn_return_any = true warn_unreachable = true -warn_unused_configs = true -warn_unused_ignores = true [tool.ruff.lint.isort] combine-as-imports = true @@ -157,6 +169,9 @@ source_pkgs = ["azul"] omit = [ "__init__.py", ] +parallel = true +relative_files = true +source = ["."] [tool.coverage.report] precision = 1 diff --git a/src/azul/async_clock.py b/src/azul/async_clock.py deleted file mode 100644 index 9e93365..0000000 --- a/src/azul/async_clock.py +++ /dev/null @@ -1,106 +0,0 @@ -"""Asynchronous Clock - Asynchronous version of pygame.time.Clock.""" - -# Programmed by CoolCat467 - -from __future__ import annotations - -# Copyright (C) 2023 CoolCat467 -# -# This program 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. -# -# This program 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. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -__title__ = "Async Clock" -__author__ = "CoolCat467" -__license__ = "GNU General Public License Version 3" -__version__ = "0.0.0" - - -from time import perf_counter_ns -from typing import NewType - -import trio - -nanoseconds = NewType("nanoseconds", int) - - -def get_ticks() -> nanoseconds: - """Get Ticks.""" - return nanoseconds(perf_counter_ns()) - - -class Clock: - """pygame.time.Clock but with asynchronous tick.""" - - __slots__ = ( - "fps", - "fps_count", - "fps_tick", - "last_tick", - "rawpassed", - "timepassed", - ) - - def __init__(self) -> None: - """Initialize variables.""" - self.fps_tick = nanoseconds(0) - self.timepassed = nanoseconds(0) - self.rawpassed = nanoseconds(0) - self.last_tick: nanoseconds = get_ticks() - self.fps = 0.0 - self.fps_count = 0 - - def __repr__(self) -> str: - """Return representation of self.""" - return f"<{self.__class__.__name__}({self.fps:2f})>" - - def get_fps(self) -> float: - """Return the clock framerate in Frames Per Second.""" - return self.fps - - def get_rawtime(self) -> nanoseconds: - """Return the actual time used in the previous tick in nanoseconds (original was milliseconds).""" - return self.rawpassed - - def get_time(self) -> nanoseconds: - """Return time used in the previous tick (in nanoseconds, original was milliseconds).""" - return self.timepassed - - async def tick(self, framerate: int = 0) -> int: - """Tick the clock. Return time passed in nanoseconds, same as get_time (original was milliseconds).""" - endtime = 1000000000 // framerate if framerate > 0 else 0 - self.rawpassed = nanoseconds(get_ticks() - self.last_tick) - delay = endtime - self.rawpassed - if delay > 0: - await trio.sleep(delay / 1e9) # nanoseconds -> seconds - else: - await trio.lowlevel.checkpoint() - - nowtime: nanoseconds = get_ticks() - self.timepassed = nanoseconds(nowtime - self.last_tick) - self.fps_count += 1 - self.last_tick = nowtime - - if not self.fps_tick: - self.fps_count = 0 - self.fps_tick = nowtime - if self.fps_count >= 10: - self.fps = self.fps_count / ( - (nowtime - self.fps_tick) / 1e9 - ) # nanoseconds -> seconds - self.fps_count = 0 - self.fps_tick = nowtime - return self.timepassed - - -if __name__ == "__main__": # pragma: nocover - print(f"{__title__} v{__version__}\nProgrammed by {__author__}.\n") diff --git a/src/azul/base_io.py b/src/azul/base_io.py deleted file mode 100644 index da0b34e..0000000 --- a/src/azul/base_io.py +++ /dev/null @@ -1,701 +0,0 @@ -"""Base IO classes.""" - -# This is the base_io module from https://github.com/py-mine/mcproto v0.3.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -import struct -from abc import ABC, abstractmethod -from enum import Enum -from itertools import count -from typing import TYPE_CHECKING, Literal, TypeAlias, TypeVar, overload - -from .utils import from_twos_complement, to_twos_complement - -if TYPE_CHECKING: - from collections.abc import Awaitable, Callable - -__all__ = [ - "FLOAT_FORMATS_TYPE", - "INT_FORMATS_TYPE", - "BaseAsyncReader", - "BaseAsyncWriter", - "BaseSyncReader", - "BaseSyncWriter", - "StructFormat", -] - -T = TypeVar("T") -R = TypeVar("R") - - -# region: Format types - - -class StructFormat(str, Enum): - """All possible write/read struct types. - - .. seealso: - :module:`struct` module documentation. - """ - - BOOL = "?" - CHAR = "c" - BYTE = "b" - UBYTE = "B" - SHORT = "h" - USHORT = "H" - INT = "i" - UINT = "I" - LONG = "l" - ULONG = "L" - FLOAT = "f" - DOUBLE = "d" - HALFFLOAT = "e" - LONGLONG = "q" - ULONGLONG = "Q" - - -INT_FORMATS_TYPE: TypeAlias = Literal[ - StructFormat.BYTE, - StructFormat.UBYTE, - StructFormat.SHORT, - StructFormat.USHORT, - StructFormat.INT, - StructFormat.UINT, - StructFormat.LONG, - StructFormat.ULONG, - StructFormat.LONGLONG, - StructFormat.ULONGLONG, -] - -FLOAT_FORMATS_TYPE: TypeAlias = Literal[ - StructFormat.FLOAT, - StructFormat.DOUBLE, - StructFormat.HALFFLOAT, -] - -# endregion - -# region: Writer classes - - -class BaseAsyncWriter(ABC): - """Base class holding asynchronous write buffer/connection interactions.""" - - __slots__ = () - - @abstractmethod - async def write(self, data: bytes, /) -> None: - """Underlying write method, sending/storing the data. - - All the writer functions will eventually call this method. - """ - - @overload - async def write_value( - self, - fmt: INT_FORMATS_TYPE, - value: int, - /, - ) -> None: ... - - @overload - async def write_value( - self, - fmt: FLOAT_FORMATS_TYPE, - value: float, - /, - ) -> None: ... - - @overload - async def write_value( - self, - fmt: Literal[StructFormat.BOOL], - value: bool, - /, - ) -> None: ... - - @overload - async def write_value( - self, - fmt: Literal[StructFormat.CHAR], - value: str, - /, - ) -> None: ... - - async def write_value(self, fmt: StructFormat, value: object, /) -> None: - """Write a given ``value`` as given struct format (``fmt``) in big-endian mode.""" - await self.write(struct.pack(">" + fmt.value, value)) - - async def _write_varuint( - self, - value: int, - /, - *, - max_bits: int | None = None, - ) -> None: - """Write an arbitrarily big unsigned integer in a variable length format. - - This is a standard way of transmitting ints, and it allows smaller numbers to take less bytes. - - Writing will be limited up to integer values of ``max_bits`` bits, and trying to write bigger values will rase - a :exc:`ValueError`. Note that setting ``max_bits`` to for example 32 bits doesn't mean that at most 4 bytes - will be sent, in this case it would actually take at most 5 bytes, due to the variable encoding overhead. - - Varints send bytes where 7 least significant bits are value bits, and the most significant bit is continuation - flag bit. If this continuation bit is set (1), it indicates that there will be another varint byte sent after - this one. The least significant group is written first, followed by each of the more significant groups, making - varints little-endian, however in groups of 7 bits, not 8. - """ - value_max = ( - (1 << (max_bits)) - 1 if max_bits is not None else float("inf") - ) - if value < 0 or value > value_max: - raise ValueError( - f"Tried to write varint outside of the range of {max_bits}-bit int.", - ) - - remaining = value - while True: - if remaining & -0x80 == 0: # final byte (~0x7F) - await self.write_value(StructFormat.UBYTE, remaining) - return - # Write only 7 least significant bits with the first bit being 1, marking there will be another byte - await self.write_value(StructFormat.UBYTE, remaining & 0x7F | 0x80) - # Subtract the value we've already sent (7 least significant bits) - remaining >>= 7 - - async def write_varint(self, value: int, /) -> None: - """Write a 32-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._write_varuint`. - - Raises ValueError if value is outside of the range of a 32-bit signed integer. - """ - val = to_twos_complement(value, bits=32) - await self._write_varuint(val, max_bits=32) - - async def write_varlong(self, value: int, /) -> None: - """Write a 64-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._write_varuint`. - - Raises ValueError if value is outside of the range of a 64-bit signed integer. - """ - val = to_twos_complement(value, bits=64) - await self._write_varuint(val, max_bits=64) - - async def write_bytearray(self, data: bytearray | bytes, /) -> None: - """Write an arbitrary sequence of bytes, prefixed with a varint of it's size. - - Raises ValueError if length is is outside of the range of a 32-bit signed integer. - """ - await self.write_varint(len(data)) - await self.write(bytes(data)) - - async def write_ascii(self, value: str, /) -> None: - """Write ISO-8859-1 encoded string, with NULL (0x00) at the end to indicate string end.""" - data = value.encode(encoding="ISO-8859-1") - await self.write(data) - await self.write(b"\x00") - - async def write_utf(self, value: str, /) -> None: - """Write a UTF-8 encoded string, prefixed with a varint of it's size (in bytes). - - The maximum amount of UTF-8 characters is limited to 32767. - - Individual UTF-8 characters can take up to 4 bytes, however most of the common ones take up less. Assuming the - worst case of 4 bytes per every character, at most 131068 data bytes will be written + 3 additional bytes from - the varint encoding overhead. - - :raises ValueError: - If the given string ``value`` has more characters than the allowed maximum (32767). - """ - if len(value) > 32767: - raise ValueError( - "Maximum character limit for writing strings is 32767 characters.", - ) - - data = value.encode(encoding="utf-8") - await self.write_varint(len(data)) - await self.write(data) - - async def write_optional( - self, - value: T | None, - /, - writer: Callable[[T], Awaitable[R]], - ) -> R | None: - """Write a bool showing if a ``value`` is present, if so, also writes this value with ``writer`` function. - - * When ``value`` is ``None``, a bool of ``False`` will be written, and ``None`` is returned. - * When ``value`` is not ``None``, a bool of ``True`` is written, after which the ``writer`` function is called, - and the return value is forwarded. - """ - if value is None: - await self.write_value(StructFormat.BOOL, False) - return None - - await self.write_value(StructFormat.BOOL, True) - return await writer(value) - - -class BaseSyncWriter(ABC): - """Base class holding synchronous write buffer/connection interactions.""" - - __slots__ = () - - @abstractmethod - def write(self, data: bytes, /) -> None: - """Write data.""" - ... - - @overload - def write_value(self, fmt: INT_FORMATS_TYPE, value: int, /) -> None: ... - - @overload - def write_value( - self, - fmt: FLOAT_FORMATS_TYPE, - value: float, - /, - ) -> None: ... - - @overload - def write_value( - self, - fmt: Literal[StructFormat.BOOL], - value: bool, - /, - ) -> None: ... - - @overload - def write_value( - self, - fmt: Literal[StructFormat.CHAR], - value: str, - /, - ) -> None: ... - - def write_value(self, fmt: StructFormat, value: object, /) -> None: - """Write a given ``value`` as given struct format (``fmt``) in big-endian mode.""" - self.write(struct.pack(">" + fmt.value, value)) - - def _write_varuint( - self, - value: int, - /, - *, - max_bits: int | None = None, - ) -> None: - """Write an arbitrarily big unsigned integer in a variable length format. - - This is a standard way of transmitting ints, and it allows smaller numbers to take less bytes. - - Writing will be limited up to integer values of ``max_bits`` bits, and trying to write bigger values will rase - a :exc:`ValueError`. Note that setting ``max_bits`` to for example 32 bits doesn't mean that at most 4 bytes - will be sent, in this case it would actually take at most 5 bytes, due to the variable encoding overhead. - - Varints send bytes where 7 least significant bits are value bits, and the most significant bit is continuation - flag bit. If this continuation bit is set (1), it indicates that there will be another varint byte sent after - this one. The least significant group is written first, followed by each of the more significant groups, making - varints little-endian, however in groups of 7 bits, not 8. - """ - value_max = ( - (1 << (max_bits)) - 1 if max_bits is not None else float("inf") - ) - if value < 0 or value > value_max: - raise ValueError( - f"Tried to write varint outside of the range of {max_bits}-bit int.", - ) - - remaining = value - while True: - if remaining & ~0x7F == 0: # final byte - self.write_value(StructFormat.UBYTE, remaining) - return - # Write only 7 least significant bits with the first bit being 1, marking there will be another byte - self.write_value(StructFormat.UBYTE, remaining & 0x7F | 0x80) - # Subtract the value we've already sent (7 least significant bits) - remaining >>= 7 - - def write_varint(self, value: int, /) -> None: - """Write a 32-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._write_varuint`. - - Raises ValueError if length is is outside of the range of a 32-bit signed integer. - """ - val = to_twos_complement(value, bits=32) - self._write_varuint(val, max_bits=32) - - def write_varlong(self, value: int, /) -> None: - """Write a 64-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._write_varuint` docstring. - - Raises ValueError if length is is outside of the range of a 64-bit signed integer. - """ - val = to_twos_complement(value, bits=64) - self._write_varuint(val, max_bits=64) - - def write_bytearray(self, data: bytearray | bytes, /) -> None: - """Write an arbitrary sequence of bytes, prefixed with a varint of it's size. - - Raises ValueError if length is is outside of the range of a 32-bit signed integer. - """ - self.write_varint(len(data)) - self.write(bytes(data)) - - def write_ascii(self, value: str, /) -> None: - """Write ISO-8859-1 encoded string, with NULL (0x00) at the end to indicate string end.""" - data = value.encode(encoding="ISO-8859-1") - self.write(data) - self.write(b"\x00") - - def write_utf(self, value: str, /) -> None: - """Write a UTF-8 encoded string, prefixed with a varint of it's size (in bytes). - - The maximum amount of UTF-8 characters is limited to 32767. - - Individual UTF-8 characters can take up to 4 bytes, however most of the common ones take up less. Assuming the - worst case of 4 bytes per every character, at most 131068 data bytes will be written + 3 additional bytes from - the varint encoding overhead. - - :raises ValueError: - If the given string ``value`` has more characters than the allowed maximum (32767). - """ - if len(value) > 32767: - raise ValueError( - "Maximum character limit for writing strings is 32767 characters.", - ) - - data = value.encode("utf-8") - self.write_varint(len(data)) - self.write(data) - - def write_optional( - self, - value: T | None, - /, - writer: Callable[[T], R], - ) -> R | None: - """Write a bool showing if a ``value`` is present, if so, also writes this value with ``writer`` function. - - * When ``value`` is ``None``, a bool of ``False`` will be written, and ``None`` is returned. - * When ``value`` is not ``None``, a bool of ``True`` is written, after which the ``writer`` function is called, - and the return value is forwarded. - """ - if value is None: - self.write_value(StructFormat.BOOL, False) - return None - - self.write_value(StructFormat.BOOL, True) - return writer(value) - - -# endregion -# region: Reader classes - - -class BaseAsyncReader(ABC): - """Base class holding asynchronous read buffer/connection interactions.""" - - __slots__ = () - - @abstractmethod - async def read(self, length: int, /) -> bytearray: - """Underlying read method, obtaining the raw data. - - All of the reader functions will eventually call this method. - """ - - @overload - async def read_value(self, fmt: INT_FORMATS_TYPE, /) -> int: ... - - @overload - async def read_value(self, fmt: FLOAT_FORMATS_TYPE, /) -> float: ... - - @overload - async def read_value(self, fmt: Literal[StructFormat.BOOL], /) -> bool: ... - - @overload - async def read_value(self, fmt: Literal[StructFormat.CHAR], /) -> str: ... - - async def read_value(self, fmt: StructFormat, /) -> object: - """Read a value as given struct format (``fmt``) in big-endian mode. - - The amount of bytes to read will be determined based on the struct format automatically. - """ - length = struct.calcsize(fmt.value) - data = await self.read(length) - unpacked = struct.unpack(">" + fmt.value, data) - return unpacked[0] - - async def _read_varuint(self, *, max_bits: int | None = None) -> int: - """Read an arbitrarily big unsigned integer in a variable length format. - - This is a standard way of transmitting ints, and it allows smaller numbers to take less bytes. - - Reading will be limited up to integer values of ``max_bits`` bits, and trying to read bigger values will rase - an :exc:`OSError`. Note that setting ``max_bits`` to for example 32 bits doesn't mean that at most 4 bytes - will be read, in this case we would actually read at most 5 bytes, due to the variable encoding overhead. - - Varints send bytes where 7 least significant bits are value bits, and the most significant bit is continuation - flag bit. If this continuation bit is set (1), it indicates that there will be another varint byte sent after - this one. The least significant group is written first, followed by each of the more significant groups, making - varints little-endian, however in groups of 7 bits, not 8. - """ - value_max = ( - (1 << (max_bits)) - 1 if max_bits is not None else float("inf") - ) - - result = 0 - for i in count(): - byte = await self.read_value(StructFormat.UBYTE) - # Read 7 least significant value bits in this byte, and shift them appropriately to be in the right place - # then simply add them (OR) as additional 7 most significant bits in our result - result |= (byte & 0x7F) << (7 * i) - - # Ensure that we stop reading and raise an error if the size gets over the maximum - # (if the current amount of bits is higher than allowed size in bits) - if result > value_max: - raise OSError( - f"Received varint was outside the range of {max_bits}-bit int.", - ) - - # If the most significant bit is 0, we should stop reading - if not byte & 0x80: - break - - return result - - async def read_varint(self) -> int: - """Read a 32-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._read_varuint`. - """ - unsigned_num = await self._read_varuint(max_bits=32) - return from_twos_complement(unsigned_num, bits=32) - - async def read_varlong(self) -> int: - """Read a 64-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._read_varuint`. - """ - unsigned_num = await self._read_varuint(max_bits=64) - return from_twos_complement(unsigned_num, bits=64) - - async def read_bytearray(self, /) -> bytearray: - """Read an arbitrary sequence of bytes, prefixed with a varint of it's size.""" - length = await self.read_varint() - return await self.read(length) - - async def read_ascii(self) -> str: - """Read ISO-8859-1 encoded string, until we encounter NULL (0x00) at the end indicating string end.""" - # Keep reading bytes until we find NULL - result = bytearray() - while len(result) == 0 or result[-1] != 0: - byte = await self.read(1) - result.extend(byte) - return result[:-1].decode("ISO-8859-1") - - async def read_utf(self) -> str: - """Read a UTF-8 encoded string, prefixed with a varint of it's size (in bytes). - - The maximum amount of UTF-8 characters is limited to 32767. - - Individual UTF-8 characters can take up to 4 bytes, however most of the common ones take up less. Assuming the - worst case of 4 bytes per every character, at most 131068 data bytes will be read + 3 additional bytes from - the varint encoding overhead. - - :raises IOError: - * If the prefix varint is bigger than the maximum (131068) bytes, the string will not be read at all, - and :exc:`IOError` will be raised immediately. - * If the received string has more than the maximum amount of characters (32767). Note that in this case, - the string will still get read in it's entirety, since it fits into the maximum bytes limit (131068), - which was simply read at once. This limitation is here only to replicate the behavior of minecraft's - implementation. - """ - length = await self.read_varint() - if length > 131068: - raise OSError( - f"Maximum read limit for utf strings is 131068 bytes, got {length}.", - ) - - data = await self.read(length) - chars = data.decode("utf-8") - - if len(chars) > 32767: - raise OSError( - f"Maximum read limit for utf strings is 32767 characters, got {len(chars)}.", - ) - - return chars - - async def read_optional( - self, - reader: Callable[[], Awaitable[R]], - ) -> R | None: - """Read a bool showing if a value is present, if so, also reads this value with ``reader`` function. - - * When ``False`` is read, the function will not read anything and ``None`` is returned. - * When ``True`` is read, the ``reader`` function is called, and it's return value is forwarded. - """ - if not await self.read_value(StructFormat.BOOL): - return None - - return await reader() - - -class BaseSyncReader(ABC): - """Base class holding synchronous read buffer/connection interactions.""" - - __slots__ = () - - @abstractmethod - def read(self, length: int, /) -> bytearray: - """Read ``length`` bytes and return in a bytearray.""" - ... - - @overload - def read_value(self, fmt: INT_FORMATS_TYPE, /) -> int: ... - - @overload - def read_value(self, fmt: FLOAT_FORMATS_TYPE, /) -> float: ... - - @overload - def read_value(self, fmt: Literal[StructFormat.BOOL], /) -> bool: ... - - @overload - def read_value(self, fmt: Literal[StructFormat.CHAR], /) -> str: ... - - def read_value(self, fmt: StructFormat, /) -> object: - """Read a value into given struct format in big-endian mode. - - The amount of bytes to read will be determined based on the struct format automatically. - """ - length = struct.calcsize(fmt.value) - data = self.read(length) - unpacked = struct.unpack(">" + fmt.value, data) - return unpacked[0] - - def _read_varuint(self, *, max_bits: int | None = None) -> int: - """Read an arbitrarily big unsigned integer in a variable length format. - - This is a standard way of transmitting ints, and it allows smaller numbers to take less bytes. - - Reading will be limited up to integer values of ``max_bits`` bits, and trying to read bigger values will rase - an :exc:`IOError`. Note that setting ``max_bits`` to for example 32 bits doesn't mean that at most 4 bytes - will be read, in this case we would actually read at most 5 bytes, due to the variable encoding overhead. - - Varints send bytes where 7 least significant bits are value bits, and the most significant bit is continuation - flag bit. If this continuation bit is set (1), it indicates that there will be another varint byte sent after - this one. The least significant group is written first, followed by each of the more significant groups, making - varints little-endian, however in groups of 7 bits, not 8. - """ - value_max = ( - (1 << (max_bits)) - 1 if max_bits is not None else float("inf") - ) - - result = 0 - for i in count(): - byte = self.read_value(StructFormat.UBYTE) - # Read 7 least significant value bits in this byte, and shift them appropriately to be in the right place - # then simply add them (OR) as additional 7 most significant bits in our result - result |= (byte & 0x7F) << (7 * i) - - # Ensure that we stop reading and raise an error if the size gets over the maximum - # (if the current amount of bits is higher than allowed size in bits) - if result > value_max: - raise OSError( - f"Received varint was outside the range of {max_bits}-bit int.", - ) - - # If the most significant bit is 0, we should stop reading - if not byte & 0x80: - break - - return result - - def read_varint(self) -> int: - """Read a 32-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._read_varuint`. - """ - unsigned_num = self._read_varuint(max_bits=32) - return from_twos_complement(unsigned_num, bits=32) - - def read_varlong(self) -> int: - """Read a 64-bit signed integer in a variable length format. - - For more information about variable length format check :meth:`._read_varuint`. - """ - unsigned_num = self._read_varuint(max_bits=64) - return from_twos_complement(unsigned_num, bits=64) - - def read_bytearray(self) -> bytearray: - """Read an arbitrary sequence of bytes, prefixed with a varint of it's size.""" - length = self.read_varint() - return self.read(length) - - def read_ascii(self) -> str: - """Read ISO-8859-1 encoded string, until we encounter NULL (0x00) at the end indicating string end.""" - # Keep reading bytes until we find NULL - result = bytearray() - while len(result) == 0 or result[-1] != 0: - byte = self.read(1) - result.extend(byte) - return result[:-1].decode("ISO-8859-1") - - def read_utf(self) -> str: - """Read a UTF-8 encoded string, prefixed with a varint of it's size (in bytes). - - The maximum amount of UTF-8 characters is limited to 32767. - - Individual UTF-8 characters can take up to 4 bytes, however most of the common ones take up less. Assuming the - worst case of 4 bytes per every character, at most 131068 data bytes will be read + 3 additional bytes from - the varint encoding overhead. - - :raises IOError: - * If the prefix varint is bigger than the maximum (131068) bytes, the string will not be read at all, - and :exc:`IOError` will be raised immediately. - * If the received string has more than the maximum amount of characters (32767). Note that in this case, - the string will still get read in it's entirety, since it fits into the maximum bytes limit (131068), - which was simply read at once. This limitation is here only to replicate the behavior of minecraft's - implementation. - """ - length = self.read_varint() - if length > 131068: - raise OSError( - f"Maximum read limit for utf strings is 131068 bytes, got {length}.", - ) - - data = self.read(length) - chars = data.decode("utf-8") - - if len(chars) > 32767: - raise OSError( - f"Maximum read limit for utf strings is 32767 characters, got {len(chars)}.", - ) - - return chars - - def read_optional(self, reader: Callable[[], R]) -> R | None: - """Read a bool showing if a value is present, if so, also reads this value with ``reader`` function. - - * When ``False`` is read, the function will not read anything and ``None`` is returned. - * When ``True`` is read, the ``reader`` function is called, and it's return value is forwarded. - """ - if not self.read_value(StructFormat.BOOL): - return None - - return reader() - - -# endregion diff --git a/src/azul/buffer.py b/src/azul/buffer.py deleted file mode 100644 index 8207bc2..0000000 --- a/src/azul/buffer.py +++ /dev/null @@ -1,101 +0,0 @@ -"""Buffer module.""" - -# This is the buffer module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -from typing import Any - -from .base_io import BaseSyncReader, BaseSyncWriter - -__all__ = ["Buffer"] - - -class Buffer(BaseSyncWriter, BaseSyncReader, bytearray): - """In-memory bytearray-like buffer supporting the common read/write operations.""" - - __slots__ = ("pos",) - - def __init__(self, *args: Any, **kwargs: Any) -> None: - """Initialize starting at position zero.""" - super().__init__(*args, **kwargs) - self.pos = 0 - - def write(self, data: bytes) -> None: - """Write/Store given ``data`` into the buffer.""" - self.extend(data) - - def read(self, length: int) -> bytearray: - """Read data stored in the buffer. - - Reading data doesn't remove that data, rather that data is treated as already read, and - next read will start from the first unread byte. If freeing the data is necessary, check - the :meth:`.clear` function. - - :param length: - Amount of bytes to be read. - - If the requested amount can't be read (buffer doesn't contain that much data/buffer - doesn't contain any data), an :exc:`IOError` will be re-raised. - - If there were some data in the buffer, but it was less than requested, this remaining - data will still be depleted and the partial data that was read will be a part of the - error message in the :exc:`IOError`. This behavior is here to mimic reading from a real - socket connection. - """ - end = self.pos + length - - if end > len(self): - data = self[self.pos : len(self)] - bytes_read = len(self) - self.pos - self.pos = len(self) - raise OSError( - "Requested to read more data than available." - f" Read {bytes_read} bytes: {data}, out of {length} requested bytes.", - ) - - try: - return self[self.pos : end] - finally: - self.pos = end - - def clear(self, only_already_read: bool = False) -> None: - """Clear out the stored data and reset position. - - :param only_already_read: - When set to ``True``, only the data that was already marked as read will be cleared, - and the position will be reset (to start at the remaining data). This can be useful - for avoiding needlessly storing large amounts of data in memory, if this data is no - longer useful. - - Otherwise, if set to ``False``, all of the data is cleared, and the position is reset, - essentially resulting in a blank buffer. - """ - if only_already_read: - del self[: self.pos] - else: - super().clear() - self.pos = 0 - - def reset(self) -> None: - """Reset the position in the buffer. - - Since the buffer doesn't automatically clear the already read data, it is possible to simply - reset the position and read the data it contains again. - """ - self.pos = 0 - - def flush(self) -> bytearray: - """Read all of the remaining data in the buffer and clear it out.""" - data = self[self.pos : len(self)] - self.clear() - return data - - @property - def remaining(self) -> int: - """Get the amount of bytes that's still remaining in the buffer to be read.""" - return len(self) - self.pos diff --git a/src/azul/client.py b/src/azul/client.py index 03815e5..e10e70e 100644 --- a/src/azul/client.py +++ b/src/azul/client.py @@ -1,490 +1,586 @@ -"""Azul Client.""" - -from __future__ import annotations - -import contextlib +"""Game Client.""" # Programmed by CoolCat467 -# Hide the pygame prompt -import os -import sys -from os import path -from pathlib import Path -from typing import TYPE_CHECKING, Any, Final - -import trio -from pygame.locals import K_ESCAPE, KEYUP, QUIT, RESIZABLE, WINDOWRESIZED -from pygame.rect import Rect - -from azul import conf, lang, objects, sprite -from azul.component import Component, ComponentManager, Event -from azul.statemachine import AsyncState, AsyncStateMachine -from azul.vector import Vector2 -os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "True" -if os.environ["PYGAME_HIDE_SUPPORT_PROMPT"]: - import pygame -del os +# Copyright (C) 2023-2024 CoolCat467 +# +# This program 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. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +from __future__ import annotations -if TYPE_CHECKING: - from collections.abc import Iterator, Sequence - -__title__ = "Azul Client" +__title__ = "Game Client" __author__ = "CoolCat467" -__version__ = "2.0.0" - -SCREEN_SIZE = Vector2(800, 600) -FPS = 30 -# FPS = 60 -VSYNC = True -# PORT = server.PORT - -ROOT_FOLDER: Final = Path(__file__).absolute().parent -DATA_FOLDER: Final = ROOT_FOLDER / "data" -FONT_FOLDER: Final = ROOT_FOLDER / "fonts" - -FONT = FONT_FOLDER / "RuneScape-UF-Regular.ttf" +__license__ = "GNU General Public License Version 3" +__version__ = "0.0.0" +import struct +import traceback +from typing import TYPE_CHECKING -class AzulClient(sprite.GroupProcessor, AsyncStateMachine): - """Gear Runner and Layered Dirty Sprite group handler.""" - - def __init__(self) -> None: - """Initialize azul client.""" - sprite.GroupProcessor.__init__(self) - AsyncStateMachine.__init__(self) +import trio +from libcomponent import network +from libcomponent.base_io import StructFormat +from libcomponent.buffer import Buffer +from libcomponent.component import Event +from libcomponent.network_utils import ClientNetworkEventComponent + +from azul.network_shared import ( + ADVERTISEMENT_IP, + ADVERTISEMENT_PORT, + ClientBoundEvents, + ServerBoundEvents, + decode_cursor_location, + decode_int8_array, + decode_numeric_uint8_counter, + decode_tile_count, + encode_cursor_location, +) +from azul.vector import Vector2 - self.add_states( - ( - HaltState(), - AzulInitialize(), - ), +if TYPE_CHECKING: + from mypy_extensions import u8 + + from azul.state import Tile + + +async def read_advertisements( + timeout: int = 3, # noqa: ASYNC109 +) -> list[tuple[str, tuple[str, int]]]: + """Read server advertisements from network. Return tuples of (motd, (host, port)).""" + # Look up multicast group address in name server and find out IP version + addrinfo = (await trio.socket.getaddrinfo(ADVERTISEMENT_IP, None))[0] + + with trio.socket.socket( + family=trio.socket.AF_INET, # IPv4 + type=trio.socket.SOCK_DGRAM, # UDP + proto=trio.socket.IPPROTO_UDP, + ) as udp_socket: + # SO_REUSEADDR: allows binding to port potentially already in use + # Allow multiple copies of this program on one machine + # (not strictly needed) + udp_socket.setsockopt( + trio.socket.SOL_SOCKET, + trio.socket.SO_REUSEADDR, + 1, ) - @property - def running(self) -> bool: - """Boolean of if state machine is running.""" - return self.active_state is not None + await udp_socket.bind(("", ADVERTISEMENT_PORT)) + + # # Tell the kernel that we are a multicast socket + # udp_socket.setsockopt(trio.socket.IPPROTO_IP, trio.socket.IP_MULTICAST_TTL, 255) + + # socket.IPPROTO_IP works on Linux and Windows + # # IP_MULTICAST_IF: force sending network traffic over specific network adapter + # IP_ADD_MEMBERSHIP: join multicast group + # udp_socket.setsockopt( + # trio.socket.IPPROTO_IP, trio.socket.IP_MULTICAST_IF, + # trio.socket.inet_aton(network_adapter) + # ) + # udp_socket.setsockopt( + # trio.socket.IPPROTO_IP, + # trio.socket.IP_ADD_MEMBERSHIP, + # struct.pack( + # "4s4s", + # trio.socket.inet_aton(group), + # trio.socket.inet_aton(network_adapter), + # ), + # ) + addr_port = addrinfo[4][0] + assert isinstance(addr_port, str) + group_bin = trio.socket.inet_pton(addrinfo[0], addr_port) + # Join group + if addrinfo[0] == trio.socket.AF_INET: # IPv4 + mreq = group_bin + struct.pack("=I", trio.socket.INADDR_ANY) + udp_socket.setsockopt( + trio.socket.IPPROTO_IP, + trio.socket.IP_ADD_MEMBERSHIP, + mreq, + ) + else: # IPv6 + mreq = group_bin + struct.pack("@I", 0) + udp_socket.setsockopt( + trio.socket.IPPROTO_IPV6, + trio.socket.IPV6_JOIN_GROUP, + mreq, + ) + + host = "" + buffer = b"" + with trio.move_on_after(timeout): + buffer, address = await udp_socket.recvfrom(512) + host, _port = address + # print(f"{buffer = }") + # print(f"{address = }") + + response: list[tuple[str, tuple[str, int]]] = [] + + start = 0 + for _ in range(1024): + ad_start = buffer.find(b"[AD]", start) + if ad_start == -1: + break + ad_end = buffer.find(b"[/AD]", ad_start) + if ad_end == -1: + break + start_block = buffer.find(b"[AZUL]", ad_end) + if start_block == -1: + break + start_end = buffer.find(b"[/AZUL]", start_block) + if start_end == -1: + break + + start = start_end + + motd = buffer[start_block + 10 : start_end].decode("utf-8") + raw_port = buffer[ad_start + 4 : ad_end].decode("utf-8") + try: + port = int(raw_port) + except ValueError: + continue + response.append((motd, (host, port))) + return response - async def raise_event(self, event: Event[Any]) -> None: - """Raise component event in all groups.""" - if self.active_state is None: - return - manager = getattr(self.active_state, "manager", None) - assert isinstance(manager, ComponentManager | None) - if manager is None: - return - await manager.raise_event(event) +class GameClient(ClientNetworkEventComponent): + """Game Client Network Event Component. -class AzulState(AsyncState[AzulClient]): - """Azul Client Asynchronous base class.""" + This class handles connecting to the game server, transmitting events + to the server, and reading and raising incoming events from the server. + """ - __slots__ = ("id", "manager") + __slots__ = ("connect_event_lock", "running") def __init__(self, name: str) -> None: - """Initialize azul state.""" + """Initialize GameClient.""" super().__init__(name) - self.id: int = 0 - self.manager = ComponentManager(self.name) - - -class HaltState(AzulState): - """Halt state to set state to None so running becomes False.""" - - def __init__(self) -> None: - """Initialize halt state.""" - super().__init__("Halt") - - async def check_conditions(self) -> None: - """Set active state to None.""" - await self.machine.set_state(None) - + # Five seconds until timeout is generous, but it gives server end wiggle + # room. + self.timeout = 5 -class ClickDestinationComponent(Component): - """Component that will use targeting to go to wherever you click on the screen.""" - - __slots__ = ("selected",) - outline = pygame.color.Color(255, 220, 0) - - def __init__(self) -> None: - """Initialize click destination component.""" - super().__init__("click_dest") + sbe = ServerBoundEvents + self.register_network_write_events( + { + "encryption_response->server": sbe.encryption_response, + "factory_clicked->server[write]": sbe.factory_clicked, + "cursor_location->server[write]": sbe.cursor_location, + "pattern_row_clicked->server[write]": sbe.pattern_row_clicked, + "table_clicked->server[write]": sbe.table_clicked, + "floor_clicked->server[write]": sbe.floor_clicked, + }, + ) + cbe = ClientBoundEvents + self.register_read_network_events( + { + cbe.encryption_request: "server->encryption_request", + cbe.callback_ping: "server->callback_ping", + cbe.initial_config: "server->initial_config", + cbe.playing_as: "server->playing_as", + cbe.game_over: "server->game_over", + cbe.board_data: "server->board_data", + cbe.pattern_data: "server->pattern_data", + cbe.factory_data: "server->factory_data", + cbe.cursor_data: "server->cursor_data", + cbe.table_data: "server->table_data", + cbe.cursor_movement_mode: "server->cursor_movement_mode", + cbe.current_turn_change: "server->current_turn_change", + cbe.cursor_position: "server->cursor_position", + cbe.floor_data: "server->floor_data", + }, + ) - self.selected = False + self.connect_event_lock = trio.Lock() + self.running = False def bind_handlers(self) -> None: - """Register PygameMouseButtonDown and tick handlers.""" + """Register event handlers.""" + super().bind_handlers() self.register_handlers( { - "click": self.click, - "drag": self.drag, - "PygameMouseButtonDown": self.mouse_down, - "tick": self.move_towards_dest, - "init": self.cache_outline, - "test": self.test, + "server->encryption_request": self.read_encryption_request, + "server->callback_ping": self.read_callback_ping, + "server->initial_config": self.read_initial_config, + "server->playing_as": self.read_playing_as, + "server->game_over": self.read_game_over, + "server->board_data": self.read_board_data, + "server->pattern_data": self.read_pattern_data, + "server->factory_data": self.read_factory_data, + "server->cursor_data": self.read_cursor_data, + "server->table_data": self.read_table_data, + "server->cursor_movement_mode": self.read_cursor_movement_mode, + "server->current_turn_change": self.read_current_turn_change, + "server->cursor_position": self.read_cursor_position, + "server->floor_data": self.read_floor_data, + "client_connect": self.handle_client_connect, + "network_stop": self.handle_network_stop, + "game_factory_clicked": self.write_game_factory_clicked, + "game_cursor_location_transmit": self.write_game_cursor_location_transmit, + "game_pattern_row_clicked": self.write_game_pattern_row_clicked, + "game_table_clicked": self.write_game_table_clicked, + "game_floor_clicked": self.write_game_floor_clicked, + # "callback_ping": self.print_callback_ping, }, ) - async def test(self, event: Event[object]) -> None: - """Print out event data.""" - print(f"{event = }") - - async def cache_outline(self, _: Event[None]) -> None: - """Precalculate outlined images.""" - image: sprite.ImageComponent = self.get_component("image") - outline: sprite.OutlineComponent = image.get_component("outline") - outline.precalculate_all_outlined(self.outline) - - async def update_selected(self) -> None: - """Update selected.""" - image: sprite.ImageComponent = self.get_component("image") - outline: sprite.OutlineComponent = image.get_component("outline") - - color = (None, self.outline)[int(self.selected)] - outline.set_color(color) - - if not self.selected: - movement: sprite.MovementComponent = self.get_component("movement") - movement.speed = 0 + async def print_callback_ping(self, event: Event[int]) -> None: + """Print received `callback_ping` event from server. + + This event is used as a sort of keepalive heartbeat, because + it stops the connection from timing out. + """ + difference = event.data + print(f"[azul.client] print_callback_ping {difference * 1e-06:.03f}ms") + await trio.lowlevel.checkpoint() + + async def raise_disconnect(self, message_key: str) -> None: + """Raise client_disconnected event with given message.""" + print(f"{self.__class__.__name__}: {message_key}") + if not self.manager_exists: + print( + f"{self.__class__.__name__}: Manager does not exist, not raising disconnect event.", + ) + return + # self.unregister_all_network_write_events() + await self.raise_event(Event("client_disconnected", message_key)) + await self.close() + assert self.not_connected - async def click( + async def write_event( self, - event: Event[sprite.PygameMouseButtonEventData], + event: Event[bytes | bytearray | memoryview], ) -> None: - """Toggle selected.""" - if event.data["button"] == 1: - self.selected = not self.selected - - await self.update_selected() + """Send event to network if running, otherwise does nothing. + + Raises: + RuntimeError: if unregistered packet id received from network + trio.BusyResourceError: if another task is already executing a + :meth:`send_all`, :meth:`wait_send_all_might_not_block`, or + :meth:`HalfCloseableStream.send_eof` on this stream. + trio.BrokenResourceError: if something has gone wrong, and the stream + is broken. + trio.ClosedResourceError: if you previously closed this stream + object, or if another task closes this stream object while + :meth:`send_all` is running. + + """ + if not self.running: + await trio.lowlevel.checkpoint() + print( + f"[azul.client.write_event] Skipping writing {event.name!r}, not running.", + ) + return + await super().write_event(event) + + async def handle_read_event(self) -> None: + """Raise events from server. + + Can raise following exceptions: + RuntimeError - Unhandled packet id + network.NetworkStreamNotConnectedError - Network stream is not connected + OSError - Stopped responding + trio.BrokenResourceError - Something is wrong and stream is broken + + Shouldn't happen with write lock but still: + trio.BusyResourceError - Another task is already writing data + + Handled exceptions: + trio.ClosedResourceError - Stream is closed or another task closes stream + network.NetworkTimeoutError - Timeout + network.NetworkEOFError - Server closed connection + """ + # print(f"{self.__class__.__name__}[{self.name}]: handle_read_event") + if not self.manager_exists: + return + if self.not_connected: + await self.raise_disconnect("error.not_connected") + return + # event: Event[bytearray] | None = None + try: + # print("handle_read_event start") + event = await self.read_event() + except trio.ClosedResourceError: + self.running = False + await self.close() + print(f"[{self.name}] Socket closed from another task.") + return + except network.NetworkTimeoutError as exc: + # print("[azul.client] Network timeout") + if self.running: + self.running = False + print(f"[{self.name}] NetworkTimeoutError") + await self.close() + traceback.print_exception(exc) + await self.raise_disconnect("error.read_event_fail") + return + except network.NetworkStreamNotConnectedError as exc: + self.running = False + print(f"[{self.name}] NetworkStreamNotConnectedError") + traceback.print_exception(exc) + await self.close() + assert self.not_connected + raise + except network.NetworkEOFError: + self.running = False + print(f"[{self.name}] NetworkEOFError") + await self.close() + await self.raise_disconnect("error.socket_eof") + return - async def drag(self, event: Event[None]) -> None: - """Drag sprite.""" - if not self.selected: - self.selected = True - await self.update_selected() - movement: sprite.MovementComponent = self.get_component("movement") - movement.speed = 0 + ## print(f'[azul.client] handle_read_event {event}') - async def mouse_down( - self, - event: Event[sprite.PygameMouseButtonEventData], - ) -> None: - """Target click pos if selected.""" - if not self.selected: - return - if event.data["button"] == 1: - movement: sprite.MovementComponent = self.get_component("movement") - movement.speed = 200 - target: sprite.TargetingComponent = self.get_component("targeting") - target.destination = Vector2.from_iter(event.data["pos"]) + await self.raise_event(event) - async def move_towards_dest( + async def handle_client_connect( self, - event: Event[sprite.TickEventData], + event: Event[tuple[str, int]], ) -> None: - """Move closer to destination.""" - target: sprite.TargetingComponent = self.get_component("targeting") - await target.move_destination_time(event.data.time_passed) - - -class MrFloppy(sprite.Sprite): - """Mr. Floppy test sprite.""" - - __slots__ = () - - def __init__(self) -> None: - """Initialize mr floppy sprite.""" - super().__init__("MrFloppy") - - self.add_components( - ( - sprite.MovementComponent(), - sprite.TargetingComponent(), - ClickDestinationComponent(), - sprite.ImageComponent(), - sprite.DragClickEventComponent(), + """Have client connect to address specified in event.""" + if self.connect_event_lock.locked(): + raise RuntimeError("2nd client connect fired!") + async with self.connect_event_lock: + # Mypy does not understand that self.not_connected becomes + # false after connect call. + if not TYPE_CHECKING and not self.not_connected: + raise RuntimeError("Already connected!") + try: + await self.connect(*event.data) + except OSError as ex: + traceback.print_exception(ex) + else: + self.running = True + while not self.not_connected and self.running: + await self.handle_read_event() + self.running = False + + await self.close() + if self.manager_exists: + await self.raise_event( + Event("client_connection_closed", None), + ) + else: + print( + "manager does not exist, cannot send client connection closed event.", + ) + return + await self.raise_disconnect("error.socket_connect_fail") + + async def read_initial_config(self, event: Event[bytearray]) -> None: + """Read initial_config event from server.""" + buffer = Buffer(event.data) + + variant_play: u8 = buffer.read_value(StructFormat.BOOL) + player_count: u8 = buffer.read_value(StructFormat.UBYTE) + factory_count: u8 = buffer.read_value(StructFormat.UBYTE) + current_turn: u8 = buffer.read_value(StructFormat.UBYTE) + floor_line_size: u8 = buffer.read_value(StructFormat.UBYTE) + + floor_line_data = decode_int8_array(buffer, (floor_line_size, 1)) + + await self.raise_event( + Event( + "game_initial_config", + ( + variant_play, + player_count, + factory_count, + current_turn, + floor_line_data, + ), ), ) - movement = self.get_component("movement") - targeting = self.get_component("targeting") - image = self.get_component("image") + async def read_playing_as(self, event: Event[bytearray]) -> None: + """Read playing_as event from server.""" + buffer = Buffer(event.data) - movement.speed = 200 + playing_as: u8 = buffer.read_value(StructFormat.UBYTE) - # lintcheck: c-extension-no-member (I1101): Module 'pygame.surface' has no 'Surface' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. - floppy: pygame.surface.Surface = pygame.image.load( - path.join("data", "mr_floppy.png"), + await self.raise_event( + Event("game_playing_as", playing_as), ) - image.add_images( - { - 0: floppy, - # '1': pygame.transform.flip(floppy, False, True) - 1: pygame.transform.rotate(floppy, 270), - 2: pygame.transform.flip(floppy, True, True), - 3: pygame.transform.rotate(floppy, 90), - }, - ) - - anim = image.get_component("animation") - anim.controller = self.controller((0, 1, 2, 3)) - - image.set_image(0) - self.visible = True - - self.location = SCREEN_SIZE / 2 - targeting.destination = self.location - - self.register_handler("drag", self.drag) + async def read_game_over(self, event: Event[bytearray]) -> None: + """Read game_over event from server.""" + buffer = Buffer(event.data) - @staticmethod - def controller( - image_identifiers: Sequence[str | int], - ) -> Iterator[str | int | None]: - """Animation controller.""" - cidx = 0 - while True: - count = len(image_identifiers) - if not count: - yield None - continue - cidx = (cidx + 1) % count - yield image_identifiers[cidx] + winner: u8 = buffer.read_value(StructFormat.UBYTE) - async def drag(self, event: Event[sprite.DragEvent]) -> None: - """Move by relative from drag.""" - if event.data.button != 1: - return - self.location += event.data.rel - self.dirty = 1 + await self.raise_event(Event("game_winner", winner)) + self.running = False + async def read_board_data(self, event: Event[bytearray]) -> None: + """Read board_data event from server, reraise as `game_board_data`.""" + buffer = Buffer(event.data) -class FPSCounter(objects.Text): - """FPS counter.""" + player_id: u8 = buffer.read_value(StructFormat.UBYTE) + array = decode_int8_array(buffer, (5, 5)) - __slots__ = () + await self.raise_event(Event("game_board_data", (player_id, array))) - def __init__(self) -> None: - """Initialize fps counter.""" - font = pygame.font.Font(FONT, 28) - super().__init__("fps", font) + async def read_pattern_data(self, event: Event[bytearray]) -> None: + """Read pattern_data event from server, reraise as `game_pattern_data`.""" + buffer = Buffer(event.data) - async def on_tick(self, event: Event[sprite.TickEventData]) -> None: - """Update text.""" - # self.text = f'FPS: {event.data["fps"]:.2f}' - self.text = f"FPS: {event.data.fps:.0f}" + player_id: u8 = buffer.read_value(StructFormat.UBYTE) + row_id: u8 = buffer.read_value(StructFormat.UBYTE) + tile_data = decode_tile_count(buffer) - async def update_loc( - self, - event: Event[dict[str, tuple[int, int]]], - ) -> None: - """Move to top left corner.""" - self.location = Vector2.from_iter(event.data["size"]) / 2 + (5, 5) - - def bind_handlers(self) -> None: - """Register event handlers.""" - super().bind_handlers() - self.register_handlers( - { - "tick": self.on_tick, - "sprite_image_resized": self.update_loc, - }, + await self.raise_event( + Event("game_pattern_data", (player_id, row_id, tile_data)), ) + async def read_factory_data(self, event: Event[bytearray]) -> None: + """Read factory_data event from server, reraise as `game_factory_data`.""" + buffer = Buffer(event.data) -class AzulInitialize(AzulState): - """Initialize Azul.""" - - __slots__ = () - - def __init__(self) -> None: - """Initialize state.""" - super().__init__("initialize") + factory_id: u8 = buffer.read_value(StructFormat.UBYTE) + tiles = decode_numeric_uint8_counter(buffer) - def group_add(self, new_sprite: sprite.Sprite) -> None: - """Add new sprite to group.""" - group = self.machine.get_group(self.id) - assert group is not None, "Expected group from new group id" - group.add(new_sprite) - self.manager.add_component(new_sprite) + await self.raise_event(Event("game_factory_data", (factory_id, tiles))) - async def entry_actions(self) -> None: - """Create group and add mr floppy.""" - self.id = self.machine.new_group("test") - floppy = MrFloppy() - print(floppy) - self.group_add(floppy) - self.group_add(FPSCounter()) + async def read_cursor_data(self, event: Event[bytearray]) -> None: + """Read cursor_data event from server, reraise as `game_cursor_data`.""" + buffer = Buffer(event.data) - await self.machine.raise_event(Event("init", None)) + tiles = decode_numeric_uint8_counter(buffer) - async def exit_actions(self) -> None: - """Remove group and unbind components.""" - self.machine.remove_group(self.id) - self.manager.unbind_components() + await self.raise_event(Event("game_cursor_data", tiles)) + async def read_table_data(self, event: Event[bytearray]) -> None: + """Read table_data event from server, reraise as `game_table_data`.""" + buffer = Buffer(event.data) -def save_crash_img() -> None: - """Save the last frame before the game crashed.""" - surface = pygame.display.get_surface().copy() - # strTime = '-'.join(time.asctime().split(' ')) - # filename = f'Crash_at_{strTime}.png' - filename = "screenshot.png" + tiles = decode_numeric_uint8_counter(buffer) - pygame.image.save(surface, path.join("screenshots", filename)) - del surface + await self.raise_event(Event("game_table_data", tiles)) + async def read_cursor_movement_mode(self, event: Event[bytearray]) -> None: + """Read cursor_movement_mode event from server, reraise as `game_cursor_set_movement_mode`.""" + buffer = Buffer(event.data) -async def async_run() -> None: - """Run client.""" - global SCREEN_SIZE - # global client - config = conf.load_config(path.join("conf", "main.conf")) - lang.load_lang(config["Language"]["lang_name"]) + client_mode = buffer.read_value(StructFormat.BOOL) - screen = pygame.display.set_mode( - tuple(SCREEN_SIZE), - RESIZABLE, - vsync=VSYNC, - ) - pygame.display.set_caption(f"{__title__} v{__version__}") - pygame.key.set_repeat(1000, 30) - screen.fill((0xFF, 0xFF, 0xFF)) - - client = AzulClient() - - background = pygame.image.load( - path.join("data", "background.png"), - ).convert() - client.clear(screen, background) - - client.set_timing_threshold(1000 / FPS) - - await client.set_state("initialize") - - clock = pygame.time.Clock() - - while client.running: - resized_window = False - - async with trio.open_nursery() as nursery: - for event in pygame.event.get(): - # pylint: disable=undefined-variable - if event.type == QUIT: - await client.set_state("Halt") - elif event.type == KEYUP and event.key == K_ESCAPE: - pygame.event.post(pygame.event.Event(QUIT)) - elif event.type == WINDOWRESIZED: - SCREEN_SIZE = Vector2(event.x, event.y) - resized_window = True - sprite_event = sprite.convert_pygame_event(event) - # print(sprite_event) - nursery.start_soon(client.raise_event, sprite_event) - await client.think() + await self.raise_event( + Event("game_cursor_set_movement_mode", client_mode), + ) - time_passed = clock.tick(FPS) + async def read_current_turn_change(self, event: Event[bytearray]) -> None: + """Read current_turn_change event from server, reraise as `game_pattern_current_turn_change`.""" + buffer = Buffer(event.data) - await client.raise_event( - Event( - "tick", - sprite.TickEventData( - time_passed / 1000, - clock.get_fps(), - ), - ), + pattern_id: u8 = buffer.read_value(StructFormat.UBYTE) + await self.raise_event( + Event("game_pattern_current_turn_change", pattern_id), ) - if resized_window: - screen.fill((0xFF, 0xFF, 0xFF)) - rects = [Rect((0, 0), tuple(SCREEN_SIZE))] - client.repaint_rect(rects[0]) - rects.extend(client.draw(screen)) - else: - rects = client.draw(screen) - pygame.display.update(rects) - client.clear_groups() - + async def read_cursor_position(self, event: Event[bytearray]) -> None: + """Read current_turn_change event from server, reraise as `game_cursor_set_destination`.""" + location = decode_cursor_location(event.data) + unit_location = Vector2.from_iter(x / 0xFFF for x in location) -class Tracer(trio.abc.Instrument): - """Tracer instrument.""" + await self.raise_event( + Event("game_cursor_set_destination", unit_location), + ) - __slots__ = ("_sleep_time",) + async def read_floor_data(self, event: Event[bytearray]) -> None: + """Read floor_data event from server, reraise as `game_floor_data`.""" + buffer = Buffer(event.data) - def before_run(self) -> None: - """Before run.""" - print("!!! run started") + floor_id: u8 = buffer.read_value(StructFormat.UBYTE) + floor_line = decode_numeric_uint8_counter(buffer) - def _print_with_task(self, msg: str, task: trio.lowlevel.Task) -> None: - """Print message with task name.""" - # repr(task) is perhaps more useful than task.name in general, - # but in context of a tutorial the extra noise is unhelpful. - print(f"{msg}: {task.name}") + await self.raise_event( + Event("game_floor_data", (floor_id, floor_line)), + ) - def task_spawned(self, task: trio.lowlevel.Task) -> None: - """Task spawned.""" - self._print_with_task("### new task spawned", task) + async def write_game_factory_clicked( + self, + event: Event[tuple[int, Tile]], + ) -> None: + """Write factory_clicked event to server.""" + factory_id, tile = event.data + buffer = Buffer() - def task_scheduled(self, task: trio.lowlevel.Task) -> None: - """Task scheduled.""" - self._print_with_task("### task scheduled", task) + buffer.write_value(StructFormat.UBYTE, factory_id) + buffer.write_value(StructFormat.UBYTE, tile) - def before_task_step(self, task: trio.lowlevel.Task) -> None: - """Before task step.""" - self._print_with_task(">>> about to run one step of task", task) + await self.write_event(Event("factory_clicked->server[write]", buffer)) - def after_task_step(self, task: trio.lowlevel.Task) -> None: - """After task step.""" - self._print_with_task("<<< task step finished", task) + async def write_game_cursor_location_transmit( + self, + event: Event[Vector2], + ) -> None: + """Write cursor_location_transmit event to server.""" + scaled_location = event.data - def task_exited(self, task: trio.lowlevel.Task) -> None: - """Task exited.""" - self._print_with_task("### task exited", task) + x, y = map(int, (scaled_location * 0xFFF).floored()) + buffer = encode_cursor_location((x, y)) - def before_io_wait(self, timeout: float) -> None: - """Before IO wait.""" - if timeout: - print(f"### waiting for I/O for up to {timeout} seconds") - else: - print("### doing a quick check for I/O") - self._sleep_time = trio.current_time() + await self.write_event(Event("cursor_location->server[write]", buffer)) - def after_io_wait(self, timeout: float) -> None: - """After IO wait.""" - duration = trio.current_time() - self._sleep_time - print(f"### finished I/O check (took {duration} seconds)") + async def write_game_pattern_row_clicked( + self, + event: Event[tuple[int, Vector2]], + ) -> None: + """Write factory_clicked event to server.""" + row_id, location = event.data + buffer = Buffer() - def after_run(self) -> None: - """After run.""" - print("!!! run finished") + buffer.write_value(StructFormat.UBYTE, row_id) + buffer.write_value(StructFormat.UBYTE, int(location.x)) + buffer.write_value(StructFormat.UBYTE, int(location.y)) + await self.write_event( + Event("pattern_row_clicked->server[write]", buffer), + ) -def run() -> None: - """Run asynchronous side of everything.""" - trio.run(async_run) # , instruments=[Tracer()]) + async def write_game_table_clicked( + self, + event: Event[Tile], + ) -> None: + """Write table_clicked event to server.""" + tile = event.data + buffer = Buffer() + buffer.write_value(StructFormat.UBYTE, tile) -# save_crash_img() + await self.write_event(Event("table_clicked->server[write]", buffer)) -if __name__ == "__main__": - print(f"{__title__} v{__version__}\nProgrammed by {__author__}.\n") + async def write_game_floor_clicked( + self, + event: Event[tuple[int, int]], + ) -> None: + """Write floor_clicked event to server.""" + floor_line_id, location_x = event.data + buffer = Buffer() - # Make sure the game will display correctly on high DPI monitors on Windows. - if sys.platform == "win32": - # Exists on windows but not on linux or macos - # Windows raises attr-defined - # others say unused-ignore - from ctypes import windll # type: ignore[attr-defined,unused-ignore] + buffer.write_value(StructFormat.UBYTE, floor_line_id) + buffer.write_value(StructFormat.UBYTE, location_x) - with contextlib.suppress(AttributeError): - windll.user32.SetProcessDPIAware() - del windll + await self.write_event(Event("floor_clicked->server[write]", buffer)) - try: - pygame.init() - run() - finally: - pygame.quit() + async def handle_network_stop(self, event: Event[None]) -> None: + """Send EOF if connected and close socket.""" + if self.not_connected: + return + self.running = False + try: + await self.send_eof() + finally: + await self.close() + assert self.not_connected + + def __del__(self) -> None: + """Print debug message.""" + print(f"del {self.__class__.__name__}") diff --git a/src/azul/component.py b/src/azul/component.py deleted file mode 100644 index 3e6865b..0000000 --- a/src/azul/component.py +++ /dev/null @@ -1,429 +0,0 @@ -"""Component system module - Components instead of chaotic class hierarchy mess.""" - -# Programmed by CoolCat467 - -# Copyright (C) 2023-2024 CoolCat467 -# -# This program 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. -# -# This program 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. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -from __future__ import annotations - -__title__ = "Component" -__author__ = "CoolCat467" -__license__ = "GNU General Public License Version 3" -__version__ = "0.0.0" - -from contextlib import contextmanager -from typing import TYPE_CHECKING, Any, Generic, TypeVar -from weakref import ref - -import trio - -if TYPE_CHECKING: - from collections.abc import Awaitable, Callable, Generator, Iterable - - from mypy_extensions import u8 - -T = TypeVar("T") - - -class Event(Generic[T]): - """Event with name, data, and re-raise levels.""" - - __slots__ = ("data", "level", "name") - - def __init__( - self, - name: str, - data: T, - levels: u8 = 0, - ) -> None: - """Initialize event.""" - self.name = name - self.data = data - self.level = levels - - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}({self.name!r}, {self.data!r}, {self.level!r})" - - def pop_level(self) -> bool: - """Travel up one level and return True if event should continue or not.""" - continue_level = self.level > 0 - self.level = max(0, self.level - 1) - return continue_level - - -class Component: - """Component base class.""" - - __slots__ = ("__manager", "name") - - def __init__(self, name: object) -> None: - """Initialise with name.""" - self.name = name - self.__manager: ref[ComponentManager] | None = None - - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}({self.name!r})" - - @property - def manager(self) -> ComponentManager: - """ComponentManager if bound to one, otherwise raise AttributeError.""" - if self.__manager is not None: - manager = self.__manager() - if manager is not None: - return manager - raise AttributeError(f"No component manager bound for {self.name}") - - def _unbind(self) -> None: - """If you use this you are evil. This is only for ComponentManagers!.""" - self.__manager = None - - @property - def manager_exists(self) -> bool: - """Return if manager is bound or not.""" - return self.__manager is not None and self.__manager() is not None - - def register_handler( - self, - event_name: str, - handler_coro: Callable[[Event[Any]], Awaitable[Any]], - ) -> None: - """Register handler with bound component manager. - - Raises AttributeError if this component is not bound. - """ - self.manager.register_component_handler( - event_name, - handler_coro, - self.name, - ) - - def register_handlers( - self, - handlers: dict[str, Callable[[Event[Any]], Awaitable[Any]]], - ) -> None: - """Register multiple handler Coroutines. - - Raises AttributeError if this component is not bound. - """ - for name, coro in handlers.items(): - self.register_handler(name, coro) - - def bind_handlers(self) -> None: - """Add handlers in subclass.""" - - def bind(self, manager: ComponentManager) -> None: - """Bind self to manager. - - Raises RuntimeError if component is already bound to a manager. - """ - if self.manager_exists: - raise RuntimeError( - f"{self.name} component is already bound to {self.manager}", - ) - self.__manager = ref(manager) - self.bind_handlers() - - def has_handler(self, event_name: str) -> bool: - """Return if manager has event handlers registered for a given event. - - Raises AttributeError if this component is not bound. - """ - return self.manager.has_handler(event_name) - - async def raise_event(self, event: Event[Any]) -> None: - """Raise event for bound manager. - - Raises AttributeError if this component is not bound. - """ - await self.manager.raise_event(event) - - def component_exists(self, component_name: str) -> bool: - """Return if component exists in manager. - - Raises AttributeError if this component is not bound. - """ - return self.manager.component_exists(component_name) - - def components_exist(self, component_names: Iterable[str]) -> bool: - """Return if all component names given exist in manager. - - Raises AttributeError if this component is not bound. - """ - return self.manager.components_exist(component_names) - - def get_component(self, component_name: str) -> Any: - """Get Component from manager. - - Raises AttributeError if this component is not bound. - """ - return self.manager.get_component(component_name) - - def get_components( - self, - component_names: Iterable[str], - ) -> list[Component]: - """Return Components from manager. - - Raises AttributeError if this component is not bound. - """ - return self.manager.get_components(component_names) - - -ComponentPassthrough = TypeVar("ComponentPassthrough", bound=Component) - - -class ComponentManager(Component): - """Component manager class.""" - - __slots__ = ("__components", "__event_handlers", "__weakref__") - - def __init__(self, name: object, own_name: object | None = None) -> None: - """If own_name is set, add self to list of components as specified name.""" - super().__init__(name) - self.__event_handlers: dict[ - str, - set[tuple[Callable[[Event[Any]], Awaitable[Any]], object]], - ] = {} - self.__components: dict[object, Component] = {} - - if own_name is not None: - self.__add_self_as_component(own_name) - self.bind_handlers() - - def __repr__(self) -> str: - """Return representation of self.""" - return f"<{self.__class__.__name__} Components: {self.__components}>" - - def __add_self_as_component(self, name: object) -> None: - """Add this manager as component to self without binding. - - Raises ValueError if a component with given name already exists. - """ - if self.component_exists(name): # pragma: nocover - raise ValueError(f'Component named "{name}" already exists!') - self.__components[name] = self - - def register_handler( - self, - event_name: str, - handler_coro: Callable[[Event[Any]], Awaitable[None]], - ) -> None: - """Register handler_func as handler for event_name (self component).""" - self.register_component_handler(event_name, handler_coro, self.name) - - def register_component_handler( - self, - event_name: str, - handler_coro: Callable[[Event[Any]], Awaitable[None]], - component_name: object, - ) -> None: - """Register handler_func as handler for event_name. - - Raises ValueError if no component with given name is registered. - """ - if ( - component_name != self.name - and component_name not in self.__components - ): - raise ValueError( - f"Component named {component_name!r} is not registered!", - ) - if event_name not in self.__event_handlers: - self.__event_handlers[event_name] = set() - self.__event_handlers[event_name].add((handler_coro, component_name)) - - def has_handler(self, event_name: str) -> bool: - """Return if there are event handlers registered for a given event.""" - return bool(self.__event_handlers.get(event_name)) - - async def raise_event_in_nursery( - self, - event: Event[Any], - nursery: trio.Nursery, - ) -> None: - """Raise event in a particular trio nursery. - - Could raise RuntimeError if given nursery is no longer open. - """ - await trio.lowlevel.checkpoint() - - # Forward leveled events up; They'll come back to us soon enough. - if self.manager_exists and event.pop_level(): - await super().raise_event(event) - return - # Make sure events not raised twice - # if not self.manager_exists: - # while event.level > 0: - # event.pop_level() - - # if not event.name.startswith("Pygame") and event.name not in {"tick", "gameboard_create_piece", "server->create_piece", "create_piece->network"}: - # print(f'''{self.__class__.__name__}({self.name!r}):\n{event = }''') - - # Call all registered handlers for this event - if event.name in self.__event_handlers: - for handler, _name in self.__event_handlers[event.name]: - nursery.start_soon(handler, event) - - # Forward events to contained managers - for component in self.get_all_components(): - # Skip self component if exists - if component is self: - continue - if isinstance(component, ComponentManager): - nursery.start_soon(component.raise_event, event) - - async def raise_event(self, event: Event[Any]) -> None: - """Raise event for all components that have handlers registered.""" - async with trio.open_nursery() as nursery: - await self.raise_event_in_nursery(event, nursery) - - def add_component(self, component: Component) -> None: - """Add component to this manager. - - Raises ValueError if component already exists with component name. - `component` must be an instance of Component. - """ - assert isinstance(component, Component), "Must be component instance" - if self.component_exists(component.name): - raise ValueError( - f'Component named "{component.name}" already exists!', - ) - self.__components[component.name] = component - component.bind(self) - - def add_components(self, components: Iterable[Component]) -> None: - """Add multiple components to this manager. - - Raises ValueError if any component already exists with component name. - `component`s must be instances of Component. - """ - for component in components: - self.add_component(component) - - def remove_component(self, component_name: object) -> None: - """Remove a component. - - Raises ValueError if component name does not exist. - """ - if not self.component_exists(component_name): - raise ValueError(f"Component {component_name!r} does not exist!") - # Remove component from registered components - component = self.__components.pop(component_name) - # Tell component they need to unbind - component._unbind() - - # Unregister component's event handlers - # List of events that will have no handlers once we are done - empty = [] - for event_name, handlers in self.__event_handlers.items(): - for item in tuple(handlers): - _handler, handler_component = item - if handler_component == component_name: - self.__event_handlers[event_name].remove(item) - if not self.__event_handlers[event_name]: - empty.append(event_name) - # Remove event handler table keys that have no items anymore - for name in empty: - self.__event_handlers.pop(name) - - def component_exists(self, component_name: object) -> bool: - """Return if component exists in this manager.""" - return component_name in self.__components - - @contextmanager - def temporary_component( - self, - component: ComponentPassthrough, - ) -> Generator[ComponentPassthrough, None, None]: - """Temporarily add given component but then remove after exit.""" - name = component.name - self.add_component(component) - try: - yield component - finally: - if self.component_exists(name): - self.remove_component(name) - - def components_exist(self, component_names: Iterable[object]) -> bool: - """Return if all component names given exist in this manager.""" - return all(self.component_exists(name) for name in component_names) - - def get_component(self, component_name: object) -> Any: - """Return Component or raise ValueError because it doesn't exist.""" - if not self.component_exists(component_name): - raise ValueError(f'"{component_name}" component does not exist') - return self.__components[component_name] - - def get_components(self, component_names: Iterable[object]) -> list[Any]: - """Return iterable of components asked for or raise ValueError.""" - return [self.get_component(name) for name in component_names] - - def list_components(self) -> tuple[object, ...]: - """Return tuple of the names of components bound to this manager.""" - return tuple(self.__components) - - def get_all_components(self) -> tuple[Component, ...]: - """Return tuple of all components bound to this manager.""" - return tuple(self.__components.values()) - - def unbind_components(self) -> None: - """Unbind all components, allows things to get garbage collected.""" - self.__event_handlers.clear() - for component in iter(self.__components.values()): - if ( - isinstance(component, ComponentManager) - and component is not self - ): - component.unbind_components() - component._unbind() - self.__components.clear() - - def __del__(self) -> None: - """Unbind components.""" - self.unbind_components() - - -class ExternalRaiseManager(ComponentManager): - """Component Manager, but raises events in an external nursery.""" - - __slots__ = ("nursery",) - - def __init__( - self, - name: object, - nursery: trio.Nursery, - own_name: object | None = None, - ) -> None: - """Initialize with name, own component name, and nursery.""" - super().__init__(name, own_name) - self.nursery = nursery - - async def raise_event(self, event: Event[Any]) -> None: - """Raise event in nursery. - - Could raise RuntimeError if self.nursery is no longer open. - """ - await self.raise_event_in_nursery(event, self.nursery) - - async def raise_event_internal(self, event: Event[Any]) -> None: - """Raise event in internal nursery.""" - await super().raise_event(event) - - -if __name__ == "__main__": # pragma: nocover - print(f"{__title__}\nProgrammed by {__author__}.") diff --git a/src/azul/conf.py b/src/azul/conf.py deleted file mode 100644 index 4f4400a..0000000 --- a/src/azul/conf.py +++ /dev/null @@ -1,32 +0,0 @@ -"""Config module.""" - -# Programmed by CoolCat467 - -__title__ = "Conf" -__author__ = "CoolCat467" -__version__ = "0.0.0" - - -from configparser import ConfigParser - - -def load_config(config_file: str) -> dict[str, dict[str, str]]: - """Return a config object from config_file.""" - config = ConfigParser() - config.read((config_file,)) - - data: dict[str, dict[str, str]] = {} - for section, values in dict(config.items()).items(): - data[section] = dict(values) - - # config.clear() - # config.update(data) - ## - # with open(config_file, mode='w', encoding='utf-8') as conf_file: - # config.write(conf_file) - - return data - - -if __name__ == "__main__": - print(f"{__title__}\nProgrammed by {__author__}.") diff --git a/src/azul/conf/main.conf b/src/azul/conf/main.conf deleted file mode 100644 index f2f1c51..0000000 --- a/src/azul/conf/main.conf +++ /dev/null @@ -1,6 +0,0 @@ -[Font] -font_folder = data -font_file = RuneScape-UF-Regular.ttf - -[Language] -lang_name = en_us diff --git a/src/azul/crop.py b/src/azul/crop.py index 9aeb132..cbdbbab 100644 --- a/src/azul/crop.py +++ b/src/azul/crop.py @@ -2,14 +2,37 @@ # Programmed by CoolCat467 +from __future__ import annotations + +# Copyright (C) 2020-2024 CoolCat467 +# +# This program 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. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + __title__ = "Crop Functions" __author__ = "CoolCat467" __version__ = "0.0.0" + +from typing import TYPE_CHECKING + from pygame.color import Color from pygame.rect import Rect from pygame.surface import Surface +if TYPE_CHECKING: + from collections.abc import Callable, Generator, Iterable + def crop_color(surface: Surface, color: Color) -> Surface: """Crop out color from surface.""" @@ -46,10 +69,40 @@ def crop_color(surface: Surface, color: Color) -> Surface: return surf -def run() -> None: - """Run test of module.""" +def auto_crop_clear( + surface: Surface, + clear: Color | None = None, +) -> Surface: + """Remove unneccicary pixels from image.""" + if clear is None: + clear = Color(0, 0, 0, 0) + surface = surface.convert_alpha() + w, h = surface.get_size() + surface.lock() + + def find_end( + iterfunc: Callable[[int], Iterable[Color]], + rangeobj: Iterable[int], + ) -> int: + for x in rangeobj: + if not all(y == clear for y in iterfunc(x)): + return x + return x + + def column(x: int) -> Generator[Color, None, None]: + return (surface.get_at((x, y)) for y in range(h)) + + def row(y: int) -> Generator[Color, None, None]: + return (surface.get_at((x, y)) for x in range(w)) + + leftc = find_end(column, range(w)) + rightc = find_end(column, range(w - 1, -1, -1)) + topc = find_end(row, range(h)) + floorc = find_end(row, range(h - 1, -1, -1)) + surface.unlock() + dim = Rect(leftc, topc, rightc - leftc, floorc - topc) + return surface.subsurface(dim) if __name__ == "__main__": print(f"{__title__}\nProgrammed by {__author__}.\n") - run() diff --git a/src/azul/data/tiles/black.png b/src/azul/data/tiles/black.png deleted file mode 100644 index d19d971..0000000 Binary files a/src/azul/data/tiles/black.png and /dev/null differ diff --git a/src/azul/data/tiles/blue.png b/src/azul/data/tiles/blue.png deleted file mode 100644 index 97cfd8e..0000000 Binary files a/src/azul/data/tiles/blue.png and /dev/null differ diff --git a/src/azul/data/tiles/cyan.png b/src/azul/data/tiles/cyan.png deleted file mode 100644 index e49b3db..0000000 Binary files a/src/azul/data/tiles/cyan.png and /dev/null differ diff --git a/src/azul/data/tiles/grey.png b/src/azul/data/tiles/grey.png deleted file mode 100644 index 0427e8f..0000000 Binary files a/src/azul/data/tiles/grey.png and /dev/null differ diff --git a/src/azul/data/tiles/grey_black.png b/src/azul/data/tiles/grey_black.png deleted file mode 100644 index aab594e..0000000 Binary files a/src/azul/data/tiles/grey_black.png and /dev/null differ diff --git a/src/azul/data/tiles/grey_blue.png b/src/azul/data/tiles/grey_blue.png deleted file mode 100644 index 3e5d1a2..0000000 Binary files a/src/azul/data/tiles/grey_blue.png and /dev/null differ diff --git a/src/azul/data/tiles/grey_cyan.png b/src/azul/data/tiles/grey_cyan.png deleted file mode 100644 index 6d4c01f..0000000 Binary files a/src/azul/data/tiles/grey_cyan.png and /dev/null differ diff --git a/src/azul/data/tiles/grey_red.png b/src/azul/data/tiles/grey_red.png deleted file mode 100644 index 7ac7401..0000000 Binary files a/src/azul/data/tiles/grey_red.png and /dev/null differ diff --git a/src/azul/data/tiles/grey_yellow.png b/src/azul/data/tiles/grey_yellow.png deleted file mode 100644 index 43f00e6..0000000 Binary files a/src/azul/data/tiles/grey_yellow.png and /dev/null differ diff --git a/src/azul/data/tiles/number_one.png b/src/azul/data/tiles/number_one.png deleted file mode 100644 index 2640208..0000000 Binary files a/src/azul/data/tiles/number_one.png and /dev/null differ diff --git a/src/azul/data/tiles/red.png b/src/azul/data/tiles/red.png deleted file mode 100644 index 478d266..0000000 Binary files a/src/azul/data/tiles/red.png and /dev/null differ diff --git a/src/azul/data/tiles/yellow.png b/src/azul/data/tiles/yellow.png deleted file mode 100644 index 1341ce2..0000000 Binary files a/src/azul/data/tiles/yellow.png and /dev/null differ diff --git a/src/azul/database.py b/src/azul/database.py new file mode 100644 index 0000000..8d19b2a --- /dev/null +++ b/src/azul/database.py @@ -0,0 +1,329 @@ +"""Database - Read and write json files.""" + +# Programmed by CoolCat467 + +from __future__ import annotations + +# Database - Read and write json files +# Copyright (C) 2024 CoolCat467 +# +# This program 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. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +__title__ = "Database" +__author__ = "CoolCat467" + +from os import makedirs, path +from pathlib import Path +from typing import TYPE_CHECKING, Any + +import orjson +import trio + +if TYPE_CHECKING: + from collections.abc import Generator, Iterable, Iterator + from types import TracebackType + + from typing_extensions import Self + + +_LOADED: dict[str, Records] = {} + + +class Database(dict[str, Any]): + """Database dict with file read write functions.""" + + __slots__ = ("__weakref__", "file") + + def __init__( + self, + file_path: str | Path | trio.Path, + auto_load: bool = True, + ) -> None: + """Initialize and set file path. + + If auto_load is True, automatically load file contents synchronously + if file exists. + """ + super().__init__() + self.file = file_path + + if auto_load and path.exists(self.file): + self.reload_file() + + def reload_file(self) -> None: + """Reload database file. + + Will raise FileNotFoundError in the event file does not exist. + """ + self.update(orjson.loads(Path(self.file).read_bytes())) + + async def reload_async(self) -> None: + """Reload database file asynchronously. + + Does not decode json data if file is empty. + Will raise FileNotFoundError in the event file does not exist. + """ + async with await trio.open_file(self.file, "rb") as file: + data = await file.read() + if not data: + return + self.update(orjson.loads(data)) + + def serialize(self) -> bytes: + """Return this object's data serialized as bytes.""" + return orjson.dumps( + self, + option=orjson.OPT_APPEND_NEWLINE + | orjson.OPT_NON_STR_KEYS + | orjson.OPT_NAIVE_UTC, + ) + + def write_file(self) -> None: + """Write database file. + + May raise PermissionError in the event of insufficient permissions. + """ + folder = path.dirname(self.file) + if not path.exists(folder): + makedirs(folder, exist_ok=False) + Path(self.file).write_bytes(self.serialize()) + + async def write_async(self) -> None: + """Write database file asynchronously. + + May raise PermissionError in the event of insufficient permissions. + """ + folder = trio.Path(self.file).parent + if not await folder.exists(): + await folder.mkdir(parents=True, exist_ok=False) + async with await trio.open_file( + self.file, + "wb", + ) as file: + await file.write(self.serialize()) + + def __enter__(self) -> Self: + """Enter context manager.""" + return self + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: + """Context manager exit.""" + self.write_file() + + async def __aenter__(self) -> Self: + """Enter async context manager. + + Automatically reloads file if it exists. + """ + if await trio.Path(self.file).exists(): + await self.reload_async() + return self + + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: + """Async context manager exit, write file contents asynchronously.""" + await self.write_async() + + +class Table: + """Table from dictionary. + + Allows getting and setting entire columns of a database + """ + + __slots__ = ("_key_name", "_records") + + def __init__(self, records: dict[str, Any], key_name: str) -> None: + """Initialize and set records and key name.""" + self._records = records + self._key_name = key_name + + def __repr__(self) -> str: + """Get text representation of table.""" + size: dict[str, int] = {} + columns = self.keys() + for column in columns: + size[column] = len(column) + for value in self[column]: + if value is None: + continue + length = ( + len(value) + if hasattr(value, "__len__") + else len(repr(value)) + ) + size[column] = max(size[column], length) + num_pad = len(str(len(self))) + lines = [] + column_names = " ".join(c.ljust(length) for c, length in size.items()) + lines.append("".rjust(num_pad) + " " + column_names) + for index in range(len(self)): + line = [str(index).ljust(num_pad)] + for column in columns: + line.append(str(self[column][index]).ljust(size[column])) + lines.append(" ".join(line)) + return "\n".join(lines) + + def __getitem__(self, column: str) -> tuple[Any, ...]: + """Get column data.""" + if column not in self.keys(): + return tuple(None for _ in range(len(self))) + if column == self._key_name: + return tuple(self._records.keys()) + return tuple(row.get(column) for row in self._records.values()) + + def __setitem__(self, column: str, value: Iterable[Any]) -> None: + """Set column data to value.""" + if column == self._key_name: + for old, new in zip(tuple(self._records), value, strict=False): + self._records[new] = self._records.pop(old) + else: + for key, set_value in zip(self._records, value, strict=True): + if set_value is None: + continue + self._records[key][column] = set_value + + def _raw_keys(self) -> set[str]: + """Return the name of every column.""" + keys = set() + for row in self._records.values(): + keys |= set(row.keys()) + return keys + + def keys(self) -> set[str]: + """Return the name of every column.""" + return self._raw_keys() | {self._key_name} + + def __iter__(self) -> Iterator[str]: + """Return iterator for column names.""" + return iter(self.keys()) + + def values(self) -> tuple[Any, ...]: + """Return every column.""" + values = [] + for key in self.keys(): + values.append(self[key]) + return tuple(values) + + def items(self) -> tuple[tuple[str, Any], ...]: + """Return tuples of column names and columns.""" + items = [] + for key in sorted(self.keys()): + items.append((key, self[key])) + return tuple(items) + + def _rows( + self, + columns: list[str], + ) -> Generator[tuple[Any, ...], None, None]: + """Yield columns in order from each row.""" + for key, value in self._records.items(): + yield (key, *tuple(value.get(col) for col in columns)) + + def rows(self) -> Generator[tuple[Any, ...], None, None]: + """Yield each row.""" + yield from self._rows(sorted(self.keys())) + + def column_and_rows(self) -> Generator[tuple[str | Any, ...], None, None]: + """Yield tuple of column row and then rows in column order.""" + columns = sorted(self._raw_keys()) + yield (self._key_name, *columns) + yield from self._rows(columns) + + def __len__(self) -> int: + """Return number of records.""" + return len(self._records) + + def get_id(self, key: str, value: object) -> int | None: + """Return index of value in column key or None if not found.""" + try: + return self[key].index(value) + except ValueError: + return None + + +class Records(Database): + """Records dict with columns.""" + + __slots__ = () + + def table(self, element_name: str) -> Table: + """Get table object given that keys are named element name.""" + return Table(self, element_name) + + +def load(file_path: str | Path | trio.Path) -> Records: + """Load database from file path or return already loaded instance.""" + file = path.abspath(file_path) + if file not in _LOADED: + _LOADED[file] = Records(file) + return _LOADED[file] + + +async def load_async(file_path: str | Path | trio.Path) -> Records: + """Load database from file path or return already loaded instance.""" + await trio.lowlevel.checkpoint() + file = path.abspath(file_path) + if file not in _LOADED: + _LOADED[file] = Records(file, auto_load=False) + if await trio.Path(file).exists(): + await _LOADED[file].reload_async() + return _LOADED[file] + + +def get_loaded() -> set[str]: + """Return set of loaded database files.""" + return set(_LOADED) + + +def unload(file_path: str | Path | trio.Path) -> None: + """If database loaded, write file and unload.""" + file = path.abspath(file_path) + if file not in get_loaded(): + return + database = load(file) + database.write_file() + del _LOADED[file] + + +async def async_unload(file_path: str | Path | trio.Path) -> None: + """If database loaded, write file and unload.""" + file = path.abspath(file_path) + if file not in get_loaded(): + return + database = load(file) + await database.write_async() + del _LOADED[file] + + +def unload_all() -> None: + """Unload all loaded databases.""" + for file_path in get_loaded(): + unload(file_path) + + +async def async_unload_all() -> None: + """Unload all loaded databases.""" + async with trio.open_nursery() as nursery: + for file_path in get_loaded(): + nursery.start_soon(async_unload, file_path) diff --git a/src/azul/element_list.py b/src/azul/element_list.py new file mode 100644 index 0000000..d461aec --- /dev/null +++ b/src/azul/element_list.py @@ -0,0 +1,140 @@ +"""Element List - List of element sprites.""" + +# Programmed by CoolCat467 + +from __future__ import annotations + +# Element List - List of element sprites. +# Copyright (C) 2024 CoolCat467 +# +# This program 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. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +__title__ = "Element List" +__author__ = "CoolCat467" +__version__ = "0.0.0" +__license__ = "GNU General Public License Version 3" + + +from typing import TYPE_CHECKING + +from azul import sprite +from azul.vector import Vector2 + +if TYPE_CHECKING: + from collections.abc import Generator + + +class Element(sprite.Sprite): + """Element sprite.""" + + __slots__ = () + + def self_destruct(self) -> None: + """Remove this element.""" + self.kill() + if self.manager_exists: + self.manager.remove_component(self.name) + + def __del__(self) -> None: + """Clean up this element for garbage collecting.""" + self.self_destruct() + super().__del__() + + +class ElementList(sprite.Sprite): + """Element List sprite.""" + + __slots__ = ("_order",) + + def __init__(self, name: object) -> None: + """Initialize connection list.""" + super().__init__(name) + + self._order: list[object] = [] + + def add_element(self, element: Element) -> None: + """Add element to this list.""" + group = self.groups()[-1] + group.add(element) # type: ignore[arg-type] + self.add_component(element) + self._order.append(element.name) + + def delete_element(self, element_name: object) -> None: + """Delete an element (only from component).""" + element = self.get_component(element_name) + index = self._order.index(element_name) + if element.visible: + assert element.image is not None + height = element.image.get_height() + self.offset_elements_after(index, (0, -height)) + self._order.pop(index) + assert isinstance(element, Element) + element.self_destruct() + + def yield_elements(self) -> Generator[Element, None, None]: + """Yield bound Element components in order.""" + for component_name in iter(self._order): + # Kind of strange to mutate in yield, maybe shouldn't do that? + if not self.component_exists(component_name): + self._order.remove(component_name) + continue + component = self.get_component(component_name) + assert isinstance(component, Element) + yield component + + def get_last_rendered_element(self) -> Element | None: + """Return last bound Element sprite or None.""" + for component_name in reversed(self._order): + if not self.component_exists(component_name): + self._order.remove(component_name) + continue + component = self.get_component(component_name) + assert isinstance(component, Element) + if component.visible: + assert component.image is not None + return component + return None + + def get_new_connection_position(self) -> Vector2: + """Return location for new connection.""" + last_element = self.get_last_rendered_element() + if last_element is None: + return Vector2.from_iter(self.rect.topleft) + location = Vector2.from_iter(last_element.rect.topleft) + assert last_element.image is not None + location += (0, last_element.image.get_height()) + return location + + def offset_elements(self, diff: tuple[int, int]) -> None: + """Offset all element locations by given difference.""" + for element in self.yield_elements(): + element.location += diff + + def offset_elements_after(self, index: int, diff: tuple[int, int]) -> None: + """Offset elements after index by given difference.""" + for idx, element in enumerate(self.yield_elements()): + if idx <= index: + continue + element.location += diff + + def _set_location(self, value: tuple[int, int]) -> None: + """Set rect center from tuple of integers.""" + current = self.location + super()._set_location(value) + diff = Vector2.from_iter(value) - current + self.offset_elements(diff) + + +if __name__ == "__main__": + print(f"{__title__} v{__version__}\nProgrammed by {__author__}.\n") diff --git a/src/azul/encrypted_event.py b/src/azul/encrypted_event.py deleted file mode 100644 index c307652..0000000 --- a/src/azul/encrypted_event.py +++ /dev/null @@ -1,130 +0,0 @@ -"""Encrypted Event - Encrypt and decrypt event data.""" - -# Programmed by CoolCat467 - -from __future__ import annotations - -# Encrypted Event - Encrypt and decrypt event data. -# Copyright (C) 2024 CoolCat467 -# -# This program 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. -# -# This program 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. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -__title__ = "Encrypted Event" -__author__ = "CoolCat467, ItsDrike, and Ammar Askar" -__version__ = "0.0.0" -__license__ = "GNU General Public License Version 3" - - -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives.ciphers import ( - Cipher, - CipherContext, - algorithms, - modes, -) - -from azul.network import NetworkEventComponent - - -class EncryptedNetworkEventComponent(NetworkEventComponent): - """Encrypted Network Event Component.""" - - __slots__ = ( - "cipher", - "decryptor", - "encryptor", - "shared_secret", - ) - - def __init__(self, name: str) -> None: - """Initialize Encrypted Network Event Component.""" - super().__init__(name) - - self.cipher: Cipher[modes.CFB8] | None = None - self.encryptor: CipherContext - self.decryptor: CipherContext - - @property - def encryption_enabled(self) -> bool: - """Return if encryption is enabled.""" - return self.cipher is not None - - def enable_encryption( - self, - shared_secret: bytes, - initialization_vector: bytes, - ) -> None: - """Enable encryption for this connection, using the ``shared_secret``. - - After calling this method, the reading and writing process for this connection - will be altered, and any future communication will be encrypted/decrypted there. - - :param shared_secret: - This is the cipher key for the AES symmetric cipher used for the encryption. - - See :func:`azul.encryption.generate_shared_secret`. - """ - self.cipher = Cipher( - algorithms.AES256(bytes(shared_secret)), - modes.CFB8(bytes(initialization_vector)), - backend=default_backend(), - ) - self.encryptor = self.cipher.encryptor() - self.decryptor = self.cipher.decryptor() - - async def write(self, data: bytes | bytearray | memoryview[int]) -> None: - """Send the given data, encrypted through the stream, blocking if necessary. - - Args: - data (bytes, bytearray, or memoryview): The data to send. - - Raises: - trio.BusyResourceError: if another task is already executing a - :meth:`send_all`, :meth:`wait_send_all_might_not_block`, or - :meth:`HalfCloseableStream.send_eof` on this stream. - trio.BrokenResourceError: if something has gone wrong, and the stream - is broken. - trio.ClosedResourceError: if you previously closed this stream - object, or if another task closes this stream object while - :meth:`send_all` is running. - - Most low-level operations in Trio provide a guarantee: if they raise - :exc:`trio.Cancelled`, this means that they had no effect, so the - system remains in a known state. This is **not true** for - :meth:`send_all`. If this operation raises :exc:`trio.Cancelled` (or - any other exception for that matter), then it may have sent some, all, - or none of the requested data, and there is no way to know which. - - Copied from Trio docs. - - """ - if self.encryption_enabled: - data = self.encryptor.update(data) - return await super().write(data) - - async def read(self, length: int) -> bytearray: - """Read `length` bytes from stream. - - Can raise following exceptions: - NetworkStreamNotConnectedError - NetworkTimeoutError - Timeout or no data - OSError - Stopped responding - trio.BusyResourceError - Another task is already writing data - trio.BrokenResourceError - Something is wrong and stream is broken - trio.ClosedResourceError - Stream is closed or another task closes stream - """ - data = await super().read(length) - if self.encryption_enabled: - return bytearray(self.decryptor.update(data)) - return data diff --git a/src/azul/encryption.py b/src/azul/encryption.py deleted file mode 100644 index e2ad0d2..0000000 --- a/src/azul/encryption.py +++ /dev/null @@ -1,140 +0,0 @@ -"""Encryption module.""" - -# This is the buffer module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -import os - -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives.asymmetric.padding import MGF1, OAEP -from cryptography.hazmat.primitives.asymmetric.rsa import ( - RSAPrivateKey as RSAPrivateKey, - RSAPublicKey as RSAPublicKey, - generate_private_key, -) -from cryptography.hazmat.primitives.hashes import SHA256 -from cryptography.hazmat.primitives.serialization import ( - Encoding, - PublicFormat, - load_der_public_key, -) - - -def generate_shared_secret() -> bytes: # pragma: no cover - """Generate a random shared secret for client. - - This secret will be sent to the server in :class:`~mcproto.packets.login.login.LoginEncryptionResponse` packet, - and used to encrypt all future communication afterwards. - - This will be symmetric encryption using AES/CFB8 stream cipher. And this shared secret will be 256-bits long. - """ - return os.urandom(256 // 8) - - -def generate_verify_token() -> bytes: # pragma: no cover - """Generate a random verify token. - - This token will be sent by the server in :class:`~mcproto.packets.login.login.LoginEncryptionRequest`, to be - encrypted by the client as a form of verification. - - This token doesn't need to be cryptographically secure, it's just a sanity check that - the client has encrypted the data correctly. - """ - return os.urandom(16) - - -def generate_rsa_key() -> RSAPrivateKey: # pragma: no cover - """Generate a random RSA key pair for server. - - This key pair will be used for :class:`~mcproto.packets.login.login.LoginEncryptionRequest` packet, - where the client will be sent the public part of this key pair, which will be used to encrypt the - shared secret (and verification token) sent in :class:`~mcproto.packets.login.login.LoginEncryptionResponse` - packet. The server will then use the private part of this key pair to decrypt that. - - This will be a 2048-bit RSA key pair. - """ - return generate_private_key( - public_exponent=65537, - key_size=2048, - backend=default_backend(), - ) - - -def encrypt_with_rsa( - public_key: RSAPublicKey, - data: bytes, -) -> bytes: - """Encrypt given data with given RSA public key.""" - return public_key.encrypt( - bytes(data), - OAEP(MGF1(SHA256()), SHA256(), None), - ) - - -def encrypt_token_and_secret( - public_key: RSAPublicKey, - verification_token: bytes, - shared_secret: bytes, -) -> tuple[bytes, bytes]: - """Encrypts the verification token and shared secret with the server's public key. - - :param public_key: The RSA public key provided by the server - :param verification_token: The verification token provided by the server - :param shared_secret: The generated shared secret - :return: A tuple containing (encrypted token, encrypted secret) - """ - encrypted_token = encrypt_with_rsa(public_key, verification_token) - encrypted_secret = encrypt_with_rsa(public_key, shared_secret) - return encrypted_token, encrypted_secret - - -def decrypt_with_rsa( - private_key: RSAPrivateKey, - data: bytes, -) -> bytes: - """Decrypt given data with given RSA private key.""" - return private_key.decrypt( - bytes(data), - OAEP(MGF1(SHA256()), SHA256(), None), - ) - - -def decrypt_token_and_secret( - private_key: RSAPrivateKey, - verification_token: bytes, - shared_secret: bytes, -) -> tuple[bytes, bytes]: - """Decrypts the verification token and shared secret with the server's private key. - - :param private_key: The RSA private key generated by the server - :param verification_token: The verification token encrypted and sent by the client - :param shared_secret: The shared secret encrypted and sent by the client - :return: A tuple containing (decrypted token, decrypted secret) - """ - decrypted_token = decrypt_with_rsa(private_key, verification_token) - decrypted_secret = decrypt_with_rsa(private_key, shared_secret) - return decrypted_token, decrypted_secret - - -def serialize_public_key( - public_key: RSAPublicKey, -) -> bytes: - """Return public key serialize as bytes.""" - return public_key.public_bytes( - encoding=Encoding.DER, - format=PublicFormat.SubjectPublicKeyInfo, - ) - - -def deserialize_public_key(serialized_public_key: bytes) -> RSAPublicKey: - """Return deserialized public key.""" - # Key type is determined by the passed key itself. - # Should be be an RSA public key in this case. - key = load_der_public_key(serialized_public_key, default_backend()) - assert isinstance(key, RSAPublicKey) - return key diff --git a/src/azul/errorbox.py b/src/azul/errorbox.py index 6c6b3e4..3fcb40c 100644 --- a/src/azul/errorbox.py +++ b/src/azul/errorbox.py @@ -52,7 +52,7 @@ def __wxpython(title: str, message: str) -> None: """Error with wxPython.""" from wxPython.wx import wxApp, wxICON_EXCLAMATION, wxMessageDialog, wxOK - class LameApp(wxApp): # type: ignore[misc] + class LameApp(wxApp): # type: ignore[misc,no-any-unimported] __slots__ = () def OnInit(self) -> int: # noqa: N802 diff --git a/src/azul/game.py b/src/azul/game.py index 84da90f..7339b19 100644 --- a/src/azul/game.py +++ b/src/azul/game.py @@ -21,53 +21,81 @@ __title__ = "Azul" __author__ = "CoolCat467" +__license__ = "GNU General Public License Version 3" __version__ = "2.0.0" +import contextlib import importlib import math -import operator import os -import random +import sys import time -from collections import Counter, deque -from functools import lru_cache, wraps +import traceback +from collections import Counter +from functools import lru_cache from pathlib import Path -from typing import TYPE_CHECKING, Final, NamedTuple, TypeVar, cast +from typing import TYPE_CHECKING, Any, Final, TypeVar import pygame +import trio +from libcomponent.async_clock import Clock +from libcomponent.component import ( + Component, + ComponentManager, + Event, + ExternalRaiseManager, +) +from libcomponent.network_utils import find_ip from numpy import array, int8 +from pygame.color import Color from pygame.locals import ( - KEYDOWN, + K_ESCAPE, KEYUP, QUIT, RESIZABLE, SRCALPHA, USEREVENT, - VIDEORESIZE, + WINDOWRESIZED, ) from pygame.rect import Rect - +from pygame.sprite import LayeredDirty + +from azul import database, element_list, objects, sprite +from azul.client import GameClient, read_advertisements +from azul.crop import auto_crop_clear +from azul.network_shared import DEFAULT_PORT +from azul.server import GameServer +from azul.sound import SoundData, play_sound as base_play_sound +from azul.state import Tile +from azul.statemachine import AsyncState from azul.tools import ( - floor_line_subtract_generator, - gen_random_proper_seq, lerp_color, - randomize, - saturate, - sort_tiles, ) from azul.vector import Vector2 +if sys.version_info < (3, 11): + from exceptiongroup import ExceptionGroup + if TYPE_CHECKING: - from collections.abc import Callable, Generator, Iterable, Sequence + from collections.abc import ( + Awaitable, + Callable, + Generator, + Iterable, + Sequence, + ) - from typing_extensions import TypeVarTuple, Unpack + from mypy_extensions import u8 + from numpy.typing import NDArray + from typing_extensions import TypeVarTuple P = TypeVarTuple("P") T = TypeVar("T") RT = TypeVar("RT") -SCREENSIZE = (650, 600) +SCREEN_SIZE = (650, 600) +VSYNC = True FPS: Final = 48 @@ -97,6 +125,9 @@ ROOT_FOLDER: Final = Path(__file__).absolute().parent DATA_FOLDER: Final = ROOT_FOLDER / "data" FONT_FOLDER: Final = ROOT_FOLDER / "fonts" +LANG_FOLDER: Final = ROOT_FOLDER / "lang" +# TODO: Way to change language +LANGUAGE: Final = "en_us" # Game stuff # Tiles @@ -111,7 +142,8 @@ ("&", ORANGE), ("1", BLUE), ) -NUMBERONETILE = 5 + + TILESIZE = 15 # Colors @@ -119,17 +151,76 @@ TILEDEFAULT = ORANGE SCORECOLOR = BLACK PATSELECTCOLOR = DARKGREEN -BUTTONTEXTCOLOR = DARKCYAN +BUTTON_TEXT_COLOR = DARKCYAN +BUTTON_TEXT_OUTLINE = BLACK BUTTONBACKCOLOR = WHITE GREYSHIFT = 0.75 # 0.65 # Font -FONT: Final = FONT_FOLDER / "RuneScape-UF-Regular.ttf" +FONT: Final = FONT_FOLDER / "VeraSerif.ttf" SCOREFONTSIZE = 30 BUTTONFONTSIZE = 60 +SOUND_LOOKUP: Final = { + "delete_piece": "pop.mp3", + "piece_move": "slide.mp3", + "piece_update": "ding.mp3", + "game_won": "newthingget.ogg", + "button_click": "select.mp3", + "tick": "tick.mp3", +} +SOUND_DATA: Final = { + "delete_piece": SoundData( + volume=50, + ), +} + + +def decode_localization_entry(localization_string: str) -> list[str]: + """Return localization entry path.""" + return localization_string.split(".") + + +def s_(localization_string: str, **kwargs: object) -> str: + """Return localization string entry, or path to it if it doesn't exist.""" + language_filename = f"{LANGUAGE}.json" + language_file = LANG_FOLDER / language_filename + # Load keeps copy in-memory, so only performance hit first time. + language_data = database.load(language_file) + + localization_entry = decode_localization_entry(localization_string) + + current: dict[str, Any] = language_data + new: dict[str, Any] | str | None + final: str | None = None + for entry in localization_entry: + new = current.get(entry) + if new is None: + break + if isinstance(new, str): + final = new + break + assert isinstance( + new, + dict, + ), f"Unexpected value in {language_file!r} for {localization_string!r}" + current = new + if final is None: + # Key does not exist + localization_key = f"[{LANGUAGE}] {localization_string}" + if kwargs: + args = ",".join(f"{k}={v!r}" for k, v in kwargs.items()) + return f"{localization_key}<{args}>" + return localization_key + return final.format(**kwargs) + + +def vec2_to_location(vec: Vector2) -> tuple[int, int]: + """Return rounded location tuple from Vector2.""" + x, y = map(int, vec.rounded()) + return x, y + -@lru_cache def make_square_surf( color: ( pygame.color.Color @@ -148,6 +239,21 @@ def make_square_surf( return surf +def play_sound( + sound_name: str, +) -> tuple[pygame.mixer.Sound, int | float]: + """Play sound effect.""" + sound_filename = SOUND_LOOKUP.get(sound_name) + if sound_filename is None: + raise RuntimeError(f"Error: Sound with ID `{sound_name}` not found.") + sound_data = SOUND_DATA.get(sound_name, SoundData()) + + return base_play_sound( + DATA_FOLDER / sound_filename, + sound_data, + ) + + def outline_rectangle( surface: pygame.surface.Surface, color: ( @@ -174,41 +280,6 @@ def outline_rectangle( return surface -def auto_crop_clear( - surface: pygame.surface.Surface, - clear: pygame.color.Color | None = None, -) -> pygame.surface.Surface: - """Remove unneccicary pixels from image.""" - if clear is None: - clear = pygame.color.Color(0, 0, 0, 0) - surface = surface.convert_alpha() - w, h = surface.get_size() - surface.lock() - - def find_end( - iterfunc: Callable[[int], Iterable[pygame.color.Color]], - rangeobj: Iterable[int], - ) -> int: - for x in rangeobj: - if not all(y == clear for y in iterfunc(x)): - return x - return x - - def column(x: int) -> Generator[pygame.color.Color, None, None]: - return (surface.get_at((x, y)) for y in range(h)) - - def row(y: int) -> Generator[pygame.color.Color, None, None]: - return (surface.get_at((x, y)) for x in range(w)) - - leftc = find_end(column, range(w)) - rightc = find_end(column, range(w - 1, -1, -1)) - topc = find_end(row, range(h)) - floorc = find_end(row, range(h - 1, -1, -1)) - surface.unlock() - dim = pygame.rect.Rect(leftc, topc, rightc - leftc, floorc - topc) - return surface.subsurface(dim) - - @lru_cache def get_tile_color( tile_color: int, @@ -216,7 +287,7 @@ def get_tile_color( ) -> tuple[int, int, int] | tuple[tuple[int, int, int], tuple[int, int, int]]: """Return the color a given tile should be.""" if tile_color < 0: - if tile_color == -6: + if tile_color == Tile.blank: return GREY color = tile_colors[abs(tile_color + 1)] assert len(color) == 3 @@ -234,7 +305,7 @@ def get_tile_symbol_and_color( ) -> tuple[str, tuple[int, int, int]]: """Return the color a given tile should be.""" if tile_color < 0: - if tile_color == -6: + if tile_color == Tile.blank: return " ", GREY symbol, scolor = TILESYMBOLS[abs(tile_color + 1)] r, g, b = lerp_color(scolor, GREY, greyshift) @@ -264,6 +335,7 @@ def add_symbol_to_tile_surf( symbolsurf, (width * scale_factor, height * scale_factor), ) + # symbolsurf = pygame.transform.scale(symbolsurf, (tilesize, tilesize)) # sw, sh = symbolsurf.get_size() @@ -282,23 +354,18 @@ def add_symbol_to_tile_surf( surf.blit(symbolsurf, (int(x), int(y))) -# surf.blit(symbolsurf, (0, 0)) - - -@lru_cache def get_tile_image( - tile: Tile, + tile_color: int, tilesize: int, greyshift: float = GREYSHIFT, outline_size: float = 0.2, ) -> pygame.surface.Surface: """Return a surface of a given tile.""" - cid = tile.color - if cid < 5: - color = get_tile_color(cid, greyshift) + if tile_color < 5: + color = get_tile_color(tile_color, greyshift) - elif cid >= 5: - color_data = tile_colors[cid] + elif tile_color >= 5: + color_data = tile_colors[tile_color] assert len(color_data) == 2 color, outline = color_data surf = outline_rectangle( @@ -307,31 +374,18 @@ def get_tile_image( outline_size, ) # Add tile symbol - add_symbol_to_tile_surf(surf, cid, tilesize, greyshift) + add_symbol_to_tile_surf(surf, tile_color, tilesize, greyshift) return surf + assert isinstance(color[0], int) surf = make_square_surf(color, tilesize) # Add tile symbol - add_symbol_to_tile_surf(surf, cid, tilesize, greyshift) + add_symbol_to_tile_surf(surf, tile_color, tilesize, greyshift) return surf -def set_alpha( - surface: pygame.surface.Surface, - alpha: int, -) -> pygame.surface.Surface: - """Return a surface by replacing the alpha channel of it with given alpha value, preserve color.""" - surface = surface.copy().convert_alpha() - w, h = surface.get_size() - for y in range(h): - for x in range(w): - r, g, b = cast("tuple[int, int, int]", surface.get_at((x, y))[:3]) - surface.set_at((x, y), pygame.Color(r, g, b, alpha)) - return surface - - def get_tile_container_image( - wh: tuple[int, int], + width_height: tuple[int, int], back: ( pygame.color.Color | int @@ -343,7 +397,7 @@ def get_tile_container_image( ), ) -> pygame.surface.Surface: """Return a tile container image from a width and a height and a background color, and use a game's cache to help.""" - image = pygame.surface.Surface(wh, flags=SRCALPHA) + image = pygame.surface.Surface(width_height, flags=SRCALPHA) if back is not None: image.fill(back) else: @@ -351,712 +405,354 @@ def get_tile_container_image( return image -class Font: - """Font object, simplify using text.""" +class TileRenderer(sprite.Sprite): + """Base class for all objects that need to render tiles.""" + + __slots__ = ("background", "tile_separation") + greyshift = GREYSHIFT + tile_size = TILESIZE def __init__( self, - font_name: str | Path, - fontsize: int = 20, - color: tuple[int, int, int] = (0, 0, 0), - cx: bool = True, - cy: bool = True, - antialias: bool = False, - background: tuple[int, int, int] | None = None, - do_cache: bool = True, + name: str, + tile_separation: int | None = None, + background: tuple[int, int, int] | None = TILEDEFAULT, ) -> None: - """Initialize font.""" - self.font = font_name - self.size = int(fontsize) - self.color = color - self.center = [cx, cy] - self.antialias = bool(antialias) - self.background = background - self.do_cache = bool(do_cache) - self.cache: pygame.surface.Surface | None = None - self.last_text: str | None = None - self._change_font() - - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}(%r, %i, %r, %r, %r, %r, %r, %r)" % ( - self.font, - self.size, - self.color, - self.center[0], - self.center[1], - self.antialias, - self.background, - self.do_cache, - ) - - def _change_font(self) -> None: - """Set self.pyfont to a new pygame.font.Font object from data we have.""" - self.pyfont = pygame.font.Font(self.font, self.size) - - def _cache(self, surface: pygame.surface.Surface) -> None: - """Set self.cache to surface.""" - self.cache = surface - - def get_height(self) -> int: - """Return the height of font.""" - return self.pyfont.get_height() + """Initialize renderer.""" + super().__init__(name) - def render_nosurf( - self, - text: str | None, - size: int | None = None, - color: tuple[int, int, int] | None = None, - background: tuple[int, int, int] | None = None, - force_update: bool = False, - ) -> pygame.surface.Surface: - """Render and return a surface of given text. Use stored data to render, if arguments change internal data and render.""" - update_cache = ( - self.cache is None or force_update or text != self.last_text - ) - # Update internal data if new values given - if size is not None: - self.size = int(size) - self._change_font() - update_cache = True - if color is not None: - self.color = color - update_cache = True - if self.background != background: - self.background = background - update_cache = True - - if self.do_cache: - if update_cache: - self.last_text = text - surf = self.pyfont.render( - text, - self.antialias, - self.color, - self.background, - ).convert_alpha() - self._cache(surf.copy()) - else: - assert self.cache is not None - surf = self.cache + if tile_separation is None: + self.tile_separation = self.tile_size / 3.75 else: - # Render the text using the pygame font - surf = self.pyfont.render( - text, - self.antialias, - self.color, - self.background, - ).convert_alpha() - return surf + self.tile_separation = tile_separation + + self.background = background - def render( + def clear_image( self, - surface: pygame.surface.Surface, - text: str, - xy: tuple[int, int], - size: int | None = None, - color: tuple[int, int, int] | None = None, - background: tuple[int, int, int] | None = None, - force_update: bool = False, + tile_dimensions: tuple[int, int], + extra: tuple[int, int] | None = None, ) -> None: - """Render given text, use stored data to render, if arguments change internal data and render.""" - surf = self.render_nosurf(text, size, color, background, force_update) - - if True in self.center: - x, y = xy - cx, cy = self.center - w, h = surf.get_size() - if cx: - x -= w // 2 - if cy: - y -= h // 2 - xy = (int(x), int(y)) - - surface.blit(surf, xy) - + """Reset self.image using tile_dimensions tuple and fills with self.background. Also updates self.width_height.""" + size = Vector2.from_iter(tile_dimensions) + tile_full = self.tile_size + self.tile_separation + size *= tile_full -class ObjectHandler: - """ObjectHandler class, meant to be used for other classes.""" + offset = Vector2(self.tile_separation, self.tile_separation) - # __slots__ = ("objects", "next_id", "cache") + if extra is not None: + offset += extra - def __init__(self) -> None: - """Initialize object handler.""" - self.objects: dict[int, Object] = {} - self.next_id = 0 - self.cache: dict[str, int] = {} - - self.recalculate_render = True - self._render_order: tuple[int, ...] = () - - def add_object(self, obj: Object) -> None: - """Add an object to the game.""" - obj.id = self.next_id - self.objects[self.next_id] = obj - self.next_id += 1 - self.recalculate_render = True - - def rm_object(self, obj: Object) -> None: - """Remove an object from the game.""" - del self.objects[obj.id] - self.recalculate_render = True - - def rm_star(self) -> None: - """Remove all objects from self.objects.""" - for oid in list(self.objects): - self.rm_object(self.objects[oid]) - self.next_id = 0 - - def get_object(self, object_id: int) -> Object | None: - """Return the object associated with object id given. Return None if object not found.""" - if object_id in self.objects: - return self.objects[object_id] - return None + size += offset - def get_objects_with_attr(self, attribute: str) -> tuple[int, ...]: - """Return a tuple of object ids with given attribute.""" - return tuple( - oid - for oid in self.objects - if hasattr(self.objects[oid], attribute) + self.image = get_tile_container_image( + vec2_to_location(size), + self.background, ) - def get_object_by_attr( + def get_tile_topleft( self, - attribute: str, - value: object, - ) -> tuple[int, ...]: - """Return a tuple of object ids with that are equal to .""" - matches = [] - for oid in self.get_objects_with_attr(attribute): - if getattr(self.objects[oid], attribute) == value: - matches.append(oid) - return tuple(matches) - - def get_object_given_name(self, name: str) -> tuple[int, ...]: - """Return a tuple of object ids with names matching .""" - return self.get_object_by_attr("name", name) - - def reset_cache(self) -> None: - """Reset the cache.""" - self.cache = {} - - def get_object_by_name(self, object_name: str) -> Object: - """Get object by name, with cache.""" - if object_name not in self.cache: - ids = self.get_object_given_name(object_name) - if ids: - self.cache[object_name] = min(ids) - else: - raise RuntimeError(f"{object_name} Object Not Found!") - result = self.get_object(self.cache[object_name]) - if result is None: - raise RuntimeError(f"{object_name} Object Not Found!") - return result - - def set_attr_all(self, attribute: str, value: object) -> None: - """Set given attribute in all of self.objects to given value in all objects with that attribute.""" - for oid in self.get_objects_with_attr(attribute): - setattr(self.objects[oid], attribute, value) - - def recalculate_render_order(self) -> None: - """Recalculate the order in which to render objects to the screen.""" - new: dict[int, int] = {} - cur = 0 - for oid in reversed(self.objects): - obj = self.objects[oid] - if hasattr(obj, "Render_Priority"): - prior = obj.Render_Priority - if isinstance(prior, str): - add = 0 - if prior[:4] == "last": - try: - add = int(prior[4:] or 0) - except ValueError: - add = 0 - pos = len(self.objects) + add - if prior[:5] == "first": - try: - add = int(prior[5:] or 0) - except ValueError: - add = 0 - pos = -1 + add - if pos not in new.values(): - new[oid] = pos - else: - while True: - if add < 0: - pos -= 1 - else: - pos += 1 - if pos not in new.values(): - new[oid] = pos - break - else: - try: - prior = int(prior) - except ValueError: - prior = cur - while True: - if prior in new.values(): - prior += 1 - else: - break - new[oid] = prior - else: - while True: - if cur in new.values(): - cur += 1 - else: - break - new[oid] = cur - cur += 1 - revnew = {new[k]: k for k in new} - self._render_order = tuple(revnew[key] for key in sorted(revnew)) - - def process_objects(self, time_passed: float) -> None: - """Call the process function on all objects.""" - if self.recalculate_render: - self.recalculate_render_order() - self.recalculate_render = False - for oid in iter(self.objects): - self.objects[oid].process(time_passed) - - def render_objects(self, surface: pygame.surface.Surface) -> None: - """Render all objects to surface.""" - if not self._render_order or self.recalculate_render: - self.recalculate_render_order() - self.recalculate_render = False - for oid in self._render_order: # reversed(list(self.objects.keys())): - self.objects[oid].render(surface) - - def __del__(self) -> None: - """Cleanup.""" - self.reset_cache() - self.rm_star() - - -class Object: - """Object object.""" - - __slots__ = ( - "Render_Priority", - "game", - "hidden", - "id", - "image", - "location", - "location_mode_on_resize", - "name", - "screen_size_last", - "wh", - ) - - def __init__(self, name: str) -> None: - """Set self.name to name, and other values for rendering. - - Defines the following attributes: - self.name - self.image - self.location - self.wh - self.hidden - self.location_mode_on_resize - self.id - """ - self.name = str(name) - self.image: pygame.surface.Surface | None = None - self.location = Vector2( - round(SCREENSIZE[0] / 2), - round(SCREENSIZE[1] / 2), - ) - self.wh = 0, 0 - self.hidden = False - self.location_mode_on_resize = "Scale" - self.screen_size_last = SCREENSIZE + tile_location: tuple[int, int], + offset: tuple[int, int] | None = None, + ) -> tuple[int, int]: + """Return top left corner location of tile given its location and optional offset.""" + tile_full = self.tile_size + self.tile_separation - self.id = 0 - self.game: Game - self.Render_Priority: str | int + position = Vector2.from_iter(tile_location) * tile_full + if offset is not None: + position += offset + position += (self.tile_separation, self.tile_separation) - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}()" + return vec2_to_location(position) - def get_image_zreo_no_fix(self) -> tuple[float, float]: - """Return the screen location of the topleft point of self.image.""" - return ( - self.location[0] - self.wh[0] / 2, - self.location[1] - self.wh[1] / 2, + def get_tile_rect( + self, + tile_location: tuple[int, int], + offset: tuple[int, int] | None = None, + ) -> Rect: + """Return Rect of area given tile exists in.""" + topleft = self.get_tile_topleft(tile_location, offset) + return Rect( + topleft, + (self.tile_size, self.tile_size), ) - def get_image_zero(self) -> tuple[int, int]: - """Return the screen location of the topleft point of self.image fixed to integer values.""" - x, y = self.get_image_zreo_no_fix() - return int(x), int(y) + def blit_tile( + self, + tile_color: int, + tile_location: tuple[int, int], + offset: tuple[int, int] | None = None, + ) -> None: + """Blit the surface of a given tile object onto self.image at given tile location. It is assumed that all tile locations are xy tuples.""" + position = self.get_tile_topleft(tile_location, offset) - def get_rect(self) -> Rect: - """Return a Rect object representing this Object's area.""" - return Rect(self.get_image_zero(), self.wh) + surf = get_tile_image(tile_color, self.tile_size, self.greyshift) + assert self.image is not None - def point_intersects( - self, - screen_location: tuple[int, int] | Vector2, - ) -> bool: - """Return True if this Object intersects with a given screen location.""" - return self.get_rect().collidepoint(tuple(screen_location)) + self.image.blit( + surf, + position, + ) def to_image_surface_location( self, screen_location: tuple[int, int] | Vector2, - ) -> tuple[int, int]: - """Return the location a screen location would be at on the objects image. Can return invalid data.""" - # Get zero zero in image locations - zx, zy = self.get_image_zero() # Zero x and y - sx, sy = screen_location # Screen x and y - return ( - int(sx) - zx, - int(sy) - zy, - ) # Location with respect to image dimensions - - def process(self, time_passed: float) -> None: - """Process Object. Replace when calling this class.""" - - def render(self, surface: pygame.surface.Surface) -> None: - """Render self.image to surface if self.image is not None. Updates self.wh.""" - if self.image is None or self.hidden: - return - self.wh = self.image.get_size() - x, y = self.get_image_zero() - surface.blit(self.image, (int(x), int(y))) - - # pygame.draw.rect(surface, MAGENTA, self.get_rect(), 1) - - def __del__(self) -> None: - """Delete self.image.""" - del self.image - - def screen_size_update(self) -> None: - """Handle screensize is changes.""" - nx, ny = self.location - - if self.location_mode_on_resize == "Scale": - ow, oh = self.screen_size_last - nw, nh = SCREENSIZE - - x, y = self.location - nx, ny = x * (nw / ow), y * (nh / oh) + ) -> Vector2: + """Return screen location with respect to top left of image.""" + return Vector2.from_points(self.rect.topleft, screen_location) - self.location = Vector2(nx, ny) - self.screen_size_last = SCREENSIZE - - -class MultipartObject(Object, ObjectHandler): - """Thing that is both an Object and an ObjectHandler, and is meant to be an Object made up of multiple Objects.""" - - def __init__(self, name: str): - """Initialize Object and ObjectHandler of self. - - Also set self._lastloc and self._lasthidden to None - """ - Object.__init__(self, name) - ObjectHandler.__init__(self) - - self._lastloc: Vector2 | None = None - self._lasthidden: bool | None = None - - def reset_position(self) -> None: - """Reset the position of all objects within.""" - raise NotImplementedError - - def get_intersection( + def get_tile_point( self, - point: tuple[int, int] | Vector2, - ) -> tuple[str, tuple[int, int]] | tuple[None, None]: - """Return where a given point touches in self. Returns (None, None) with no intersections.""" - for oid in self.objects: - obj = self.objects[oid] - if hasattr(obj, "get_tile_point"): - output = obj.get_tile_point(point) - if output is not None: - return obj.name, output - else: - raise Warning( - "Not all of self.objects have the get_tile_point attribute!", - ) - return None, None - - def process(self, time_passed: float) -> None: - """Process Object self and ObjectHandler self and call self.reset_position on location change.""" - Object.process(self, time_passed) - ObjectHandler.process_objects(self, time_passed) - - if self.location != self._lastloc: - self.reset_position() - self._lastloc = self.location - - if self.hidden != self._lasthidden: - self.set_attr_all("hidden", self.hidden) - self._lasthidden = self.hidden + screen_location: tuple[int, int] | Vector2, + ) -> Vector2 | None: + """Return the xy choordinates of which tile intersects given a point or None.""" + # Can't get tile if screen location doesn't intersect our hitbox! + if isinstance(screen_location, Vector2): + screen_location = vec2_to_location(screen_location) + if not self.is_selected(screen_location): + return None - def render(self, surface: pygame.surface.Surface) -> None: - """Render self and all parts to the surface.""" - Object.render(self, surface) - ObjectHandler.render_objects(self, surface) + # Find out where screen point is in image locations + # board x and y + surface_pos = self.to_image_surface_location(screen_location) + # Subtract separation boarder offset + surface_pos -= (self.tile_separation, self.tile_separation) - def __del__(self) -> None: - """Delete data.""" - Object.__del__(self) - ObjectHandler.__del__(self) + tile_full = self.tile_size + self.tile_separation + # Get tile position and offset into that tile + tile_position, offset = divmod(surface_pos, tile_full) + for value in offset: + # If in separation region, not selected + if value > self.tile_size: + return None + # Otherwise, not in separation region, so we should be good + return tile_position.floored() -class Tile(NamedTuple): - """Represents a Tile.""" - color: int +class EventClock(Component): + """Event Clock Component. + Will raise `self.event_to_raise` every `self.duration` seconds. + If more than duration seconds pass before ticks, will only raise + one event. -class TileRenderer(Object): - """Base class for all objects that need to render tiles.""" + Do not pass leveled events, event reference is maintained and + when first event is mutated with pop_level it will be same object + and only first run will be leveled event. + """ - __slots__ = ("back", "image_update", "tile_full", "tile_seperation") - greyshift = GREYSHIFT - tile_size = TILESIZE + __slots__ = ("duration", "event_to_raise", "time_passed") def __init__( self, name: str, - game: Game, - tile_seperation: int | None = None, - background: tuple[int, int, int] | None = TILEDEFAULT, + duration: float, + event_to_raise: Event[Any], ) -> None: - """Initialize renderer. Needs a game object for its cache and optional tile separation value and background RGB color. - - Defines the following attributes during initialization and uses throughout: - self.game - self.wh - self.tile_seperation - self.tile_full - self.back - and finally, self.image_update - - The following functions are also defined: - self.clear_image - self.render_tile - self.update_image (but not implemented) - self.process - """ + """Initialize with name, duration, and event to raise.""" super().__init__(name) - self.game = game - - if tile_seperation is None: - self.tile_seperation = self.tile_size / 3.75 - else: - self.tile_seperation = tile_seperation - self.tile_full = self.tile_size + self.tile_seperation - self.back = background + self.time_passed: float = 0.0 + self.duration = duration + self.event_to_raise = event_to_raise - self.image_update = True + def bind_handlers(self) -> None: + """Register tick event handler.""" + self.register_handler("tick", self.handle_tick) - def get_rect(self) -> Rect: - """Return a Rect object representing this row's area.""" - wh = ( - self.wh[0] - self.tile_seperation * 2, - self.wh[1] - self.tile_seperation * 2, - ) - location = self.location[0] - wh[0] / 2, self.location[1] - wh[1] / 2 - return Rect(location, wh) - - def clear_image(self, tile_dimensions: tuple[int, int]) -> None: - """Reset self.image using tile_dimensions tuple and fills with self.back. Also updates self.wh.""" - tw, th = tile_dimensions - self.wh = ( - round(tw * self.tile_full + self.tile_seperation), - round(th * self.tile_full + self.tile_seperation), - ) - self.image = get_tile_container_image(self.wh, self.back) + async def handle_tick(self, event: Event[sprite.TickEventData]) -> None: + """Handle tick event.""" + self.time_passed += event.data.time_passed + truediv, self.time_passed = divmod(self.time_passed, self.duration) - def render_tile( - self, - tile_object: Tile, - tile_location: tuple[int, int], - ) -> None: - """Blit the surface of a given tile object onto self.image at given tile location. It is assumed that all tile locations are xy tuples.""" - x, y = tile_location - surf = get_tile_image(tile_object, self.tile_size, self.greyshift) - assert self.image is not None - self.image.blit( - surf, - ( - round(x * self.tile_full + self.tile_seperation), - round(y * self.tile_full + self.tile_seperation), - ), - ) + # Could raise multiple times, but I am deciding that we will + # only raise at most once even if we miss the train + if truediv: + # Known issue: Event to raise cannot be a leveled event, + # because event.pop_level mutates the event object in place + await self.raise_event(self.event_to_raise) - def update_image(self) -> None: - """Process image changes, directed by self.image_update being True.""" - raise NotImplementedError - def process(self, time_passed: float) -> None: - """Call self.update_image() if self.image_update is True, then set self.update_image to False.""" - if self.image_update: - self.update_image() - self.image_update = False +class Cursor(TileRenderer): + """Cursor TileRenderer. + Registers following event handlers: + - cursor_drag + - cursor_reached_destination + - cursor_set_destination + - cursor_set_movement_mode + - client_disconnected -class Cursor(TileRenderer): - """Cursor Object.""" + Sometimes registered: + - PygameMouseMotion + """ - __slots__ = ("holding_number_one", "tiles") + __slots__ = ( + "client_mode", + "last_transmit_pos", + "tiles", + "time_passed", + ) greyshift = GREYSHIFT - Render_Priority = "last" + duration = 0.25 - def __init__(self, game: Game) -> None: + def __init__(self) -> None: """Initialize cursor with a game it belongs to.""" - super().__init__("Cursor", game, background=None) + super().__init__("Cursor", background=None) + self.update_location_on_resize = True + + self.add_components( + ( + sprite.MovementComponent(speed=600), + sprite.TargetingComponent("cursor_reached_destination"), + ), + ) - self.holding_number_one = False - self.tiles: deque[Tile] = deque() + # Stored in reverse render order + self.tiles: list[int] = [] + self.last_transmit_pos = self.location + self.time_passed = 0.0 + self.client_mode = False def update_image(self) -> None: """Update self.image.""" - self.clear_image((len(self.tiles), 1)) - - for x in range(len(self.tiles)): - self.render_tile(self.tiles[x], (x, 0)) - - def is_pressed(self) -> bool: - """Return True if the right mouse button is pressed.""" - return bool(pygame.mouse.get_pressed()[0]) - - def get_held_count(self, count_number_one: bool = False) -> int: - """Return the number of held tiles, can be discounting number one tile.""" - length = len(self.tiles) - if self.holding_number_one and not count_number_one: - return length - 1 - return length - - def is_holding(self, count_number_one: bool = False) -> bool: - """Return True if the mouse is dragging something.""" - return self.get_held_count(count_number_one) > 0 + tile_count = len(self.tiles) + self.clear_image((tile_count, 1)) + + # Render in reverse order so keeping number one on end is easier + for x in range(tile_count): + self.blit_tile(self.tiles[tile_count - x - 1], (x, 0)) + if tile_count: + self.dirty = 1 + self.visible = bool(tile_count) + + def bind_handlers(self) -> None: + """Register handlers.""" + self.register_handlers( + { + "game_cursor_data": self.handle_cursor_drag, + "cursor_reached_destination": self.handle_cursor_reached_destination, + "game_cursor_set_destination": self.handle_cursor_set_destination, + "game_cursor_set_movement_mode": self.handle_cursor_set_movement_mode, + "client_disconnected": self.handle_client_disconnected, + }, + ) - def get_held_info( - self, - count_number_one_tile: bool = False, - ) -> tuple[Tile | None, int]: - """Return color of tiles are and number of tiles held.""" - if not self.is_holding(count_number_one_tile): - return None, 0 - return self.tiles[0], self.get_held_count(count_number_one_tile) - - def process(self, time_passed: float) -> None: - """Process cursor.""" - x, y = pygame.mouse.get_pos() - x = saturate(x, 0, SCREENSIZE[0]) - y = saturate(y, 0, SCREENSIZE[1]) - self.location = Vector2(x, y) - if self.image_update: - if len(self.tiles): - self.update_image() + async def handle_cursor_drag(self, event: Event[Counter[int]]) -> None: + """Drag one or more tiles.""" + self.tiles.clear() + for tile_color in event.data.elements(): + if tile_color == Tile.one: + self.tiles.insert(0, tile_color) else: - self.image = None - self.image_update = False - - def force_hold(self, tiles: Iterable[Tile]) -> None: - """Pretty much it's drag but with no constraints.""" - for tile in tiles: - if tile.color == NUMBERONETILE: - self.holding_number_one = True - self.tiles.append(tile) - else: - self.tiles.appendleft(tile) - self.image_update = True - - def drag(self, tiles: Iterable[Tile]) -> None: - """Drag one or more tiles, as long as it's a list.""" - for tile in tiles: - if tile is not None and tile.color == NUMBERONETILE: - self.holding_number_one = True - self.tiles.append(tile) - else: - self.tiles.appendleft(tile) - self.image_update = True + self.tiles.append(tile_color) + self.update_image() + await trio.lowlevel.checkpoint() - def drop( + async def handle_cursor_reached_destination( self, - number: int | None = None, - allow_number_one_tile: bool = False, - ) -> list[Tile]: - """Return all of the tiles the Cursor is carrying.""" - if self.is_holding(allow_number_one_tile): - if number is None: - number = self.get_held_count(allow_number_one_tile) - else: - number = saturate( - number, - 0, - self.get_held_count(allow_number_one_tile), - ) - - tiles = [] - for tile in (self.tiles.popleft() for i in range(number)): - if tile.color == NUMBERONETILE and not allow_number_one_tile: - self.tiles.append(tile) - continue - tiles.append(tile) - self.image_update = True - - self.holding_number_one = NUMBERONETILE in { - tile.color for tile in self.tiles - } - return tiles - return [] - - def drop_one_tile(self) -> Tile | None: - """If holding the number one tile, drop it (returns it).""" - if self.holding_number_one: - not_number_one_tile = self.drop(None, False) - one = self.drop(1, True) - self.drag(not_number_one_tile) - self.holding_number_one = False - return one[0] - return None + event: Event[None], + ) -> None: + """Stop ticking.""" + self.unregister_handler_type("tick") + await trio.lowlevel.checkpoint() + def move_to_front(self) -> None: + """Move this sprite to front.""" + group = self.groups()[-1] + assert isinstance(group, LayeredDirty) + group.move_to_front(self) -G = TypeVar("G", bound="Grid") + async def handle_cursor_set_destination( + self, + event: Event[tuple[int, int]], + ) -> None: + """Start moving towards new destination.""" + destination = Vector2.from_iter( + x * y for x, y in zip(event.data, SCREEN_SIZE, strict=True) + ).floored() + # print(f"handle_cursor_set_destination {destination = }") + + targeting: sprite.TargetingComponent = self.get_component("targeting") + targeting.destination = destination + if not self.has_handler("tick"): + self.register_handler( + "tick", + self.handle_tick, + ) + self.move_to_front() + await trio.lowlevel.checkpoint() -def gsc_bound_index( - bounds_failure_return: T, -) -> Callable[ - [Callable[[G, tuple[int, int], *P], RT]], - Callable[[G, tuple[int, int], *P], RT | T], -]: - """Return a decorator for any grid or grid subclass that will keep index positions within bounds.""" + async def handle_pygame_mouse_motion( + self, + event: Event[sprite.PygameMouseMotion], + ) -> None: + """Set location to event data.""" + self.move_to_front() + self.location = event.data["pos"] + await trio.lowlevel.checkpoint() + + async def handle_tick(self, event: Event[sprite.TickEventData]) -> None: + """Handle tick event.""" + if self.client_mode: + self.time_passed += event.data.time_passed + truediv, self.time_passed = divmod(self.time_passed, self.duration) + + if self.last_transmit_pos != self.location and truediv: + self.last_transmit_pos = self.location + else: + await trio.lowlevel.checkpoint() + return - def gsc_bounds_keeper( - function: Callable[[G, tuple[int, int], *P], RT], - ) -> Callable[[G, tuple[int, int], *P], RT | T]: - """Grid or Grid Subclass Decorator that keeps index positions within bounds, as long as index is first argument after self arg.""" + transmit_location = Vector2.from_iter( + x / y for x, y in zip(self.location, SCREEN_SIZE, strict=True) + ) - @wraps(function) - def keep_within_bounds( - self: G, - index: tuple[int, int], - *args: Unpack[P], - ) -> RT | T: - """Ensure a index position tuple is valid.""" - x, y = index - if x < 0 or x >= self.size[0]: - return bounds_failure_return - if y < 0 or y >= self.size[1]: - return bounds_failure_return - return function(self, index, *args) + # Transmit to server + # Event level to so reaches client + await self.raise_event( + Event( + "game_cursor_location_transmit", + transmit_location, + 2, + ), + ) + else: + # Server mode + targeting: sprite.TargetingComponent = self.get_component( + "targeting", + ) + await targeting.move_destination_time(event.data.time_passed) - return keep_within_bounds + async def handle_cursor_set_movement_mode( + self, + event: Event[bool], + ) -> None: + """Change cursor movement mode. True if client mode, False if server mode.""" + self.client_mode = event.data + # print(f'handle_cursor_set_movement_mode {self.client_mode = }') + if self.client_mode: + self.register_handlers( + { + "PygameMouseMotion": self.handle_pygame_mouse_motion, + "tick": self.handle_tick, + }, + ) + else: + self.unregister_handler_type("PygameMouseMotion") + self.unregister_handler_type("tick") + await trio.lowlevel.checkpoint() - return gsc_bounds_keeper + async def handle_client_disconnected( + self, + event: Event[None], + ) -> None: + """Unregister tick event handler.""" + # print("[azul.game.Cursor] Got client disconnect, unregistering tick") + self.unregister_handler_type("tick") + await trio.lowlevel.checkpoint() class Grid(TileRenderer): @@ -1066,1746 +762,671 @@ class Grid(TileRenderer): def __init__( self, + name: str, size: tuple[int, int], - game: Game, - tile_seperation: int | None = None, + tile_separation: int | None = None, background: tuple[int, int, int] | None = TILEDEFAULT, ) -> None: """Grid Objects require a size and game at least.""" - super().__init__("Grid", game, tile_seperation, background) + super().__init__(name, tile_separation, background) self.size = size self.data = array( - [-6 for i in range(int(self.size[0] * self.size[1]))], + [Tile.blank for i in range(int(self.size[0] * self.size[1]))], int8, ).reshape(self.size) - def update_image(self) -> None: + def get_tile(self, xy: tuple[int, int]) -> int: + """Return tile color at given index.""" + x, y = xy + return int(self.data[y, x]) + + def update_image( + self, + offset: tuple[int, int] | None = None, + extra_space: tuple[int, int] | None = None, + ) -> None: """Update self.image.""" - self.clear_image(self.size) + self.clear_image(self.size, extra_space) - for y in range(self.size[1]): - for x in range(self.size[0]): - self.render_tile(Tile(self.data[y, x]), (x, y)) + width, height = self.size - def get_tile_point( - self, - screen_location: tuple[int, int] | Vector2, - ) -> tuple[int, int] | None: - """Return the xy choordinates of which tile intersects given a point. Returns None if no intersections.""" - # Can't get tile if screen location doesn't intersect our hitbox! - if not self.point_intersects(screen_location): - return None - # Otherwise, find out where screen point is in image locations - # board x and y - bx, by = self.to_image_surface_location(screen_location) - # Finally, return the full divides (no decimals) of xy location by self.tile_full. - return int(bx // self.tile_full), int(by // self.tile_full) + for y in range(height): + for x in range(width): + pos = (x, y) + self.blit_tile(self.get_tile(pos), pos, offset) - @gsc_bound_index(None) - def place_tile(self, xy: tuple[int, int], tile: Tile) -> bool: - """Place a Tile Object if permitted to do so. Return True if success.""" - x, y = xy - if self.data[y, x] < 0: - self.data[y, x] = tile.color - del tile - self.image_update = True - return True - return False - - @gsc_bound_index(None) - def get_tile(self, xy: tuple[int, int], replace: int = -6) -> Tile | None: - """Return a Tile Object from a given position in the grid if permitted. Return None on failure.""" - x, y = xy - tile_color = int(self.data[y, x]) - if tile_color < 0: - return None - self.data[y, x] = replace - self.image_update = True - return Tile(tile_color) + def fake_tile_exists(self, xy: tuple[int, int]) -> bool: + """Return if tile at given position is a fake tile.""" + return self.get_tile(xy) < 0 - @gsc_bound_index(None) - def get_info(self, xy: tuple[int, int]) -> Tile: - """Return the Tile Object at a given position without deleting it from the Grid.""" + def place_tile(self, xy: tuple[int, int], tile_color: int) -> None: + """Place tile at given position.""" x, y = xy - color = int(self.data[y, x]) - return Tile(color) + self.data[y, x] = tile_color + self.update_image() + + def pop_tile(self, xy: tuple[int, int], replace: int = Tile.blank) -> int: + """Return popped tile from given position in the grid.""" + tile_color = self.get_tile(xy) + self.place_tile(xy, replace) + return tile_color - def get_colors(self) -> list[int]: - """Return a list of the colors of tiles within self.""" + def get_colors(self) -> set[int]: + """Return a set of the colors of tiles within self.""" colors = set() - for y in range(self.size[1]): - for x in range(self.size[0]): - info_color = int(self.data[y, x]) - assert info_color is not None - colors.add(info_color) - return list(colors) - - def is_empty(self, empty_color: int = -6) -> bool: + width, height = self.size + for y in range(height): + for x in range(width): + colors.add(self.get_tile((x, y))) + return colors + + def is_empty(self, empty_color: int = Tile.blank) -> bool: """Return True if Grid is empty (all tiles are empty_color).""" colors = self.get_colors() - # Colors should only be [-6] if empty - return colors == [empty_color] - - def __del__(self) -> None: - """Delete data.""" - super().__del__() - del self.data + return len(colors) == 1 and colors.pop() == empty_color class Board(Grid): """Represents the board in the Game.""" - __slots__ = ("additions", "player", "variant_play", "wall_tiling") - bcolor = ORANGE + __slots__ = ("board_id",) - def __init__(self, player: Player, variant_play: bool = False) -> None: + def __init__(self, board_id: int) -> None: """Initialize player's board.""" - super().__init__((5, 5), player.game, background=self.bcolor) - self.name = "Board" - self.player = player + super().__init__(f"board_{board_id}", (5, 5), background=ORANGE) - self.variant_play = variant_play - self.additions: dict[int, Tile | int | None] = {} + self.board_id = board_id - self.wall_tiling = False + self.update_location_on_resize = True + + # Clear image so rect is set + self.clear_image((5, 5)) def __repr__(self) -> str: """Return representation of self.""" - return ( - f"{self.__class__.__name__}({self.player!r}, {self.variant_play})" - ) - - def set_colors(self, keep_read: bool = True) -> None: - """Reset tile colors.""" - for y in range(self.size[1]): - for x in range(self.size[0]): - if not keep_read or self.data[y, x] < 0: - self.data[y, x] = -( - (self.size[1] - y + x) % REGTILECOUNT + 1 - ) - - # print(self.data[y, x], end=' ') - # print() - # print('-'*10) - - def get_row(self, index: int) -> Generator[Tile, None, None]: - """Return a row from self. Does not delete data from internal grid.""" - for x in range(self.size[0]): - tile = self.get_info((x, index)) - assert tile is not None - yield tile - - def get_column(self, index: int) -> Generator[Tile, None, None]: - """Return a column from self. Does not delete data from internal grid.""" - for y in range(self.size[1]): - tile = self.get_info((index, y)) - assert tile is not None - yield tile - - def get_colors_in_row( - self, - index: int, - exclude_negatives: bool = True, - ) -> list[int]: - """Return the colors placed in a given row in internal grid.""" - row_colors = [tile.color for tile in self.get_row(index)] - if exclude_negatives: - row_colors = [c for c in row_colors if c >= 0] - ccolors = Counter(row_colors) - return sorted(ccolors.keys()) - - def get_colors_in_column( - self, - index: int, - exclude_negatives: bool = True, - ) -> list[int]: - """Return the colors placed in a given row in internal grid.""" - column_colors = [tile.color for tile in self.get_column(index)] - if exclude_negatives: - column_colors = [c for c in column_colors if c >= 0] - ccolors = Counter(column_colors) - return sorted(ccolors.keys()) - - def is_wall_tiling(self) -> bool: - """Return True if in Wall Tiling Mode.""" - return self.wall_tiling - - def get_tile_for_cursor_by_row(self, row: int) -> Tile | None: - """Return A COPY OF tile the mouse should hold. Returns None on failure.""" - if row in self.additions: - data = self.additions[row] - if isinstance(data, Tile): - return data - return None - - @gsc_bound_index(False) - def can_place_tile_color_at_point( - self, - position: tuple[int, int], - tile: Tile, - ) -> bool: - """Return True if tile's color is valid at given position.""" - column, row = position - colors = set( - self.get_colors_in_column(column) + self.get_colors_in_row(row), + return f"{self.__class__.__name__}({self.board_id})" + + def bind_handlers(self) -> None: + """Register event handlers.""" + self.register_handlers( + { + "game_board_data": self.handle_game_board_data, + }, ) - return tile.color not in colors - - def get_rows_to_tile_map(self) -> dict[int, int]: - """Return a dictionary of row numbers and row color to be wall tiled.""" - rows = {} - for row, tile in self.additions.items(): - if not isinstance(tile, Tile): - continue - rows[row] = tile.color - return rows - - def calculate_valid_locations_for_tile_row( - self, - row: int, - ) -> tuple[int, ...]: - """Return the valid drop columns of the additions tile for a given row.""" - valid = [] - # ??? Why overwriting row? - if row in self.additions: - tile = self.additions[row] - if isinstance(tile, Tile): - for column in range(self.size[0]): - if self.can_place_tile_color_at_point((column, row), tile): - valid.append(column) - return tuple(valid) - return () - - def remove_invalid_additions(self) -> None: - """Remove invalid additions that would not be placeable.""" - # In the wall-tiling phase, it may happen that you - # are not able to move the rightmost tile of a certain - # pattern line over to the wall because there is no valid - # space left for it. In this case, you must immediately - # place all tiles of that pattern line in your floor line. - for row in range(self.size[1]): - row_tile = self.additions[row] - if not isinstance(row_tile, Tile): - continue - valid = self.calculate_valid_locations_for_tile_row(row) - if not valid: - floor = self.player.get_object_by_name("floor_line") - assert isinstance(floor, FloorLine) - floor.place_tile(row_tile) - self.additions[row] = None - - @gsc_bound_index(False) - def wall_tile_from_point(self, position: tuple[int, int]) -> bool: - """Given a position, wall tile. Return success on placement. Also updates if in wall tiling mode.""" - success = False - column, row = position - at_point = self.get_info(position) - assert at_point is not None - if at_point.color <= 0 and row in self.additions: - tile = self.additions[row] - if isinstance(tile, Tile) and self.can_place_tile_color_at_point( - position, - tile, - ): - self.place_tile(position, tile) - self.additions[row] = column - # Update invalid placements after new placement - self.remove_invalid_additions() - success = True - if not self.get_rows_to_tile_map(): - self.wall_tiling = False - return success - - def wall_tiling_mode(self, moved_table: dict[int, Tile]) -> None: - """Set self into Wall Tiling Mode. Finishes automatically if not in variant play mode.""" - self.wall_tiling = True - for key, value in moved_table.items(): - key = int(key) - 1 - if key in self.additions: - raise RuntimeError( - f"Key {key!r} Already in additions dictionary!", - ) - self.additions[key] = value - if not self.variant_play: - for row in range(self.size[1]): - if row in self.additions: - rowdata = [tile.color for tile in self.get_row(row)] - tile = self.additions[row] - if not isinstance(tile, Tile): - continue - negative_tile_color = -(tile.color + 1) - if negative_tile_color in rowdata: - column = rowdata.index(negative_tile_color) - self.place_tile((column, row), tile) - # Set data to the column placed in, use for scoring - self.additions[row] = column - else: - raise RuntimeError( - f"{negative_tile_color} not in row {row}!", - ) - else: - raise RuntimeError(f"{row} not in moved_table!") - self.wall_tiling = False - else: - # Invalid additions can only happen in variant play mode. - self.remove_invalid_additions() - @gsc_bound_index(([], [])) - def get_touches_continuous( + async def handle_game_board_data( self, - xy: tuple[int, int], - ) -> tuple[list[Tile], list[Tile]]: - """Return two lists, each of which contain all the tiles that touch the tile at given x y position, including that position.""" - rs, cs = self.size - x, y = xy - # Get row and column tile color data - row = [tile.color for tile in self.get_row(y)] - column = [tile.color for tile in self.get_column(x)] - - # Both - def get_greater_than(v: int, size: int, data: list[int]) -> list[int]: - """Go through data forward and backward from point v out by size, and return all points from data with a value >= 0.""" - - def try_range(range_: Iterable[int]) -> list[int]: - """Try range. Return all of data in range up to when indexed value is < 0.""" - ret = [] - for tv in range_: - if data[tv] < 0: - break - ret.append(tv) - return ret - - nt = try_range(reversed(range(v))) - pt = try_range(range(v + 1, size)) - return nt + pt - - def comb(one: Iterable[T], two: Iterable[RT]) -> list[tuple[T, RT]]: - """Combine two lists by zipping together and returning list object.""" - return list(zip(one, two, strict=False)) - - def get_all(lst: list[tuple[int, int]]) -> Generator[Tile, None, None]: - """Return all of the self.get_info points for each value in lst.""" - for pos in lst: - tile = self.get_info(pos) - assert tile is not None - yield tile - - # Get row touches - row_touches = comb(get_greater_than(x, rs, row), [y] * rs) - # Get column touches - column_touches = comb([x] * cs, get_greater_than(y, cs, column)) - # Get real tiles from indexes and return - return list(get_all(row_touches)), list(get_all(column_touches)) - - def score_additions(self) -> int: - """Return the number of points the additions scored. - - Uses self.additions, which is set in self.wall_tiling_mode() - """ - score = 0 - for x, y in ((self.additions[y], y) for y in range(self.size[1])): - if x is not None: - assert isinstance(x, int) - rowt, colt = self.get_touches_continuous((x, y)) - horiz = len(rowt) - verti = len(colt) - if horiz > 1: - score += horiz - if verti > 1: - score += verti - if horiz <= 1 and verti <= 1: - score += 1 - del self.additions[y] - return score - - def get_filled_rows(self) -> int: - """Return the number of filled rows on this board.""" - count = 0 - for row in range(self.size[1]): - real = (t.color >= 0 for t in self.get_row(row)) - if all(real): - count += 1 - return count - - def has_filled_row(self) -> bool: - """Return True if there is at least one completely filled horizontal line.""" - return self.get_filled_rows() >= 1 + event: Event[tuple[int, NDArray[int8]]], + ) -> None: + """Handle `game_board_data` event.""" + board_id, array = event.data - def get_filled_columns(self) -> int: - """Return the number of filled rows on this board.""" - count = 0 - for column in range(self.size[0]): - real = (t.color >= 0 for t in self.get_column(column)) - if all(real): - count += 1 - return count - - def get_filled_colors(self) -> int: - """Return the number of completed colors on this board.""" - tiles = ( - self.get_info((x, y)) - for x in range(self.size[0]) - for y in range(self.size[1]) - ) - color_count = Counter(t.color for t in tiles if t is not None) - count = 0 - for fill_count in color_count.values(): - if fill_count >= 5: - count += 1 - return count + if board_id != self.board_id: + await trio.lowlevel.checkpoint() + return - def end_of_game_scoreing(self) -> int: - """Return the additional points for this board at the end of the game.""" - score = 0 - score += self.get_filled_rows() * 2 - score += self.get_filled_columns() * 7 - score += self.get_filled_colors() * 10 - return score + assert array.ndim == 2 + assert len(array.shape) == 2 + w, h = array.shape + # Prove to typechecker that array is 2D + self.data = array.reshape((w, h)) + self.update_image() + self.visible = True - def process(self, time_passed: float) -> None: - """Process board.""" - if self.image_update and not self.variant_play: - self.set_colors(True) - super().process(time_passed) + await trio.lowlevel.checkpoint() -class Row(TileRenderer): +class PatternRows(TileRenderer): """Represents one of the five rows each player has.""" - __slots__ = ("color", "player", "size", "tiles") + __slots__ = ( + "rows", + "rows_id", + ) greyshift = GREYSHIFT def __init__( self, - player: Player, - size: int, - tile_seperation: int | None = None, - background: tuple[int, int, int] | None = None, + rows_id: int, ) -> None: """Initialize row.""" - super().__init__( - "Row", - player.game, - tile_seperation, - background, + super().__init__(f"Pattern_Rows_{rows_id}", background=None) + + self.add_component(sprite.DragClickEventComponent()) + + self.rows_id = rows_id + self.rows: dict[int, tuple[int, int]] = dict.fromkeys( + range(5), + (Tile.blank, 0), ) - self.player = player - self.size = int(size) - self.color = -6 - self.tiles = deque([Tile(self.color)] * self.size) + self.update_image() + self.visible = True def __repr__(self) -> str: """Return representation of self.""" - return f"{self.__class__.__name__}(%r, %i, ...)" % ( - self.game, - self.size, + return f"{self.__class__.__name__}({self.rows_id})" + + def bind_handlers(self) -> None: + """Register event handlers.""" + self.register_handlers( + { + "click": self.handle_click, + "game_pattern_current_turn_change": self.handle_game_pattern_current_turn_change, + "game_pattern_data": self.handle_game_pattern_data, + }, ) def update_image(self) -> None: """Update self.image.""" - self.clear_image((self.size, 1)) - - for x in range(len(self.tiles)): - self.render_tile(self.tiles[x], (x, 0)) + self.clear_image((5, 5)) - def get_tile_point(self, screen_location: tuple[int, int]) -> int | None: - """Return the xy choordinates of which tile intersects given a point. Returns None if no intersections.""" - # `Grid.get_tile_point` inlined - # Can't get tile if screen location doesn't intersect our hitbox! - if not self.point_intersects(screen_location): - return None - # Otherwise, find out where screen point is in image locations - # board x and y - bx, _by = self.to_image_surface_location(screen_location) - # Finally, return the full divides (no decimals) of xy location by self.tile_full. - - return self.size - 1 - int(bx // self.tile_full) + for y in range(5): + tile_color, count = self.rows[y] + for x in range(count, (y + 1)): + self.blit_tile(Tile.blank, (4 - x, y)) + for x in range(count): + self.blit_tile(tile_color, (4 - x, y)) + self.dirty = 1 - def get_placed(self) -> int: - """Return the number of tiles in self that are not fake tiles, like grey ones.""" - return len([tile for tile in self.tiles if tile.color >= 0]) + def set_row_data( + self, + row_id: int, + tile_color: Tile, + tile_count: int, + ) -> None: + """Set row data and update image.""" + assert row_id in self.rows + self.rows[row_id] = (tile_color, tile_count) + self.update_image() - def get_placeable(self) -> int: - """Return the number of tiles permitted to be placed on self.""" - return self.size - self.get_placed() + def get_tile_point( + self, + screen_location: tuple[int, int] | Vector2, + ) -> Vector2 | None: + """Return the x choordinate of which tile intersects given a point. Returns None if no intersections.""" + point = super().get_tile_point(screen_location) + if point is None: + return None + # If point is not valid for that row, say invalid + if (4 - point.x) > point.y: + return None + return point - def is_full(self) -> bool: - """Return True if this row is full.""" - return self.get_placed() == self.size + def set_background(self, color: tuple[int, int, int] | None) -> None: + """Set the background color for this row.""" + self.background = color + self.update_image() - def get_info(self, location: int) -> Tile | None: - """Return tile at location without deleting it. Return None on invalid location.""" - index = self.size - 1 - location - if index < 0 or index > len(self.tiles): - return None - return self.tiles[index] + async def handle_click( + self, + event: Event[sprite.PygameMouseButtonEventData], + ) -> None: + """Handle click event.""" + point = self.get_tile_point(event.data["pos"]) + if point is None: + await trio.lowlevel.checkpoint() + return - def can_place(self, tile: Tile) -> bool: - """Return True if permitted to place given tile object on self.""" - placeable = (tile.color == self.color) or ( - self.color < 0 and tile.color >= 0 - ) - if not placeable: - return False - color_correct = tile.color >= 0 and tile.color < 5 - if not color_correct: - return False - number_correct = self.get_placeable() > 0 - if not number_correct: - return False - - board = self.player.get_object_by_name("Board") - assert isinstance(board, Board) - # Is color not present? - return tile.color not in board.get_colors_in_row( - self.size - 1, + # Transmit to server + await self.raise_event( + Event( + "game_pattern_row_clicked", + ( + self.rows_id, + point.floored(), + ), + 2, + ), ) - def get_tile(self, replace: int = -6) -> Tile: - """Return the leftmost tile while deleting it from self.""" - self.tiles.appendleft(Tile(replace)) - self.image_update = True - return self.tiles.pop() - - def place_tile(self, tile: Tile) -> None: - """Place a given Tile Object on self if permitted.""" - if self.can_place(tile): - self.color = tile.color - self.tiles.append(tile) - end = self.tiles.popleft() - if not end.color < 0: - raise RuntimeError( - "Attempted deletion of real tile from Row!", - ) - self.image_update = True - else: - raise ValueError("Not allowed to place.") - - def can_place_tiles(self, tiles: list[Tile]) -> bool: - """Return True if permitted to place all of given tiles objects on self.""" - if len(tiles) > self.get_placeable(): - return False - for tile in tiles: - if not self.can_place(tile): - return False - tile_colors = [] - for tile in tiles: - if tile.color not in tile_colors: - tile_colors.append(tile.color) - return not len(tile_colors) > 1 - - def place_tiles(self, tiles: list[Tile]) -> None: - """Place multiple tile objects on self if permitted.""" - if self.can_place_tiles(tiles): - for tile in tiles: - self.place_tile(tile) + async def handle_game_pattern_current_turn_change( + self, + event: Event[int], + ) -> None: + """Handle game_pattern_current_turn_change event.""" + player_id = event.data + + if player_id == self.rows_id: + self.set_background(DARKGREEN) else: - raise ValueError("Not allowed to place tiles.") + self.set_background(None) - def wall_tile( + async def handle_game_pattern_data( self, - add_to_table: dict[str, list[Tile] | Tile | None], - empty_color: int = -6, + event: Event[tuple[int, int, tuple[int, int]]], ) -> None: - """Move tiles around and into add dictionary for the wall tiling phase of the game. Removes tiles from self.""" - if "tiles_for_box" not in add_to_table: - add_to_table["tiles_for_box"] = [] - if not self.is_full(): - add_to_table[str(self.size)] = None + """Handle game_pattern_data event.""" + player_id, row_id, (raw_tile_color, tile_count) = event.data + + if player_id != self.rows_id: + await trio.lowlevel.checkpoint() return - self.color = empty_color - add_to_table[str(self.size)] = self.get_tile() - for_box = add_to_table["tiles_for_box"] - assert isinstance(for_box, list) - for _i in range(self.size - 1): - for_box.append(self.get_tile()) + tile_color = Tile(raw_tile_color) + self.set_row_data(row_id, tile_color, tile_count) + await trio.lowlevel.checkpoint() - def set_background(self, color: tuple[int, int, int] | None) -> None: - """Set the background color for this row.""" - self.back = color - self.image_update = True +class FloorLine(TileRenderer): + """Represents a player's floor line.""" -class PatternLine(MultipartObject): - """Represents multiple rows to make the pattern line.""" + __slots__ = ("floor_line_id", "numbers") - __slots__ = ("player", "row_seperation") - size = (5, 5) + def __init__(self, floor_line_id: int, numbers: NDArray[int8]) -> None: + """Initialize floor line.""" + super().__init__( + f"floor_line_{floor_line_id}", + background=RED, + ) - def __init__(self, player: Player, row_seperation: int = 0) -> None: - """Initialize pattern line.""" - super().__init__("PatternLine") - self.player = player - self.row_seperation = row_seperation + self.add_component(sprite.DragClickEventComponent()) - for x, _y in zip( - range(self.size[0]), - range(self.size[1]), - strict=True, - ): - self.add_object(Row(self.player, x + 1)) - - self.set_background(None) - - self._lastloc = Vector2(0, 0) - - def set_background(self, color: tuple[int, int, int] | None) -> None: - """Set the background color for all rows in the pattern line.""" - self.set_attr_all("back", color) - self.set_attr_all("image_update", True) - - def get_row(self, row: int) -> Row: - """Return given row.""" - object_ = self.get_object(row) - assert isinstance(object_, Row) - return object_ - - def reset_position(self) -> None: - """Reset Locations of Rows according to self.location.""" - last = self.size[1] - w = self.get_row(last - 1).wh[0] - if w is None: - raise RuntimeError( - "Image Dimensions for Row Object (row.wh) are None!", - ) - h1 = self.get_row(0).tile_full - h = int(last * h1) - self.wh = w, h - w1 = h1 / 2 - - x, y = self.location - y -= h / 2 - w1 - for rid in self.objects: - row = self.get_row(rid) - diff = last - row.size - row.location = Vector2(x + (diff * w1), y + rid * h1) - - def get_tile_point( - self, - screen_location: tuple[int, int], - ) -> tuple[int, int] | None: - """Return the xy choordinates of which tile intersects given a point. Returns None if no intersections.""" - for y in range(self.size[1]): - x = self.get_row(y).get_tile_point(screen_location) - if x is not None: - return x, y - return None - - def is_full(self) -> bool: - """Return True if self is full.""" - return all(self.get_row(rid).is_full() for rid in range(self.size[1])) + self.floor_line_id = floor_line_id - def wall_tiling(self) -> dict[str, list[Tile] | Tile | None]: - """Return a dictionary to be used with wall tiling. Removes tiles from rows.""" - values: dict[str, list[Tile] | Tile | None] = {} - for rid in range(self.size[1]): - self.get_row(rid).wall_tile(values) - return values + self.numbers = tuple(numbers.flat) + self.tiles: Counter[Tile] = Counter() - def process(self, time_passed: float) -> None: - """Process all the rows that make up the pattern line.""" - if self.hidden != self._lasthidden: - self.set_attr_all("image_update", True) - super().process(time_passed) - - -class Text(Object): - """Text object, used to render text with a given font.""" - - __slots__ = ("_cxy", "_last", "font") - - def __init__( - self, - font_size: int, - color: tuple[int, int, int], - background: tuple[int, int, int] | None = None, - cx: bool = True, - cy: bool = True, - name: str = "", - ) -> None: - """Initialize text.""" - super().__init__(f"Text{name}") - self.font = Font( - FONT, - font_size, - color, - cx, - cy, - True, - background, - True, - ) - self._cxy = cx, cy - self._last: str | None = None - - def get_image_zero(self) -> tuple[int, int]: - """Return the screen location of the topleft point of self.image.""" - x = int(self.location[0]) - y = int(self.location[1]) - if self._cxy[0]: - x -= self.wh[0] // 2 - if self._cxy[1]: - y -= self.wh[1] // 2 - return x, y + self.update_image() + self.visible = True def __repr__(self) -> str: """Return representation of self.""" - return f"<{self.__class__.__name__} Object>" + return f"{self.__class__.__name__}({self.floor_line_id})" - @staticmethod - def get_font_height(font: str | Path, size: int) -> int: - """Return the height of font at fontsize size.""" - return pygame.font.Font(font, size).get_height() + def update_image(self) -> None: + """Update self.image.""" + self.clear_image((len(self.numbers), 1)) + + font = pygame.font.Font(FONT, size=self.tile_size) + + for x, tile in enumerate(sorted(self.tiles.elements(), reverse=True)): + self.blit_tile(tile, (x, 0)) + for x in range(self.tiles.total(), len(self.numbers)): + self.blit_tile(Tile.blank, (x, 0)) + # Draw number on top + number_surf = font.render(str(self.numbers[x]), False, BLACK) + tile_topleft = self.get_tile_topleft((x, 0)) + self.image.blit(number_surf, tile_topleft) + self.dirty = 1 + + def bind_handlers(self) -> None: + """Register event handlers.""" + self.register_handlers( + { + "game_floor_data": self.handle_game_floor_data, + "click": self.handle_click, + }, + ) - def update_value( + async def handle_game_floor_data( self, - text: str | None, - size: int | None = None, - color: tuple[int, int, int] | None = None, - background: tuple[int, int, int] | None = None, - ) -> pygame.surface.Surface: - """Return a surface of given text rendered in FONT.""" - self.image = self.font.render_nosurf(text, size, color, background) - return self.image - - def get_surface(self) -> pygame.surface.Surface: - """Return self.image.""" - assert self.image is not None - return self.image - - def get_tile_point(self, location: tuple[int, int]) -> None: - """Set get_tile_point attribute so that errors are not raised.""" - return + event: Event[tuple[int, Counter[u8]]], + ) -> None: + """Handle game_floor_data event.""" + line_id, floor_data = event.data - def process(self, time_passed: float) -> None: - """Process text.""" - if self.font.last_text != self._last: - self.update_value(self.font.last_text) - self._last = self.font.last_text + if line_id != self.floor_line_id: + await trio.lowlevel.checkpoint() + return + self.tiles.clear() + self.tiles.update({Tile(k): v for k, v in floor_data.items()}) + self.update_image() -class FloorLine(Row): - """Represents a player's floor line.""" + await trio.lowlevel.checkpoint() - size = 7 - number_one_color = NUMBERONETILE + async def handle_click( + self, + event: Event[sprite.PygameMouseButtonEventData], + ) -> None: + """Handle click event.""" + point = self.get_tile_point(event.data["pos"]) + if point is None: + await trio.lowlevel.checkpoint() + return - def __init__(self, player: Player) -> None: - """Initialize floor line.""" - super().__init__(player, self.size, background=ORANGE) - self.name = "floor_line" - - # self.font = Font(FONT, round(self.tile_size*1.2), color=BLACK, cx=False, cy=False) - self.text = Text( - round(self.tile_size * 1.2), - BLACK, - cx=False, - cy=False, + # Transmit to server + await self.raise_event( + Event( + "game_floor_clicked", + ( + self.floor_line_id, + int(point.floored().x), + ), + 2, + ), ) - self.has_number_one_tile = False - - gen = floor_line_subtract_generator(1) - self.numbers = [next(gen) for i in range(self.size)] - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}({self.player!r})" - def render(self, surface: pygame.surface.Surface) -> None: - """Update self.image.""" - super().render(surface) - - sx, sy = self.location - assert self.wh is not None, "Should be impossible." - w, h = self.wh - for x in range(self.size): - xy = round( - x * self.tile_full + self.tile_seperation + sx - w / 2, - ), round( - self.tile_seperation + sy - h / 2, - ) - self.text.update_value(str(self.numbers[x])) - self.text.location = Vector2(*xy) - self.text.render(surface) - - # self.font.render(surface, str(self.numbers[x]), xy) - - def place_tile(self, tile: Tile) -> None: - """Place a given Tile Object on self if permitted.""" - self.tiles.insert(self.get_placed(), tile) - - if tile.color == self.number_one_color: - self.has_number_one_tile = True - - box_lid = self.player.game.get_object_by_name("BoxLid") - assert isinstance(box_lid, BoxLid) - - def handle_end(end: Tile) -> None: - """Handle the end tile we are replacing. Ensures number one tile is not removed.""" - if not end.color < 0: - if end.color == self.number_one_color: - handle_end(self.tiles.pop()) - self.tiles.appendleft(end) - return - box_lid.add_tile(end) - - handle_end(self.tiles.pop()) - - self.image_update = True - - def score_tiles(self) -> int: - """Score self.tiles and return how to change points.""" - running_total = 0 - for x in range(self.size): - if self.tiles[x].color >= 0: - running_total += self.numbers[x] - elif x < self.size - 1 and self.tiles[x + 1].color >= 0: - raise RuntimeError( - "Player is likely cheating! Invalid placement of floor_line tiles!", - ) - return running_total - - def get_tiles( - self, - empty_color: int = -6, - ) -> tuple[list[Tile], Tile | None]: - """Return tuple of tiles gathered, and then either the number one tile or None.""" - tiles = [] - number_one_tile = None - for tile in (self.tiles.pop() for i in range(len(self.tiles))): - if tile.color == self.number_one_color: - number_one_tile = tile - self.has_number_one_tile = False - elif tile.color >= 0: - tiles.append(tile) - - for _i in range(self.size): - self.tiles.append(Tile(empty_color)) - self.image_update = True - return tiles, number_one_tile - - def can_place_tiles(self, tiles: list[Tile]) -> bool: - """Return True.""" - return True - - -class Factory(Grid): +class Factory(TileRenderer): """Represents a Factory.""" - size = (2, 2) + __slots__ = ("factory_id", "tiles") color = WHITE outline = BLUE - out_size = 0.1 - def __init__(self, game: Game, factory_id: int) -> None: + def __init__(self, factory_id: int) -> None: """Initialize factory.""" - super().__init__(self.size, game, background=None) - self.number = factory_id - self.name = f"Factory{self.number}" + super().__init__(f"Factory_{factory_id}", background=None) - self.radius = math.ceil( - self.tile_full * self.size[0] * self.size[1] / 3 + 3, - ) + self.factory_id = factory_id + self.tiles: Counter[int] = Counter() + + self.update_location_on_resize = True + + self.add_component(sprite.DragClickEventComponent()) def __repr__(self) -> str: """Return representation of self.""" - return f"{self.__class__.__name__}(%r, %i)" % (self.game, self.number) - - def add_circle(self, surface: pygame.surface.Surface) -> None: - """Add circle to self.image.""" - # if f"FactoryCircle{self.radius}" not in self.game.cache: - rad = math.ceil(self.radius) - surf = set_alpha(pygame.surface.Surface((2 * rad, 2 * rad)), 1) - pygame.draw.circle(surf, self.outline, (rad, rad), rad) - pygame.draw.circle( - surf, - self.color, - (rad, rad), - math.ceil(rad * (1 - self.out_size)), - ) - # self.game.cache[f"FactoryCircle{self.radius}"] = surf - # surf = self.game.cache[f"FactoryCircle{self.radius}"].copy() - surface.blit( - surf, - ( - round(self.location[0] - self.radius), - round(self.location[1] - self.radius), - ), + return f"{self.__class__.__name__}({self.factory_id})" + + def bind_handlers(self) -> None: + """Register event handlers.""" + self.register_handlers( + { + "game_factory_data": self.handle_factory_data, + "click": self.handle_click, + }, ) - def render(self, surface: pygame.surface.Surface) -> None: - """Render Factory.""" - if not self.hidden: - self.add_circle(surface) - super().render(surface) - - def fill(self, tiles: list[Tile]) -> None: - """Fill self with tiles. Will raise exception if insufficiant tiles.""" - if len(tiles) < self.size[0] * self.size[1]: - size = self.size[0] * self.size[1] - raise RuntimeError( - f"Insufficiant quantity of tiles! Needs {size}!", - ) - for y in range(self.size[1]): - for tile, x in zip( - (tiles.pop() for i in range(self.size[0])), - range(self.size[0]), - strict=True, - ): - self.place_tile((x, y), tile) - if tiles: - raise RuntimeError("Too many tiles!") - - def grab(self) -> list[Tile]: - """Return all tiles on this factory.""" - return [ - tile - for tile in ( - self.get_tile((x, y), -6) - for x in range(self.size[0]) - for y in range(self.size[1]) - ) - if tile is not None and tile.color != -6 - ] - - def grab_color(self, color: int) -> tuple[list[Tile], list[Tile]]: - """Return all tiles of color given in the first list, and all non-matches in the second list.""" - tiles = self.grab() - right, wrong = [], [] - for tile in tiles: - if tile.color == color: - right.append(tile) - else: - wrong.append(tile) - return right, wrong - - def process(self, time_passed: float) -> None: - """Process self.""" - if self.image_update: - self.radius = int( - self.tile_full * self.size[0] * self.size[1] // 3 + 3, - ) - super().process(time_passed) + def update_image(self) -> None: + """Update image.""" + self.clear_image((2, 2), extra=(16, 16)) + radius = 29 + pygame.draw.circle( + self.image, + self.outline, + (radius, radius), + radius, + ) + pygame.draw.circle( + self.image, + self.color, + (radius, radius), + math.ceil(radius * 0.9), + ) -class Factories(MultipartObject): - """Factories Multipart Object, made of multiple Factory Objects.""" + for index, tile_color in enumerate(self.tiles.elements()): + y, x = divmod(index, 2) + self.blit_tile(tile_color, (x, y), (8, 8)) + self.dirty = 1 - teach = 4 + def get_tile_point( + self, + screen_location: tuple[int, int] | Vector2, + ) -> Vector2 | None: + """Get tile point accounting for offset.""" + point = super().get_tile_point( + Vector2.from_iter(screen_location) - (8, 8), + ) + if point is None: + return None + if any(x >= 2 for x in point): + return None + return point - def __init__( + async def handle_factory_data( self, - game: Game, - factories: int, - size: int | None = None, + event: Event[tuple[int, Counter[int]]], ) -> None: - """Initialize factories.""" - super().__init__("Factories") + """Handle `game_factory_data` event.""" + factory_id, tiles = event.data - self.game = game - self.count = factories - - for i in range(self.count): - self.add_object(Factory(self.game, i)) - - if size is None: - factory = self.objects[0] - assert isinstance(factory, Factory) - factory.process(0) - rad = factory.radius - self.size = rad * 5 - else: - self.size = size - self.size = math.ceil(self.size) + if factory_id != self.factory_id: + await trio.lowlevel.checkpoint() + return - self.play_tiles_from_bag() + self.tiles = tiles + self.update_image() + self.visible = True - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}(%r, %i, ...)" % ( - self.game, - self.count, - ) + await trio.lowlevel.checkpoint() - def reset_position(self) -> None: - """Reset the position of all factories within.""" - degrees = 360 / self.count - for i in range(self.count): - radians = math.radians(degrees * i) - self.objects[i].location = Vector2( - math.sin(radians) * self.size + self.location[0], - math.cos(radians) * self.size + self.location[1], - ) - - def process(self, time_passed: float) -> None: - """Process factories. Does not react to cursor if hidden.""" - super().process(time_passed) - if self.hidden: - return - cursor = self.game.get_object_by_name("Cursor") - assert isinstance(cursor, Cursor) - if not cursor.is_pressed() or cursor.is_holding(): - return - obj, point = self.get_intersection(cursor.location) - if obj is None or point is None: + async def handle_click( + self, + event: Event[sprite.PygameMouseButtonEventData], + ) -> None: + """Handle click event.""" + point = self.get_tile_point(event.data["pos"]) + if point is None: + await trio.lowlevel.checkpoint() return - oid = int(obj[7:]) - factory = self.objects[oid] - assert isinstance(factory, Factory) - - tile_at_point = factory.get_info(point) - if tile_at_point is None or tile_at_point.color < 0: + index = int(point.y * 2 + point.x) + tiles = tuple(self.tiles.elements()) + if not tiles: + await trio.lowlevel.checkpoint() return - table = self.game.get_object_by_name("TableCenter") - assert isinstance(table, TableCenter) - select, tocenter = factory.grab_color( - tile_at_point.color, - ) - if tocenter: - table.add_tiles(tocenter) - cursor.drag(select) - - def play_tiles_from_bag(self, empty_color: int = -6) -> None: - """Divy up tiles to each factory from the bag.""" - # For every factory we have, - for fid in range(self.count): - # Draw tiles for the factory - drawn = [] - for _i in range(self.teach): - # If the bag is not empty, - if not self.game.bag.is_empty(): - # Draw a tile from the bag. - tile = self.game.bag.draw_tile() - assert tile is not None - drawn.append(tile) - else: # Otherwise, get the box lid - box_lid = self.game.get_object_by_name("BoxLid") - assert isinstance(box_lid, BoxLid) - # If the box lid is not empty, - if not box_lid.is_empty(): - # Add all the tiles from the box lid to the bag - self.game.bag.add_tiles(box_lid.get_tiles()) - # and shake the bag to randomize everything - self.game.bag.reset() - # Then, grab a tile from the bag like usual. - tile = self.game.bag.draw_tile() - assert tile is not None - drawn.append(tile) - else: - # "In the rare case that you run out of tiles again - # while there are none left in the lid, start a new - # round as usual even though are not all factory - # displays are properly filled." - drawn.append(Tile(empty_color)) - # Place drawn tiles on factory - factory = self.objects[fid] - assert isinstance(factory, Factory) - factory.fill(drawn) - - def is_all_empty(self) -> bool: - """Return True if all factories are empty.""" - for fid in range(self.count): - factory = self.objects[fid] - assert isinstance(factory, Factory) - if not factory.is_empty(): - return False - return True - - -class TableCenter(Grid): - """Object that represents the center of the table.""" - - size = (6, 6) - first_tile_color = NUMBERONETILE - - def __init__(self, game: Game, has_number_one_tile: bool = True) -> None: - """Initialize center of table.""" - super().__init__(self.size, game, background=None) - self.game = game - self.name = "TableCenter" - - self.number_one_tile_exists = False - if has_number_one_tile: - self.add_number_one_tile() - - self.next_position = (0, 0) + tile_color = tiles[index] - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}({self.game!r})" - - def add_number_one_tile(self) -> None: - """Add the number one tile to the internal grid.""" - if not self.number_one_tile_exists: - x, y = self.size - self.place_tile((x - 1, y - 1), Tile(self.first_tile_color)) - self.number_one_tile_exists = True - - def add_tile(self, tile: Tile) -> None: - """Add a Tile Object to the Table Center Grid.""" - self.place_tile(self.next_position, tile) - x, y = self.next_position - x += 1 - y += int(x // self.size[0]) - x %= self.size[0] - y %= self.size[1] - self.next_position = (x, y) - self.image_update = True - - def add_tiles(self, tiles: Iterable[Tile], sort: bool = True) -> None: - """Add multiple Tile Objects to the Table Center Grid.""" - for tile in tiles: - self.add_tile(tile) - if sort and tiles: - self.reorder_tiles() - - def reorder_tiles(self, replace: int = -6) -> None: - """Re-organize tiles by Color.""" - full = [] - for y in range(self.size[1]): - for x in range(self.size[0]): - if self.number_one_tile_exists: - tile = self.get_info((x, y)) - assert tile is not None - if tile.color == self.first_tile_color: - continue - at = self.get_tile((x, y), replace) - - if at is not None: - full.append(at) - sorted_tiles = sorted(full, key=sort_tiles) - self.next_position = (0, 0) - self.add_tiles(sorted_tiles, False) - - def pull_tiles(self, tile_color: int, replace: int = -6) -> list[Tile]: - """Remove all of the tiles of tile_color from the Table Center Grid.""" - to_pull: list[tuple[int, int]] = [] - for y in range(self.size[1]): - for x in range(self.size[0]): - info_tile = self.get_info((x, y)) - assert info_tile is not None - if info_tile.color == tile_color: - to_pull.append((x, y)) - elif ( - self.number_one_tile_exists - and info_tile.color == self.first_tile_color - ): - to_pull.append((x, y)) - self.number_one_tile_exists = False - tiles = [] - for pos in to_pull: - tile = self.get_tile(pos, replace) - assert tile is not None - tiles.append(tile) - self.reorder_tiles(replace) - return tiles - - def process(self, time_passed: float) -> None: - """Process factories.""" - if self.hidden: - super().process(time_passed) + if tile_color < 0: + # Do not send non-real tiles + await trio.lowlevel.checkpoint() return - cursor = self.game.get_object_by_name("Cursor") - assert isinstance(cursor, Cursor) - if ( - cursor.is_pressed() - and not cursor.is_holding() - and not self.is_empty() - and self.point_intersects(cursor.location) - ): - point = self.get_tile_point(cursor.location) - # Shouldn't return none anymore since we have point_intersects now. - assert point is not None - tile = self.get_info(point) - assert isinstance(tile, Tile) - color_at_point = tile.color - if color_at_point >= 0 and color_at_point < 5: - cursor.drag(self.pull_tiles(color_at_point)) - super().process(time_passed) - - -class Bag: - """Represents the bag full of tiles.""" - - __slots__ = ( - "percent_each", - "tile_count", - "tile_names", - "tile_types", - "tiles", - ) - def __init__(self, tile_count: int = 100, tile_types: int = 5) -> None: - """Initialize bag of tiles.""" - self.tile_count = int(tile_count) - self.tile_types = int(tile_types) - self.tile_names = [chr(65 + i) for i in range(self.tile_types)] - self.percent_each = (self.tile_count / self.tile_types) / 100 - self.tiles: deque[str] - self.full_reset() - - def full_reset(self) -> None: - """Reset the bag to a full, re-randomized bag.""" - self.tiles = deque( - gen_random_proper_seq( - self.tile_count, - **dict.fromkeys(self.tile_names, self.percent_each), + # Transmit to server + # Needs level 2 to reach server client + await self.raise_event( + Event( + "game_factory_clicked", + ( + self.factory_id, + Tile(tile_color), + ), + 2, ), ) - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}(%i, %i)" % ( - self.tile_count, - self.tile_types, - ) - - def reset(self) -> None: - """Randomize all the tiles in the bag.""" - self.tiles = deque(randomize(self.tiles)) - - def get_color(self, tile_name: str) -> int: - """Return the color of a named tile.""" - if tile_name not in self.tile_names: - raise ValueError(f"Tile Name {tile_name} Not Found!") - return self.tile_names.index(tile_name) - - def get_tile(self, tile_name: str) -> Tile: - """Return a Tile Object from a tile name.""" - return Tile(self.get_color(tile_name)) - - def get_count(self) -> int: - """Return number of tiles currently held.""" - return len(self.tiles) - - def is_empty(self) -> bool: - """Return True if no tiles are currently held.""" - return self.get_count() == 0 - def draw_tile(self) -> Tile | None: - """Return a random Tile Object from the bag. Return None if no tiles to draw.""" - if not self.is_empty(): - return self.get_tile(self.tiles.pop()) - return None - - def get_name(self, tile_color: int) -> str: - """Return the name of a tile given it's color.""" - try: - return self.tile_names[tile_color] - except IndexError as exc: - raise ValueError("Invalid Tile Color!") from exc - - def add_tile(self, tile_object: Tile) -> None: - """Add a Tile Object to the bag.""" - name = self.get_name(int(tile_object.color)) - range_ = (0, len(self.tiles) - 1) - if range_[1] - range_[0] <= 1: - index = 0 - else: - # S311 Standard pseudo-random generators are not suitable for cryptographic purposes - index = random.randint(range_[0], range_[1]) # noqa: S311 - # self.tiles.insert(random.randint(0, len(self.tiles)-1), self.get_name(int(tile_object.color))) - self.tiles.insert(index, name) - del tile_object +class TableCenter(TileRenderer): + """sprite.Sprite that represents the center of the table.""" - def add_tiles(self, tile_objects: Iterable[Tile]) -> None: - """Add multiple Tile Objects to the bag.""" - for tile_object in tile_objects: - self.add_tile(tile_object) + __slots__ = ("tiles",) + size = (6, 6) + def __init__(self) -> None: + """Initialize center of table.""" + super().__init__("TableCenter", background=None) -class BoxLid(Object): - """BoxLid Object, represents the box lid were tiles go before being added to the bag again.""" + self.tiles: Counter[int] = Counter() + self.update_image() + self.visible = True - def __init__(self, game: Game) -> None: - """Initialize box lid.""" - super().__init__("BoxLid") - self.game = game - self.tiles: deque[Tile] = deque() + self.add_component(sprite.DragClickEventComponent()) def __repr__(self) -> str: """Return representation of self.""" - return f"{self.__class__.__name__}({self.game!r})" + return f"{self.__class__.__name__}()" - def add_tile(self, tile: Tile) -> None: - """Add a tile to self.""" - if tile.color >= 0 and tile.color < 5: - self.tiles.append(tile) - return - raise ValueError( - f"BoxLid.add_tile tried to add an invalid tile to self ({tile.color = }).", + def bind_handlers(self) -> None: + """Register event handlers.""" + self.register_handlers( + { + "game_table_data": self.update_board_data, + "click": self.handle_click, + }, ) - def add_tiles(self, tiles: Iterable[Tile]) -> None: - """Add multiple tiles to self.""" - for tile in tiles: - self.add_tile(tile) - - def get_tiles(self) -> list[Tile]: - """Return all tiles in self while deleting them from self.""" - return [self.tiles.popleft() for i in range(len(self.tiles))] + def iter_tiles(self) -> Generator[int, None, None]: + """Yield tile colors.""" + count = 0 + for tile_type in sorted(set(self.tiles) - {Tile.one}): + tile_count = self.tiles[tile_type] + for _ in range(tile_count): + yield tile_type + count += 1 - def is_empty(self) -> bool: - """Return True if self is empty (no tiles on it).""" - return len(self.tiles) == 0 + width, height = self.size + remaining = width * height - count + one_count = self.tiles.get(Tile.one, 0) + remaining = max(remaining - one_count, 0) + for _ in range(remaining): + yield Tile.blank + for _ in range(one_count): + yield Tile.one -class Player(MultipartObject): - """Represents a player. Made of lots of objects.""" + def update_image(self) -> None: + """Reset/update image.""" + self.clear_image(self.size) - def __init__( + width, height = self.size + tile_generator = self.iter_tiles() + for y in range(height): + for x in range(width): + tile = next(tile_generator) + # if tile == Tile.blank: + # continue + self.blit_tile(tile, (x, y)) + self.dirty = 1 + + def add_tile(self, tile: int) -> None: + """Add a tile to the center of the table.""" + self.tiles.update((tile,)) + self.update_image() + + def add_tiles(self, tiles: Iterable[int]) -> None: + """Add multiple int Objects to the Table Center Grid.""" + self.tiles.update(tiles) + self.update_image() + + def pull_tiles(self, tile_color: int) -> list[int]: + """Pop all of tile_color. Raises KeyError if not exists.""" + tile_count = self.tiles.pop(tile_color) + return [tile_color] * tile_count + + async def update_board_data(self, event: Event[Counter[int]]) -> None: + """Update table center board data.""" + self.tiles = event.data + self.update_image() + await trio.lowlevel.checkpoint() + + async def handle_click( self, - game: Game, - player_id: int, - networked: bool = False, - varient_play: bool = False, + event: Event[sprite.PygameMouseButtonEventData], ) -> None: - """Initialize player.""" - super().__init__(f"Player{player_id}") + """Handle click event.""" + point = self.get_tile_point(event.data["pos"]) + if point is None: + await trio.lowlevel.checkpoint() + return - self.game = game - self.player_id = player_id - self.networked = networked - self.varient_play = varient_play + index = int(point.y * 6 + point.x) + tile_color = tuple(self.iter_tiles())[index] - self.add_object(Board(self, self.varient_play)) - self.add_object(PatternLine(self)) - self.add_object(FloorLine(self)) - self.add_object(Text(SCOREFONTSIZE, SCORECOLOR)) + if tile_color < 0: + # Do not send non-real tiles + await trio.lowlevel.checkpoint() + return - self.score = 0 - self.is_turn = False - self.is_wall_tiling = False - self.just_held = False - self.just_dropped = False + # Transmit to server + # Needs level 2 to reach server client + await self.raise_event( + Event( + "game_table_clicked", + Tile(tile_color), + 2, + ), + ) - self.update_score() - self._lastloc = Vector2(0, 0) +class HaltState(AsyncState["AzulClient"]): + """Halt state to set state to None so running becomes False.""" - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}(%r, %i, %s, %s)" % ( - self.game, - self.player_id, - self.networked, - self.varient_play, - ) + __slots__ = () - def update_score(self) -> None: - """Update the scorebox for this player.""" - score_box = self.get_object_by_name("Text") - assert isinstance(score_box, Text) - score_box.update_value(f"Player {self.player_id + 1}: {self.score}") - - def trigger_turn_now(self) -> None: - """Handle start of turn.""" - if not self.is_turn: - pattern_line = self.get_object_by_name("PatternLine") - assert isinstance(pattern_line, PatternLine) - if self.is_wall_tiling: - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - rows = board.get_rows_to_tile_map() - for rowpos, value in rows.items(): - color = get_tile_color(value, board.greyshift) - assert isinstance(color[0], int) - pattern_line.get_row(rowpos).set_background( - color, - ) - else: - pattern_line.set_background(PATSELECTCOLOR) - self.is_turn = True - - def end_of_turn(self) -> None: - """Handle end of turn.""" - if self.is_turn: - pattern_line = self.get_object_by_name("PatternLine") - assert isinstance(pattern_line, PatternLine) - pattern_line.set_background(None) - self.is_turn = False - - def end_of_game_trigger(self) -> None: - """Handle end of game. - - Called by end state when game is over - Hide pattern lines and floor line. - """ - pattern = self.get_object_by_name("PatternLine") - floor = self.get_object_by_name("floor_line") - - pattern.hidden = True - floor.hidden = True - - def reset_position(self) -> None: - """Reset positions of all parts of self based off self.location.""" - x, y = self.location - - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - bw, bh = board.wh - board.location = Vector2(x + bw // 2, y) - - pattern_line = self.get_object_by_name("PatternLine") - assert isinstance(pattern_line, PatternLine) - lw = pattern_line.wh[0] // 2 - pattern_line.location = Vector2(x - lw, y) - - floor_line = self.get_object_by_name("floor_line") - assert isinstance(floor_line, FloorLine) - floor_line.wh[0] - floor_line.location = Vector2( - int(x - lw * (2 / 3) + TILESIZE / 3.75), - int(y + bh * (2 / 3)), - ) + def __init__(self) -> None: + """Initialize Halt State.""" + super().__init__("Halt") - text = self.get_object_by_name("Text") - assert isinstance(text, Text) - text.location = Vector2(x - (bw // 3), y - (bh * 2 // 3)) - - def wall_tiling(self) -> None: - """Do the wall tiling phase of the game for this player.""" - self.is_wall_tiling = True - pattern_line = self.get_object_by_name("PatternLine") - assert isinstance(pattern_line, PatternLine) - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - box_lid = self.game.get_object_by_name("BoxLid") - assert isinstance(box_lid, BoxLid) - - data = pattern_line.wall_tiling() - tiles_for_box = data["tiles_for_box"] - assert isinstance(tiles_for_box, list) - box_lid.add_tiles(tiles_for_box) - del data["tiles_for_box"] - - cleaned = {} - for key, value in data.items(): - if not isinstance(value, Tile): - continue - cleaned[int(key)] = value - - board.wall_tiling_mode(cleaned) - - def done_wall_tiling(self) -> bool: - """Return True if internal Board is done wall tiling.""" - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - return not board.is_wall_tiling() - - def next_round(self) -> None: - """Handle end of wall tiling.""" - self.is_wall_tiling = False - - def score_phase(self) -> Tile | None: - """Do the scoring phase of the game for this player. Return number one tile or None.""" - board = self.get_object_by_name("Board") - floor_line = self.get_object_by_name("floor_line") - box_lid = self.game.get_object_by_name("BoxLid") - assert isinstance(board, Board) - assert isinstance(floor_line, FloorLine) - assert isinstance(box_lid, BoxLid) - - def saturatescore() -> None: - if self.score < 0: - self.score = 0 - - self.score += board.score_additions() - self.score += floor_line.score_tiles() - saturatescore() - - tiles_for_box, number_one = floor_line.get_tiles() - box_lid.add_tiles(tiles_for_box) - - self.update_score() - - return number_one - - def end_of_game_scoring(self) -> None: - """Update final score with additional end of game points.""" - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - - self.score += board.end_of_game_scoreing() - - self.update_score() - - def has_horzontal_line(self) -> bool: - """Return True if this player has a horizontal line on their game board filled.""" - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - - return board.has_filled_row() - - def get_horizontal_lines(self) -> int: - """Return the number of filled horizontal lines this player has on their game board.""" - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - - return board.get_filled_rows() - - def process(self, time_passed: float) -> None: - """Process Player.""" - if not self.is_turn: # Is our turn? - self.set_attr_all("hidden", self.hidden) - super().process(time_passed) - return - if self.hidden and self.is_wall_tiling and self.varient_play: - # If hidden, not anymore. Our turn. - self.hidden = False - if self.networked: # We are networked. - self.set_attr_all("hidden", self.hidden) - super().process(time_passed) - return + async def check_conditions(self) -> None: + """Set active state to None.""" + assert self.machine is not None + await self.machine.set_state(None) - cursor = self.game.get_object_by_name("Cursor") - assert isinstance(cursor, Cursor) - box_lid = self.game.get_object_by_name("BoxLid") - assert isinstance(box_lid, BoxLid) - pattern_line = self.get_object_by_name("PatternLine") - assert isinstance(pattern_line, PatternLine) - floor_line = self.get_object_by_name("floor_line") - assert isinstance(floor_line, FloorLine) - board = self.get_object_by_name("Board") - assert isinstance(board, Board) - - if not cursor.is_pressed(): - # Mouse up - if self.just_held: - self.just_held = False - if self.just_dropped: - self.just_dropped = False - self.set_attr_all("hidden", self.hidden) - super().process(time_passed) - return - # Mouse down - obj, point = self.get_intersection(cursor.location) - if obj is None or point is None: - if self.is_wall_tiling and self.done_wall_tiling(): - self.next_round() - self.game.next_turn() - self.set_attr_all("hidden", self.hidden) - super().process(time_passed) - return - # Something pressed - if cursor.is_holding(): # Cursor holding tiles - move_made = False - if not self.is_wall_tiling: # Is wall tiling: - if obj == "PatternLine": - pos, row_number = point - row = pattern_line.get_row(row_number) - if not row.is_full(): - info = row.get_info(pos) - if info is not None and info.color < 0: - _color, _held = cursor.get_held_info() - todrop = min( - pos + 1, - row.get_placeable(), - ) - tiles = cursor.drop(todrop) - if row.can_place_tiles(tiles): - row.place_tiles(tiles) - move_made = True - else: - cursor.force_hold(tiles) - elif obj == "floor_line": - tiles_to_add = cursor.drop() - if floor_line.is_full(): - # Floor is full, - # Add tiles to box instead. - box_lid.add_tiles(tiles_to_add) - elif floor_line.get_placeable() < len( - tiles_to_add, - ): - # Floor is not full but cannot fit all in floor line. - # Add tiles to floor line and then to box - while len(tiles_to_add) > 0: - if floor_line.get_placeable() > 0: - floor_line.place_tile( - tiles_to_add.pop(), - ) - else: - box_lid.add_tile( - tiles_to_add.pop(), - ) - else: - # Otherwise add to floor line for all. - floor_line.place_tiles(tiles_to_add) - move_made = True - elif not self.just_held and obj == "Board": - tile = board.get_info(point) - assert isinstance(tile, Tile) - if tile.color == -6: - # Cursor holding and wall tiling - _column, row_id = point - cursor_tile = cursor.drop(1)[0] - board_tile = board.get_tile_for_cursor_by_row( - row_id, - ) - if ( - board_tile is not None - and cursor_tile.color == board_tile.color - and board.wall_tile_from_point(point) - ): - self.just_dropped = True - pattern_line.get_row( - row_id, - ).set_background(None) - if move_made and not self.is_wall_tiling: - if cursor.holding_number_one: - one_tile = cursor.drop_one_tile() - assert one_tile is not None - floor_line.place_tile(one_tile) - if cursor.get_held_count(True) == 0: - self.game.next_turn() - elif self.is_wall_tiling and obj == "Board" and not self.just_dropped: - # Mouse down, something pressed, and not holding anything - # Wall tiling, pressed, not holding - _column_number, row_number = point - tile = board.get_tile_for_cursor_by_row( - row_number, - ) - if tile is not None: - cursor.drag([tile]) - self.just_held = True - if self.is_wall_tiling and self.done_wall_tiling(): - self.next_round() - self.game.next_turn() - self.set_attr_all("hidden", self.hidden) - super().process(time_passed) +class GameState(AsyncState["AzulClient"]): + """Checkers Game Asynchronous State base class.""" + __slots__ = ("id", "manager") -class Button(Text): - """Button Object.""" + def __init__(self, name: str) -> None: + """Initialize Game State.""" + super().__init__(name) - textcolor = BUTTONTEXTCOLOR - backcolor = BUTTONBACKCOLOR + self.id: int = 0 + self.manager = ComponentManager(self.name) + + def add_actions(self) -> None: + """Add internal component manager to state machine's component manager.""" + assert self.machine is not None + self.machine.manager.add_component(self.manager) + + def group_add(self, new_sprite: sprite.Sprite) -> None: + """Add new sprite to state machine's group.""" + assert self.machine is not None + group = self.machine.get_group(self.id) + assert group is not None, "Expected group from new group id" + group.add(new_sprite) + self.manager.add_component(new_sprite) + + async def exit_actions(self) -> None: + """Remove group and unbind all components.""" + assert self.machine is not None + self.machine.remove_group(self.id) + self.manager.unbind_components() + self.id = 0 - def __init__( + def change_state( self, - state: MenuState, - name: str, - minimum_size: int = 10, - initial_value: str = "", - font_size: int = BUTTONFONTSIZE, - ) -> None: - """Initialize button.""" - super().__init__(font_size, self.textcolor, background=None) - self.name = f"Button{name}" - self.state = state + new_state: str | None, + ) -> Callable[[Event[Any]], Awaitable[None]]: + """Return an async function that will change state to `new_state`.""" - self.minsize = int(minimum_size) - self.update_value(initial_value) + async def set_state(*args: object, **kwargs: object) -> None: + play_sound("button_click") + await self.machine.set_state(new_state) - self.borderWidth = math.floor(font_size / 12) # 5 + return set_state - self.delay = 0.6 - self.cur_time = 1.0 - self.action: Callable[[], None] = lambda: None +class KwargOutlineText(objects.OutlinedText): + """Outlined objects.Text with attributes settable via keyword arguments.""" - def __repr__(self) -> str: - """Return representation of self.""" - return f"Button({self.name[6:]}, {self.state}, {self.minsize}, {self.font.last_text}, {self.font.pyfont})" - - def get_height(self) -> int: - """Return font height.""" - return self.font.get_height() - - def bind_action(self, function: Callable[[], None]) -> None: - """When self is pressed, call given function exactly once. Function takes no arguments.""" - self.action = function + __slots__ = () - def update_value( + def __init__( self, - text: str | None, - size: int | None = None, - color: tuple[int, int, int] | None = None, - background: tuple[int, int, int] | None = None, - ) -> pygame.surface.Surface: - """Update button text.""" - disp = str(text or "").center(self.minsize) - surface = super().update_value(f" {disp} ", size, color, background) - self.font.last_text = disp - return surface - - def render(self, surface: pygame.surface.Surface) -> None: - """Render button.""" - if not self.hidden: - text_rect = self.get_rect() - # if PYGAME_VERSION < 201: - # pygame.draw.rect(surface, self.backcolor, text_rect) - # pygame.draw.rect(surface, BLACK, text_rect, self.borderWidth) - # else: - pygame.draw.rect( - surface, - self.backcolor, - text_rect, - border_radius=20, - ) - pygame.draw.rect( - surface, - BLACK, - text_rect, - width=self.borderWidth, - border_radius=20, - ) - super().render(surface) - - def is_pressed(self) -> bool: - """Return True if this button is pressed.""" - assert self.state.game is not None - cursor = self.state.game.get_object_by_name("Cursor") - assert isinstance(cursor, Cursor) - return ( - not self.hidden - and cursor.is_pressed() - and self.point_intersects(cursor.location) - ) - - def process(self, time_passed: float) -> None: - """Call self.action one time when pressed, then wait self.delay to call again.""" - if self.cur_time > 0: - self.cur_time = max(self.cur_time - time_passed, 0) - elif self.is_pressed(): - self.action() - self.cur_time = self.delay - if self.font.last_text != self._last: - self.textSize = self.font.pyfont.size(f" {self.font.last_text} ") - super().process(time_passed) - + name: str, + font: pygame.font.Font, + **kwargs: object, + ) -> None: + """Initialize attributes via keyword arguments.""" + super().__init__(name, font) -class GameState: - """Base class for all game states.""" + for key, value in kwargs.items(): + setattr(self, key, value) - __slots__ = ("game", "name") - def __init__(self, name: str) -> None: - """Initialize state with a name, set self.game to None to be overwritten later.""" - self.game: Game | None = None - self.name = name +class KwargButton(objects.Button): + """objects.Button with attributes settable via keyword arguments.""" - def __repr__(self) -> str: - """Return representation of self.""" - return f"<{self.__class__.__name__} {self.name}>" - - def entry_actions(self) -> None: - """Perform entry actions for this GameState.""" - - def do_actions(self) -> None: - """Perform actions for this GameState.""" + __slots__ = () - def check_state(self) -> str | None: - """Check state and return new state. None remains in current state.""" - return None + def __init__( + self, + name: str, + font: pygame.font.Font, + **kwargs: object, + ) -> None: + """Initialize attributes via keyword arguments.""" + super().__init__(name, font) - def exit_actions(self) -> None: - """Perform exit actions for this GameState.""" + for key, value in kwargs.items(): + setattr(self, key, value) class MenuState(GameState): @@ -2815,11 +1436,8 @@ class MenuState(GameState): fontsize = BUTTONFONTSIZE def __init__(self, name: str) -> None: - """Initialize GameState and set up self.bh.""" + """Initialize GameState and set up 30.""" super().__init__(name) - self.bh = Text.get_font_height(FONT, self.fontsize) - - self.next_state: str | None = None def add_button( self, @@ -2829,54 +1447,51 @@ def add_button( location: tuple[int, int] | None = None, size: int = fontsize, minlen: int = button_minimum, - ) -> int: - """Add a new Button object to self.game with arguments. Return button id.""" - button = Button(self, name, minlen, value, size) - button.bind_action(action) - if location is not None: - button.location = Vector2(*location) - assert self.game is not None - self.game.add_object(button) - return button.id + ) -> None: + """Add a new objects.Button object to group.""" + button = KwargButton( + name, + font=pygame.font.Font(FONT, size), + visible=True, + color=Color(0, 0, 0), + text=value, + location=location, + handle_click=action, + ) + self.group_add(button) def add_text( self, name: str, value: str, location: tuple[int, int], - color: tuple[int, int, int] = BUTTONTEXTCOLOR, - cx: bool = True, - cy: bool = True, + color: tuple[int, int, int] = BUTTON_TEXT_COLOR, size: int = fontsize, - ) -> int: - """Add a new Text object to self.game with arguments. Return text id.""" - text = Text(size, color, None, cx, cy, name) - text.location = Vector2(*location) - text.update_value(value) - assert self.game is not None - self.game.add_object(text) - return text.id - - def entry_actions(self) -> None: - """Clear all objects, add cursor object, and set up next_state.""" - self.next_state = None - - assert self.game is not None - self.game.rm_star() - self.game.add_object(Cursor(self.game)) + outline: tuple[int, int, int] = BUTTON_TEXT_OUTLINE, + ) -> None: + """Add a new objects.Text object to self.game with arguments. Return text id.""" + text = KwargOutlineText( + name, + font=pygame.font.Font(FONT, size), + visible=True, + color=color, + text=value, + location=location, + ) + self.group_add(text) def set_var(self, attribute: str, value: object) -> None: """Set MenuState.{attribute} to {value}.""" setattr(self, attribute, value) - def to_state(self, state_name: str) -> Callable[[], None]: + def to_state(self, new_state: str) -> Callable[[], Awaitable[None]]: """Return a function that will change game state to state_name.""" - def to_state_name() -> None: - """Set MenuState.next_state to {state_name}.""" - self.next_state = state_name + async def set_state(*args: object, **kwargs: object) -> None: + play_sound("button_click") + await self.machine.set_state(new_state) - return to_state_name + return set_state def var_dependant_to_state( self, @@ -2901,1067 +1516,715 @@ def to_state_by_attributes() -> None: return to_state_by_attributes - def with_update( - self, - update_function: Callable[[], None], - ) -> Callable[[Callable[[], None]], Callable[[], None]]: - """Return a wrapper for a function that will call update_function after function.""" - - def update_wrapper(function: Callable[[], None]) -> Callable[[], None]: - """Wrap anything that might require a screen update.""" - - @wraps(function) - def function_with_update() -> None: - """Call main function, then update function.""" - function() - update_function() - - return function_with_update - - return update_wrapper - - def update_text( - self, - text_name: str, - value_function: Callable[[], str], - ) -> Callable[[], None]: - """Update text object with text_name's display value.""" - - def updater() -> None: - """Update text object {text_name}'s value with {value_function}.""" - assert self.game is not None - text = self.game.get_object_by_name(f"Text{text_name}") - assert isinstance(text, Text) - text.update_value(value_function()) - - return updater - - def toggle_button_state( - self, - textname: str, - boolattr: str, - textfunc: Callable[[bool], str], - ) -> Callable[[], None]: - """Return function that will toggle the value of text object , toggling attribute , and setting text value with textfunc.""" - - def valfunc() -> str: - """Return the new value for the text object. Gets called AFTER value is toggled.""" - return textfunc(getattr(self, boolattr)) - @self.with_update(self.update_text(textname, valfunc)) - def toggle_value() -> None: - """Toggle the value of boolattr.""" - self.set_var(boolattr, not getattr(self, boolattr)) - - return toggle_value - - def check_state(self) -> str | None: - """Return self.next_state.""" - return self.next_state - - -class InitState(GameState): +class InitializeState(AsyncState["AzulClient"]): """Initialize state.""" __slots__ = () def __init__(self) -> None: """Initialize self.""" - super().__init__("Init") - - def entry_actions(self) -> None: - """Register keyboard handlers.""" - assert self.game is not None - assert self.game.keyboard is not None - self.game.keyboard.add_listener("\x7f", "Delete") - self.game.keyboard.bind_action("Delete", "screenshot", 5) + super().__init__("initialize") - self.game.keyboard.add_listener("\x1b", "Escape") - self.game.keyboard.bind_action("Escape", "raise_close", 5) - - self.game.keyboard.add_listener("0", "Debug") - self.game.keyboard.bind_action("Debug", "debug", 5) - - def check_state(self) -> str: + async def check_conditions(self) -> str: """Go to title state.""" - return "Title" + return "title" -class TitleScreen(MenuState): +class TitleState(MenuState): """Game state when the title screen is up.""" __slots__ = () def __init__(self) -> None: """Initialize title.""" - super().__init__("Title") + super().__init__("title") - def entry_actions(self) -> None: + async def entry_actions(self) -> None: """Set up buttons.""" - super().entry_actions() - sw, sh = SCREENSIZE - self.add_button( - "ToSettings", - "New Game", - self.to_state("Settings"), - (sw // 2, sh // 2 - self.bh // 2), + assert self.machine is not None + self.id = self.machine.new_group("title") + + button_font = pygame.font.Font(FONT, 28) + title_font = pygame.font.Font(FONT, 56) + + title_text = KwargOutlineText( + "title_text", + title_font, + visible=True, + color=Color(0, 0, 0), + outline=(255, 0, 0), + border_width=4, + text=s_("title.game_title"), ) - self.add_button( - "ToCredits", - "Credits", - self.to_state("Credits"), - (sw // 2, sh // 2 + self.bh * 3), - int(self.fontsize / 1.5), + title_text.location = (SCREEN_SIZE[0] // 2, title_text.rect.h) + self.group_add(title_text) + + hosting_button = KwargButton( + "hosting_button", + button_font, + visible=True, + color=Color(0, 0, 0), + text=s_("title.host_game"), + location=[x // 2 for x in SCREEN_SIZE], + handle_click=self.change_state("play_hosting"), ) - assert self.game is not None - self.add_button( - "Quit", - "Quit", - self.game.raise_close, - (sw // 2, sh // 2 + self.bh * 4), - int(self.fontsize / 1.5), + self.group_add(hosting_button) + + join_button = KwargButton( + "join_button", + button_font, + visible=True, + color=Color(0, 0, 0), + text=s_("title.join_game"), + location=hosting_button.location + + Vector2( + 0, + hosting_button.rect.h + 10, + ), + handle_click=self.change_state("play_joining"), + ) + self.group_add(join_button) + + internal_button = KwargButton( + "internal_hosting", + button_font, + visible=True, + color=Color(0, 0, 0), + text=s_("title.singleplayer"), + location=hosting_button.location + - Vector2( + 0, + hosting_button.rect.h + 10, + ), + handle_click=self.change_state("play_hosting_internal"), ) + self.group_add(internal_button) + + quit_button = KwargButton( + "quit_button", + button_font, + visible=True, + color=Color(0, 0, 0), + text=s_("title.quit"), + location=join_button.location + + Vector2( + 0, + join_button.rect.h + 10, + ), + handle_click=self.change_state("Halt"), + ) + self.group_add(quit_button) -class CreditsScreen(MenuState): +class CreditsState(MenuState): """Game state when credits for original game are up.""" __slots__ = () def __init__(self) -> None: """Initialize credits.""" - super().__init__("Credits") + super().__init__("credits") def check_state(self) -> str: """Return to title.""" - return "Title" + return "title" + +class EndScreen(MenuState): + """End screen state.""" -class SettingsScreen(MenuState): - """Game state when user is defining game type, players, etc.""" + __slots__ = () def __init__(self) -> None: - """Initialize settings.""" - super().__init__("Settings") - - self.player_count = 0 # 2 - self.host_mode = True - self.variant_play = False - - def entry_actions(self) -> None: - """Add cursor object and tons of button and text objects to the game.""" - super().entry_actions() - - def add_numbers( - start: int, - end: int, - width_each: int, - cx: int, - cy: int, - ) -> None: - """Add numbers.""" - count = end - start + 1 - evencount = count % 2 == 0 - mid = count // 2 - - def add_number( - number: int, - display: str | int, - ) -> None: - """Add number.""" - if evencount: - if number < mid: - x = number - start - 0.5 - else: - x = number - mid + 0.5 - else: - if number < mid: - x = number - start + 1 - elif number == mid: - x = 0 - else: - x = number - mid - - @self.with_update( - self.update_text( - "Players", - lambda: f"Players: {self.player_count}", - ), - ) - def set_player_count() -> None: - """Set variable player_count to {display} while updating text.""" - return self.set_var("player_count", display) - - self.add_button( - f"SetCount{number}", - str(display), - set_player_count, - (int(cx + (width_each * x)), int(cy)), - size=int(self.fontsize / 1.5), - minlen=3, - ) + """Initialize end screen.""" + super().__init__("End") - for i in range(count): - add_number(i, start + i) - sw, sh = SCREENSIZE - cx = sw // 2 - cy = sh // 2 +class PlayHostingState(AsyncState["AzulClient"]): + """Start running server.""" - def host_text(x: object) -> str: - return f"Host Mode: {x}" + __slots__ = ("address",) - self.add_text( - "Host", - host_text(self.host_mode), - (cx, cy - self.bh * 3), - ) - self.add_button( - "ToggleHost", - "Toggle", - self.toggle_button_state("Host", "host_mode", host_text), - (cx, cy - self.bh * 2), - size=int(self.fontsize / 1.5), - ) + internal_server = False - # TEMPORARY: Hide everything to do with "Host Mode", networked games aren't done yet. - assert self.game is not None - self.game.set_attr_all("hidden", True) + def __init__(self) -> None: + """Initialize Play internal hosting / hosting State.""" + extra = "_internal" if self.internal_server else "" + super().__init__(f"play_hosting{extra}") + + async def entry_actions(self) -> None: + """Start hosting server.""" + assert self.machine is not None + self.machine.manager.add_components( + ( + GameServer(self.internal_server), + GameClient("network"), + ), + ) - def varient_text(x: object) -> str: - return f"Variant Play: {x}" + host = "localhost" if self.internal_server else await find_ip() + port = DEFAULT_PORT - self.add_text( - "Variant", - varient_text(self.variant_play), - (cx, cy - self.bh), - ) - self.add_button( - "ToggleVarient", - "Toggle", - self.toggle_button_state("Variant", "variant_play", varient_text), - (cx, cy), - size=int(self.fontsize / 1.5), - ) + self.address = (host, port) - self.add_text( - "Players", - f"Players: {self.player_count}", - (cx, cy + self.bh), - ) - add_numbers(2, 4, 70, cx, int(cy + self.bh * 2)) + await self.machine.raise_event(Event("server_start", self.address)) - var_to_state = self.var_dependant_to_state( - FactoryOffer=("host_mode", True), - FactoryOfferNetworked=("host_mode", False), - ) - self.add_button( - "StartGame", - "Start Game", - var_to_state, - (cx, cy + self.bh * 3), + async def exit_actions(self) -> None: + """Have client connect.""" + assert self.machine is not None + await self.machine.raise_event( + Event("client_connect", self.address), ) - def exit_actions(self) -> None: - """Start game.""" - assert self.game is not None - self.game.start_game( - self.player_count, - self.variant_play, - self.host_mode, - ) - self.game.bag.full_reset() + async def check_conditions(self) -> str | None: + """Return to Play state when server is up and running.""" + server: GameServer = self.machine.manager.get_component("GameServer") + return "play" if server.running else None -class PhaseFactoryOffer(GameState): - """Game state when it's the Factory Offer Stage.""" +class PlayInternalHostingState(PlayHostingState): + """Host server with internal server mode.""" __slots__ = () - def __init__(self) -> None: - """Initialize factory offer phase.""" - super().__init__("FactoryOffer") - - def entry_actions(self) -> None: - """Advance turn.""" - assert self.game is not None - self.game.next_turn() - - def check_state(self) -> str | None: - """If all tiles are gone, go to wall tiling. Otherwise keep waiting for that to happen.""" - assert self.game is not None - fact = self.game.get_object_by_name("Factories") - assert isinstance(fact, Factories) - table = self.game.get_object_by_name("TableCenter") - assert isinstance(table, TableCenter) - cursor = self.game.get_object_by_name("Cursor") - assert isinstance(cursor, Cursor) - if ( - fact.is_all_empty() - and table.is_empty() - and not cursor.is_holding(True) - ): - return "WallTiling" - return None + internal_server = True -class PhaseFactoryOfferNetworked(PhaseFactoryOffer): - """Factory offer phase but networked.""" +class ReturnElement(element_list.Element, objects.Button): + """Connection list return to title element sprite.""" __slots__ = () - def __init__(self) -> None: - """Initialize factory offer networked.""" - GameState.__init__(self, "FactoryOfferNetworked") - - def check_state(self) -> str: - """Go to networked wall tiling.""" - return "WallTilingNetworked" - - -class PhaseWallTiling(GameState): - """Wall tiling game phase.""" + def __init__(self, name: str, font: pygame.font.Font) -> None: + """Initialize return element.""" + super().__init__(name, font) - # __slots__ = () - def __init__(self) -> None: - """Initialize will tiling phase.""" - super().__init__("WallTiling") - - def entry_actions(self) -> None: - """Start wall tiling.""" - assert self.game is not None - self.next_starter: int = 0 - self.not_processed = [] - - self.game.player_turn_over() - - # For each player, - for player_id in range(self.game.players): - # Activate wall tiling mode. - player = self.game.get_player(player_id) - player.wall_tiling() - # Add that player's player_id to the list of not-processed players. - self.not_processed.append(player.player_id) - - # Start processing players. - self.game.next_turn() - - def do_actions(self) -> None: - """Do game actions.""" - assert self.game is not None - if self.not_processed: - if self.game.player_turn in self.not_processed: - player = self.game.get_player(self.game.player_turn) - if player.done_wall_tiling(): - # Once player is done wall tiling, score their moves. - # Also gets if they had the number one tile. - number_one = player.score_phase() - - if number_one: - # If player had the number one tile, remember that. - self.next_starter = self.game.player_turn - # Then, add the number one tile back to the table center. - table = self.game.get_object_by_name("TableCenter") - assert isinstance(table, TableCenter) - table.add_number_one_tile() - # After calculating their score, delete player from un-processed list - self.not_processed.remove(self.game.player_turn) - # and continue to the next un-processed player. - self.game.next_turn() - else: - self.game.next_turn() - - def check_state(self) -> str | None: - """Go to next state if ready.""" - assert self.game is not None - cursor = self.game.get_object_by_name("Cursor") - assert isinstance(cursor, Cursor) - if not self.not_processed and not cursor.is_holding(): - return "PrepareNext" - return None + self.update_location_on_resize = False + self.border_width = 4 + self.outline = RED + self.text = s_("connect.return_title") + self.visible = True + self.location = (SCREEN_SIZE[0] // 2, self.location.y + 10) - def exit_actions(self) -> None: - """Update who's turn it is.""" - assert self.game is not None - # Set up the player that had the number one tile to be the starting player next round. - self.game.player_turn_over() - # Goal: make (self.player_turn + 1) % self.players = self.next_starter - nturn = self.next_starter - 1 - if nturn < 0: - nturn += self.game.players - self.game.player_turn = nturn + async def handle_click( + self, + _: Event[sprite.PygameMouseButtonEventData], + ) -> None: + """Handle Click Event.""" + await self.raise_event( + Event("return_to_title", None, 2), + ) -class PhaseWallTilingNetworked(PhaseWallTiling): - """Wall tiling networked state.""" +class ConnectionElement(element_list.Element, objects.Button): + """Connection list element sprite.""" __slots__ = () - def __init__(self) -> None: - """Initialize will tiling networked.""" - GameState.__init__(self, "WallTilingNetworked") + def __init__( + self, + name: tuple[str, int], + font: pygame.font.Font, + motd: str, + ) -> None: + """Initialize connection element.""" + super().__init__(name, font) - def check_state(self) -> str: - """Go to networked next prepare.""" - return "PrepareNextNetworked" + self.text = f"[{name[0]}:{name[1]}]\n{motd}" + self.visible = True + async def handle_click( + self, + _: Event[sprite.PygameMouseButtonEventData], + ) -> None: + """Handle Click Event.""" + details = self.name + await self.raise_event( + Event("join_server", details, 2), + ) -class PhasePrepareNext(GameState): - """Prepare next phase of game.""" - __slots__ = ("new_round",) +class PlayJoiningState(GameState): + """Start running client.""" + + __slots__ = ("font",) def __init__(self) -> None: - """Initialize prepare next state.""" - super().__init__("PrepareNext") - self.new_round = False - - def entry_actions(self) -> None: - """Find out if game continues.""" - assert self.game is not None - players = ( - self.game.get_player(player_id) - for player_id in range(self.game.players) + """Initialize Joining State.""" + super().__init__("play_joining") + + self.font = pygame.font.Font( + FONT, + 12, ) - complete = (player.has_horzontal_line() for player in players) - self.new_round = not any(complete) - - def do_actions(self) -> None: - """Perform actions of state.""" - assert self.game is not None - if self.new_round: - fact = self.game.get_object_by_name("Factories") - assert isinstance(fact, Factories) - # This also handles bag re-filling from box lid. - fact.play_tiles_from_bag() - def check_state(self) -> str: - """Go to factory offer if new round else end screen.""" - if self.new_round: - return "FactoryOffer" - return "End" + async def entry_actions(self) -> None: + """Add game client component.""" + await super().entry_actions() + assert self.machine is not None + self.id = self.machine.new_group("join") + client = GameClient("network") + # Add network to higher level manager + self.machine.manager.add_component(client) -class PhasePrepareNextNetworked(PhasePrepareNext): - """Prepare for next, networked.""" + connections = element_list.ElementList("connection_list") + self.manager.add_component(connections) + group = self.machine.get_group(self.id) + assert group is not None + group.add(connections) - __slots__ = () + return_font = pygame.font.Font( + FONT, + 30, + ) + return_button = ReturnElement("return_button", return_font) + connections.add_element(return_button) + + self.manager.register_handlers( + { + "update_listing": self.handle_update_listing, + "return_to_title": self.handle_return_to_title, + "join_server": self.handle_join_server, + }, + ) - def __init__(self) -> None: - """Initialize prepare for next stage.""" - GameState.__init__(self, "PrepareNextNetworked") + await self.manager.raise_event(Event("update_listing", None)) - def check_state(self) -> str: - """Go to networked end.""" - return "EndNetworked" + async def handle_update_listing(self, _: Event[None]) -> None: + """Update server listing.""" + assert self.machine is not None + connections = self.manager.get_component("connection_list") -class EndScreen(MenuState): - """End screen state.""" + old: list[tuple[str, int]] = [] + current: list[tuple[str, int]] = [] - def __init__(self) -> None: - """Initialize end screen.""" - super().__init__("End") - self.ranking: dict[int, list[int]] = {} - self.wininf = "" - - def get_winners(self) -> None: - """Update self.ranking by player scores.""" - assert self.game is not None - self.ranking.clear() - scpid = {} - for player_id in range(self.game.players): - player = self.game.get_player(player_id) - assert isinstance(player, Player) - player.end_of_game_trigger() - if player.score not in scpid: - scpid[player.score] = [player_id] - else: - scpid[player.score] += [player_id] - # make sure no ties and establish rank - rank = 1 - for score in sorted(scpid, reverse=True): - pids = scpid[score] - if len(pids) > 1: - # If players have same score, - # most horizontal lines is tie breaker. - players = [ - self.game.get_player(player_id) for player_id in pids - ] - lines = [ - (p.get_horizontal_lines(), p.player_id) for p in players - ] - last = None - for c, player_id in sorted( - lines, - key=operator.itemgetter(0), - reverse=True, - ): - if last == c: - self.ranking[rank - 1] += [player_id + 1] - continue - last = c - self.ranking[rank] = [player_id + 1] - rank += 1 - else: - self.ranking[rank] = [pids[0] + 1] - rank += 1 - # Finally, make nice text. - text = "" - for rank in sorted(self.ranking): - line = "Player" - players_rank = self.ranking[rank] - cnt = len(players_rank) - if cnt > 1: - line += "s" - line += " " - if cnt == 1: - line += "{}" - elif cnt == 2: - line += "{} and {}" - elif cnt >= 3: - tmp = (["{}"] * (cnt - 1)) + ["and {}"] - line += ", ".join(tmp) - line += " " - if cnt == 1: - line += "got" - else: - line += "tied for" - line += " " - if rank <= 2: - line += ("1st", "2nd")[rank - 1] - else: - line += f"{rank}th" - line += " place!\n" - text += line.format(*players_rank) - self.wininf = text[:-1] - - def entry_actions(self) -> None: - """Set up end screen.""" - assert self.game is not None - # Figure out who won the game by points. - self.get_winners() - # Hide everything - table = self.game.get_object_by_name("TableCenter") - assert isinstance(table, TableCenter) - table.hidden = True - - fact = self.game.get_object_by_name("Factories") - assert isinstance(fact, Factories) - fact.set_attr_all("hidden", True) - - # Add buttons - bid = self.add_button( - "ReturnTitle", - "Return to Title", - self.to_state("Title"), - (SCREENSIZE[0] // 2, SCREENSIZE[1] * 4 // 5), - ) - buttontitle = self.game.get_object(bid) - assert isinstance(buttontitle, Button) - buttontitle.Render_Priority = "last-1" - buttontitle.cur_time = 2 - - # Add score board - x = SCREENSIZE[0] // 2 - y = 10 - for idx, line in enumerate(self.wininf.split("\n")): - self.add_text(f"Line{idx}", line, (x, y), cx=True, cy=False) - # self.game.get_object(bid).Render_Priority = f'last{-(2+idx)}' - button = self.game.get_object(bid) - assert isinstance(button, Button) - button.Render_Priority = "last-2" - y += self.bh - - -class EndScreenNetworked(EndScreen): - """Networked end screen.""" + # print(f'{self.machine.active_state = }') + # print(f'{self.name = }') + while ( + self.machine.active_state is not None + and self.machine.active_state is self + ): + # print("handle_update_listing click") - def __init__(self) -> None: - """Initialize end screen.""" - MenuState.__init__(self, "EndNetworked") - self.ranking = {} - self.wininf = "" + for motd, details in await read_advertisements(): + current.append(details) + if connections.component_exists(details): + continue + element = ConnectionElement(details, self.font, motd) + element.rect.topleft = ( + connections.get_new_connection_position() + ) + element.rect.topleft = (10, element.location.y + 3) + connections.add_element(element) + for details in old: + if details in current: + continue + connections.delete_element(details) + old, current = current, [] + + async def handle_join_server(self, event: Event[tuple[str, int]]) -> None: + """Handle join server event.""" + details = event.data + await self.machine.raise_event( + Event("client_connect", details), + ) + await self.machine.set_state("play") - def check_state(self) -> str: - """Go to title.""" - return "Title" + async def handle_return_to_title(self, _: Event[None]) -> None: + """Handle return to title event.""" + # Fire server stop event so server shuts down if it exists + await self.machine.raise_event_internal(Event("network_stop", None)) + if self.machine.manager.component_exists("network"): + self.machine.manager.remove_component("network") -class Game(ObjectHandler): - """Game object, contains most of what's required for Azul.""" + await self.machine.set_state("title") - tile_size = 30 - def __init__(self) -> None: - """Initialize game.""" - super().__init__() - # Gets overwritten by Keyboard object - self.keyboard: Keyboard | None = None +class PlayState(GameState): + """Game Play State.""" - self.states: dict[str, GameState] = {} - self.active_state: GameState | None = None + __slots__ = ("current_turn", "exit_data") - self.add_states( - [ - InitState(), - TitleScreen(), - CreditsScreen(), - SettingsScreen(), - PhaseFactoryOffer(), - PhaseWallTiling(), - PhasePrepareNext(), - EndScreen(), - PhaseFactoryOfferNetworked(), - PhaseWallTilingNetworked(), - PhasePrepareNextNetworked(), - EndScreenNetworked(), - ], + def __init__(self) -> None: + """Initialize Play State.""" + super().__init__("play") + + self.current_turn: int = 0 + + # (0: normal | 1: error) + self.exit_data: tuple[int, str, bool] | None = None + + def register_handlers(self) -> None: + """Register event handlers.""" + self.manager.register_handlers( + { + "game_initial_config": self.handle_game_initial_config, + "client_disconnected": self.handle_client_disconnected, + "game_winner": self.handle_game_over, + }, ) - self.initialized_state = False - self.background_color = BACKGROUND + def add_actions(self) -> None: + """Register handlers.""" + super().add_actions() + self.register_handlers() - self.is_host = True - self.players = 0 - self.factories = 0 + async def entry_actions(self) -> None: + """Add GameBoard and raise init event.""" + assert self.machine is not None + if self.id == 0: + self.id = self.machine.new_group("play") - self.player_turn: int = 0 + self.group_add(Cursor()) - # Tiles - self.bag = Bag(TILECOUNT, REGTILECOUNT) + center = TableCenter() + center.location = Vector2.from_iter(SCREEN_SIZE) // 2 + self.group_add(center) - # # Cache - # self.cache: dict[int, pygame.surface.Surface] = {} + # self.group_add(()) + ##gameboard = GameBoard( + ## 45, + ##) + ##gameboard.location = [x // 2 for x in SCREEN_SIZE] + ##self.group_add(gameboard) - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}()" - - def debug(self) -> None: - """Debug.""" + async def handle_game_initial_config( + self, + event: Event[tuple[bool, int, int, int, NDArray[int8]]], + ) -> None: + """Handle `game_initial_config` event.""" + ( + variant_play, + player_count, + factory_count, + self.current_turn, + floor_line_data, + ) = event.data + + center = Vector2.from_iter(SCREEN_SIZE) // 2 + + # Add factories + each = 360 / factory_count + degrees: float = -90 + for index in range(factory_count): + factory = Factory(index) + factory.location = vec2_to_location( + Vector2.from_degrees( + degrees, + 145, + ) + + center, + ) + self.group_add(factory) + + degrees += each + + # Add players + each = 360 / player_count + degrees = -(90 / player_count) + for index in range(player_count): + board = Board(index) + board.rect.midleft = vec2_to_location( + Vector2.from_degrees( + degrees, + 300, + ) + + center, + ) + self.group_add(board) - def screenshot(self) -> None: - """Save a screenshot of this game's most recent frame.""" - surface = pygame.surface.Surface(SCREENSIZE) - self.render(surface) - str_time = "-".join(time.asctime().split(" ")) - filename = f"Screenshot_at_{str_time}.png" + pattern_rows = PatternRows(index) + pattern_rows.rect.bottomright = board.rect.bottomleft + if index == self.current_turn: + pattern_rows.set_background(DARKGREEN) + self.group_add(pattern_rows) - if not os.path.exists("Screenshots"): - os.mkdir("Screenshots") + floor_line = FloorLine(index, floor_line_data) + floor_line.rect.topleft = pattern_rows.rect.bottomleft + self.group_add(floor_line) - surface.unlock() - pygame.image.save( - surface, - os.path.join("Screenshots", filename), - filename, - ) - del surface + degrees += each - savepath = os.path.join(os.getcwd(), "Screenshots") + async def check_conditions(self) -> str | None: + """Return to title if client component doesn't exist.""" + if not self.machine.manager.component_exists("network"): + return "title" + return None - print(f'Saved screenshot as "{filename}" in "{savepath}".') + async def exit_actions(self) -> None: + """Raise network stop event and remove components.""" + # Fire server stop event so server shuts down if it exists + # await self.machine.raise_event(Event("network_stop", None)) + await self.machine.raise_event_internal(Event("network_stop", None)) - def raise_close(self) -> None: - """Raise a window close event.""" - pygame.event.post(pygame.event.Event(QUIT)) + if self.machine.manager.component_exists("network"): + self.machine.manager.remove_component("network") + if self.machine.manager.component_exists("GameServer"): + self.machine.manager.remove_component("GameServer") - def add_states(self, states: Iterable[GameState]) -> None: - """Add game states to self.""" - for state in states: - if not isinstance(state, GameState): - raise ValueError( - f'"{state}" Object is not a subclass of GameState!', - ) - state.game = self - self.states[state.name] = state - - def set_state(self, new_state_name: str) -> None: - """Change states and perform any exit / entry actions.""" - # Ensure the new state is valid. - if new_state_name not in self.states: - raise ValueError(f'State "{new_state_name}" does not exist!') - - # If we have an active state, - if self.active_state is not None: - # Perform exit actions - self.active_state.exit_actions() - - # The active state is the new state - self.active_state = self.states[new_state_name] - # Perform entry actions for new active state - self.active_state.entry_actions() - - def update_state(self) -> None: - """Perform the actions of the active state and potentially change states.""" - # Only continue if there is an active state - if self.active_state is None: - return + # Unbind components and remove group + await super().exit_actions() - # Perform the actions of the active state and check conditions - self.active_state.do_actions() - - new_state_name = self.active_state.check_state() - if new_state_name is not None: - self.set_state(new_state_name) - - def add_object(self, obj: Object) -> None: - """Add an object to the game.""" - obj.game = self - super().add_object(obj) - - def render(self, surface: pygame.surface.Surface) -> None: - """Render all of self.objects to the screen.""" - surface.fill(self.background_color) - self.render_objects(surface) - - def process(self, time_passed: float) -> None: - """Process all the objects and self.""" - if not self.initialized_state and self.keyboard is not None: - self.set_state("Init") - self.initialized_state = True - self.process_objects(time_passed) - self.update_state() - - def get_player(self, player_id: int) -> Player: - """Get the player with player id player_id.""" - if self.players: - player = self.get_object_by_name(f"Player{player_id}") - assert isinstance(player, Player) - return player - raise RuntimeError("No players!") - - def player_turn_over(self) -> None: - """Call end_of_turn for current player.""" - if self.player_turn >= 0 and self.player_turn < self.players: - old_player = self.get_player(self.player_turn) - if old_player.is_turn: - old_player.end_of_turn() - - def next_turn(self) -> None: - """Tell current player it's the end of their turn, and update who's turn it is and now it's their turn.""" - if self.is_host: - self.player_turn_over() - last = self.player_turn - self.player_turn = (self.player_turn + 1) % self.players - if self.player_turn == last and self.players > 1: - self.next_turn() - return - new_player = self.get_player(self.player_turn) - new_player.trigger_turn_now() + self.register_handlers() - def start_game( - self, - players: int, - varient_play: bool = False, - host_mode: bool = True, - address: str = "", - ) -> None: - """Start a new game.""" - self.reset_cache() - max_players = 4 - self.players = saturate(players, 1, max_players) - self.is_host = host_mode - self.factories = self.players * 2 + 1 - - self.rm_star() - - self.add_object(Cursor(self)) - self.add_object(TableCenter(self)) - self.add_object(BoxLid(self)) - - if self.is_host: - self.bag.reset() - # S311 Standard pseudo-random generators are not suitable for cryptographic purposes - self.player_turn = random.randint( # noqa: S311 - -1, - self.players - 1, - ) - else: - raise NotImplementedError() + assert self.manager.has_handler("game_winner") - cx, cy = SCREENSIZE[0] / 2, SCREENSIZE[1] / 2 - out = math.sqrt(cx**2 + cy**2) // 3 * 2 + async def handle_game_over(self, event: Event[int]) -> None: + """Handle game over event.""" + winner = event.data + self.exit_data = (0, s_("play.win", winner=winner), False) - mdeg = 360 // max_players + await self.machine.raise_event_internal(Event("network_stop", None)) - for player_id in range(self.players): - networked = False - newp = Player(self, player_id, networked, varient_play) + async def handle_client_disconnected(self, event: Event[str]) -> None: + """Handle client disconnected error.""" + error = event.data + print(f"[azul.game.PlayState] handle_client_disconnected {error = }") - truedeg = (self.players + 1 - player_id) * (360 / self.players) - closedeg = truedeg // mdeg * mdeg + 45 - rad = math.radians(closedeg) + client_disconnected = s_("error.client_disconnected") + error_text = s_(error) + self.exit_data = (1, f"{client_disconnected}$${error_text}", False) - newp.location = Vector2( - round(cx + out * math.sin(rad)), - round( - cy + out * math.cos(rad), - ), - ) - self.add_object(newp) - if self.is_host: - self.next_turn() + async def do_actions(self) -> None: + """Perform actions for this State.""" + # print(f"{self.__class__.__name__} do_actions tick") + if self.exit_data is None: + return - factory = Factories(self, self.factories) - factory.location = Vector2(cx, cy) - self.add_object(factory) - self.process_objects(0) + exit_status, raw_message, handled = self.exit_data - if self.is_host: - self.next_turn() + if handled: + return + self.exit_data = (exit_status, raw_message, True) - def screen_size_update(self) -> None: - """Handle screen size updates.""" - objs_with_attr = self.get_objects_with_attr("screen_size_update") - for oid in objs_with_attr: - obj = self.get_object(oid) - assert obj is not None - obj.screen_size_update() + font = pygame.font.Font( + FONT, + 28, + ) + error_message = "" + if exit_status == 1: + message, error_message = raw_message.split("$$", 1) + else: + message = raw_message + + if not self.manager.component_exists("continue_button"): + continue_button = KwargButton( + "continue_button", + font, + visible=True, + color=Color(0, 0, 0), + text=s_("play.return_title_msg", message=message), + location=[x // 2 for x in SCREEN_SIZE], + handle_click=self.change_state("title"), + ) + self.group_add(continue_button) + group = continue_button.groups()[0] + # LayeredDirty, not just AbstractGroup + group.move_to_front(continue_button) # type: ignore[attr-defined] + else: + continue_button = self.manager.get_component("continue_button") -class Keyboard: - """Keyboard object, handles keyboard input.""" + if exit_status == 1: + if not self.manager.component_exists("error_text"): + error_text = objects.OutlinedText("error_text", font) + error_text.text = "" + else: + error_text = self.manager.get_component("error_text") + error_text.visible = True + error_text.color = Color(255, 0, 0) + error_text.border_width = 1 + error_text.text += error_message + "\n" + error_text.location = continue_button.location + Vector2( + 0, + continue_button.rect.h + 10, + ) - __slots__ = ("actions", "active", "delay", "keys", "target", "time") + if not self.manager.component_exists("error_text"): + self.group_add(error_text) - def __init__( - self, - target: Game, - **kwargs: tuple[str, str], - ) -> None: - """Initialize keyboard.""" - self.target = target - self.target.keyboard = self - - # Map of keyboard events to names - self.keys: dict[str, str] = {} - # Map of keyboard event names to functions - self.actions: dict[str, Callable[[], None]] = {} - # Map of names to time until function should be called again - self.time: dict[str, float] = {} - # Map of names to duration timer waits for function recalls - self.delay: dict[str, float | None] = {} - # Map of names to boolian of pressed or not - self.active: dict[str, bool] = {} - - for name in kwargs: - if not hasattr(kwargs[name], "__iter__"): - raise ValueError( - "Keyword arguments must be given as name=[key, self.target.function_name, delay]", - ) - # if len(kwargs[name]) == 2: - key, function_name = kwargs[name] - # elif len(kwargs[name]) == 3: - # key, function_name, _delay = kwargs[name] - # else: - # raise ValueError - self.add_listener(key, name) - self.bind_action(name, function_name) - def __repr__(self) -> str: - """Return representation of self.""" - return f"{self.__class__.__name__}({self.target!r})" +class AzulClient(sprite.GroupProcessor): + """Azul Game Client.""" - def is_pressed(self, key: str) -> bool: - """Return True if is pressed.""" - return self.active.get(key, False) + __slots__ = ("manager",) - def add_listener(self, key: str, name: str) -> None: - """Listen for key down events with event.key == key argument and when that happens set self.actions[name] to true.""" - self.keys[key] = name # key to name - self.actions[name] = lambda: None # name to function - self.time[name] = 0 # name to time until function recall - self.delay[name] = None # name to function recall delay - self.active[name] = False # name to boolian of pressed + def __init__(self, manager: ExternalRaiseManager) -> None: + """Initialize Checkers Client.""" + super().__init__() + self.manager = manager - def get_function_from_target( - self, - function_name: str, - ) -> Callable[[], None]: - """Return function with name function_name from self.target.""" - if hasattr(self.target, function_name): - attribute = getattr(self.target, function_name) - assert callable(attribute) - return cast("Callable[[], None]", attribute) - return lambda: None - - def bind_action( - self, - name: str, - target_function_name: str, - delay: float | None = None, - ) -> None: - """Bind an event we are listening for to calling a function, can call multiple times if delay is not None.""" - self.actions[name] = self.get_function_from_target( - target_function_name, + self.add_states( + ( + HaltState(), + InitializeState(), + TitleState(), + CreditsState(), + PlayHostingState(), + PlayInternalHostingState(), + PlayJoiningState(), + PlayState(), + ), ) - self.delay[name] = delay - - def set_active(self, name: str, value: bool) -> None: - """Set active value for key name to .""" - if name in self.active: - self.active[name] = bool(value) - if not value: - self.time[name] = 0 - - def set_key(self, key: str, value: bool) -> None: - """Set active value for key to .""" - if key in self.keys: - self.set_active(self.keys[key], value) - - # elif isinstance(key, int) and key < 0x110000: - # self.set_key(chr(key), value) - - def read_event(self, event: pygame.event.Event) -> None: - """Handle an event.""" - if event.type == KEYDOWN: - self.set_key(event.key, True) - elif event.type == KEYUP: - self.set_key(event.key, False) - - def read_events(self, events: Iterable[pygame.event.Event]) -> None: - """Handle a list of events.""" - for event in events: - self.read_event(event) - - def process(self, time_passed: float) -> None: - """Send commands to self.target based on pressed keys and time.""" - for name in self.active: - if self.active[name]: - self.time[name] = max(self.time[name] - time_passed, 0) - if self.time[name] == 0: - self.actions[name]() - delay = self.delay[name] - if delay is not None: - self.time[name] = delay - else: - self.time[name] = math.inf - - -def network_shutdown() -> None: - """Handle network shutdown.""" + async def raise_event(self, event: Event[Any]) -> None: + """Raise component event in all groups.""" + await self.manager.raise_event(event) -def run() -> None: + async def raise_event_internal(self, event: Event[Any]) -> None: + """Raise component event in all groups.""" + await self.manager.raise_event_internal(event) + + +async def async_run() -> None: """Run program.""" - # global game - global SCREENSIZE + # Set up globals + global SCREEN_SIZE + # Set up the screen - screen = pygame.display.set_mode(SCREENSIZE, RESIZABLE, 16) - pygame.display.set_caption(f"{__title__} {__version__}") + screen = pygame.display.set_mode(SCREEN_SIZE, RESIZABLE, 16, vsync=VSYNC) + pygame.display.set_caption(f"{__title__} v{__version__}") # pygame.display.set_icon(pygame.image.load('icon.png')) - pygame.display.set_icon(get_tile_image(Tile(5), 32)) - - # Set up the FPS clock - clock = pygame.time.Clock() - - game = Game() - keyboard = Keyboard(game) - - music_end = USEREVENT + 1 # This event is sent when a music track ends - - # Set music end event to our new event - pygame.mixer.music.set_endevent(music_end) - - # Load and start playing the music - # pygame.mixer.music.load('sound/') - # pygame.mixer.music.play() - - running = True - - # While the game is active - while running: - # Event handler - for event in pygame.event.get(): - if event.type == QUIT: - running = False - elif event.type == music_end: - # If the music ends, stop it and play it again. - pygame.mixer.music.stop() - pygame.mixer.music.play() - elif event.type == VIDEORESIZE: - SCREENSIZE = event.size - game.screen_size_update() - else: - # If it's not a quit or music end event, tell the keyboard handler about it. - keyboard.read_event(event) + pygame.display.set_icon(get_tile_image(Tile.one, 32)) + screen.fill((0xFF, 0xFF, 0xFF)) + + ## try: + async with trio.open_nursery() as main_nursery: + event_manager = ExternalRaiseManager( + "checkers", + main_nursery, # "client" + ) + client = AzulClient(event_manager) + + background = pygame.image.load( + DATA_FOLDER / "background.png", + ).convert() + client.clear(screen, background) + + client.set_timing_threshold(1000 / 80) + + await client.set_state("initialize") + + music_end = USEREVENT + 1 # This event is sent when a music track ends + + # Set music end event to our new event + pygame.mixer.music.set_endevent(music_end) + + # Load and start playing the music + # pygame.mixer.music.load('sound/') + # pygame.mixer.music.play() + + clock = Clock() + + resized_window = False + while client.running: + async with trio.open_nursery() as event_nursery: + for event in pygame.event.get(): + if event.type == QUIT: + await client.set_state("Halt") + elif event.type == KEYUP and event.key == K_ESCAPE: + pygame.event.post(pygame.event.Event(QUIT)) + elif event.type == music_end: + # If the music ends, stop it and play it again. + pygame.mixer.music.stop() + pygame.mixer.music.play() + elif event.type == WINDOWRESIZED: + SCREEN_SIZE = (event.x, event.y) + resized_window = True + sprite_event = sprite.convert_pygame_event(event) + # print(sprite_event) + event_nursery.start_soon( + event_manager.raise_event, + sprite_event, + ) + event_nursery.start_soon(client.think) + event_nursery.start_soon(clock.tick, FPS) + + await client.raise_event( + Event( + "tick", + sprite.TickEventData( + time_passed=clock.get_time() + / 1e9, # nanoseconds -> seconds + fps=clock.get_fps(), + ), + ), + ) - # Get the time passed from the FPS clock - time_passed = clock.tick(FPS) - time_passed_secconds = time_passed / 1000 + if resized_window: + resized_window = False + screen.fill((0xFF, 0xFF, 0xFF)) + rects = [Rect((0, 0), SCREEN_SIZE)] + client.repaint_rect(rects[0]) + rects.extend(client.draw(screen)) + else: + rects = client.draw(screen) + pygame.display.update(rects) + client.clear_groups() - # Process the game - game.process(time_passed_secconds) - keyboard.process(time_passed_secconds) + # Once the game has ended, stop the music + pygame.mixer.music.stop() - # Render the grid to the screen. - game.render(screen) - # Update the display - pygame.display.update() - # Once the game has ended, stop the music and de-initalize pygame. - pygame.mixer.music.stop() +def run() -> None: + """Start asynchronous run.""" + trio.run(async_run) -def save_crash_img() -> None: +def screenshot_last_frame() -> None: """Save the last frame before the game crashed.""" surface = pygame.display.get_surface().copy() - str_time = "-".join(time.asctime().split(" ")) + str_time = "_".join(time.asctime().split(" ")).replace(":", "_") filename = f"Crash_at_{str_time}.png" - if not os.path.exists("Screenshots"): - os.mkdir("Screenshots") + path = Path("screenshots").absolute() + if not path.exists(): + os.mkdir(path) - # surface.lock() - pygame.image.save(surface, os.path.join("Screenshots", filename), filename) - # surface.unlock() - del surface + fullpath = path / filename - savepath = os.path.join(os.getcwd(), "Screenshots") + pygame.image.save(surface, fullpath, filename) + del surface - print(f'Saved screenshot as "{filename}" in "{savepath}".') + print(s_("screenshot_save", fullpath=fullpath)) def cli_run() -> None: """Run from command line interface.""" - # Linebreak before, as pygame prints a message on import. - print(f"\n{__title__} v{__version__}\nProgrammed by {__author__}.") + print(f"{__title__} v{__version__}\nProgrammed by {__author__}.\n") + + # Make sure the game will display correctly on high DPI monitors on Windows. + if sys.platform == "win32": + from ctypes import windll + + with contextlib.suppress(AttributeError): + windll.user32.SetProcessDPIAware() + del windll + + exception: str | None = None try: # Initialize Pygame _success, fail = pygame.init() if fail > 0: - print( - "Warning! Some modules of Pygame have not initialized properly!", - ) - print( - "This can occur when not all required modules of SDL, which pygame utilizes, are installed.", - ) + print(s_("error.pygame_uninitialized")) run() - # except BaseException as ex: - # reraise = True#False - ## - # print('Debug: Activating Post motem.') - # import pdb - # pdb.post_mortem() - ## - # try: - # save_crash_img() - # except BaseException as svex: - # print(f'Could not save crash screenshot: {", ".join(svex.args)}') - # try: - # import errorbox - # except ImportError: - # reraise = True - # print(f'A {type(ex).__name__} Error Has Occored: {", ".join(ex.args)}') - # else: - # errorbox.errorbox('Error', f'A {type(ex).__name__} Error Has Occored: {", ".join(ex.args)}') - # if reraise: - # raise + except ExceptionGroup as exc: + ## print(exc) + exception = "".join(traceback.format_exception(exc)) + ## print(exception) + ## raise + ## except BaseException as ex: + ## screenshot_last_frame() + ## # errorbox.errorbox('Error', f'A {type(ex).__name__} Error Has Occored: {", ".join(ex.args)}') + ## raise finally: pygame.quit() - network_shutdown() + if exception is not None: + print(exception, file=sys.stderr) if __name__ == "__main__": diff --git a/src/azul/keyboard.py b/src/azul/keyboard.py index daeda5b..a305365 100644 --- a/src/azul/keyboard.py +++ b/src/azul/keyboard.py @@ -9,7 +9,7 @@ __version__ = "0.0.0" -from azul.component import ComponentManager +from libcomponent.component import ComponentManager class Keyboard(ComponentManager): diff --git a/src/azul/lang.py b/src/azul/lang.py deleted file mode 100644 index b97d6c0..0000000 --- a/src/azul/lang.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Language file handler.""" - -from __future__ import annotations - -# Programmed by CoolCat467 - -__title__ = "lang" -__author__ = "CoolCat467" -__version__ = "0.0.0" - -import json -from functools import cache -from os.path import exists, join - - -def load_json(filename: str) -> dict[str, str]: - """Return json data loaded from filename.""" - with open(filename, encoding="utf-8") as loaded: - data = json.load(loaded) - assert isinstance(data, dict) - return data - - -@cache -def load_lang(name: str) -> dict[str, str] | None: - """Return full data for language with given name.""" - filename = join("lang", f"{name}.json") - if not exists(filename): - return None - return load_json(filename) diff --git a/src/azul/lang/en_us.json b/src/azul/lang/en_us.json index 67ac85b..35b1356 100644 --- a/src/azul/lang/en_us.json +++ b/src/azul/lang/en_us.json @@ -1,24 +1,25 @@ { - "main_menu": { - "title": "Azul", - "host_server": "Host Game", - "join_server": "Join Game", - "close": "Close" - }, - "host_server_menu": { - "title": "Host Server", - "port_input": "Enter Host Port ({})", - "start_server": "Start Server", + "connect": { "return_title": "Return to Title" }, - "join_server_menu": { - "title": "Join Server", - "host_input": "Enter Host Address", - "port_input": "Enter Host Port ({})", - "join_server": "Connect to Server", - "return_title": "Return to Title" + "error": { + "client_disconnected": "Client Disconnected", + "not_connected": "Not connected to server.", + "pygame_uninitialized": "Warning! Some modules of Pygame have not initialized properly!\nThis can occur when not all required modules of SDL are installed.", + "read_event_fail": "Failed to read event from server.", + "socket_connect_fail": "Error connecting to server.", + "socket_eof": "Server closed connection." + }, + "play": { + "return_title_msg": "{message} - Return to Title", + "win": "{winner} Won" }, - "connect_server": { - "connecting": "Connecting to Server..." + "screenshot_save": "Saved screenshot to \"{fullpath}\".", + "title": { + "game_title": "Azul", + "host_game": "Host Networked Game", + "join_game": "Join Networked Game", + "quit": "Quit", + "singleplayer": "Singleplayer Game" } } diff --git a/src/azul/mr_floppy_test.py b/src/azul/mr_floppy_test.py new file mode 100644 index 0000000..3b55725 --- /dev/null +++ b/src/azul/mr_floppy_test.py @@ -0,0 +1,513 @@ +"""Azul Client.""" + +from __future__ import annotations + +import contextlib + +# Programmed by CoolCat467 +# Hide the pygame prompt +import os +import sys +from os import path +from pathlib import Path +from typing import TYPE_CHECKING, Any, Final + +import trio +from libcomponent.component import Component, ComponentManager, Event +from pygame.locals import K_ESCAPE, KEYUP, QUIT, RESIZABLE, WINDOWRESIZED +from pygame.rect import Rect + +from azul import objects, sprite +from azul.statemachine import AsyncState, AsyncStateMachine +from azul.vector import Vector2 + +os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "True" +if os.environ["PYGAME_HIDE_SUPPORT_PROMPT"]: + import pygame +del os + + +if TYPE_CHECKING: + from collections.abc import Iterator, Sequence + +__title__ = "Azul Client" +__author__ = "CoolCat467" +__version__ = "2.0.0" + +SCREEN_SIZE = Vector2(800, 600) +FPS = 30 +# FPS = 60 +VSYNC = True +# PORT = server.PORT + +ROOT_FOLDER: Final = Path(__file__).absolute().parent +DATA_FOLDER: Final = ROOT_FOLDER / "data" +FONT_FOLDER: Final = ROOT_FOLDER / "fonts" + +FONT = FONT_FOLDER / "RuneScape-UF-Regular.ttf" + + +class GameClient(sprite.GroupProcessor, AsyncStateMachine): + """Gear Runner and Layered Dirty Sprite group handler.""" + + def __init__(self) -> None: + """Initialize azul client.""" + sprite.GroupProcessor.__init__(self) + AsyncStateMachine.__init__(self) + + self.add_states( + ( + HaltState(), + AzulInitialize(), + ), + ) + + @property + def running(self) -> bool: + """Boolean of if state machine is running.""" + return self.active_state is not None + + async def raise_event(self, event: Event[Any]) -> None: + """Raise component event in all groups.""" + if self.active_state is None: + return + manager = getattr(self.active_state, "manager", None) + assert isinstance(manager, ComponentManager | None) + if manager is None: + return + await manager.raise_event(event) + + +class AzulState(AsyncState[GameClient]): + """Azul Client Asynchronous base class.""" + + __slots__ = ("id", "manager") + + def __init__(self, name: str) -> None: + """Initialize azul state.""" + super().__init__(name) + + self.id: int = 0 + self.manager = ComponentManager(self.name) + + +class HaltState(AzulState): + """Halt state to set state to None so running becomes False.""" + + def __init__(self) -> None: + """Initialize halt state.""" + super().__init__("Halt") + + async def check_conditions(self) -> None: + """Set active state to None.""" + await self.machine.set_state(None) + + +class ClickDestinationComponent(Component): + """Component that will use targeting to go to wherever you click on the screen.""" + + __slots__ = ("selected",) + outline = pygame.color.Color(255, 220, 0) + + def __init__(self) -> None: + """Initialize click destination component.""" + super().__init__("click_dest") + + self.selected = False + + def bind_handlers(self) -> None: + """Register PygameMouseButtonDown and tick handlers.""" + self.register_handlers( + { + "click": self.click, + # "drag": self.drag, + "PygameMouseButtonDown": self.mouse_down, + "tick": self.move_towards_dest, + "init": self.cache_outline, + "test": self.test, + }, + ) + + async def test(self, event: Event[object]) -> None: + """Print out event data.""" + print(f"{event = }") + + await trio.lowlevel.checkpoint() + + async def cache_outline(self, _: Event[None]) -> None: + """Precalculate outlined images.""" + image: sprite.ImageComponent = self.get_component("image") + outline: sprite.OutlineComponent = image.get_component("outline") + outline.precalculate_all_outlined(self.outline) + + await trio.lowlevel.checkpoint() + + async def update_selected(self) -> None: + """Update selected.""" + image: sprite.ImageComponent = self.get_component("image") + outline: sprite.OutlineComponent = image.get_component("outline") + + color = (None, self.outline)[int(self.selected)] + outline.set_color(color) + + if not self.selected: + movement: sprite.MovementComponent = self.get_component("movement") + movement.speed = 0 + + await trio.lowlevel.checkpoint() + + async def click( + self, + event: Event[sprite.PygameMouseButtonEventData], + ) -> None: + """Toggle selected.""" + if event.data["button"] != 1: + await trio.lowlevel.checkpoint() + return + self.selected = not self.selected + + await self.update_selected() + + async def drag(self, event: Event[None]) -> None: + """Drag sprite.""" + if not self.selected: + self.selected = True + await self.update_selected() + movement: sprite.MovementComponent = self.get_component("movement") + movement.speed = 0 + + await trio.lowlevel.checkpoint() + + async def mouse_down( + self, + event: Event[sprite.PygameMouseButtonEventData], + ) -> None: + """Target click pos if selected.""" + if not self.selected: + await trio.lowlevel.checkpoint() + return + if event.data["button"] == 1: + movement: sprite.MovementComponent = self.get_component("movement") + movement.speed = 200 + target: sprite.TargetingComponent = self.get_component("targeting") + target.destination = Vector2.from_iter(event.data["pos"]) + + await trio.lowlevel.checkpoint() + + async def move_towards_dest( + self, + event: Event[sprite.TickEventData], + ) -> None: + """Move closer to destination.""" + target: sprite.TargetingComponent = self.get_component("targeting") + await target.move_destination_time(event.data.time_passed) + + +class MrFloppy(sprite.Sprite): + """Mr. Floppy test sprite.""" + + __slots__ = () + + def __init__(self) -> None: + """Initialize mr floppy sprite.""" + super().__init__("MrFloppy") + + image_component = sprite.ImageComponent() + image_component.add_component(sprite.AnimationComponent()) + self.add_components( + ( + sprite.MovementComponent(), + sprite.TargetingComponent(), + ClickDestinationComponent(), + image_component, + sprite.DragClickEventComponent(), + ), + ) + + movement = self.get_component("movement") + targeting = self.get_component("targeting") + image = self.get_component("image") + + movement.speed = 200 + + # lintcheck: c-extension-no-member (I1101): Module 'pygame.surface' has no 'Surface' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. + floppy: pygame.surface.Surface = pygame.image.load( + path.join("data", "mr_floppy.png"), + ) + + image.add_images( + { + 0: floppy, + # '1': pygame.transform.flip(floppy, False, True) + 1: pygame.transform.rotate(floppy, 270), + 2: pygame.transform.flip(floppy, True, True), + 3: pygame.transform.rotate(floppy, 90), + }, + ) + + anim = image.get_component("animation") + anim.controller = self.controller((0, 1, 2, 3)) + + image.set_image(0) + self.visible = True + + self.location = SCREEN_SIZE / 2 + targeting.destination = self.location + + self.register_handler("drag", self.drag) + + @staticmethod + def controller( + image_identifiers: Sequence[str | int], + ) -> Iterator[str | int | None]: + """Animation controller.""" + cidx = 0 + while True: + count = len(image_identifiers) + if not count: + yield None + continue + cidx = (cidx + 1) % count + yield image_identifiers[cidx] + + async def drag(self, event: Event[sprite.DragEvent]) -> None: + """Move by relative from drag.""" + if not event.data.buttons.get(1): + await trio.lowlevel.checkpoint() + return + self.location += event.data.rel + self.dirty = 1 + + await trio.lowlevel.checkpoint() + + +class FPSCounter(objects.Text): + """FPS counter.""" + + __slots__ = () + + def __init__(self) -> None: + """Initialize fps counter.""" + font = pygame.font.Font(FONT, 28) + super().__init__("fps", font) + + self.text = "FPS: ???" + self.visible = True + + async def on_tick(self, event: Event[sprite.TickEventData]) -> None: + """Update text.""" + # self.text = f'FPS: {event.data["fps"]:.2f}' + self.text = f"FPS: {event.data.fps:.0f}" + + await trio.lowlevel.checkpoint() + + async def update_loc( + self, + event: Event[dict[str, tuple[int, int]]], + ) -> None: + """Move to top left corner.""" + self.location = Vector2.from_iter(event.data["size"]) / 2 + (5, 5) + + def bind_handlers(self) -> None: + """Register event handlers.""" + super().bind_handlers() + self.register_handlers( + { + "tick": self.on_tick, + "sprite_image_resized": self.update_loc, + }, + ) + + +class AzulInitialize(AzulState): + """Initialize Azul.""" + + __slots__ = () + + def __init__(self) -> None: + """Initialize state.""" + super().__init__("initialize") + + def group_add(self, new_sprite: sprite.Sprite) -> None: + """Add new sprite to group.""" + group = self.machine.get_group(self.id) + assert group is not None, "Expected group from new group id" + group.add(new_sprite) + self.manager.add_component(new_sprite) + + async def entry_actions(self) -> None: + """Create group and add mr floppy.""" + self.id = self.machine.new_group("test") + floppy = MrFloppy() + print(f"{floppy = }") + self.group_add(floppy) + self.group_add(FPSCounter()) + + await self.machine.raise_event(Event("init", None)) + + async def exit_actions(self) -> None: + """Remove group and unbind components.""" + self.machine.remove_group(self.id) + self.manager.unbind_components() + + await trio.lowlevel.checkpoint() + + +def save_crash_img() -> None: + """Save the last frame before the game crashed.""" + surface = pygame.display.get_surface().copy() + # strTime = '-'.join(time.asctime().split(' ')) + # filename = f'Crash_at_{strTime}.png' + filename = "screenshot.png" + + pygame.image.save(surface, path.join("screenshots", filename)) + del surface + + +async def async_run() -> None: + """Run client.""" + global SCREEN_SIZE + # global client + + screen = pygame.display.set_mode( + tuple(SCREEN_SIZE), + RESIZABLE, + vsync=VSYNC, + ) + pygame.display.set_caption(f"{__title__} v{__version__}") + pygame.key.set_repeat(1000, 30) + screen.fill((0xFF, 0xFF, 0xFF)) + + client = GameClient() + + background = pygame.image.load( + path.join("data", "background.png"), + ).convert() + client.clear(screen, background) + + client.set_timing_threshold(1000 / FPS) + + await client.set_state("initialize") + + clock = pygame.time.Clock() + + while client.running: + resized_window = False + + async with trio.open_nursery() as nursery: + for event in pygame.event.get(): + # pylint: disable=undefined-variable + if event.type == QUIT: + await client.set_state("Halt") + elif event.type == KEYUP and event.key == K_ESCAPE: + pygame.event.post(pygame.event.Event(QUIT)) + elif event.type == WINDOWRESIZED: + SCREEN_SIZE = Vector2(event.x, event.y) + resized_window = True + sprite_event = sprite.convert_pygame_event(event) + # print(sprite_event) + nursery.start_soon(client.raise_event, sprite_event) + await client.think() + + time_passed = clock.tick(FPS) + + await client.raise_event( + Event( + "tick", + sprite.TickEventData( + time_passed / 1000, + clock.get_fps(), + ), + ), + ) + + if resized_window: + screen.fill((0xFF, 0xFF, 0xFF)) + rects = [Rect((0, 0), tuple(SCREEN_SIZE))] + client.repaint_rect(rects[0]) + rects.extend(client.draw(screen)) + else: + rects = client.draw(screen) + pygame.display.update(rects) + client.clear_groups() + + +class Tracer(trio.abc.Instrument): + """Tracer instrument.""" + + __slots__ = ("_sleep_time",) + + def before_run(self) -> None: + """Before run.""" + print("!!! run started") + + def _print_with_task(self, msg: str, task: trio.lowlevel.Task) -> None: + """Print message with task name.""" + # repr(task) is perhaps more useful than task.name in general, + # but in context of a tutorial the extra noise is unhelpful. + print(f"{msg}: {task.name}") + + def task_spawned(self, task: trio.lowlevel.Task) -> None: + """Task spawned.""" + self._print_with_task("### new task spawned", task) + + def task_scheduled(self, task: trio.lowlevel.Task) -> None: + """Task scheduled.""" + self._print_with_task("### task scheduled", task) + + def before_task_step(self, task: trio.lowlevel.Task) -> None: + """Before task step.""" + self._print_with_task(">>> about to run one step of task", task) + + def after_task_step(self, task: trio.lowlevel.Task) -> None: + """After task step.""" + self._print_with_task("<<< task step finished", task) + + def task_exited(self, task: trio.lowlevel.Task) -> None: + """Task exited.""" + self._print_with_task("### task exited", task) + + def before_io_wait(self, timeout: float) -> None: + """Before IO wait.""" + if timeout: + print(f"### waiting for I/O for up to {timeout} seconds") + else: + print("### doing a quick check for I/O") + self._sleep_time = trio.current_time() + + def after_io_wait(self, timeout: float) -> None: + """After IO wait.""" + duration = trio.current_time() - self._sleep_time + print(f"### finished I/O check (took {duration} seconds)") + + def after_run(self) -> None: + """After run.""" + print("!!! run finished") + + +def run() -> None: + """Run asynchronous side of everything.""" + trio.run(async_run) # , instruments=[Tracer()]) + + +# save_crash_img() + +if __name__ == "__main__": + print(f"{__title__} v{__version__}\nProgrammed by {__author__}.\n") + + # Make sure the game will display correctly on high DPI monitors on Windows. + if sys.platform == "win32": + # Exists on windows but not on linux or macos + # Windows raises attr-defined + # others say unused-ignore + from ctypes import windll # type: ignore[attr-defined,unused-ignore] + + with contextlib.suppress(AttributeError): + windll.user32.SetProcessDPIAware() + del windll + + try: + pygame.init() + run() + finally: + pygame.quit() diff --git a/src/azul/namedtuple_mod.py b/src/azul/namedtuple_mod.py index 1832697..21ba1e2 100644 --- a/src/azul/namedtuple_mod.py +++ b/src/azul/namedtuple_mod.py @@ -1,4 +1,14 @@ -"""typing.NamedTupleMeta mod.""" +"""typing.NamedTupleMeta modification. + +Removes the requirement that NamedTuple can only inherit from +NamedTuple or Generic + +Licensed under the Python Software Foundation License +(see https://github.com/python/cpython/blob/main/LICENSE) + +Original source that this is a modified portion of: +https://github.com/python/cpython/blob/main/Lib/typing.py +""" from __future__ import annotations diff --git a/src/azul/network.py b/src/azul/network.py deleted file mode 100644 index 165a270..0000000 --- a/src/azul/network.py +++ /dev/null @@ -1,512 +0,0 @@ -"""Network - Module for sending events over the network.""" - -# Programmed by CoolCat467 - -from __future__ import annotations - -# Copyright (C) 2023-2024 CoolCat467 -# -# This program 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. -# -# This program 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. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -__title__ = "Network" -__author__ = "CoolCat467" -__license__ = "GNU General Public License Version 3" -__version__ = "0.0.0" - - -import contextlib -from typing import ( - TYPE_CHECKING, - Any, - Literal, - NoReturn, -) - -import trio - -from azul.base_io import ( - BaseAsyncReader, - BaseAsyncWriter, - StructFormat, -) -from azul.buffer import Buffer -from azul.component import ( - Component, - ComponentManager, - Event, -) - -if TYPE_CHECKING: - from types import TracebackType - - from typing_extensions import Self - - -class NetworkTimeoutError(Exception): - """Network Timeout Error.""" - - __slots__ = () - - -class NetworkEOFError(Exception): - """Network End of File Error.""" - - __slots__ = () - - -class NetworkStreamNotConnectedError(Exception): - """Network Stream Not Connected Error.""" - - __slots__ = () - - -class NetworkComponent(Component, BaseAsyncReader, BaseAsyncWriter): - """Network Component (client).""" - - __slots__ = ("_stream", "timeout") - - def __init__(self, name: str) -> None: - """Initialize Network Component.""" - super().__init__(name) - - self.timeout: int | float = 3 - self._stream: trio.SocketStream | None = None - - @property - def not_connected(self) -> bool: - """Is stream None?.""" - return self._stream is None - - @property - def stream(self) -> trio.SocketStream: - """Trio SocketStream or raise NetworkStreamNotConnectedError.""" - if self._stream is None: - raise NetworkStreamNotConnectedError("Stream not connected!") - return self._stream - - @classmethod - def from_stream( - cls, - *args: object, - kwargs: dict[str, object] | None = None, - stream: trio.SocketStream, - ) -> Self: - """Initialize from stream.""" - if kwargs is None: - kwargs = {} - self = cls(*args, **kwargs) # type: ignore[arg-type] - self._stream = stream - return self - - async def connect(self, host: str, port: int) -> None: - """Connect to host:port on TCP. - - Raises: - OSError: if the connection fails. - RuntimeError: if stream is already connected - - """ - if not self.not_connected: - raise RuntimeError("Already connected!") - try: # pragma: nocover - self._stream = await trio.open_tcp_stream(host, port) - except OSError: # pragma: nocover - await self.close() - raise - - async def read(self, length: int) -> bytearray: - """Read `length` bytes from stream. - - Can raise following exceptions: - NetworkStreamNotConnectedError - Network stream is not connected - NetworkTimeoutError - Timeout - NetworkEOFError - End of File - OSError - Stopped responding - trio.BusyResourceError - Another task is already writing data - trio.BrokenResourceError - Something is wrong and stream is broken - trio.ClosedResourceError - Stream is closed or another task closes stream - """ - content = bytearray() - while max_read_count := length - len(content): - received = b"" - # try: - with trio.move_on_after(self.timeout) as cancel_scope: - received = await self.stream.receive_some(max_read_count) - cancel_called = cancel_scope.cancel_called - # except (trio.BrokenResourceError, trio.ClosedResourceError): - # await self.close() - # raise - if len(received) == 0: - # No information at all - if len(content) == 0: - if cancel_called: - raise NetworkTimeoutError("Read timed out.") - raise NetworkEOFError( - "Server did not respond with any information.", - ) - # Only sent a few bytes, but we requested more - raise OSError( - f"Server stopped responding (got {len(content)} bytes, " - f"but expected {length} bytes)." - f" Partial obtained packet: {content!r}", - ) - content.extend(received) - return content - - async def write(self, data: bytes | bytearray | memoryview) -> None: - """Send the given data through the stream, blocking if necessary. - - Args: - data (bytes, bytearray, or memoryview): The data to send. - - Raises: - trio.BusyResourceError: if another task is already executing a - :meth:`send_all`, :meth:`wait_send_all_might_not_block`, or - :meth:`HalfCloseableStream.send_eof` on this stream. - trio.BrokenResourceError: if something has gone wrong, and the stream - is broken. - trio.ClosedResourceError: if you previously closed this stream - object, or if another task closes this stream object while - :meth:`send_all` is running. - - Most low-level operations in Trio provide a guarantee: if they raise - :exc:`trio.Cancelled`, this means that they had no effect, so the - system remains in a known state. This is **not true** for - :meth:`send_all`. If this operation raises :exc:`trio.Cancelled` (or - any other exception for that matter), then it may have sent some, all, - or none of the requested data, and there is no way to know which. - - Copied from Trio docs. - - """ - await self.stream.send_all(data) - - # try: - # await self.stream.send_all(data) - # except (trio.BrokenResourceError, trio.ClosedResourceError): - # await self.close() - # raise - - async def close(self) -> None: - """Close the stream, possibly blocking.""" - if self._stream is None: - await trio.lowlevel.checkpoint() - return - await self._stream.aclose() - self._stream = None - - async def send_eof(self) -> None: - """Close the sending half of the stream. - - This corresponds to ``shutdown(..., SHUT_WR)`` (`man - page `__). - - If an EOF has already been sent, then this method should silently - succeed. - - Raises: - trio.BusyResourceError: if another task is already executing a - :meth:`~SendStream.send_all`, - :meth:`~SendStream.wait_send_all_might_not_block`, or - :meth:`send_eof` on this stream. - trio.BrokenResourceError: if something has gone wrong, and the stream - is broken. - - Suppresses: - trio.ClosedResourceError: if you previously closed this stream - object, or if another task closes this stream object while - :meth:`send_eof` is running. - - Copied from trio docs. - - """ - with contextlib.suppress(trio.ClosedResourceError): - await self.stream.send_eof() - - async def wait_write_might_not_block(self) -> None: - """Block until it's possible that :meth:`write` might not block. - - This method may return early: it's possible that after it returns, - :meth:`send_all` will still block. (In the worst case, if no better - implementation is available, then it might always return immediately - without blocking. It's nice to do better than that when possible, - though.) - - This method **must not** return *late*: if it's possible for - :meth:`send_all` to complete without blocking, then it must - return. When implementing it, err on the side of returning early. - - Raises: - trio.BusyResourceError: if another task is already executing a - :meth:`send_all`, :meth:`wait_send_all_might_not_block`, or - :meth:`HalfCloseableStream.send_eof` on this stream. - trio.BrokenResourceError: if something has gone wrong, and the stream - is broken. - trio.ClosedResourceError: if you previously closed this stream - object, or if another task closes this stream object while - :meth:`wait_send_all_might_not_block` is running. - - Note: - This method is intended to aid in implementing protocols that want - to delay choosing which data to send until the last moment. E.g., - suppose you're working on an implementation of a remote display server - like `VNC - `__, and - the network connection is currently backed up so that if you call - :meth:`send_all` now then it will sit for 0.5 seconds before actually - sending anything. In this case it doesn't make sense to take a - screenshot, then wait 0.5 seconds, and then send it, because the - screen will keep changing while you wait; it's better to wait 0.5 - seconds, then take the screenshot, and then send it, because this - way the data you deliver will be more - up-to-date. Using :meth:`wait_send_all_might_not_block` makes it - possible to implement the better strategy. - - If you use this method, you might also want to read up on - ``TCP_NOTSENT_LOWAT``. - - Further reading: - - * `Prioritization Only Works When There's Pending Data to Prioritize - `__ - - * WWDC 2015: Your App and Next Generation Networks: `slides - `__, - `video and transcript - `__ - - Copied from Trio docs. - - """ - return await self.stream.wait_send_all_might_not_block() - - async def __aenter__(self) -> Self: - """Async context manager enter.""" - return self - - async def __aexit__( - self, - exc_type: type[BaseException] | None, - exc_value: BaseException | None, - traceback: TracebackType | None, - ) -> None: - """Async context manager exit. Close connection.""" - await self.close() - - -# async def send_eof_and_close(self) -> None: -# """Send EOF and close.""" -# await self.send_eof() -# await self.close() - - -class NetworkEventComponent(NetworkComponent): - """Network Event Component - Send events over the network.""" - - __slots__ = ( - "_read_packet_id_to_event_name", - "_write_event_name_to_packet_id", - "read_lock", - "write_lock", - ) - - # Max of 255 packet ids - # Next higher is USHORT with 65535 packet ids - packet_id_format: Literal[StructFormat.UBYTE] = StructFormat.UBYTE - - def __init__(self, name: str) -> None: - """Initialize Network Event Component.""" - super().__init__(name) - self._read_packet_id_to_event_name: dict[int, str] = {} - self._write_event_name_to_packet_id: dict[str, int] = {} - self.read_lock = trio.Lock() - self.write_lock = trio.Lock() - - def bind_handlers(self) -> None: - """Register serverbound event handlers.""" - self.register_handlers( - dict.fromkeys( - self._write_event_name_to_packet_id, - self.write_event, - ), - ) - - def register_network_write_event( - self, - event_name: str, - packet_id: int, - ) -> None: - """Map event name to serverbound packet id. - - Raises: - ValueError: Event name already registered or infinite network loop. - - """ - if event_name in self._write_event_name_to_packet_id: - raise ValueError(f"{event_name!r} event already registered!") - if self._read_packet_id_to_event_name.get(packet_id) == event_name: - raise ValueError( - f"{event_name!r} events are also being received " - f"from server with packet id {packet_id!r}, " - "which will would lead to infinite looping over network", - ) - self._write_event_name_to_packet_id[event_name] = packet_id - if self.manager_exists: - self.register_handler(event_name, self.write_event) - - def register_network_write_events(self, event_map: dict[str, int]) -> None: - """Map event names to serverbound packet ids.""" - for event_name, packet_id in event_map.items(): - self.register_network_write_event(event_name, packet_id) - - async def write_event(self, event: Event[bytearray | bytes]) -> None: - """Send event to network. - - Raises: - RuntimeError: if unregistered packet id received from network - trio.BusyResourceError: if another task is already executing a - :meth:`send_all`, :meth:`wait_send_all_might_not_block`, or - :meth:`HalfCloseableStream.send_eof` on this stream. - trio.BrokenResourceError: if something has gone wrong, and the stream - is broken. - trio.ClosedResourceError: if you previously closed this stream - object, or if another task closes this stream object while - :meth:`send_all` is running. - - """ - packet_id = self._write_event_name_to_packet_id.get(event.name) - if packet_id is None: - raise RuntimeError(f"Unhandled network event name {event.name!r}") - buffer = Buffer() - buffer.write_value(self.packet_id_format, packet_id) - buffer.write_bytearray(event.data) - async with self.write_lock: - await self.write(buffer) - - async def read_event(self) -> Event[bytearray]: - """Receive event from network. - - Can raise following exceptions: - RuntimeError - Unhandled packet id - NetworkStreamNotConnectedError - Network stream is not connected - NetworkTimeoutError - Timeout or no data - OSError - Stopped responding - trio.BrokenResourceError - Something is wrong and stream is broken - trio.ClosedResourceError - Stream is closed or another task closes stream - - Shouldn't happen with write lock but still: - trio.BusyResourceError - Another task is already writing data - """ - async with self.read_lock: - packet_id = await self.read_value(self.packet_id_format) - event_data = await self.read_bytearray() - event_name = self._read_packet_id_to_event_name.get(packet_id) - if event_name is None: - raise RuntimeError(f"Unhandled packet ID {packet_id!r}") - return Event(event_name, event_data) - - def register_read_network_event( - self, - packet_id: int, - event_name: str, - ) -> None: - """Map clientbound packet id to event name.""" - if packet_id in self._read_packet_id_to_event_name: - raise ValueError(f"Packet ID {packet_id!r} already registered!") - if self._write_event_name_to_packet_id.get(event_name) == packet_id: - raise ValueError( - f"Packet id {packet_id!r} packets are also being received " - f"from server with as {event_name!r} events, " - "which will would lead to infinite looping over network", - ) - self._read_packet_id_to_event_name[packet_id] = event_name - - def register_read_network_events(self, packet_map: dict[int, str]) -> None: - """Map clientbound packet ids to event names.""" - for packet_id, event_name in packet_map.items(): - self.register_read_network_event(packet_id, event_name) - - -class Server(ComponentManager): - """Asynchronous TCP Server.""" - - __slots__ = ("serve_cancel_scope",) - - def __init__(self, name: str, own_name: str | None = None) -> None: - """Initialize Server.""" - super().__init__(name, own_name) - self.serve_cancel_scope: trio.CancelScope | None = None - - def stop_serving(self) -> None: - """Cancel serve scope immediately. - - This method is idempotent, i.e., if the scope was already - cancelled then this method silently does nothing. - """ - if self.serve_cancel_scope is None: - return - self.serve_cancel_scope.cancel() - - # "Implicit return in function which does not return" - async def serve( # type: ignore[misc] # pragma: nocover - self, - port: int, - host: str | bytes | None = None, - backlog: int | None = None, - ) -> NoReturn: - """Serve over TCP. See trio.open_tcp_listeners for argument details.""" - self.serve_cancel_scope = trio.CancelScope() - async with trio.open_nursery() as nursery: - listeners = await trio.open_tcp_listeners( - port, - host=host, - backlog=backlog, - ) - - async def handle_serve( - task_status: trio.TaskStatus[Any] = trio.TASK_STATUS_IGNORED, - ) -> None: - assert self.serve_cancel_scope is not None - try: - with self.serve_cancel_scope: - await trio.serve_listeners( - self.handler, - listeners, - handler_nursery=nursery, - task_status=task_status, - ) - except trio.Cancelled: - # Close all listeners - async with trio.open_nursery() as cancel_nursery: - for listener in listeners: - cancel_nursery.start_soon(listener.aclose) - - await nursery.start(handle_serve) - - async def handler( - self, - stream: trio.SocketStream, - ) -> None: # pragma: nocover - """Handle new client streams. - - Override in a subclass - Default only closes the stream - """ - try: - await stream.send_eof() - finally: - await stream.aclose() - - -if __name__ == "__main__": # pragma: nocover - print(f"{__title__}\nProgrammed by {__author__}.\n") diff --git a/src/azul/network_shared.py b/src/azul/network_shared.py index ce56c88..65b2ba1 100644 --- a/src/azul/network_shared.py +++ b/src/azul/network_shared.py @@ -24,11 +24,18 @@ __license__ = "GNU General Public License Version 3" +from collections import Counter from enum import IntEnum, auto -from typing import Final, NamedTuple, TypeAlias +from typing import TYPE_CHECKING, Final, TypeAlias -import trio +from libcomponent.base_io import StructFormat +from libcomponent.buffer import Buffer from mypy_extensions import u8 +from numpy import int8, zeros + +if TYPE_CHECKING: + from numpy.typing import NDArray + ADVERTISEMENT_IP: Final = "224.0.2.60" ADVERTISEMENT_PORT: Final = 4445 @@ -38,41 +45,83 @@ Pos: TypeAlias = tuple[u8, u8] -class TickEventData(NamedTuple): - """Tick Event Data.""" +def encode_tile_count(tile_color: u8, tile_count: u8) -> Buffer: + """Return buffer from tile color and count.""" + buffer = Buffer() + + buffer.write_value(StructFormat.UBYTE, tile_color) + buffer.write_value(StructFormat.UBYTE, tile_count) + + return buffer + + +def decode_tile_count(buffer: Buffer) -> tuple[u8, u8]: + """Read and return tile color and count from buffer.""" + tile_color = buffer.read_value(StructFormat.UBYTE) + tile_count = buffer.read_value(StructFormat.UBYTE) + + return (tile_color, tile_count) + + +def encode_numeric_uint8_counter(counter: Counter[int]) -> Buffer: + """Return buffer from uint8 counter.""" + buffer = Buffer() + + buffer.write_value(StructFormat.UBYTE, len(counter)) + for key, value in counter.items(): + assert isinstance(key, int) + assert value >= 0 + buffer.extend(encode_tile_count(key, value)) + + return buffer + + +def decode_numeric_uint8_counter(buffer: Buffer) -> Counter[u8]: + """Read and return uint8 counter from buffer.""" + data: dict[u8, u8] = {} + + pair_count = buffer.read_value(StructFormat.UBYTE) + for _ in range(pair_count): + key, value = decode_tile_count(buffer) + assert key not in data + data[key] = value + + return Counter(data) + + +def encode_int8_array(array: NDArray[int8]) -> Buffer: + """Return buffer from int8 array flat values.""" + buffer = Buffer() + + for value in array.flat: + buffer.write_value(StructFormat.BYTE, int(value)) + + return buffer - time_passed: float - fps: float +def decode_int8_array(buffer: Buffer, size: tuple[int, ...]) -> NDArray[int8]: + """Return flattened int8 array from buffer.""" + array = zeros(size, dtype=int8) -# Stolen from WOOF (Web Offer One File), Copyright (C) 2004-2009 Simon Budig, -# available at http://www.home.unix-ag.org/simon/woof -# with modifications + for index in range(array.size): + array.flat[index] = buffer.read_value(StructFormat.BYTE) -# Utility function to guess the IP (as a string) where the server can be -# reached from the outside. Quite nasty problem actually. + return array -async def find_ip() -> str: # pragma: nocover - """Guess the IP where the server can be found from the network.""" - # we get a UDP-socket for the TEST-networks reserved by IANA. - # It is highly unlikely, that there is special routing used - # for these networks, hence the socket later should give us - # the IP address of the default route. - # We're doing multiple tests, to guard against the computer being - # part of a test installation. +def encode_cursor_location(scaled_location: tuple[int, int]) -> bytes: + """Return buffer from cursor location.""" + x, y = scaled_location + position = ((x & 0xFFF) << 12) | (y & 0xFFF) + return (position & 0xFFFFFF).to_bytes(3) - candidates: list[str] = [] - for test_ip in ("192.0.2.0", "198.51.100.0", "203.0.113.0"): - sock = trio.socket.socket(trio.socket.AF_INET, trio.socket.SOCK_DGRAM) - await sock.connect((test_ip, 80)) - ip_addr: str = sock.getsockname()[0] - sock.close() - if ip_addr in candidates: - return ip_addr - candidates.append(ip_addr) - return candidates[0] +def decode_cursor_location(buffer: bytes | bytearray) -> tuple[int, int]: + """Return cursor location from buffer.""" + value = int.from_bytes(buffer) & 0xFFFFFF + x = (value >> 12) & 0xFFF + y = value & 0xFFF + return (x, y) class ClientBoundEvents(IntEnum): @@ -82,21 +131,24 @@ class ClientBoundEvents(IntEnum): callback_ping = auto() initial_config = auto() playing_as = auto() - create_piece = auto() - select_piece = auto() - create_tile = auto() - delete_tile = auto() - animation_state = auto() - delete_piece_animation = auto() - update_piece_animation = auto() - move_piece_animation = auto() - action_complete = auto() game_over = auto() + board_data = auto() + pattern_data = auto() + factory_data = auto() + cursor_data = auto() + table_data = auto() + cursor_movement_mode = auto() + current_turn_change = auto() + cursor_position = auto() + floor_data = auto() class ServerBoundEvents(IntEnum): """Server bound event IDs.""" encryption_response = 0 - select_piece = auto() - select_tile = auto() + factory_clicked = auto() + cursor_location = auto() + pattern_row_clicked = auto() + table_clicked = auto() + floor_clicked = auto() diff --git a/src/azul/objects.py b/src/azul/objects.py index 0f89f07..77d71a8 100644 --- a/src/azul/objects.py +++ b/src/azul/objects.py @@ -35,10 +35,9 @@ from azul import sprite if TYPE_CHECKING: + from libcomponent.component import Event from pygame.font import Font - from azul.component import Event - class Text(sprite.Sprite): """Text element. diff --git a/src/azul/screenshots/Crash_at_Wed-Nov-13-15_30_42-2024.png b/src/azul/screenshots/Crash_at_Wed-Nov-13-15_30_42-2024.png new file mode 100644 index 0000000..2f51576 Binary files /dev/null and b/src/azul/screenshots/Crash_at_Wed-Nov-13-15_30_42-2024.png differ diff --git a/src/azul/screenshots/Crash_at_Wed_Nov_13_16_02_46_2024.png b/src/azul/screenshots/Crash_at_Wed_Nov_13_16_02_46_2024.png new file mode 100644 index 0000000..61fe23d Binary files /dev/null and b/src/azul/screenshots/Crash_at_Wed_Nov_13_16_02_46_2024.png differ diff --git a/src/azul/screenshots/Crash_at_Wed_Nov_13_16_20_10_2024.png b/src/azul/screenshots/Crash_at_Wed_Nov_13_16_20_10_2024.png new file mode 100644 index 0000000..0d712ac Binary files /dev/null and b/src/azul/screenshots/Crash_at_Wed_Nov_13_16_20_10_2024.png differ diff --git a/src/azul/screenshots/Screenshot_at_Sun-Jun-13-10:46:34-2021.png b/src/azul/screenshots/Screenshot_at_Sun-Jun-13-10_46_34-2021.png similarity index 100% rename from src/azul/screenshots/Screenshot_at_Sun-Jun-13-10:46:34-2021.png rename to src/azul/screenshots/Screenshot_at_Sun-Jun-13-10_46_34-2021.png diff --git a/src/azul/server.py b/src/azul/server.py new file mode 100755 index 0000000..9b3697e --- /dev/null +++ b/src/azul/server.py @@ -0,0 +1,1462 @@ +#!/usr/bin/env python3 +# Azul Game Server + +"""Azul Game Server.""" + +# Programmed by CoolCat467 + +from __future__ import annotations + +# Copyright (C) 2023-2024 CoolCat467 +# +# This program 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. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +__title__ = "Server" +__author__ = "CoolCat467" +__license__ = "GNU General Public License Version 3" +__version__ = "0.0.0" + +import traceback +from collections import deque +from enum import IntEnum, auto +from functools import partial +from typing import TYPE_CHECKING, NoReturn + +import trio +from libcomponent import network +from libcomponent.base_io import StructFormat +from libcomponent.buffer import Buffer +from libcomponent.component import ( + ComponentManager, + Event, + ExternalRaiseManager, +) +from libcomponent.network_utils import ( + ServerClientNetworkEventComponent, + find_ip, +) +from numpy import array, int8 + +from azul.network_shared import ( + ADVERTISEMENT_IP, + ADVERTISEMENT_PORT, + DEFAULT_PORT, + ClientBoundEvents, + ServerBoundEvents, + decode_cursor_location, + encode_cursor_location, + encode_int8_array, + encode_numeric_uint8_counter, + encode_tile_count, +) +from azul.state import FLOOR_LINE_DATA, Phase, State, Tile + +if TYPE_CHECKING: + from collections import Counter + from collections.abc import Awaitable, Callable + + from numpy.typing import NDArray + + +# cursor_set_movement_mode +# cursor_set_destination + + +class ServerClient(ServerClientNetworkEventComponent): + """Server Client Network Event Component. + + When clients connect to server, this class handles the incoming + connections to the server in the way of reading and raising events + that are transferred over the network. + """ + + __slots__ = ("client_id",) + + def __init__(self, client_id: int) -> None: + """Initialize Server Client.""" + self.client_id = client_id + super().__init__(f"client_{client_id}") + + self.timeout = 3 + + cbe = ClientBoundEvents + self.register_network_write_events( + { + "server[write]->encryption_request": cbe.encryption_request, + "server[write]->callback_ping": cbe.callback_ping, + "server[write]->initial_config": cbe.initial_config, + "server[write]->playing_as": cbe.playing_as, + "server[write]->game_over": cbe.game_over, + "server[write]->board_data": cbe.board_data, + "server[write]->pattern_data": cbe.pattern_data, + "server[write]->factory_data": cbe.factory_data, + "server[write]->cursor_data": cbe.cursor_data, + "server[write]->table_data": cbe.table_data, + "server[write]->cursor_movement_mode": cbe.cursor_movement_mode, + "server[write]->current_turn_change": cbe.current_turn_change, + "server[write]->cursor_position": cbe.cursor_position, + "server[write]->floor_data": cbe.floor_data, + }, + ) + sbe = ServerBoundEvents + self.register_read_network_events( + { + sbe.encryption_response: f"client[{self.client_id}]->encryption_response", + sbe.factory_clicked: f"client[{self.client_id}]->factory_clicked", + sbe.cursor_location: f"client[{self.client_id}]->cursor_location", + sbe.pattern_row_clicked: f"client[{self.client_id}]->pattern_row_clicked", + sbe.table_clicked: f"client[{self.client_id}]->table_clicked", + sbe.floor_clicked: f"client[{self.client_id}]->floor_clicked", + }, + ) + + def bind_handlers(self) -> None: + """Bind event handlers.""" + super().bind_handlers() + self.register_handlers( + { + f"client[{self.client_id}]->encryption_response": self.handle_encryption_response, + f"client[{self.client_id}]->factory_clicked": self.read_factory_clicked, + f"client[{self.client_id}]->cursor_location": self.read_cursor_location, + f"client[{self.client_id}]->pattern_row_clicked": self.read_pattern_row_clicked, + f"client[{self.client_id}]->table_clicked": self.read_table_clicked, + f"client[{self.client_id}]->floor_clicked": self.read_floor_clicked, + f"callback_ping->network[{self.client_id}]": self.handle_callback_ping, + "initial_config->network": self.write_initial_config, + f"playing_as->network[{self.client_id}]": self.write_playing_as, + "game_over->network": self.write_game_over, + "board_data->network": self.write_board_data, + "factory_data->network": self.write_factory_data, + "cursor_data->network": self.write_cursor_data, + "table_data->network": self.write_table_data, + f"cursor_movement_mode->network[{self.client_id}]": self.write_cursor_movement_mode, + f"cursor_position->network[{self.client_id}]": self.write_cursor_position, + "current_turn_change->network": self.write_current_turn_change, + "pattern_data->network": self.write_pattern_data, + "floor_data->network": self.write_floor_data, + }, + ) + + async def start_encryption_request(self) -> None: + """Start encryption request and raise as `server[write]->encryption_request`.""" + await super().start_encryption_request() + + event = await self.read_event() + if event.name != f"client[{self.client_id}]->encryption_response": + raise RuntimeError( + f"Expected encryption response, got but {event.name!r}", + ) + await self.handle_encryption_response(event) + + async def read_factory_clicked(self, event: Event[bytearray]) -> None: + """Read factory_clicked event from client. Raise as `factory_clicked->server`.""" + buffer = Buffer(event.data) + + factory_id = buffer.read_value(StructFormat.UBYTE) + tile_color = Tile(buffer.read_value(StructFormat.UBYTE)) + + await self.raise_event( + Event( + "factory_clicked->server", + ( + self.client_id, + factory_id, + tile_color, + ), + ), + ) + + async def read_cursor_location(self, event: Event[bytearray]) -> None: + """Read factory_clicked event from client. Raise as `factory_clicked->server`.""" + x, y = decode_cursor_location(event.data) + + await self.raise_event( + Event( + "cursor_location->server", + ( + self.client_id, + (x, y), + ), + ), + ) + + async def read_pattern_row_clicked(self, event: Event[bytearray]) -> None: + """Read pattern_row_clicked event from client. Raise as `pattern_row_clicked->server`.""" + buffer = Buffer(event.data) + + row_id = buffer.read_value(StructFormat.UBYTE) + row_pos_x = buffer.read_value(StructFormat.UBYTE) + row_pos_y = buffer.read_value(StructFormat.UBYTE) + + await self.raise_event( + Event( + "pattern_row_clicked->server", + ( + self.client_id, + row_id, + (row_pos_x, row_pos_y), + ), + ), + ) + + async def read_table_clicked(self, event: Event[bytearray]) -> None: + """Read table_clicked event from client. Raise as `table_clicked->server`.""" + buffer = Buffer(event.data) + + tile_color = Tile(buffer.read_value(StructFormat.UBYTE)) + + await self.raise_event( + Event( + "table_clicked->server", + ( + self.client_id, + tile_color, + ), + ), + ) + + async def read_floor_clicked(self, event: Event[bytearray]) -> None: + """Read floor_clicked event from client. Raise as `floor_clicked->server`.""" + buffer = Buffer(event.data) + + floor_line_id = buffer.read_value(StructFormat.UBYTE) + floor_line_pos_x = buffer.read_value(StructFormat.UBYTE) + + await self.raise_event( + Event( + "floor_clicked->server", + ( + self.client_id, + floor_line_id, + floor_line_pos_x, + ), + ), + ) + + async def handle_callback_ping( + self, + _: Event[None], + ) -> None: + """Reraise as server[write]->callback_ping.""" + await self.write_callback_ping() + + async def write_initial_config( + self, + event: Event[tuple[bool, int, int, int, tuple[int, ...]]], + ) -> None: + """Read initial config event and reraise as server[write]->initial_config.""" + variant_play, player_count, factory_count, current_turn, floor_data = ( + event.data + ) + + buffer = Buffer() + + buffer.write_value(StructFormat.BOOL, variant_play) + buffer.write_value(StructFormat.UBYTE, player_count) + buffer.write_value(StructFormat.UBYTE, factory_count) + buffer.write_value(StructFormat.UBYTE, current_turn) + buffer.write_value(StructFormat.UBYTE, len(floor_data)) + buffer.extend(encode_int8_array(array(floor_data, dtype=int8))) + + await self.write_event(Event("server[write]->initial_config", buffer)) + + async def write_playing_as( + self, + event: Event[int], + ) -> None: + """Read playing as event and reraise as server[write]->playing_as.""" + playing_as = event.data + + buffer = Buffer() + buffer.write_value(StructFormat.UBYTE, playing_as) + await self.write_event(Event("server[write]->playing_as", buffer)) + + async def write_game_over(self, event: Event[int]) -> None: + """Read game over event and reraise as server[write]->game_over.""" + winner = event.data + + buffer = Buffer() + + buffer.write_value(StructFormat.UBYTE, winner) + + await self.write_event(Event("server[write]->game_over", buffer)) + + async def write_board_data( + self, + event: Event[tuple[int, NDArray[int8]]], + ) -> None: + """Reraise as server[write]->board_data.""" + player_id, array = event.data + + buffer = Buffer() + buffer.write_value(StructFormat.UBYTE, player_id) + buffer.extend(encode_int8_array(array)) + + await self.write_event(Event("server[write]->board_data", buffer)) + + async def write_factory_data( + self, + event: Event[tuple[int, Counter[int]]], + ) -> None: + """Reraise as server[write]->factory_data.""" + factory_id, tiles = event.data + + buffer = Buffer() + buffer.write_value(StructFormat.UBYTE, factory_id) + buffer.extend(encode_numeric_uint8_counter(tiles)) + + await self.write_event(Event("server[write]->factory_data", buffer)) + + async def write_cursor_data( + self, + event: Event[Counter[int]], + ) -> None: + """Reraise as server[write]->cursor_data.""" + tiles = event.data + + buffer = encode_numeric_uint8_counter(tiles) + + await self.write_event(Event("server[write]->cursor_data", buffer)) + + async def write_table_data( + self, + event: Event[Counter[int]], + ) -> None: + """Reraise as server[write]->table_data.""" + tiles = event.data + + buffer = encode_numeric_uint8_counter(tiles) + + await self.write_event(Event("server[write]->table_data", buffer)) + + async def write_cursor_movement_mode( + self, + event: Event[bool], + ) -> None: + """Reraise as server[write]->cursor_movement_mode.""" + client_mode = event.data + + buffer = Buffer() + buffer.write_value(StructFormat.BOOL, client_mode) + + await self.write_event( + Event("server[write]->cursor_movement_mode", buffer), + ) + + async def write_cursor_position( + self, + event: Event[tuple[int, int]], + ) -> None: + """Reraise as server[write]->cursor_position.""" + buffer = encode_cursor_location(event.data) + + await self.write_event( + Event("server[write]->cursor_position", buffer), + ) + + async def write_current_turn_change( + self, + event: Event[int], + ) -> None: + """Reraise as server[write]->current_turn_change.""" + pattern_id = event.data + + buffer = Buffer() + buffer.write_value(StructFormat.UBYTE, pattern_id) + + await self.write_event( + Event("server[write]->current_turn_change", buffer), + ) + + async def write_pattern_data( + self, + event: Event[tuple[int, int, tuple[int, int]]], + ) -> None: + """Reraise as server[write]->board_data.""" + player_id, row_id, (tile_color, tile_count) = event.data + + buffer = Buffer() + buffer.write_value(StructFormat.UBYTE, player_id) + buffer.write_value(StructFormat.UBYTE, row_id) + assert tile_color >= 0 + buffer.extend(encode_tile_count(tile_color, tile_count)) + + await self.write_event( + Event("server[write]->pattern_data", buffer), + ) + + async def write_floor_data( + self, + event: Event[tuple[int, Counter[int]]], + ) -> None: + """Reraise as server[write]->floor_data.""" + floor_id, floor_line = event.data + + buffer = Buffer() + buffer.write_value(StructFormat.UBYTE, floor_id) + buffer.extend(encode_numeric_uint8_counter(floor_line)) + + await self.write_event( + Event("server[write]->floor_data", buffer), + ) + + +class ServerPlayer(IntEnum): + """Server Player enum.""" + + one = 0 + two = auto() + three = auto() + four = auto() + singleplayer_all = auto() + spectator = auto() + + +class GameServer(network.Server): + """Azul server. + + Handles accepting incoming connections from clients and handles + main game logic via State subclass above. + """ + + __slots__ = ( + "actions_queue", + "advertisement_scope", + "client_count", + "client_players", + "internal_singleplayer_mode", + "players_can_interact", + "running", + "state", + ) + + max_clients = 4 + + def __init__(self, internal_singleplayer_mode: bool = False) -> None: + """Initialize server.""" + super().__init__("GameServer") + + self.client_count: int = 0 + self.state = State.blank() + + self.client_players: dict[int, int] = {} + self.players_can_interact: bool = False + + self.internal_singleplayer_mode = internal_singleplayer_mode + self.advertisement_scope: trio.CancelScope | None = None + self.running = False + + def bind_handlers(self) -> None: + """Register start_server and stop_server.""" + self.register_handlers( + { + "server_start": self.start_server, + "network_stop": self.stop_server, + "server_send_game_start": self.handle_server_start_new_game, + "factory_clicked->server": self.handle_client_factory_clicked, + "pattern_row_clicked->server": self.handle_client_pattern_row_clicked, + "cursor_location->server": self.handle_cursor_location, + "table_clicked->server": self.handle_client_table_clicked, + "floor_clicked->server": self.handle_client_floor_clicked, + }, + ) + + async def stop_server(self, event: Event[None] | None = None) -> None: + """Stop serving and disconnect all NetworkEventComponents.""" + self.stop_serving() + self.stop_advertising() + + close_methods: deque[Callable[[], Awaitable[object]]] = deque() + for component in self.get_all_components(): + if isinstance(component, network.NetworkEventComponent): + close_methods.append(component.close) + print(f"stop_server {component.name = }") + self.remove_component(component.name) + async with trio.open_nursery() as nursery: + while close_methods: + nursery.start_soon(close_methods.popleft()) + self.running = False + + async def post_advertisement( + self, + udp_socket: trio.socket.SocketType, + send_to_ip: str | int, + hosting_port: int, + ) -> None: + """Post server advertisement packet.""" + motd = "Azul Game" + advertisement = ( + f"[AD]{hosting_port}[/AD][AZUL]{motd}[/AZUL]" + ).encode() + # print("post_advertisement") + await udp_socket.sendto( + advertisement, + (send_to_ip, ADVERTISEMENT_PORT), + ) + + def stop_advertising(self) -> None: + """Cancel self.advertisement_scope.""" + if self.advertisement_scope is None: + return + self.advertisement_scope.cancel() + + async def post_advertisements(self, hosting_port: int) -> None: + """Post lan UDP packets so server can be found.""" + self.stop_advertising() + self.advertisement_scope = trio.CancelScope() + + # Look up multicast group address in name server and find out IP version + addrinfo = (await trio.socket.getaddrinfo(ADVERTISEMENT_IP, None))[0] + send_to_ip = addrinfo[4][0] + + with trio.socket.socket( + family=trio.socket.AF_INET, # IPv4 + type=trio.socket.SOCK_DGRAM, # UDP + proto=trio.socket.IPPROTO_UDP, # UDP + ) as udp_socket: + # Set Time-to-live (optional) + # ttl_bin = struct.pack('@i', MYTTL) + # if addrinfo[0] == trio.socket.AF_INET: # IPv4 + # udp_socket.setsockopt( + # trio.socket.IPPROTO_IP, trio.socket.IP_MULTICAST_TTL, ttl_bin) + # else: + # udp_socket.setsockopt( + # trio.socket.IPPROTO_IPV6, trio.socket.IPV6_MULTICAST_HOPS, ttl_bin) + with self.advertisement_scope: + print("Starting advertisement posting.") + while True: # not self.can_start(): + try: + await self.post_advertisement( + udp_socket, + send_to_ip, + hosting_port, + ) + except OSError as exc: + traceback.print_exception(exc) + print( + f"{self.__class__.__name__}: Failed to post server advertisement", + ) + break + await trio.sleep(1.5) + print("Stopped advertisement posting.") + + @staticmethod + def setup_teams_internal(client_ids: list[int]) -> dict[int, int]: + """Return teams for internal server mode given sorted client ids.""" + players: dict[int, int] = {} + for idx, client_id in enumerate(client_ids): + if idx == 0: + players[client_id] = ServerPlayer.singleplayer_all + else: + players[client_id] = ServerPlayer.spectator + return players + + @staticmethod + def setup_teams(client_ids: list[int]) -> dict[int, int]: + """Return teams given sorted client ids.""" + players: dict[int, int] = {} + for idx, client_id in enumerate(client_ids): + if idx < 4: + players[client_id] = ServerPlayer(idx % 4) + else: + players[client_id] = ServerPlayer.spectator + return players + + def new_game_init(self, variant_play: bool = False) -> None: + """Start new game.""" + print("server new_game_init") + self.client_players.clear() + + self.state = State.new_game( + max(2, min(4, self.client_count)), + variant_play, + ) + + # Why keep track of another object just to know client ID numbers + # if we already have that with the components? No need! + client_ids: set[int] = set() + for component in self.get_all_components(): + if isinstance(component, ServerClient): + client_ids.add(component.client_id) + + sorted_client_ids = sorted(client_ids) + if self.internal_singleplayer_mode: + self.client_players = self.setup_teams_internal(sorted_client_ids) + else: + self.client_players = self.setup_teams(sorted_client_ids) + + self.players_can_interact = True + + # "Implicit return in function which does not return" + async def start_server( # type: ignore[misc] + self, + event: Event[tuple[str | None, int]], + ) -> NoReturn: + """Serve clients.""" + print(f"{self.__class__.__name__}: Closing old server clients") + await self.stop_server() + print(f"{self.__class__.__name__}: Starting Server") + + host, port = event.data + + self.running = True + async with trio.open_nursery() as nursery: + # Do not post advertisements when using internal singleplayer mode + if not self.internal_singleplayer_mode: + nursery.start_soon(self.post_advertisements, port) + # Serve runs forever until canceled + nursery.start_soon(partial(self.serve, port, host, backlog=0)) + + async def transmit_new_round_data(self) -> None: + """Transmit all player board data, factory data, and table center data.""" + async with trio.open_nursery() as nursery: + for player_id, player_data in self.state.player_data.items(): + # Transmit board data + nursery.start_soon( + self.raise_event, + Event( + "board_data->network", + ( + player_id, + player_data.wall, + ), + ), + ) + # Transmit floor line data + nursery.start_soon( + self.raise_event, + Event( + "floor_data->network", + ( + player_id, + player_data.floor, + ), + ), + ) + # Transmit factory data + for ( + factory_id, + factory_tiles, + ) in self.state.factory_displays.items(): + nursery.start_soon( + self.raise_event, + Event( + "factory_data->network", + ( + factory_id, + factory_tiles, + ), + ), + ) + + # Transmit table center data + await self.raise_event( + Event( + "table_data->network", + self.state.table_center, + ), + ) + + async def transmit_pattern_line_data(self) -> None: + """Transmit all pattern line data for all players.""" + async with trio.open_nursery() as nursery: + # Transmit pattern line data + for player_id, player_data in self.state.player_data.items(): + for line_id, line_data in enumerate(player_data.lines): + nursery.start_soon( + self.raise_event, + Event( + "pattern_data->network", + ( + player_id, + line_id, + ( + max(0, int(line_data.color)), + line_data.count_, + ), + ), + ), + ) + + async def transmit_cursor_movement_mode(self) -> None: + """Update current cursor movement mode for all clients.""" + client_id = self.find_client_id_from_state_turn( + self.state.current_turn, + ) + + await self.raise_event( + Event( + f"cursor_movement_mode->network[{client_id}]", + True, + ), + ) + + async with trio.open_nursery() as nursery: + for other_client_id in self.client_players: + if other_client_id != client_id: + nursery.start_soon( + self.raise_event, + Event( + f"cursor_movement_mode->network[{other_client_id}]", + False, + ), + ) + + async def transmit_playing_as(self) -> None: + """Transmit playing as.""" + async with trio.open_nursery() as nursery: + for client_id, team in self.client_players.items(): + nursery.start_soon( + self.raise_event, + Event(f"playing_as->network[{client_id}]", team), + ) + + async def handle_server_start_new_game(self, event: Event[bool]) -> None: + """Handle game start.""" + variant_play = event.data + ## # Delete all pieces from last state (shouldn't be needed but still.) + ## async with trio.open_nursery() as nursery: + ## for piece_pos, _piece_type in self.state.get_pieces(): + ## nursery.start_soon( + ## self.raise_event, + ## Event("delete_piece->network", piece_pos), + ## ) + + # Choose which team plays first + # Using non-cryptographically secure random because it doesn't matter + self.new_game_init(variant_play) + + ## # Send create_piece events for all pieces + ## async with trio.open_nursery() as nursery: + ## for piece_pos, piece_type in self.state.get_pieces(): + ## nursery.start_soon( + ## self.raise_event, + ## Event("create_piece->network", (piece_pos, piece_type)), + ## ) + + # Raise initial config event with board size and initial turn. + await self.raise_event( + Event( + "initial_config->network", + ( + self.state.variant_play, + len(self.state.player_data), + len(self.state.factory_displays), + self.state.current_turn, + FLOOR_LINE_DATA, + ), + ), + ) + + await self.transmit_new_round_data() + + await self.transmit_cursor_movement_mode() + + await self.transmit_playing_as() + + async def client_network_loop( + self, + client: ServerClient, + controls_lobby: bool = False, + ) -> None: + """Network loop for given ServerClient. + + Could raise the following exceptions: + trio.BrokenResourceError: if something has gone wrong, and the stream + is broken. + trio.ClosedResourceError: if stream was previously closed + + Probably couldn't raise because of write lock but still: + trio.BusyResourceError: More than one task is trying to write + to socket at once. + """ + while not self.can_start() and not client.not_connected: + try: + await client.write_callback_ping() + await trio.sleep(1.5) + except ( + trio.BrokenResourceError, + trio.ClosedResourceError, + network.NetworkStreamNotConnectedError, + ): + print(f"{client.name} Disconnected in lobby.") + return + while not client.not_connected: + event: Event[bytearray] | None = None + try: + # await client.write_callback_ping() + with trio.move_on_after(1.5): + event = await client.read_event() + except network.NetworkTimeoutError: + print(f"{client.name} Timeout") + break + except network.NetworkEOFError: + print(f"{client.name} EOF") + break + except RuntimeError as exc: + traceback.print_exception(exc) + print(f"{client.name} Bad packet") + break + except ( + trio.BrokenResourceError, + trio.ClosedResourceError, + ) as exc: + traceback.print_exception(exc) + print(f"{client.name} Socket connection issue") + break + except Exception as exc: + traceback.print_exception(exc) + print(f"{client.name} Unhandled exception") + break + if event is not None: + # if controls_lobby: + # print(f"{client.name} client_network_loop tick") + # print(f"{client.name} {event = }") + await client.raise_event(event) + await client.write_callback_ping() + + def can_start(self) -> bool: + """Return if game can start.""" + if self.internal_singleplayer_mode: + return self.client_count >= 1 + return self.client_count >= 2 + + def game_active(self) -> bool: + """Return if game is active.""" + return self.state.current_phase != Phase.end + + async def send_spectator_join_packets( + self, + client: ServerClient, + ) -> None: + """Send spectator start data.""" + print("send_spectator_join_packets") + + private_events_pocket = ComponentManager( + f"private_events_pocket for {client.client_id}", + ) + with self.temporary_component(private_events_pocket): + with private_events_pocket.temporary_component(client): + # Raise initial config event with board size and initial turn. + await client.raise_event( + Event( + "initial_config->network", + ( + self.state.variant_play, + len(self.state.player_data), + len(self.state.factory_displays), + self.state.current_turn, + FLOOR_LINE_DATA, + ), + ), + ) + + await client.raise_event( + Event(f"playing_as->network[{client.client_id}]", 255), + ) + + async def handler(self, stream: trio.SocketStream) -> None: + """Accept clients. Called by network.Server.serve.""" + if self.client_count == 0 and self.game_active(): + # Old game was running but everyone left, restart + print("TODO: restart") + self.new_game_init() + new_client_id = self.client_count + + # Is controlling player? + is_zee_capitan = new_client_id == 0 + + print( + f"{self.__class__.__name__}: client connected [client_id {new_client_id}]", + ) + self.client_count += 1 + + can_start = self.can_start() + print(f"[azul.server] {can_start = }") + game_active = self.game_active() + print(f"[azul.server] {game_active = }") + # if can_start: + # self.stop_serving() + + if self.client_count > self.max_clients: + print( + f"{self.__class__.__name__}: client disconnected, too many clients", + ) + await stream.aclose() + self.client_count -= 1 + return + + async with ServerClient.from_stream( + new_client_id, + stream=stream, + ) as client: + # Encrypt traffic + await client.start_encryption_request() + assert client.encryption_enabled + + if can_start and game_active: + print("TODO: Joined as spectator") + # await self.send_spectator_join_packets(client) + with self.temporary_component(client): + if can_start and not game_active: # and is_zee_capitan: + print("[azul.server] game start trigger.") + variant_play = False + await self.raise_event( + Event("server_send_game_start", variant_play), + ) + try: + await self.client_network_loop(client, is_zee_capitan) + finally: + print( + f"{self.__class__.__name__}: client disconnected [client_id {new_client_id}]", + ) + self.client_count -= 1 + # ServerClient's `with` block handles closing stream. + + def find_client_id_from_server_player_id( + self, + server_player_id: ServerPlayer, + ) -> int | None: + """Return client id from server player id or None if not found.""" + for client_id, current_server_player_id in self.client_players.items(): + if current_server_player_id == server_player_id: + return client_id + # Return singleplayer client id if exists + if current_server_player_id == ServerPlayer.singleplayer_all: + return client_id + return None + + def find_server_player_id_from_state_turn( + self, + state_turn: int, + ) -> ServerPlayer: + """Return ServerPlayer id from game state turn.""" + if self.internal_singleplayer_mode: + return ServerPlayer.singleplayer_all + return ServerPlayer(state_turn) + + def find_client_id_from_state_turn(self, state_turn: int) -> int | None: + """Return client id from state turn or None if not found.""" + server_player_id = self.find_server_player_id_from_state_turn( + state_turn, + ) + return self.find_client_id_from_server_player_id(server_player_id) + + async def handle_client_factory_clicked( + self, + event: Event[tuple[int, int, Tile]], + ) -> None: + """Handle client clicked a factory tile.""" + if not self.players_can_interact: + print("Players are not allowed to interact.") + await trio.lowlevel.checkpoint() + return + + client_id, factory_id, tile = event.data + + server_player_id = self.client_players[client_id] + + if server_player_id == ServerPlayer.spectator: + print(f"Spectator cannot select {factory_id = } {tile}") + await trio.lowlevel.checkpoint() + return + + player_id = int(server_player_id) + if server_player_id == ServerPlayer.singleplayer_all: + player_id = self.state.current_turn + + if player_id != self.state.current_turn: + print( + f"Player {player_id} (client ID {client_id}) cannot select factory tile, not their turn.", + ) + await trio.lowlevel.checkpoint() + return + + if self.state.current_phase != Phase.factory_offer: + print( + f"Player {player_id} (client ID {client_id}) cannot select factory tile, not in factory offer phase.", + ) + await trio.lowlevel.checkpoint() + return + + factory_display = self.state.factory_displays.get(factory_id) + if factory_display is None: + print( + f"Player {player_id} (client ID {client_id}) cannot select invalid factory {factory_id!r}.", + ) + await trio.lowlevel.checkpoint() + return + + if tile < 0 or tile not in factory_display: + print( + f"Player {player_id} (client ID {client_id}) cannot select nonexistent color {tile}.", + ) + await trio.lowlevel.checkpoint() + return + + if not self.state.can_cursor_select_factory_color( + factory_id, + int(tile), + ): + print( + f"Player {player_id} (client ID {client_id}) cannot select factory tile, state says no.", + ) + await trio.lowlevel.checkpoint() + return + + # Perform move + self.state = self.state.cursor_selects_factory(factory_id, int(tile)) + + # Send updates to client + # Send factory display changes + await self.raise_event( + Event( + "factory_data->network", + ( + factory_id, + self.state.factory_displays[factory_id], + ), + ), + ) + await self.raise_event( + Event( + "cursor_data->network", + self.state.cursor_contents, + ), + ) + await self.raise_event( + Event( + "table_data->network", + self.state.table_center, + ), + ) + + async def handle_client_table_clicked( + self, + event: Event[tuple[int, Tile]], + ) -> None: + """Handle client clicked a table center tile.""" + if not self.players_can_interact: + print("Players are not allowed to interact.") + await trio.lowlevel.checkpoint() + return + + client_id, tile = event.data + + server_player_id = self.client_players[client_id] + + if server_player_id == ServerPlayer.spectator: + print(f"Spectator cannot select table center {tile}") + await trio.lowlevel.checkpoint() + return + + player_id = int(server_player_id) + if server_player_id == ServerPlayer.singleplayer_all: + player_id = self.state.current_turn + + if player_id != self.state.current_turn: + print( + f"Player {player_id} (client ID {client_id}) cannot select table center tile, not their turn.", + ) + await trio.lowlevel.checkpoint() + return + + if self.state.current_phase != Phase.factory_offer: + print( + f"Player {player_id} (client ID {client_id}) cannot select table center tile, not in factory offer phase.", + ) + await trio.lowlevel.checkpoint() + return + + if not self.state.can_cursor_select_center( + int(tile), + ): + print( + f"Player {player_id} (client ID {client_id}) cannot select table center tile, state says no.", + ) + await trio.lowlevel.checkpoint() + return + + # Perform move + self.state = self.state.cursor_selects_table_center(int(tile)) + + # Send updates to client + await self.raise_event( + Event( + "cursor_data->network", + self.state.cursor_contents, + ), + ) + await self.raise_event( + Event( + "table_data->network", + self.state.table_center, + ), + ) + + async def handle_client_pattern_row_clicked( + self, + event: Event[tuple[int, int, tuple[int, int]]], + ) -> None: + """Handle client clicking on pattern row.""" + if not self.players_can_interact: + print("Players are not allowed to interact.") + await trio.lowlevel.checkpoint() + return + + client_id, row_id, row_pos = event.data + + server_player_id = self.client_players[client_id] + + if server_player_id == ServerPlayer.spectator: + print(f"Spectator cannot select {row_id = } {row_pos}") + await trio.lowlevel.checkpoint() + return + + player_id = int(server_player_id) + if server_player_id == ServerPlayer.singleplayer_all: + player_id = self.state.current_turn + + if player_id != self.state.current_turn: + print( + f"Player {player_id} (client ID {client_id}) cannot select pattern row, not their turn.", + ) + await trio.lowlevel.checkpoint() + return + + if self.state.current_phase != Phase.factory_offer: + print( + f"Player {player_id} (client ID {client_id}) cannot select pattern row, not in factory offer phase.", + ) + await trio.lowlevel.checkpoint() + return + + if player_id != row_id: + print( + f"Player {player_id} (client ID {client_id}) cannot select pattern row {row_id} that does not belong to them.", + ) + await trio.lowlevel.checkpoint() + return + + column, line_id = row_pos + + if line_id >= 5: + print( + f"Player {player_id} (client ID {client_id}) cannot select pattern row {row_id} line {line_id} (invalid line id).", + ) + await trio.lowlevel.checkpoint() + return + if column >= 5: + print( + f"Player {player_id} (client ID {client_id}) cannot select pattern row {row_id} column {column} (invalid column).", + ) + await trio.lowlevel.checkpoint() + return + + currently_placed = self.state.get_player_line_current_place_count( + line_id, + ) + + place_count = 5 - column - currently_placed + + if self.state.is_cursor_empty(): + print( + f"Player {player_id} (client ID {client_id}) cannot select pattern row {row_id} when not holding tiles.", + ) + await trio.lowlevel.checkpoint() + return + color = self.state.get_cursor_holding_color() + + current_hold_count = self.state.cursor_contents[color] + place_count = min(place_count, current_hold_count) + + if not self.state.can_player_select_line(line_id, color, place_count): + print( + f"Player {player_id} (client ID {client_id}) cannot select pattern line {line_id} placing {place_count} {Tile(color)} tiles.", + ) + await trio.lowlevel.checkpoint() + return + + prev_player_turn = self.state.current_turn + + self.state = self.state.player_selects_pattern_line( + line_id, + place_count, + ) + + if ( + self.state.current_turn != player_id + and not self.internal_singleplayer_mode + ): + new_client_id = self.find_client_id_from_state_turn( + self.state.current_turn, + ) + assert new_client_id is not None + await self.raise_event( + Event( + f"cursor_movement_mode->network[{client_id}]", + False, + ), + ) + await self.raise_event( + Event( + f"cursor_movement_mode->network[{new_client_id}]", + True, + ), + ) + + await self.raise_event( + Event( + "floor_data->network", + ( + player_id, + self.state.player_data[player_id].floor, + ), + ), + ) + + if self.state.current_phase != Phase.wall_tiling: + raw_tile_color, tile_count = self.state.player_data[ + prev_player_turn + ].lines[line_id] + # Do not send blank colors, clamp to zero + tile_color = max(0, int(raw_tile_color)) + await self.raise_event( + Event( + "pattern_data->network", + ( + prev_player_turn, + line_id, + (tile_color, tile_count), + ), + ), + ) + + await self.raise_event( + Event( + "cursor_data->network", + self.state.cursor_contents, + ), + ) + + did_auto_wall_tile = False + if self.state.current_phase == Phase.wall_tiling: + if not self.state.variant_play: + did_auto_wall_tile = True + self.state = self.state.apply_auto_wall_tiling() + await self.transmit_new_round_data() + await self.transmit_pattern_line_data() + + if self.state.current_phase == Phase.end: + print("TODO: Handle end of game.") + + if self.state.current_turn != player_id or did_auto_wall_tile: + await self.raise_event( + Event( + "current_turn_change->network", + self.state.current_turn, + ), + ) + + async def handle_cursor_location( + self, + event: Event[tuple[int, tuple[int, int]]], + ) -> None: + """Handle cursor location sent from client.""" + if not self.players_can_interact: + print("Players are not allowed to interact.") + await trio.lowlevel.checkpoint() + return + + client_id, location = event.data + + server_player_id = self.client_players[client_id] + + if server_player_id == ServerPlayer.spectator: + print("Spectator cannot control cursor") + await trio.lowlevel.checkpoint() + return + + player_id = int(server_player_id) + if server_player_id == ServerPlayer.singleplayer_all: + player_id = self.state.current_turn + + if player_id != self.state.current_turn: + print( + f"Player {player_id} (client ID {client_id}) cannot move cursor, not their turn.", + ) + await trio.lowlevel.checkpoint() + return + + # print(f"handle_cursor_location {client_id = } {location = }") + + if self.internal_singleplayer_mode: + await trio.lowlevel.checkpoint() + return + + async with trio.open_nursery() as nursery: + for other_client_id in self.client_players: + if other_client_id != client_id: + nursery.start_soon( + self.raise_event, + Event( + f"cursor_position->network[{other_client_id}]", + location, + ), + ) + + async def handle_client_floor_clicked( + self, + event: Event[tuple[int, int, int]], + ) -> None: + """Handle client clicking floor line.""" + if not self.players_can_interact: + print("Players are not allowed to interact.") + await trio.lowlevel.checkpoint() + return + + client_id, floor_line_id, location_x = event.data + + server_player_id = self.client_players[client_id] + + if server_player_id == ServerPlayer.spectator: + print("Spectator cannot select floor line") + await trio.lowlevel.checkpoint() + return + + player_id = int(server_player_id) + if server_player_id == ServerPlayer.singleplayer_all: + player_id = self.state.current_turn + + if player_id != self.state.current_turn: + print( + f"Player {player_id} (client ID {client_id}) cannot select floor line, not their turn.", + ) + await trio.lowlevel.checkpoint() + return + + if self.state.current_phase != Phase.factory_offer: + print( + f"Player {player_id} (client ID {client_id}) cannot select floor line, not in factory offer phase.", + ) + await trio.lowlevel.checkpoint() + return + + if player_id != floor_line_id: + print( + f"Player {player_id} (client ID {client_id}) cannot select floor line {floor_line_id} that does not belong to them.", + ) + await trio.lowlevel.checkpoint() + return + + if self.state.is_cursor_empty(): + print( + f"Player {player_id} (client ID {client_id}) cannot select floor line when not holding tiles.", + ) + await trio.lowlevel.checkpoint() + return + + color = self.state.get_cursor_holding_color() + + place_count = min(location_x + 1, self.state.cursor_contents[color]) + + self.state = self.state.player_select_floor_line(color, place_count) + + await self.raise_event( + Event( + "cursor_data->network", + self.state.cursor_contents, + ), + ) + + await self.raise_event( + Event( + "floor_data->network", + ( + player_id, + self.state.player_data[player_id].floor, + ), + ), + ) + + did_auto_wall_tile = False + if self.state.current_phase == Phase.wall_tiling: + if not self.state.variant_play: + did_auto_wall_tile = True + self.state = self.state.apply_auto_wall_tiling() + await self.transmit_new_round_data() + await self.transmit_pattern_line_data() + + if self.state.current_phase == Phase.end: + print("TODO: Handle end of game.") + + if self.state.current_turn != player_id or did_auto_wall_tile: + await self.raise_event( + Event( + "current_turn_change->network", + self.state.current_turn, + ), + ) + + def __del__(self) -> None: + """Debug print.""" + print(f"del {self.__class__.__name__}") + super().__del__() + + +async def run_server( + server_class: type[GameServer], + host: str, + port: int, +) -> None: + """Run machine client and raise tick events.""" + async with trio.open_nursery() as main_nursery: + event_manager = ExternalRaiseManager( + "azul", + main_nursery, + ) + server = server_class() + event_manager.add_component(server) + + await event_manager.raise_event(Event("server_start", (host, port))) + while not server.running: + print("Server starting...") + await trio.sleep(1) + + print("\nServer running.") + + try: + while server.running: # noqa: ASYNC110 # sleep in while loop + # Process background tasks in the main nursery + await trio.sleep(0.01) + except KeyboardInterrupt: + print("\nClosing from keyboard interrupt.") + await server.stop_server() + server.unbind_components() + + +async def cli_run_async() -> None: + """Run game server.""" + host = await find_ip() + port = DEFAULT_PORT + await run_server(GameServer, host, port) + + +def cli_run() -> None: + """Run game server.""" + trio.run(cli_run_async) + + +if __name__ == "__main__": + cli_run() diff --git a/src/azul/sound.py b/src/azul/sound.py new file mode 100644 index 0000000..1576592 --- /dev/null +++ b/src/azul/sound.py @@ -0,0 +1,67 @@ +"""Sound - Play sounds.""" + +# Programmed by CoolCat467 + +from __future__ import annotations + +# Sound - Play sounds +# Copyright (C) 2024 CoolCat467 +# +# This program 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. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +__title__ = "sound" +__author__ = "CoolCat467" +__version__ = "0.0.0" +__license__ = "GNU General Public License Version 3" + + +from typing import TYPE_CHECKING, NamedTuple + +from pygame import mixer + +if TYPE_CHECKING: + from os import PathLike + + +class SoundData(NamedTuple): + """Sound data container.""" + + loops: int = 0 + maxtime: int = 0 + fade_ms: int = 0 + volume: int = 100 + # volume_left: int = 100 + # volume_right: int = 100 + + +def play_sound( # pragma: nocover + filename: PathLike[str] | str, + sound_data: SoundData, +) -> tuple[mixer.Sound, int | float]: + """Play sound with pygame.""" + sound_object = mixer.Sound(filename) + sound_object.set_volume(sound_data.volume) + seconds: int | float = sound_object.get_length() + if sound_data.maxtime > 0: + seconds = sound_data.maxtime + _channel = sound_object.play( + loops=sound_data.loops, + maxtime=sound_data.maxtime, + fade_ms=sound_data.fade_ms, + ) + # channel.set_volume( + # sound_data.volume_left, + # sound_data.volume_right, + # ) + return sound_object, seconds diff --git a/src/azul/sprite.py b/src/azul/sprite.py index 8dc9aa6..7d9dd20 100644 --- a/src/azul/sprite.py +++ b/src/azul/sprite.py @@ -27,6 +27,7 @@ from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple, TypedDict, cast import trio +from libcomponent.component import Component, ComponentManager, Event from pygame.color import Color from pygame.event import Event as PygameEvent, event_name from pygame.mask import Mask, from_surface as mask_from_surface @@ -34,7 +35,6 @@ from pygame.sprite import LayeredDirty, LayeredUpdates, WeakDirtySprite from pygame.surface import Surface -from azul.component import Component, ComponentManager, Event from azul.statemachine import AsyncStateMachine from azul.vector import Vector2 @@ -160,7 +160,7 @@ class ImageComponent(ComponentManager): """Allow sprite to use multiple images easily. Components Supplied: - AnimationComponent + # AnimationComponent OutlineComponent Requires Component: @@ -186,7 +186,7 @@ def __init__(self) -> None: self.add_components( ( - AnimationComponent(), + # AnimationComponent(), OutlineComponent(), ), ) @@ -253,7 +253,7 @@ def get_mask(self, identifier: int | str) -> Mask: while True: if not self.image_exists(identifier): raise ValueError( - f'No image saved for identifier "{identifier}"', + f'No mask saved for identifier "{identifier}"', ) mask = self.__masks[identifier] if isinstance(mask, Mask): @@ -325,7 +325,7 @@ def set_color(self, color: Color | None) -> None: assert manager.set_surface is not None manager.set_image(manager.set_surface) - def get_outline_discriptor(self, identifier: str | int) -> str: + def get_outline_descriptor(self, identifier: str | int) -> str: """Return outlined identifier for given original identifier.""" color = "_".join(map(str, self.__color)) return f"{identifier}{self.mod}{color}_{self.size}" @@ -334,7 +334,7 @@ def save_outline(self, identifier: str | int) -> None: """Save outlined version of given identifier image.""" manager = cast("ImageComponent", self.manager) - outlined = self.get_outline_discriptor(identifier) + outlined = self.get_outline_descriptor(identifier) if manager.image_exists(outlined): return @@ -367,7 +367,7 @@ def save_outline(self, identifier: str | int) -> None: def get_outline(self, identifier: str | int) -> str: """Return saved outline effect identifier.""" self.save_outline(identifier) - return self.get_outline_discriptor(identifier) + return self.get_outline_descriptor(identifier) def precalculate_outline( self, @@ -429,7 +429,7 @@ async def tick(self, tick_event: Event[TickEventData]) -> None: await trio.lowlevel.checkpoint() passed = tick_event.data.time_passed - new = None + new: int | str | None = None if self.update_every == 0: new = self.fetch_controller_new_state() else: @@ -501,6 +501,9 @@ def move_heading_time(self, time_passed: float) -> None: class TargetingComponent(Component): """Sprite that moves toward a destination and then stops. + Registered Component Name: + targeting + Requires components: Sprite MovementComponent @@ -526,6 +529,7 @@ def update_heading(self) -> None: """Update the heading of the movement component.""" movement = cast("MovementComponent", self.get_component("movement")) to_dest = self.to_destination() + # If magnitude is zero if to_dest @ to_dest == 0: movement.heading = Vector2(0, 0) return @@ -555,6 +559,7 @@ def to_destination(self) -> Vector2: async def move_destination_time(self, time_passed: float) -> None: """Move with time_passed.""" if self.__reached: + await trio.lowlevel.checkpoint() return sprite, movement = cast( @@ -566,16 +571,27 @@ async def move_destination_time(self, time_passed: float) -> None: self.__reached = True await self.raise_event(Event(self.event_raise_name, None)) return - await trio.lowlevel.checkpoint() - travel_distance = min( - self.to_destination().magnitude(), - movement.speed * time_passed, - ) + to_destination = self.to_destination() + dest_magnitude = to_destination.magnitude() + travel_distance = movement.speed * time_passed if travel_distance > 0: - movement.move_heading_distance(travel_distance) - self.update_heading() # Fix imprecision + if travel_distance > dest_magnitude: + sprite.location = self.destination + else: + # Fix imprecision + self.update_heading() + if travel_distance > 0: + movement.move_heading_distance(travel_distance) + await trio.lowlevel.checkpoint() + + async def move_destination_time_ticks( + self, + event: Event[TickEventData], + ) -> None: + """Move with tick data.""" + await self.move_destination_time(event.data.time_passed) class DragEvent(NamedTuple): @@ -583,7 +599,7 @@ class DragEvent(NamedTuple): pos: tuple[int, int] rel: tuple[int, int] - button: int + buttons: dict[int, bool] class DragClickEventComponent(Component): @@ -658,20 +674,17 @@ async def motion( if not self.manager_exists: return async with trio.open_nursery() as nursery: - for button, pressed in self.pressed.items(): - if not pressed: - continue - nursery.start_soon( - self.raise_event, - Event( - "drag", - DragEvent( - event.data["pos"], - event.data["rel"], - button, - ), + nursery.start_soon( + self.raise_event, + Event( + "drag", + DragEvent( + event.data["pos"], + event.data["rel"], + self.pressed, ), - ) + ), + ) class GroupProcessor(AsyncStateMachine): @@ -679,13 +692,12 @@ class GroupProcessor(AsyncStateMachine): __slots__ = ("_clear", "_timing", "group_names", "groups", "new_gid") sub_renderer_class: ClassVar = LayeredDirty - groups: dict[int, sub_renderer_class] def __init__(self) -> None: """Initialize group processor.""" super().__init__() - self.groups = {} + self.groups: dict[int, LayeredDirty[Sprite]] = {} self.group_names: dict[str, int] = {} self.new_gid = 0 self._timing = 1000 / 80 @@ -737,7 +749,7 @@ def remove_group(self, gid: int) -> None: del self.group_names[name] return - def get_group(self, gid_name: str | int) -> sub_renderer_class | None: + def get_group(self, gid_name: str | int) -> LayeredDirty[Sprite] | None: """Return group from group ID or name.""" named = None if isinstance(gid_name, str): @@ -772,12 +784,12 @@ def clear_groups(self) -> None: for group_id in tuple(self.groups): self.remove_group(group_id) - def __del__(self) -> None: + def __del__(self) -> None: # pragma: nocover """Clear groups.""" self.clear_groups() -def convert_pygame_event(event: PygameEvent) -> Event[Any]: +def convert_pygame_event(event: PygameEvent) -> Event[Any]: # pragma: nocover """Convert Pygame Event to Component Event.""" # data = event.dict # data['type_int'] = event.type diff --git a/src/azul/state.py b/src/azul/state.py new file mode 100644 index 0000000..6514ac0 --- /dev/null +++ b/src/azul/state.py @@ -0,0 +1,1288 @@ +"""Azul State.""" + +# Programmed by CoolCat467 + +from __future__ import annotations + +# Copyright (C) 2024 CoolCat467 +# +# This program 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. +# +# This program 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. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +__title__ = "Azul State" +__author__ = "CoolCat467" +__license__ = "GNU General Public License Version 3" +__version__ = "0.0.0" + + +import random +from collections import Counter +from enum import IntEnum, auto +from typing import ( + TYPE_CHECKING, + Any, + Final, + NamedTuple, + TypeVar, +) + +from numpy import full, int8 + +if TYPE_CHECKING: + from collections.abc import Generator + + from numpy.typing import NDArray + from typing_extensions import Self + +T = TypeVar("T") + +FLOOR_LINE_COUNT: Final = 7 + + +def floor_line_subtract_generator(seed: int = 1) -> Generator[int, None, None]: + """Floor Line subtraction number generator. Can continue indefinitely.""" + while True: + yield from (-seed,) * (seed + 1) + seed += 1 + + +FLOOR_LINE_DATA: Final = tuple( + value + for _, value in zip( + range(FLOOR_LINE_COUNT), + floor_line_subtract_generator(), + strict=False, + ) +) + + +class Tile(IntEnum): + """All type types.""" + + blank = -6 + fake_cyan = -5 + fake_black = -4 + fake_red = -3 + fake_yellow = -2 + fake_blue = -1 + blue = 0 + yellow = auto() + red = auto() + black = auto() + cyan = auto() + one = auto() + + +REAL_TILES: Final = {Tile.blue, Tile.yellow, Tile.red, Tile.black, Tile.cyan} + + +class Phase(IntEnum): + """Game phases.""" + + factory_offer = 0 + wall_tiling = auto() + end = auto() + + +def generate_bag_contents() -> Counter[int]: + """Generate and return unrandomized bag.""" + tile_types = 5 + tile_count = 100 + count_each = tile_count // tile_types + return Counter(dict.fromkeys(range(tile_types), count_each)) + + +def bag_draw_tile(bag: Counter[int]) -> int: + """Return drawn tile from bag. Mutates bag.""" + # S311 Standard pseudo-random generators are not suitable for + # cryptographic purposes + tile = random.choice(tuple(bag.elements())) # noqa: S311 + bag[tile] -= 1 + return tile + + +def select_color(holder: Counter[int], color: int) -> int: + """Pop color tiles from bag. Returns count. Mutates holder. + + Raises KeyError if color not in holder. + """ + return holder.pop(color) + + +class PatternLine(NamedTuple): + """Player pattern line row.""" + + color: Tile + count_: int + + @classmethod + def blank(cls) -> Self: + """Return new blank pattern line.""" + return cls( + color=Tile.blank, + count_=0, + ) + + def place_tiles(self, color: Tile, place_count: int) -> Self: + """Return new pattern line after placing tiles of given color.""" + assert self.color == Tile.blank or self.color == color + assert place_count > 0 + return self._replace( + color=color, + count_=self.count_ + place_count, + ) + + +def remove_counter_zeros(counter: Counter[Any]) -> None: + """Remove any zero counts from given counter. Mutates counter.""" + for key, count in tuple(counter.items()): + if count == 0: + del counter[key] + + +def floor_fill_tile_excess( + floor: Counter[int], + tile: int, + count: int, +) -> Counter[int]: + """Fill floor with count of tile, return excess for box lid. Mutates floor.""" + excess: Counter[int] = Counter() + while floor.total() < FLOOR_LINE_COUNT and count > 0: + floor[tile] += 1 + count -= 1 + # If overflow and it's number one tile + if count and tile == Tile.one: + # Move non-one tiles from floor to excess + non_one = floor.total() - floor[Tile.one] + assert non_one > 0 + for _ in range(min(non_one, count)): + non_one_tiles = set(floor.elements()) - {Tile.one} + non_one_tile = sorted(non_one_tiles).pop() + # Move non-one tile from floor to box lid + floor[non_one_tile] -= 1 + excess[non_one_tile] += 1 + # Add one tile to floor + floor[tile] += 1 + count -= 1 + remove_counter_zeros(floor) + assert count >= 0 + if count: + # Add overflow tiles to box lid. + excess[tile] += count + + return excess + + +class UnplayableTileError(Exception): + """Unplayable Tile Exception.""" + + __slots__ = ("y",) + + def __init__(self, y: int) -> None: + """Remember Y position.""" + self.y = y + + +class PlayerData(NamedTuple): + """Player data.""" + + score: int + wall: NDArray[int8] + lines: tuple[PatternLine, ...] + floor: Counter[int] + + @classmethod + def new(cls, variant_play: bool = False) -> Self: + """Return new player data instance.""" + wall = full((5, 5), Tile.blank, int8) + + if not variant_play: + for y in range(5): + for x in range(5): + color = -((5 - y + x) % len(REAL_TILES) + 1) + wall[y, x] = color + + return cls( + score=0, + wall=wall, + lines=(PatternLine.blank(),) * 5, + floor=Counter(), + ) + + def copy(self) -> Self: + """Return copy of self.""" + return self._replace( + floor=self.floor.copy(), + ) + + def line_id_valid(self, line_id: int) -> bool: + """Return if given line id is valid.""" + return line_id >= 0 and line_id < len(self.lines) + + @staticmethod + def get_line_max_count(line_id: int) -> int: + """Return max count allowed in given line.""" + # Line id is keeping track of max count + return line_id + 1 + + def get_line_current_place_count(self, line_id: int) -> int: + """Return count of currently placed tiles for given line.""" + assert self.line_id_valid(line_id) + return self.lines[line_id].count_ + + def get_line_max_placable_count(self, line_id: int) -> int: + """Return max placable count for given line.""" + assert self.line_id_valid(line_id) + max_count = self.get_line_max_count(line_id) + return max_count - self.lines[line_id].count_ + + def get_row_colors_used(self, line_id: int) -> set[Tile]: + """Return set of tile colors used in wall for given row.""" + row = self.wall[line_id, :] + return {Tile(int(x)) for x in row[row >= 0]} + + def get_row_unused_colors(self, line_id: int) -> set[Tile]: + """Return set of tiles colors not currently used in wall for given row.""" + return REAL_TILES - self.get_row_colors_used(line_id) + + def yield_possible_placement_rows( + self, + color: int, + ) -> Generator[tuple[int, int], None, None]: + """Yield row line ids and number of placable for rows able to place color at.""" + for line_id, line in enumerate(self.lines): + # Color must match + if line.color != Tile.blank and int(line.color) != color: + # print("color mismatch") + continue + placable = self.get_line_max_placable_count(line_id) + # Must have placable spots + if not placable: + continue + # Must not already use color + if color in self.get_row_colors_used(line_id): + continue + yield (line_id, placable) + + def can_select_line( + self, + line_id: int, + color: int, + place_count: int, + ) -> bool: + """Return if can select given line with given color and place count.""" + if not self.line_id_valid(line_id): + # print("invalid line id") + return False + line = self.lines[line_id] + # Don't allow placing zero + if place_count <= 0: + # print("place count too smol") + return False + # Color must match + if line.color != Tile.blank and int(line.color) != color: + # print("color mismatch") + return False + # Must have space to place + if place_count > self.get_line_max_placable_count(line_id): + return False + # Can't place in row that uses that color already + return Tile(color) not in self.get_row_colors_used(line_id) + + @staticmethod + def replace_pattern_line( + lines: tuple[PatternLine, ...], + line_id: int, + new: PatternLine, + ) -> tuple[PatternLine, ...]: + """Return new pattern line data after replacing one of them.""" + left = lines[:line_id] + right = lines[line_id + 1 :] + return (*left, new, *right) + + def place_pattern_line_tiles( + self, + line_id: int, + color: int, + place_count: int, + ) -> Self: + """Return new player data after placing tiles in a pattern line.""" + assert self.can_select_line(line_id, color, place_count) + line = self.lines[line_id] + return self._replace( + lines=self.replace_pattern_line( + self.lines, + line_id, + line.place_tiles(Tile(color), place_count), + ), + ) + + def is_floor_line_full(self) -> bool: + """Return if floor line is full.""" + return self.floor.total() >= FLOOR_LINE_COUNT + + def place_floor_line_tiles( + self, + color: int, + place_count: int, + ) -> tuple[Self, Counter[int]]: + """Return new player and excess tiles for box lid.""" + floor = self.floor.copy() + for_box_lid = floor_fill_tile_excess(floor, color, place_count) + assert all(x > 0 for x in for_box_lid.values()), for_box_lid + return ( + self._replace(floor=floor), + for_box_lid, + ) + + def get_horizontal_linked_wall_count( + self, + x: int, + y: int, + wall: NDArray[int8], + ) -> int: + """Return horizontally-linked tile count.""" + count = 0 + for range_ in (range(x - 1, -1, -1), range(x + 1, 5)): + for cx in range_: + if wall[y, cx] < 0: + break + count += 1 + return count + + def get_vertically_linked_wall_count( + self, + x: int, + y: int, + wall: NDArray[int8], + ) -> int: + """Return vertically-linked tile count.""" + count = 0 + for range_ in (range(y - 1, -1, -1), range(y + 1, 5)): + for cy in range_: + if wall[cy, x] < 0: + break + count += 1 + return count + + def get_score_from_wall_placement( + self, + color: int, + x: int, + y: int, + wall: NDArray[int8], + ) -> int: + """Return score increment value from placing tile at given coordinates.""" + # Should be blank or fake at position + assert wall[y, x] < 0 + count = 1 + count += self.get_horizontal_linked_wall_count(x, y, wall) + count += self.get_vertically_linked_wall_count(x, y, wall) + return count + + def get_floor_line_scoring(self) -> int: + """Return score increment value from floor line.""" + total_count = self.floor.total() + assert total_count <= FLOOR_LINE_COUNT + score = 0 + for idx in range(total_count): + score += FLOOR_LINE_DATA[idx] + return score + + def perform_auto_wall_tiling(self) -> tuple[Self, Counter[int], bool]: + """Return new player data and tiles for box lid after performing automatic wall tiling.""" + for_box_lid: Counter[int] = Counter() + + score = self.score + new_lines = self.lines + new_wall = self.wall.copy() + for line_id, line in enumerate(self.lines): + if line.count_ != self.get_line_max_count(line_id): + continue + left = max(0, line.count_ - 1) + if left: + for_box_lid[line.color] += left + # placed tile is stuck in the wall now + x = tuple(map(int, new_wall[line_id, :])).index(-line.color - 1) + score += self.get_score_from_wall_placement( + line.color, + x, + line_id, + new_wall, + ) + new_wall[line_id, x] = line.color + new_lines = self.replace_pattern_line( + new_lines, + line_id, + PatternLine.blank(), + ) + + score += self.get_floor_line_scoring() + if score < 0: + score = 0 + + # Get one tile from floor line + floor = self.floor.copy() + has_one = False + if floor[Tile.one]: + floor[Tile.one] -= 1 + remove_counter_zeros(floor) + has_one = True + for_box_lid.update(floor) + + return ( + self._replace( + lines=new_lines, + wall=new_wall, + score=score, + floor=Counter(), + ), + for_box_lid, + has_one, + ) + + def has_horizontal_wall_line(self) -> bool: + """Return if full horizontal line is filled anywhere.""" + return any(all(self.wall[y, :] >= 0) for y in range(5)) + + def get_filled_horizontal_line_count(self) -> int: + """Return number of filled horizontal lines.""" + count = 0 + for y in range(5): + if all(self.wall[y, :] >= 0): + count += 1 + return count + + def get_end_of_game_score(self) -> int: + """Return end of game score for this player.""" + score = self.score + score += self.get_filled_horizontal_line_count() * 2 + for x in range(5): + if all(self.wall[:, x] >= 0): + score += 7 + counts = Counter(int(x) for x in self.wall[self.wall >= 0]) + for count in counts.values(): + if count == 5: + score += 10 + return score + + def perform_end_of_game_scoring(self) -> Self: + """Return new player data after performing end of game scoring.""" + return self._replace(score=self.get_end_of_game_score()) + + def get_manual_wall_tile_location(self) -> tuple[int, list[int]] | None: + """Return tuple of row and placable columns for wall tiling, or None if done. + + Raises UnplayableTileError if no valid placement locations. + """ + for y, line in enumerate(self.lines): + if line.color == Tile.blank: + continue + if line.count_ != self.get_line_max_count(y): + continue + + valid_x: list[int] = [] + for x, is_open in enumerate(self.wall[y, :] >= 0): + if not is_open: + continue + if line.color in {Tile(int(v)) for v in self.wall[:, x]}: + continue + valid_x.append(x) + if not valid_x: + raise UnplayableTileError(y) + return (y, valid_x) + return None + + def handle_unplayable_wall_tiling( + self, + y: int, + ) -> tuple[Self, Counter[int]]: + """Return new player data and tiles for floor line.""" + line = self.lines[y] + assert line.color != Tile.blank + + new_lines = self.replace_pattern_line( + self.lines, + y, + PatternLine.blank(), + ) + + return self._replace( + lines=new_lines, + ).place_floor_line_tiles(line.color, line.count_) + + def manual_wall_tiling_action( + self, + line_id: int, + x_pos: int, + ) -> tuple[Self, Counter[int]]: + """Wall tile given full line to given x position in that row. + + Return new player data and any tiles to return to box lid. + """ + for_box_lid: Counter[int] = Counter() + + score = self.score + new_lines = self.lines + new_wall = self.wall.copy() + + line = self.lines[line_id] + + assert line.count_ == self.get_line_max_count(line_id) + assert line.color != Tile.blank + assert new_wall[line_id, x_pos] == Tile.blank + + left = max(0, line.count_ - 1) + if left: + for_box_lid[line.color] += left + # placed tile is stuck in wall now + score += self.get_score_from_wall_placement( + line.color, + x_pos, + line_id, + new_wall, + ) + new_wall[line_id, x_pos] = line.color + new_lines = self.replace_pattern_line( + new_lines, + line_id, + PatternLine.blank(), + ) + + return ( + self._replace( + lines=new_lines, + wall=new_wall, + score=score, + ), + for_box_lid, + ) + + def finish_manual_wall_tiling(self) -> tuple[Self, Counter[int], bool]: + """Return new player data and tiles for box lid after performing manual wall tiling.""" + for_box_lid: Counter[int] = Counter() + + score = self.score + + score += self.get_floor_line_scoring() + if score < 0: + score = 0 + + # Get one tile from floor line + floor = self.floor.copy() + has_one = False + if floor[Tile.one]: + floor[Tile.one] -= 1 + remove_counter_zeros(floor) + has_one = True + for_box_lid.update(floor) + + return ( + self._replace( + score=score, + floor=Counter(), + ), + for_box_lid, + has_one, + ) + + +def factory_displays_deepcopy( + factory_displays: dict[int, Counter[int]], +) -> dict[int, Counter[int]]: + """Return deepcopy of factory displays.""" + return {k: v.copy() for k, v in factory_displays.items()} + + +def player_data_deepcopy( + player_data: dict[int, PlayerData], +) -> dict[int, PlayerData]: + """Return deepcopy of player data.""" + return {k: v.copy() for k, v in player_data.items()} + + +class SelectableSource(IntEnum): + """Selectable tile source.""" + + table_center = 0 + factory = auto() + + +class SelectableSourceTiles(NamedTuple): + """Selectable source tiles data.""" + + source: SelectableSource + tiles: Tile + # Factory ids + source_id: int | None = None + + +class SelectableDestination(IntEnum): + """Selectable tile destination.""" + + floor_line = 0 + pattern_line = auto() + + +class SelectableDestinationTiles(NamedTuple): + """Selectable destination tiles data.""" + + destination: SelectableDestination + place_count: int + # Pattern line ids + destination_id: int | None = None + + +class State(NamedTuple): + """Represents state of an azul game.""" + + variant_play: bool + current_phase: Phase + bag: Counter[int] + box_lid: Counter[int] + table_center: Counter[int] + factory_displays: dict[int, Counter[int]] + cursor_contents: Counter[int] + current_turn: int + player_data: dict[int, PlayerData] + + @classmethod + def blank(cls) -> Self: + """Return new blank state.""" + return cls( + variant_play=False, + current_phase=Phase.end, + bag=Counter(), + box_lid=Counter(), + table_center=Counter(), + factory_displays={}, + cursor_contents=Counter(), + current_turn=0, + player_data={}, + ) + + @classmethod + def new_game(cls, player_count: int, variant_play: bool = False) -> Self: + """Return state of a new game.""" + factory_count = player_count * 2 + 1 + bag = generate_bag_contents() + + factory_displays: dict[int, Counter[int]] = {} + for x in range(factory_count): + tiles: Counter[int] = Counter() + for _ in range(4): + tiles[bag_draw_tile(bag)] += 1 + factory_displays[x] = tiles + + return cls( + variant_play=variant_play, + current_phase=Phase.factory_offer, + bag=bag, + box_lid=Counter(), + table_center=Counter({Tile.one: 1}), + factory_displays=factory_displays, + cursor_contents=Counter(), + current_turn=0, + player_data={ + x: PlayerData.new(variant_play) for x in range(player_count) + }, + ) + + def is_cursor_empty(self) -> bool: + """Return if cursor is empty.""" + return self.cursor_contents.total() == 0 + + def can_cursor_select_factory(self, factory_id: int) -> bool: + """Return if cursor can select a specific factory.""" + assert self.current_phase == Phase.factory_offer + if not self.is_cursor_empty(): + return False + factory = self.factory_displays.get(factory_id, None) + if factory is None: + return False + return factory.total() > 0 + + def can_cursor_select_factory_color( + self, + factory_id: int, + color: int, + ) -> bool: + """Return if cursor can select color at factory.""" + if not self.can_cursor_select_factory(factory_id): + return False + factory = self.factory_displays[factory_id] + return factory[color] > 0 + + def cursor_selects_factory(self, factory_id: int, color: int) -> Self: + """Return new state after cursor selects factory.""" + assert self.can_cursor_select_factory_color(factory_id, color) + # Only mutate copies + factory_displays = factory_displays_deepcopy(self.factory_displays) + table_center = self.table_center.copy() + cursor_contents = self.cursor_contents.copy() + + factory = factory_displays[factory_id] + count = select_color(factory, color) + # Add to cursor + cursor_contents[color] += count + # Add all non-matching colored tiles to center of table + table_center.update(factory) + factory.clear() + + return self._replace( + table_center=table_center, + factory_displays=factory_displays, + cursor_contents=cursor_contents, + ) + + def can_cursor_select_center(self, color: int) -> bool: + """Return if cursor can select color from table center.""" + assert self.current_phase == Phase.factory_offer + if not self.is_cursor_empty(): + return False + return color != Tile.one and self.table_center[color] > 0 + + def cursor_selects_table_center(self, color: int) -> Self: + """Return new state after cursor selects from table center.""" + assert self.can_cursor_select_center(color) + table_center = self.table_center.copy() + cursor_contents = self.cursor_contents.copy() + + # Get all of color from table center and add to cursor + cursor_contents[color] += select_color(table_center, color) + # Handle number one tile + if table_center[Tile.one]: + cursor_contents[Tile.one] += select_color(table_center, Tile.one) + remove_counter_zeros(table_center) + + return self._replace( + table_center=table_center, + cursor_contents=cursor_contents, + ) + + def yield_table_center_selections( + self, + ) -> Generator[SelectableSourceTiles, None, None]: + """Yield SelectableSourceTiles objects from table center.""" + for color, count in self.table_center.items(): + if color == Tile.one or count <= 0: + continue + yield SelectableSourceTiles( + source=SelectableSource.table_center, + tiles=Tile(color), + ) + + def yield_selectable_tiles_factory_offer( + self, + ) -> Generator[SelectableSourceTiles, None, None]: + """Yield SelectableSourceTiles objects from all sources.""" + yield from self.yield_table_center_selections() + for factory_id, factory_display in self.factory_displays.items(): + for color in factory_display: + yield SelectableSourceTiles( + source=SelectableSource.factory, + tiles=Tile(color), + source_id=factory_id, + ) + + def apply_source_select_action_factory_offer( + self, + selection: SelectableSourceTiles, + ) -> Self: + """Return new state after applying selection action.""" + color = selection.tiles + if selection.source == SelectableSource.table_center: + return self.cursor_selects_table_center(color) + if selection.source == SelectableSource.factory: + assert selection.source_id is not None + return self.cursor_selects_factory(selection.source_id, color) + raise NotImplementedError(selection.source) + + def get_cursor_holding_color(self) -> int: + """Return color of tile cursor is holding.""" + cursor_colors = set(self.cursor_contents.elements()) + # Do not count number one tile + cursor_colors.discard(Tile.one) + assert len(cursor_colors) == 1, "Cursor should only exactly one color" + return cursor_colors.pop() + + def can_player_select_line( + self, + line_id: int, + color: int, + place_count: int, + ) -> bool: + """Return if player can select line.""" + player_data = self.player_data[self.current_turn] + + # Cannot place more than we have + # Can't be pulling tiles out of thin air now can we? + if place_count > self.cursor_contents[color]: + return False + + return player_data.can_select_line(line_id, color, place_count) + + def get_player_line_max_placable_count(self, line_id: int) -> int: + """Return max placable count for given line.""" + player_data = self.player_data[self.current_turn] + + return player_data.get_line_max_placable_count(line_id) + + def get_player_line_current_place_count(self, line_id: int) -> int: + """Return current place count for given line.""" + player_data = self.player_data[self.current_turn] + + return player_data.get_line_current_place_count(line_id) + + def all_pullable_empty(self) -> bool: + """Return if all pullable tile locations are empty, not counting cursor.""" + if self.table_center.total(): + return False + for factory_display in self.factory_displays.values(): + if factory_display.total(): + return False + return True + + def _factory_offer_maybe_next_turn(self) -> Self: + """Return either current state or new state if player's turn is over.""" + assert self.current_phase == Phase.factory_offer + # If cursor is still holding things, turn is not over. + if not self.is_cursor_empty(): + return self + # Turn is over + # Increment who's turn it is + current_turn = (self.current_turn + 1) % len(self.player_data) + + current_phase: Phase = self.current_phase + if self.all_pullable_empty(): + # Go to wall tiling phase + current_phase = Phase.wall_tiling + + ##if current_phase == Phase.wall_tiling and not self.variant_play: + ## return new_state.apply_auto_wall_tiling() + ##return new_state + return self._replace( + current_phase=current_phase, + current_turn=current_turn, + ) + + def player_select_floor_line(self, color: int, place_count: int) -> Self: + """Return new state after player adds tiles to floor line.""" + assert self.current_phase == Phase.factory_offer + cursor_contents = self.cursor_contents.copy() + assert place_count > 0 + assert place_count <= cursor_contents[color] + + box_lid = self.box_lid.copy() + current_player_data = self.player_data[self.current_turn] + + # Remove from cursor + cursor_contents[color] -= place_count + # Add to floor line + new_player_data, for_box_lid = ( + current_player_data.place_floor_line_tiles( + color, + place_count, + ) + ) + # Add overflow tiles to box lid + assert all(x > 0 for x in for_box_lid.values()), for_box_lid + box_lid.update(for_box_lid) + + # If has number one tile, add to floor line + if cursor_contents[Tile.one]: + # Add to floor line + new_player_data, for_box_lid = ( + new_player_data.place_floor_line_tiles( + Tile.one, + cursor_contents.pop(Tile.one), + ) + ) + # Add overflow tiles to box lid + assert all(x > 0 for x in for_box_lid.values()), for_box_lid + box_lid.update(for_box_lid) + + remove_counter_zeros(cursor_contents) + + # Update player data + player_data = player_data_deepcopy(self.player_data) + player_data[self.current_turn] = new_player_data + + return self._replace( + box_lid=box_lid, + cursor_contents=cursor_contents, + player_data=player_data, + )._factory_offer_maybe_next_turn() + + def player_selects_pattern_line( + self, + line_id: int, + place_count: int, + ) -> Self: + """Return new state after player selects line.""" + assert self.current_phase == Phase.factory_offer + assert not self.is_cursor_empty() + color = self.get_cursor_holding_color() + + assert self.can_player_select_line(line_id, color, place_count) + current_player_data = self.player_data[self.current_turn] + + new_player_data = current_player_data.place_pattern_line_tiles( + line_id, + color, + place_count, + ) + + cursor_contents = self.cursor_contents.copy() + cursor_contents[color] -= place_count + + # Might need to change box lid + box_lid = self.box_lid + + # If has number one tile, add to floor line + if cursor_contents[Tile.one]: + # Will be mutating box lid then + box_lid = self.box_lid.copy() + # Add to floor line + new_player_data, for_box_lid = ( + new_player_data.place_floor_line_tiles( + Tile.one, + cursor_contents.pop(Tile.one), + ) + ) + # Add overflow tiles to box lid + assert all(x > 0 for x in for_box_lid.values()), for_box_lid + box_lid.update(for_box_lid) + + remove_counter_zeros(cursor_contents) + + player_data = player_data_deepcopy(self.player_data) + player_data[self.current_turn] = new_player_data + + return self._replace( + box_lid=box_lid, + player_data=player_data, + cursor_contents=cursor_contents, + )._factory_offer_maybe_next_turn() + + def yield_selectable_tile_destinations_factory_offer( + self, + ) -> Generator[SelectableDestinationTiles, None, None]: + """Yield selectable tile destinations for factory offer phase.""" + assert self.current_phase == Phase.factory_offer + assert not self.is_cursor_empty() + + current_player_data = self.player_data[self.current_turn] + + color = self.get_cursor_holding_color() + count = self.cursor_contents[color] + + for ( + line_id, + placable, + ) in current_player_data.yield_possible_placement_rows(color): + ## for place_count in range(1, min(count, placable + 1)): + place_count = min(count, placable) + yield SelectableDestinationTiles( + destination=SelectableDestination.pattern_line, + place_count=place_count, + destination_id=line_id, + ) + # Can always place in floor line, even if full, + # because of box lid overflow + ## for place_count in range(1, count): + yield SelectableDestinationTiles( + destination=SelectableDestination.floor_line, + place_count=count, + ) + + def apply_destination_select_action_factory_offer( + self, + selection: SelectableDestinationTiles, + ) -> Self: + """Return new state after applying destination selection action.""" + assert self.current_phase == Phase.factory_offer + assert not self.is_cursor_empty() + + if selection.destination == SelectableDestination.floor_line: + color = self.get_cursor_holding_color() + return self.player_select_floor_line( + color, + selection.place_count, + ) + if selection.destination == SelectableDestination.pattern_line: + assert selection.destination_id is not None + return self.player_selects_pattern_line( + selection.destination_id, + selection.place_count, + ) + raise NotImplementedError(selection.destination) + + def apply_auto_wall_tiling(self) -> Self: + """Return new state after performing automatic wall tiling.""" + assert self.current_phase == Phase.wall_tiling + assert not self.variant_play + box_lid = self.box_lid.copy() + new_players = player_data_deepcopy(self.player_data) + + is_end = False + current_turn = self.current_turn + for player_id, player in self.player_data.items(): + new_player, for_box_lid, has_one = ( + player.perform_auto_wall_tiling() + ) + new_players[player_id] = new_player + box_lid.update(for_box_lid) + if not is_end: + is_end = new_player.has_horizontal_wall_line() + if has_one: + current_turn = player_id + + bag = self.bag.copy() + factory_displays: dict[int, Counter[int]] = {} + + if is_end: + for player_id in self.player_data: + new_players[player_id] = new_players[ + player_id + ].perform_end_of_game_scoring() + else: + out_of_tiles = False + for factory_id in self.factory_displays: + tiles: Counter[int] = Counter() + if out_of_tiles: + factory_displays[factory_id] = tiles + continue + for _ in range(4): + if bag.total() > 0: + tiles[bag_draw_tile(bag)] += 1 + else: + bag = box_lid + box_lid = Counter() + if bag.total() <= 0: + # "In the rare case that you run out of + # tiles again while there are one left in + # the lid, start the new round as usual even + # though not all Factory displays are + # properly filled." + out_of_tiles = True + break + factory_displays[factory_id] = tiles + + return self._replace( + current_phase=Phase.end if is_end else Phase.factory_offer, + current_turn=current_turn, + player_data=new_players, + bag=bag, + box_lid=box_lid, + factory_displays=factory_displays, + table_center=Counter({Tile.one: 1}), + ) + + def get_win_order(self) -> list[tuple[int, int]]: + """Return player ranking with (id, compare_score) entries.""" + counts: dict[int, int] = {} + # get_filled_horizontal_line_count can return at most 5 + for player_id, player in self.player_data.items(): + counts[player_id] = ( + player.score * 6 + player.get_filled_horizontal_line_count() + ) + return sorted(counts.items(), key=lambda x: x[1], reverse=True) + + def yield_all_factory_offer_destinations( + self, + ) -> Generator[tuple[SelectableDestinationTiles, ...]]: + """Yield all factory offer destinations.""" + if self.is_cursor_empty(): + yield () + else: + for ( + destination + ) in self.yield_selectable_tile_destinations_factory_offer(): + new = self.apply_destination_select_action_factory_offer( + destination, + ) + did_not_iter = True + for action in new.yield_all_factory_offer_destinations(): + did_not_iter = False + yield (destination, *action) + if did_not_iter: + yield (destination,) + + def yield_actions( + self, + ) -> Generator[ + tuple[SelectableDestinationTiles, ...] + | tuple[SelectableSourceTiles, tuple[SelectableDestinationTiles, ...]], + None, + None, + ]: + """Yield all possible actions able to be performed on this state.""" + if self.current_phase == Phase.factory_offer: + if not self.is_cursor_empty(): + yield from self.yield_all_factory_offer_destinations() + else: + for selection in self.yield_selectable_tiles_factory_offer(): + new = self.apply_source_select_action_factory_offer( + selection, + ) + for ( + action_chain + ) in new.yield_all_factory_offer_destinations(): + yield (selection, action_chain) + elif self.current_phase == Phase.end: + pass + else: + raise NotImplementedError(f"{self.current_phase = }") + + def preform_action( + self, + action: ( + tuple[SelectableDestinationTiles, ...] + | tuple[ + SelectableSourceTiles, + tuple[SelectableDestinationTiles, ...], + ] + ), + ) -> Self: + """Return new state after applying an action.""" + if self.current_phase == Phase.factory_offer: + if isinstance(action[0], SelectableDestinationTiles): + new = self + for destination in action: + assert isinstance(destination, SelectableDestinationTiles) + new = new.apply_destination_select_action_factory_offer( + destination, + ) + return new + selection, destinations = action + assert isinstance(selection, SelectableSourceTiles) + new = self.apply_source_select_action_factory_offer( + selection, + ) + for destination_ in destinations: + assert isinstance(destination_, SelectableDestinationTiles) + new = new.apply_destination_select_action_factory_offer( + destination_, + ) + return new + raise NotImplementedError() + + def _manual_wall_tiling_maybe_next_turn(self) -> Self: + # return self + raise NotImplementedError() + + def get_manual_wall_tiling_locations_for_player( + self, + player_id: int, + ) -> tuple[int, list[int]] | None | Self: + """Either return player wall tiling location data or new state. + + New state when player cannot wall tile their current row. + """ + current_player_data = self.player_data[player_id] + + try: + return current_player_data.get_manual_wall_tile_location() + except UnplayableTileError as unplayable_exc: + # kind of hacky, but it works + y_position = unplayable_exc.y + + new_player_data, for_box_lid = ( + current_player_data.handle_unplayable_wall_tiling(y_position) + ) + + box_lid = self.box_lid.copy() + + # Add overflow tiles to box lid + assert all(x > 0 for x in for_box_lid.values()), for_box_lid + box_lid.update(for_box_lid) + + # Update player data + player_data = player_data_deepcopy(self.player_data) + player_data[player_id] = new_player_data + + return self._replace( + box_lid=box_lid, + player_data=player_data, + )._manual_wall_tiling_maybe_next_turn() + + def manual_wall_tiling_action( + self, + player_id: int, + line_id: int, + x_pos: int, + ) -> Self: + """Perform manual wall tiling action.""" + current_player_data = self.player_data[player_id] + + new_player_data, for_box_lid = ( + current_player_data.manual_wall_tiling_action(line_id, x_pos) + ) + box_lid = self.box_lid.copy() + + # Add overflow tiles to box lid + assert all(x > 0 for x in for_box_lid.values()), for_box_lid + box_lid.update(for_box_lid) + + # Update player data + player_data = player_data_deepcopy(self.player_data) + player_data[player_id] = new_player_data + + new_state = self._replace( + box_lid=box_lid, + player_data=player_data, + ) + + result = new_state.get_manual_wall_tiling_locations_for_player( + player_id, + ) + if not isinstance(result, self.__class__): + return new_state._manual_wall_tiling_maybe_next_turn() + return result._manual_wall_tiling_maybe_next_turn() + + +def run() -> None: + """Run program.""" + from market_api import pretty_print_response as pprint + + random.seed(0) + state = State.new_game(2) + ticks = 0 + try: + ## last_turn = -1 + while state.current_phase == Phase.factory_offer: + ## assert last_turn != state.current_turn + ## last_turn = state.current_turn + actions = tuple(state.yield_actions()) + print(f"{len(actions) = }") + # S311 Standard pseudo-random generators are not suitable + # for cryptographic purposes + action = random.choice(actions) # noqa: S311 + ## pprint(action) + state = state.preform_action(action) + + ticks += 1 + print(f"{state.get_win_order() = }") + except BaseException: + print(f"{ticks = }") + ## print(f'{state = }') + pprint(state) + raise + ## print(f'{destination = }') + ## pprint(state) + pprint(state) + + +if __name__ == "__main__": + run() diff --git a/src/azul/tools.py b/src/azul/tools.py index 5c8629f..ccdc9e4 100644 --- a/src/azul/tools.py +++ b/src/azul/tools.py @@ -3,14 +3,7 @@ from __future__ import annotations # Programmed by CoolCat467 -import math -import random -from typing import TYPE_CHECKING, TypeVar - -if TYPE_CHECKING: - from collections.abc import Generator, Iterable - - from azul.game import Tile +from typing import TypeVar T = TypeVar("T") Numeric = TypeVar("Numeric", int, float) @@ -37,32 +30,6 @@ def saturate(value: Numeric, low: Numeric, high: Numeric) -> Numeric: return min(max(value, low), high) -def randomize(iterable: Iterable[T]) -> list[T]: - """Randomize all values of an iterable.""" - lst = list(iterable) - random.shuffle(lst) - return lst - - -def gen_random_proper_seq(length: int, **kwargs: float) -> list[str]: - """Generate a random sequence of letters given keyword arguments of =.""" - letters = [] - if sum(list(kwargs.values())) != 1: - raise ArithmeticError( - "Sum of percentages of " - + " ".join(list(kwargs.keys())) - + " are not equal to 100 percent!", - ) - for letter in kwargs: - letters += [letter] * math.ceil(length * kwargs[letter]) - return randomize(letters) - - -def sort_tiles(tile_object: Tile) -> int: - """Key function for sorting tiles.""" - return tile_object.color - - # def getCacheSignatureTile(tile, tilesize, greyshift, outlineSize): # """Return the string a tile and it's configuration information would use to identify itself in the tile cache.""" # safeFloat = lambda x: round(x*100) @@ -70,12 +37,3 @@ def sort_tiles(tile_object: Tile) -> int: # data = tile.color, safeFloat(tilesize), safeFloat(greyshift), safeFloat(outlineSize) # # types: ^ # return ''.join((str(i) for i in data)) - - -def floor_line_subtract_generator(seed: int = 1) -> Generator[int, None, None]: - """Floor Line subtraction number generator. Can continue indefinitely.""" - num = seed - while True: - nxt = [-num] * (num + 1) - yield from nxt - num += 1 diff --git a/src/azul/utils.py b/src/azul/utils.py deleted file mode 100644 index 22bf7ec..0000000 --- a/src/azul/utils.py +++ /dev/null @@ -1,49 +0,0 @@ -"""Two's Complement Utilities.""" - -# This is the base_io module from https://github.com/py-mine/mcproto v0.3.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -__all__ = ["from_twos_complement", "to_twos_complement"] - - -def to_twos_complement(number: int, bits: int) -> int: - """Convert a given ``number`` into twos complement format of given amount of ``bits``. - - :raises ValueError: - Given ``number`` is out of range, and can't be converted into twos complement format, since - it wouldn't fit into the given amount of ``bits``. - """ - value_max = 1 << (bits - 1) - value_min = value_max * -1 - # With two's complement, we have one more negative number than positive - # this means we can't be exactly at value_max, but we can be at exactly value_min - if number >= value_max or number < value_min: - raise ValueError( - f"Can't convert number {number} into {bits}-bit twos complement format - out of range", - ) - - return number + (1 << bits) if number < 0 else number - - -def from_twos_complement(number: int, bits: int) -> int: - """Convert a given ``number`` from twos complement format of given amount of ``bits``. - - :raises ValueError: - Given ``number`` doesn't fit into given amount of ``bits``. This likely means that you're using - the wrong number, or that the number was converted into twos complement with higher amount of ``bits``. - """ - value_max = (1 << bits) - 1 - if number < 0 or number > value_max: - raise ValueError( - f"Can't convert number {number} from {bits}-bit twos complement format - out of range", - ) - - if number & (1 << (bits - 1)) != 0: - number -= 1 << bits - - return number diff --git a/src/azul/vector.py b/src/azul/vector.py index 5302e6d..60fb30e 100644 --- a/src/azul/vector.py +++ b/src/azul/vector.py @@ -23,12 +23,13 @@ __title__ = "Vector Module" __author__ = "CoolCat467" __license__ = "GNU General Public License Version 3" -__version__ = "2.0.0" +__version__ = "2.0.1" import math import sys from typing import ( TYPE_CHECKING, + ClassVar, ) from azul.namedtuple_mod import NamedTupleMeta @@ -41,7 +42,7 @@ # As a forward to the madness below, we are doing something incredibly sneeky. # We have BaseVector, which we want to have all of the shared functionality # of all Vector subclasses. We also want each Vector class to be a NamedTuple -# so we can let Python handle storing data in the most efficiant way and +# so we can let Python handle storing data in the most efficient way and # make Vectors immutable. # # Problem is, we can't have Vector classes be @@ -66,11 +67,20 @@ class BaseVector: __slots__ = () if TYPE_CHECKING: + # Because of type hacks later on, pretend we have + # the same things NamedTuple does + _field_defaults: ClassVar[dict[str, float]] + _fields: ClassVar[tuple[str, ...]] # D105 is 'Missing docstring in magic method', but this is to handle # typing issues def __iter__(self) -> Iterator[float]: ... # noqa: D105 def __getitem__(self, value: int) -> float: ... # noqa: D105 + def _asdict(self) -> dict[str, float]: ... + def _replace(self, /, **kwds: int | float) -> Self: ... + def __getnewargs__(self) -> tuple[float, ...]: ... # noqa: D105 + @classmethod + def _make(cls, iterable: Iterable[float]) -> Self: ... @classmethod def from_iter(cls: type[Self], iterable: Iterable[float]) -> Self: @@ -99,6 +109,10 @@ def normalized(self: Self) -> Self: """Return a normalized (unit) vector.""" return self / self.magnitude() + def __bool__(self: Self) -> bool: + """Return if any component is nonzero.""" + return any(self) + # rhs is Right Hand Side def __add__( self: Self, @@ -158,6 +172,12 @@ def __round__( """Return result of rounding self components to given number of digits.""" return self.rounded(ndigits) + def floored( + self: Self, + ) -> Self: + """Return result of rounding self components to given number of digits.""" + return self.from_iter(int(c) for c in self) + def __abs__( self: Self, ) -> Self: @@ -212,6 +232,7 @@ def clamp(self: Self, min_value: float, max_value: float) -> Self: if TYPE_CHECKING: VectorBase = BaseVector else: + # In reality, it's a NamedTuple metaclass VectorBase = type.__new__( NamedTupleMeta, "VectorBase", diff --git a/test-requirements.in b/test-requirements.in deleted file mode 100644 index 449c3e8..0000000 --- a/test-requirements.in +++ /dev/null @@ -1,28 +0,0 @@ -# For tests -pytest >= 5.0 # for faulthandler in core -coverage >= 7.2.5 -pytest-trio -pytest-cov - -# Tools -black; implementation_name == "cpython" -mypy >= 1.13.0 # Would use mypy[faster-cache], but orjson has build issues on pypy -orjson; implementation_name == "cpython" -ruff >= 0.6.6 -uv >= 0.2.24 -codespell - -# https://github.com/python-trio/trio/pull/654#issuecomment-420518745 -mypy-extensions -typing-extensions - -# Azul's own dependencies -# -cryptography>=43.0.0 -exceptiongroup; python_version < '3.11' -mypy_extensions>=1.0.0 -numpy~=2.1.3 -pygame~=2.6.0 -trio~=0.27.0 -typing_extensions>=4.12.2 -# diff --git a/tests/helpers.py b/tests/helpers.py deleted file mode 100644 index 0739739..0000000 --- a/tests/helpers.py +++ /dev/null @@ -1,175 +0,0 @@ -# This is the base_io module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -import inspect -import unittest.mock -from functools import partial -from typing import TYPE_CHECKING, Any, Generic, TypeVar - -import trio -from typing_extensions import ParamSpec - -if TYPE_CHECKING: - from collections.abc import Callable, Coroutine - -T = TypeVar("T") -P = ParamSpec("P") -T_Mock = TypeVar("T_Mock", bound=unittest.mock.Mock) - - -def synchronize(f: Callable[P, Coroutine[Any, Any, T]]) -> Callable[P, T]: - """Take an asynchronous function, and return a synchronous alternative. - - This is needed because we sometimes want to test asynchronous behavior in a synchronous test function, - where we can't simply await something. This function uses `trio.run` and generates a wrapper - around the original asynchronous function, that awaits the result in a blocking synchronous way, - returning the obtained value. - """ - - def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: - return trio.run(partial(f, *args, **kwargs)) - - return wrapper - - -class SynchronizedMixin: - """Class acting as another wrapped object, with all async methods synchronized. - - This class needs :attr:`._WRAPPED_ATTRIBUTE` class variable to be set as the name of the internally - held attribute, holding the object we'll be wrapping around. - - Child classes of this mixin will have their lookup logic changed, to instead perform a lookup - on the wrapped attribute. Only if that lookup fails, we fallback to this class, meaning if both - the wrapped attribute and this class have some attribute defined, the attribute from the wrapped - object is returned. The only exceptions to this are lookup of the ``_WRAPPED_ATTRIBUTE`` variable, - and of the attribute name stored under the ``_WRAPPED_ATTRIBUTE`` (the wrapped object). - - If the attribute held by the wrapped object is an asynchronous function, instead of returning it - directly, the :func:`.synchronize` function will be called, returning a wrapped synchronous - alternative for the requested async function. - - This is useful when we need to quickly create a synchronous alternative to a class holding async methods. - However it isn't useful in production, since will cause typing issues (attributes will be accessible, but - type checkers won't know that they exist here, because of the dynamic nature of this implementation). - """ - - _WRAPPED_ATTRIBUTE: str - - def __getattribute__(self, name: str, /) -> Any: - """Return attributes of the wrapped object, if the attribute is a coroutine function, synchronize it. - - The only exception to this behavior is getting the :attr:`._WRAPPED_ATTRIBUTE` variable itself, or the - attribute named as the content of the ``_WRAPPED_ATTRIBUTE`` variable. All other attribute access will - be delegated to the wrapped attribute. If the wrapped object doesn't have given attribute, the lookup - will fallback to regular lookup for variables belonging to this class. - """ - if ( - name == "_WRAPPED_ATTRIBUTE" or name == self._WRAPPED_ATTRIBUTE - ): # Order is important - return super().__getattribute__(name) - - wrapped = getattr(self, self._WRAPPED_ATTRIBUTE) - - if hasattr(wrapped, name): - obj = getattr(wrapped, name) - if inspect.iscoroutinefunction(obj): - return synchronize(obj) - return obj - - return super().__getattribute__(name) - - def __setattr__(self, name: str, value: object, /) -> None: - """Allow for changing attributes of the wrapped object. - - * If wrapped object isn't yet set, fall back to :meth:`~object.__setattr__` of this class. - * If wrapped object doesn't already contain the attribute we want to set, also fallback to this class. - * Otherwise, run ``__setattr__`` on it to update it. - """ - try: - wrapped = getattr(self, self._WRAPPED_ATTRIBUTE) - except AttributeError: - return super().__setattr__(name, value) - else: - if hasattr(wrapped, name): - return setattr(wrapped, name, value) - - return super().__setattr__(name, value) - - -class UnpropagatingMockMixin(Generic[T_Mock]): - """Provides common functionality for our :class:`~unittest.mock.Mock` classes. - - By default, mock objects propagate themselves by returning a new instance of the same mock - class, with same initialization attributes. This is done whenever we're accessing new - attributes that mock class. - - This propagation makes sense for simple mocks without any additional restrictions, however when - dealing with limited mocks to some ``spec_set``, it doesn't usually make sense to propagate - those same ``spec_set`` restrictions, since we generally don't have attributes/methods of a - class be of/return the same class. - - This mixin class stops this propagation, and instead returns instances of specified mock class, - defined in :attr:`.child_mock_type` class variable, which is by default set to - :class:`~unittest.mock.MagicMock`, as it can safely represent most objects. - - .. note: - This propagation handling will only be done for the mock classes that inherited from this - mixin class. That means if the :attr:`.child_mock_type` is one of the regular mock classes, - and the mock is propagated, a regular mock class is returned as that new attribute. This - regular class then won't have the same overrides, and will therefore propagate itself, like - any other mock class would. - - If you wish to counteract this, you can set the :attr:`.child_mock_type` to a mock class - that also inherits from this mixin class, perhaps to your class itself, overriding any - propagation recursively. - """ - - child_mock_type: T_Mock = unittest.mock.MagicMock - - # Since this is a mixin class, we can access some attributes defined in mock classes safely. - # Define the types of these variables here, for proper static type analysis. - _mock_sealed: bool - _extract_mock_name: Callable[[], str] - - def _get_child_mock(self, **kwargs) -> T_Mock: - """Make :attr:`.child_mock_type`` instances instead of instances of the same class. - - By default, this method creates a new mock instance of the same original class, and passes - over the same initialization arguments. This overrides that behavior to instead create an - instance of :attr:`.child_mock_type` class. - """ - # Mocks can be sealed, in which case we wouldn't want to allow propagation of any kind - # and rather raise an AttributeError, informing that given attr isn't accessible - if self._mock_sealed: - mock_name = self._extract_mock_name() - obj_name = ( - f"{mock_name}.{kwargs['name']}" - if "name" in kwargs - else f"{mock_name}()" - ) - raise AttributeError(f"Can't access {obj_name}, mock is sealed.") - - # Propagate any other children as simple `unittest.mock.Mock` instances - # rather than `self.__class__` instances - return self.child_mock_type(**kwargs) - - -class CustomMockMixin(UnpropagatingMockMixin): - """Provides common functionality for our custom mock types. - - * Stops propagation of same ``spec_set`` restricted mock in child mocks - (see :class:`.UnpropagatingMockMixin` for more info) - * Allows using the ``spec_set`` attribute as class attribute - """ - - spec_set = None - - def __init__(self, **kwargs): - if "spec_set" in kwargs: - self.spec_set = kwargs.pop("spec_set") - super().__init__(spec_set=self.spec_set, **kwargs) # type: ignore # Mixin class, this __init__ is valid diff --git a/tests/protocol_helpers.py b/tests/protocol_helpers.py deleted file mode 100644 index 74a16f9..0000000 --- a/tests/protocol_helpers.py +++ /dev/null @@ -1,87 +0,0 @@ -# This is the base_io module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -from unittest.mock import AsyncMock, Mock - - -class WriteFunctionMock(Mock): - """Mock write function, storing the written data.""" - - def __init__(self, *a, **kw): - super().__init__(*a, **kw) - self.combined_data = bytearray() - - def __call__( - self, - data: bytes, - ) -> None: # pyright: ignore[reportIncompatibleMethodOverride] - """Override mock's ``__call__`` to extend our :attr:`.combined_data` bytearray. - - This allows us to keep track of exactly what data was written by the mocked write function - in total, rather than only having tools like :meth:`.assert_called_with`, which might let us - get the data from individual calls, but not the combined data, which is what we'll need. - """ - self.combined_data.extend(data) - return super().__call__(data) - - def assert_has_data( - self, - data: bytearray, - ensure_called: bool = True, - ) -> None: - """Ensure that the combined write data by the mocked function matches expected ``data``.""" - if ensure_called: - self.assert_called() - - if self.combined_data != data: - raise AssertionError( - f"Write function mock expected data {data!r}, but was {self.call_data!r}", - ) - - -class WriteFunctionAsyncMock(WriteFunctionMock, AsyncMock): - """Asynchronous mock write function, storing the written data.""" - - -class ReadFunctionMock(Mock): - """Mock read function, giving pre-defined data.""" - - def __init__(self, *a, combined_data: bytearray | None = None, **kw): - super().__init__(*a, **kw) - if combined_data is None: - combined_data = bytearray() - self.combined_data = combined_data - - def __call__( - self, - length: int, - ) -> bytearray: # pyright: ignore[reportIncompatibleMethodOverride] - """Override mock's __call__ to make it return part of our :attr:`.combined_data` bytearray. - - This allows us to make the return value always be the next requested part (length) of - the :attr:`.combined_data`. It would be difficult to replicate this with regular mocks, - because some functions can end up making multiple read calls, and each time the result - needs to be different (the next part). - """ - self.return_value = self.combined_data[:length] - del self.combined_data[:length] - return super().__call__(length) - - def assert_read_everything(self, ensure_called: bool = True) -> None: - """Ensure that the passed :attr:`.combined_data` was fully read and depleted.""" - if ensure_called: - self.assert_called() - - if len(self.combined_data) != 0: - raise AssertionError( - f"Read function didn't deplete all of it's data, remaining data: {self.combined_data!r}", - ) - - -class ReadFunctionAsyncMock(ReadFunctionMock, AsyncMock): - """Asynchronous mock read function, giving pre-defined data.""" diff --git a/tests/test_async_clock.py b/tests/test_async_clock.py deleted file mode 100644 index aac40bd..0000000 --- a/tests/test_async_clock.py +++ /dev/null @@ -1,29 +0,0 @@ -import pytest - -from azul import async_clock - - -@pytest.mark.trio -async def test_tick() -> None: - clock = async_clock.Clock() - - await clock.tick(60) - result = await clock.tick(60) - assert isinstance(result, int) - assert result >= 0 - assert repr(clock).startswith(" None: - clock = async_clock.Clock() - - for _ in range(20): - await clock.tick(60) - fps = clock.get_fps() - assert isinstance(fps, float) - assert fps >= 0 diff --git a/tests/test_base_io.py b/tests/test_base_io.py deleted file mode 100644 index 456aad8..0000000 --- a/tests/test_base_io.py +++ /dev/null @@ -1,746 +0,0 @@ -# This is the base_io module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -import platform -import struct -from abc import ABC, abstractmethod -from typing import Any -from unittest.mock import AsyncMock, Mock - -import pytest -from helpers import SynchronizedMixin -from protocol_helpers import ( - ReadFunctionAsyncMock, - ReadFunctionMock, - WriteFunctionAsyncMock, - WriteFunctionMock, -) - -from azul.base_io import ( - INT_FORMATS_TYPE, - BaseAsyncReader, - BaseAsyncWriter, - BaseSyncReader, - BaseSyncWriter, - StructFormat, -) -from azul.utils import to_twos_complement - -# region: Initializable concrete implementations of ABC classes. - - -class SyncWriter(BaseSyncWriter): - """Initializable concrete implementation of :class:`~mcproto.protocol.base_io.BaseSyncWriter` ABC.""" - - def write(self, data: bytes) -> None: - """Concrete implementation of abstract write method. - - Since :class:`abc.ABC` classes can't be initialized if they have any abstract methods - which weren't overridden with a concrete implementations, this is a fake implementation, - without any actual logic, purely to allow the initialization of this class. - - This method is expected to be mocked using :class:`~tests.mcproto.protocol.helpers.WriteFunctionMock` - if it's supposed to get called during testing. - - If this method gets called without being mocked, it will raise :exc:`NotImplementedError`. - """ - raise NotImplementedError( - "This concrete override of abstract write method isn't intended for actual use!\n" - " - If you're writing a new test, did you forget to mock it?\n" - " - If you're seeing this in an existing test, this method got called without the test expecting it," - " this probably means you changed something in the code leading to this call, but you haven't updated" - " the tests to mock this function.", - ) - - -class SyncReader(BaseSyncReader): - """Testable concrete implementation of :class:`~mcproto.protocol.base_io.BaseSyncReader` ABC.""" - - def read(self, length: int) -> bytearray: - """Concrete implementation of abstract read method. - - Since :class:`abc.ABC` classes can't be initialized if they have any abstract methods - which weren't overridden with a concrete implementations, this is a fake implementation, - without any actual logic, purely to allow the initialization of this class. - - This method is expected to be mocked using :class:`~tests.mcproto.protocol.helpers.ReadFunctionMock` - if it's supposed to get called during testing. - - If this method gets called without being mocked, it will raise :exc:`NotImplementedError`. - """ - raise NotImplementedError( - "This concrete override of abstract read method isn't intended for actual use!\n" - " - If you're writing a new test, did you forget to mock it?\n" - " - If you're seeing this in an existing test, this method got called without the test expecting it," - " this probably means you changed something in the code leading to this call, but you haven't updated" - " the tests to mock this function.", - ) - - -class AsyncWriter(BaseAsyncWriter): - """Initializable concrete implementation of :class:`~mcproto.protocol.base_io.BaseAsyncWriter` ABC.""" - - async def write(self, data: bytes) -> None: - """Concrete implementation of abstract write method. - - Since :class:`abc.ABC` classes can't be initialized if they have any abstract methods - which weren't overridden with a concrete implementations, this is a fake implementation, - without any actual logic, purely to allow the initialization of this class. - - This method is expected to be mocked using :class:`~tests.mcproto.protocol.helpers.WriteFunctionAsyncMock` - if it's supposed to get called during testing. - - If this method gets called without being mocked, it will raise :exc:`NotImplementedError`. - """ - raise NotImplementedError( - "This concrete override of abstract write method isn't intended for actual use!\n" - " - If you're writing a new test, did you forget to mock it?\n" - " - If you're seeing this in an existing test, this method got called without the test expecting it," - " this probably means you changed something in the code leading to this call, but you haven't updated" - " the tests to mock this function.", - ) - - -class AsyncReader(BaseAsyncReader): - """Testable concrete implementation of BaseAsyncReader ABC.""" - - async def read(self, length: int) -> bytearray: - """Concrete implementation of abstract read method. - - Since :class:`abc.ABC` classes can't be initialized if they have any abstract methods - which weren't overridden with a concrete implementations, this is a fake implementation, - without any actual logic, purely to allow the initialization of this class. - - This method is expected to be mocked using :class:`~tests.mcproto.protocol.helpers.ReadFunctionAsyncMock` - if it's supposed to get called during testing. - - If this method gets called without being mocked, it will raise :exc:`NotImplementedError`. - """ - raise NotImplementedError( - "This concrete override of abstract read method isn't intended for actual use!\n" - " - If you're writing a new test, did you forget to mock it?\n" - " - If you're seeing this in an existing test, this method got called without the test expecting it," - " this probably means you changed something in the code leading to this call, but you haven't updated" - " the tests to mock this function.", - ) - - -# endregion -# region: Synchronized classes - - -class WrappedAsyncReader(SynchronizedMixin): - """Wrapped synchronous implementation of asynchronous :class:`.AsyncReader` class. - - This essentially mimics :class:`~mcproto.protocol.base_io.BaseSyncReader`. - """ - - _WRAPPED_ATTRIBUTE = "_reader" - - def __init__(self): - self._reader = AsyncReader() - - -class WrappedAsyncWriter(SynchronizedMixin): - """Wrapped synchronous implementation of asynchronous :class:`.AsyncWriter` class. - - This essentially mimics :class:`~mcproto.protocol.base_io.BaseSyncWriter`. - """ - - _WRAPPED_ATTRIBUTE = "_writer" - - def __init__(self): - self._writer = AsyncWriter() - - -# endregion -# region: Abstract test classes - - -class WriterTests(ABC): - """Collection of tests for both sync and async versions of the writer.""" - - writer: BaseSyncWriter | BaseAsyncWriter - - @classmethod - @abstractmethod - def setup_class(cls): - """Initialize writer instance to be tested.""" - ... - - @pytest.fixture - def method_mock(self) -> Mock | AsyncMock: - """Obtain the appropriate type of mock, supporting both sync and async modes.""" - if isinstance(self.writer, BaseSyncWriter): - return Mock - return AsyncMock - - @pytest.fixture - def autopatch(self, monkeypatch: pytest.MonkeyPatch): - """Create a simple function, supporting patching both sync/async writer functions with appropriate mocks. - - This returned function takes in the name of the function to patch, and returns the mock object. - This mock object will either be Mock, or AsyncMock instance, depending on whether we're in async or sync mode. - """ - if isinstance(self.writer, SyncWriter): - patch_path = "mcproto.protocol.base_io.BaseSyncWriter" - mock_type = Mock - else: - patch_path = "mcproto.protocol.base_io.BaseAsyncWriter" - mock_type = AsyncMock - - def autopatch(function_name: str) -> Mock | AsyncMock: - mock_f = mock_type() - monkeypatch.setattr(f"{patch_path}.{function_name}", mock_f) - return mock_f - - return autopatch - - @pytest.fixture - def write_mock(self, monkeypatch: pytest.MonkeyPatch): - """Monkeypatch the write function with a mock which is returned.""" - mock_f = ( - WriteFunctionMock() - if isinstance(self.writer, BaseSyncWriter) - else WriteFunctionAsyncMock() - ) - monkeypatch.setattr(self.writer.__class__, "write", mock_f) - return mock_f - - @pytest.mark.parametrize( - ("fmt", "value", "expected_bytes"), - [ - (StructFormat.UBYTE, 0, [0]), - (StructFormat.UBYTE, 15, [15]), - (StructFormat.UBYTE, 255, [255]), - (StructFormat.BYTE, 0, [0]), - (StructFormat.BYTE, 15, [15]), - (StructFormat.BYTE, 127, [127]), - (StructFormat.BYTE, -20, [to_twos_complement(-20, bits=8)]), - (StructFormat.BYTE, -128, [to_twos_complement(-128, bits=8)]), - ], - ) - def test_write_value( - self, - fmt: INT_FORMATS_TYPE, - value: Any, - expected_bytes: list[int], - write_mock: WriteFunctionMock, - ): - """Test writing values sends expected bytes.""" - self.writer.write_value(fmt, value) - write_mock.assert_has_data(bytearray(expected_bytes)) - - @pytest.mark.parametrize( - ("fmt", "value"), - [ - (StructFormat.UBYTE, -1), - (StructFormat.UBYTE, 256), - (StructFormat.BYTE, -129), - (StructFormat.BYTE, 128), - ], - ) - def test_write_value_out_of_range( - self, - fmt: INT_FORMATS_TYPE, - value: Any, - ): - """Test writing out of range values for the given format raises :exc:`struct.error`.""" - with pytest.raises(struct.error): - self.writer.write_value(fmt, value) - - @pytest.mark.parametrize( - ("number", "expected_bytes"), - [ - (0, [0]), - (1, [1]), - (2, [2]), - (15, [15]), - (127, [127]), - (128, [128, 1]), - (129, [129, 1]), - (255, [255, 1]), - (1000000, [192, 132, 61]), - (2147483647, [255, 255, 255, 255, 7]), - ], - ) - def test_write_varuint( - self, - number: int, - expected_bytes: list[int], - write_mock: WriteFunctionMock, - ): - """Test writing varuints results in correct bytes.""" - self.writer._write_varuint(number) - write_mock.assert_has_data(bytearray(expected_bytes)) - - @pytest.mark.parametrize( - ("write_value", "max_bits"), - [ - (-1, 128), - (-1, 1), - (2**16, 16), - (2**32, 32), - ], - ) - def test_write_varuint_out_of_range(self, write_value: int, max_bits: int): - """Test writing out of range varuints raises :exc:`ValueError`.""" - with pytest.raises( - ValueError, - match="^Tried to write varint outside of the range of", - ): - self.writer._write_varuint(write_value, max_bits=max_bits) - - @pytest.mark.parametrize( - ("number", "expected_bytes"), - [ - (127, [127]), - (16384, [128, 128, 1]), - (-128, [128, 255, 255, 255, 15]), - (-16383, [129, 128, 255, 255, 15]), - ], - ) - def test_write_varint( - self, - number: int, - expected_bytes: list[int], - write_mock: WriteFunctionMock, - ): - """Test writing varints results in correct bytes.""" - self.writer.write_varint(number) - write_mock.assert_has_data(bytearray(expected_bytes)) - - @pytest.mark.parametrize( - ("number", "expected_bytes"), - [ - (127, [127]), - (16384, [128, 128, 1]), - (-128, [128, 255, 255, 255, 255, 255, 255, 255, 255, 1]), - (-16383, [129, 128, 255, 255, 255, 255, 255, 255, 255, 1]), - ], - ) - def test_write_varlong( - self, - number: int, - expected_bytes: list[int], - write_mock: WriteFunctionMock, - ): - """Test writing varlongs results in correct bytes.""" - self.writer.write_varlong(number) - write_mock.assert_has_data(bytearray(expected_bytes)) - - @pytest.mark.parametrize( - ("data", "expected_bytes"), - [ - (b"", [0]), - (b"\x01", [1, 1]), - ( - b"hello\0world", - [11, 104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100], - ), - (b"\x01\x02\x03four\x05", [8, 1, 2, 3, 102, 111, 117, 114, 5]), - ], - ) - def test_write_bytearray( - self, - data: bytes, - expected_bytes: list[int], - write_mock: WriteFunctionMock, - ): - """Test writing ASCII string results in correct bytes.""" - self.writer.write_bytearray(data) - write_mock.assert_has_data(bytearray(expected_bytes)) - - @pytest.mark.parametrize( - ("string", "expected_bytes"), - [ - ("test", [*list(map(ord, "test")), 0]), - ("a" * 100, [*list(map(ord, "a" * 100)), 0]), - ("", [0]), - ], - ) - def test_write_ascii( - self, - string: str, - expected_bytes: list[int], - write_mock: WriteFunctionMock, - ): - """Test writing ASCII string results in correct bytes.""" - self.writer.write_ascii(string) - write_mock.assert_has_data(bytearray(expected_bytes)) - - @pytest.mark.parametrize( - ("string", "expected_bytes"), - [ - ("test", [len("test"), *list(map(ord, "test"))]), - ("a" * 100, [len("a" * 100), *list(map(ord, "a" * 100))]), - ("", [0]), - ("नमस्ते", [18] + [int(x) for x in "नमस्ते".encode()]), - ], - ) - def test_write_utf( - self, - string: str, - expected_bytes: list[int], - write_mock: WriteFunctionMock, - ): - """Test writing UTF string results in correct bytes.""" - self.writer.write_utf(string) - write_mock.assert_has_data(bytearray(expected_bytes)) - - @pytest.mark.skipif( - platform.system() == "Windows", - reason="environment variable limit on Windows", - ) - def test_write_utf_limit(self, write_mock: WriteFunctionMock): - """Test writing a UTF string too big raises a :exc:`ValueError`.""" - with pytest.raises( - ValueError, - match="Maximum character limit for writing strings is 32767 characters.", - ): - self.writer.write_utf("a" * (32768)) - - def test_write_optional_true( - self, - method_mock: Mock | AsyncMock, - write_mock: WriteFunctionMock, - ): - """Test writing non-``None`` value writes ``True`` and runs the writer function.""" - mock_v = Mock() - mock_f = method_mock() - self.writer.write_optional(mock_v, mock_f) - mock_f.assert_called_once_with(mock_v) - write_mock.assert_has_data(bytearray([1])) - - def test_write_optional_false( - self, - method_mock: Mock | AsyncMock, - write_mock: WriteFunctionMock, - ): - """Test writing ``None`` value should write ``False`` and skip running the writer function.""" - mock_f = method_mock() - self.writer.write_optional(None, mock_f) - mock_f.assert_not_called() - write_mock.assert_has_data(bytearray([0])) - - -class ReaderTests(ABC): - """Collection of tests for both sync and async versions of the reader.""" - - reader: BaseSyncReader | BaseAsyncReader - - @classmethod - @abstractmethod - def setup_class(cls): - """Initialize reader instance to be tested.""" - ... - - @pytest.fixture - def method_mock(self) -> Mock | AsyncMock: - """Obtain the appropriate type of mock, supporting both sync and async modes.""" - if isinstance(self.reader, BaseSyncReader): - return Mock - return AsyncMock - - @pytest.fixture - def autopatch(self, monkeypatch: pytest.MonkeyPatch): - """Create a simple function, supporting patching both sync/async reader functions with appropriate mocks. - - This returned function takes in the name of the function to patch, and returns the mock object. - This mock object will either be Mock, or AsyncMock instance, depending on whether we're in async or sync mode. - """ - if isinstance(self.reader, SyncReader): - patch_path = "mcproto.protocol.base_io.BaseSyncReader" - mock_type = Mock - else: - patch_path = "mcproto.protocol.base_io.BaseAsyncReader" - mock_type = AsyncMock - - def autopatch(function_name: str) -> Mock | AsyncMock: - mock_f = mock_type() - monkeypatch.setattr(f"{patch_path}.{function_name}", mock_f) - return mock_f - - return autopatch - - @pytest.fixture - def read_mock(self, monkeypatch: pytest.MonkeyPatch): - """Monkeypatch the read function with a mock which is returned.""" - mock_f = ( - ReadFunctionMock() - if isinstance(self.reader, SyncReader) - else ReadFunctionAsyncMock() - ) - monkeypatch.setattr(self.reader.__class__, "read", mock_f) - yield mock_f - # Run this assertion after the test, to ensure that all specified data - # to be read, actually was read - mock_f.assert_read_everything() - - @pytest.mark.parametrize( - ("fmt", "read_bytes", "expected_value"), - [ - (StructFormat.UBYTE, [0], 0), - (StructFormat.UBYTE, [10], 10), - (StructFormat.UBYTE, [255], 255), - (StructFormat.BYTE, [0], 0), - (StructFormat.BYTE, [20], 20), - (StructFormat.BYTE, [127], 127), - (StructFormat.BYTE, [to_twos_complement(-20, bits=8)], -20), - (StructFormat.BYTE, [to_twos_complement(-128, bits=8)], -128), - ], - ) - def test_read_value( - self, - fmt: INT_FORMATS_TYPE, - read_bytes: list[int], - expected_value: Any, - read_mock: ReadFunctionMock, - ): - """Test reading bytes gets expected value.""" - read_mock.combined_data = bytearray(read_bytes) - assert self.reader.read_value(fmt) == expected_value - - @pytest.mark.parametrize( - ("read_bytes", "expected_value"), - [ - ([0], 0), - ([1], 1), - ([2], 2), - ([15], 15), - ([127], 127), - ([128, 1], 128), - ([129, 1], 129), - ([255, 1], 255), - ([192, 132, 61], 1000000), - ([255, 255, 255, 255, 7], 2147483647), - ], - ) - def test_read_varuint( - self, - read_bytes: list[int], - expected_value: int, - read_mock: ReadFunctionMock, - ): - """Test reading varuint bytes results in correct values.""" - read_mock.combined_data = bytearray(read_bytes) - assert self.reader._read_varuint() == expected_value - - @pytest.mark.parametrize( - ("read_bytes", "max_bits"), - [ - ([128, 128, 4], 16), - ([128, 128, 128, 128, 16], 32), - ], - ) - def test_read_varuint_out_of_range( - self, - read_bytes: list[int], - max_bits: int, - read_mock: ReadFunctionMock, - ): - """Test reading out-of-range varuints raises :exc:`IOError`.""" - read_mock.combined_data = bytearray(read_bytes) - with pytest.raises( - IOError, - match="^Received varint was outside the range of", - ): - self.reader._read_varuint(max_bits=max_bits) - - @pytest.mark.parametrize( - ("read_bytes", "expected_value"), - [ - ([127], 127), - ([128, 128, 1], 16384), - ([128, 255, 255, 255, 15], -128), - ([129, 128, 255, 255, 15], -16383), - ], - ) - def test_read_varint( - self, - read_bytes: list[int], - expected_value: int, - read_mock: ReadFunctionMock, - ): - """Test reading varuint bytes results in correct values.""" - read_mock.combined_data = bytearray(read_bytes) - assert self.reader.read_varint() == expected_value - - @pytest.mark.parametrize( - ("read_bytes", "expected_value"), - [ - ([127], 127), - ([128, 128, 1], 16384), - ([128, 255, 255, 255, 255, 255, 255, 255, 255, 1], -128), - ([129, 128, 255, 255, 255, 255, 255, 255, 255, 1], -16383), - ], - ) - def test_read_varlong( - self, - read_bytes: list[int], - expected_value: int, - read_mock: ReadFunctionMock, - ): - """Test reading varuint bytes results in correct values.""" - read_mock.combined_data = bytearray(read_bytes) - assert self.reader.read_varlong() == expected_value - - @pytest.mark.parametrize( - ("read_bytes", "expected_bytes"), - [ - ([0], b""), - ([1, 1], b"\x01"), - ( - [11, 104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100], - b"hello\0world", - ), - ([8, 1, 2, 3, 102, 111, 117, 114, 5], b"\x01\x02\x03four\x05"), - ], - ) - def test_read_bytearray( - self, - read_bytes: list[int], - expected_bytes: bytes, - read_mock: ReadFunctionMock, - ): - """Test reading ASCII string results in correct bytes.""" - read_mock.combined_data = bytearray(read_bytes) - assert self.reader.read_bytearray() == expected_bytes - - @pytest.mark.parametrize( - ("read_bytes", "expected_string"), - [ - ([*list(map(ord, "test")), 0], "test"), - ([*list(map(ord, "a" * 100)), 0], "a" * 100), - ([0], ""), - ], - ) - def test_read_ascii( - self, - read_bytes: list[int], - expected_string: str, - read_mock: ReadFunctionMock, - ): - """Test reading ASCII string results in correct bytes.""" - read_mock.combined_data = bytearray(read_bytes) - assert self.reader.read_ascii() == expected_string - - @pytest.mark.parametrize( - ("read_bytes", "expected_string"), - [ - ([len("test"), *list(map(ord, "test"))], "test"), - ([len("a" * 100), *list(map(ord, "a" * 100))], "a" * 100), - ([0], ""), - ([18] + [int(x) for x in "नमस्ते".encode()], "नमस्ते"), - ], - ) - def test_read_utf( - self, - read_bytes: list[int], - expected_string: str, - read_mock: ReadFunctionMock, - ): - """Test reading UTF string results in correct values.""" - read_mock.combined_data = bytearray(read_bytes) - assert self.reader.read_utf() == expected_string - - @pytest.mark.skipif( - platform.system() == "Windows", - reason="environment variable limit on Windows", - ) - @pytest.mark.parametrize( - ("read_bytes"), - [ - [253, 255, 7], - [128, 128, 2, *list(map(ord, "a" * 32768))], - ], - # Temporary workaround. - # https://github.com/pytest-dev/pytest/issues/6881#issuecomment-596381626 - ids=["a", "b"], - ) - def test_read_utf_limit( - self, - read_bytes: list[int], - read_mock: ReadFunctionMock, - ): - """Test reading a UTF string too big raises an IOError.""" - read_mock.combined_data = bytearray(read_bytes) - with pytest.raises( - IOError, - match="^Maximum read limit for utf strings is ", - ): - self.reader.read_utf() - - def test_read_optional_true( - self, - method_mock: Mock | AsyncMock, - read_mock: ReadFunctionMock, - ): - """Test reading optional runs reader function when first bool is ``True``.""" - mock_f = method_mock() - read_mock.combined_data = bytearray([1]) - self.reader.read_optional(mock_f) - mock_f.assert_called_once_with() - - def test_read_optional_false( - self, - method_mock: Mock | AsyncMock, - read_mock: ReadFunctionMock, - ): - """Test reading optional doesn't run reader function when first bool is ``False``.""" - mock_f = method_mock() - read_mock.combined_data = bytearray([0]) - self.reader.read_optional(mock_f) - mock_f.assert_not_called() - - -# endregion -# region: Concrete test classes - - -class TestBaseSyncWriter(WriterTests): - """Tests for individual write methods implemented in :class:`~mcproto.protocol.base_io.BaseSyncWriter`.""" - - @classmethod - def setup_class(cls): - """Initialize writer instance to be tested.""" - cls.writer = SyncWriter() - - -class TestBaseSyncReader(ReaderTests): - """Tests for individual write methods implemented in :class:`~mcproto.protocol.base_io.BaseSyncReader`.""" - - @classmethod - def setup_class(cls): - """Initialize reader instance to be tested.""" - cls.reader = SyncReader() - - -class TestBaseAsyncWriter(WriterTests): - """Tests for individual write methods implemented in :class:`~mcproto.protocol.base_io.BaseSyncReader`.""" - - writer: WrappedAsyncWriter - - @classmethod - def setup_class(cls): - """Initialize writer instance to be tested.""" - cls.writer = WrappedAsyncWriter() - - -class TestBaseAsyncReader(ReaderTests): - """Tests for individual write methods implemented in :class:`~mcproto.protocol.base_io.BaseSyncReader`.""" - - reader: WrappedAsyncReader - - @classmethod - def setup_class(cls): - """Initialize writer instance to be tested.""" - cls.reader = WrappedAsyncReader() - - -# endregion diff --git a/tests/test_buffer.py b/tests/test_buffer.py deleted file mode 100644 index ac30774..0000000 --- a/tests/test_buffer.py +++ /dev/null @@ -1,97 +0,0 @@ -# This is the base_io module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -import pytest - -from azul.buffer import Buffer - - -def test_write() -> None: - """Writing into the buffer should store data.""" - buf = Buffer() - buf.write(b"Hello") - assert buf, bytearray(b"Hello") - - -def test_read() -> None: - """Reading from buffer should return stored data.""" - buf = Buffer(b"Reading is cool") - data = buf.read(len(buf)) - assert data == b"Reading is cool" - - -def test_read_multiple() -> None: - """Multiple reads should deplete the data.""" - buf = Buffer(b"Something random") - data = buf.read(9) - assert data == b"Something" - data = buf.read(7) - assert data == b" random" - - -def test_no_data_read() -> None: - """Reading more data than available should raise IOError.""" - buf = Buffer(b"Blip") - with pytest.raises( - IOError, - match="^Requested to read more data than available.", - ): - buf.read(len(buf) + 1) - - -def test_reset() -> None: - """Resetting should treat already read data as new unread data.""" - buf = Buffer(b"Will it reset?") - data = buf.read(len(buf)) - buf.reset() - data2 = buf.read(len(buf)) - assert data == data2 - assert data == b"Will it reset?" - - -def test_clear() -> None: - """Clearing should remove all stored data from buffer.""" - buf = Buffer(b"Will it clear?") - buf.clear() - assert buf == bytearray() - - -def test_clear_resets_position() -> None: - """Clearing should reset reading position for new data to be read.""" - buf = Buffer(b"abcdef") - buf.read(3) - buf.clear() - buf.write(b"012345") - data = buf.read(3) - assert data == b"012" - - -def test_clear_read_only() -> None: - """Clearing should allow just removing the already read data.""" - buf = Buffer(b"0123456789") - buf.read(5) - buf.clear(only_already_read=True) - assert buf == bytearray(b"56789") - - -def test_flush() -> None: - """Flushing should read all available data and clear out the buffer.""" - buf = Buffer(b"Foobar") - data = buf.flush() - assert data == b"Foobar" - assert buf == bytearray() - - -def test_remainig() -> None: - """Buffer should report correct amount of remaining bytes to be read.""" - buf = Buffer(b"012345") # 6 bytes to be read - assert buf.remaining == 6 - buf.read(2) - assert buf.remaining == 4 - buf.clear() - assert buf.remaining == 0 diff --git a/tests/test_component.py b/tests/test_component.py deleted file mode 100644 index d2ce838..0000000 --- a/tests/test_component.py +++ /dev/null @@ -1,370 +0,0 @@ -from __future__ import annotations - -import gc - -import pytest -import trio - -from azul.component import ( - Component, - ComponentManager, - Event, - ExternalRaiseManager, -) - - -def test_event_init() -> None: - event = Event("event_name", {"fish": 27}, 3) - assert event.name == "event_name" - assert event.data == {"fish": 27} - assert event.level == 3 - - -def test_event_pop_level() -> None: - event = Event("event_name", None, 3) - assert event.pop_level() - assert event.level == 2 - assert event.pop_level() - assert event.level == 1 - assert event.pop_level() - assert event.level == 0 - - assert not event.pop_level() - assert event.level == 0 - - -def test_event_repr() -> None: - assert repr(Event("cat_moved", (3, 3))) == "Event('cat_moved', (3, 3), 0)" - - -def test_component_init() -> None: - component = Component("component_name") - assert component.name == "component_name" - - -def test_component_repr() -> None: - assert repr(Component("fish")) == "Component('fish')" - - -def test_component_manager_property_error() -> None: - component = Component("waffle") - assert not component.manager_exists - with pytest.raises( - AttributeError, - match="^No component manager bound for", - ): - component.manager # noqa: B018 - - -def test_componentmanager_add_has_manager_property() -> None: - manager = ComponentManager("manager") - sound_effect = Component("sound_effect") - with pytest.raises(AttributeError): - print(sound_effect.manager) - manager.add_component(sound_effect) - assert manager.component_exists("sound_effect") - assert sound_effect.manager_exists - assert sound_effect.manager is manager - assert sound_effect.component_exists("sound_effect") - assert sound_effect.components_exist(("sound_effect",)) - assert not sound_effect.components_exist(("sound_effect", "waffle")) - assert manager.list_components() == ("sound_effect",) - assert sound_effect.get_component("sound_effect") is sound_effect - assert sound_effect.get_components(("sound_effect",)) == [sound_effect] - - -def test_componentmanager_manager_property_weakref_failure() -> None: - # Have to override __del__, unbind_components called and unbinds - # components so weakref failure branch never hit in normal - # circumstances - class EvilNoUnbindManager(ComponentManager): - def __del__(self) -> None: - return - - manager = EvilNoUnbindManager("manager") - sound_effect = Component("sound_effect") - with pytest.raises(AttributeError): - print(sound_effect.manager) - manager.add_component(sound_effect) - assert sound_effect.manager is manager - del manager - # make sure gc collects manager - for _ in range(3): - gc.collect() - with pytest.raises(AttributeError): - print(sound_effect.manager) - - -def test_double_bind_error() -> None: - manager = ComponentManager("manager") - sound_effect = Component("sound_effect") - manager.add_component(sound_effect) - manager_two = ComponentManager("manager_two") - with pytest.raises(RuntimeError, match="component is already bound to"): - manager_two.add_component(sound_effect) - - -def test_self_component() -> None: - manager = ComponentManager("manager", "cat_event") - assert manager.component_exists("cat_event") - assert manager.get_component("cat_event") is manager - - cat_event = Component("cat_event") - with pytest.raises(ValueError, match="already exists"): - manager.add_component(cat_event) - - -def test_add_multiple() -> None: - manager = ComponentManager("manager") - manager.add_components( - ( - Component("fish"), - Component("waffle"), - ), - ) - assert manager.component_exists("fish") - assert manager.component_exists("waffle") - - manager.unbind_components() - assert not manager.get_all_components() - - -def test_component_not_exist_error() -> None: - manager = ComponentManager("manager") - with pytest.raises(ValueError, match="does not exist"): - manager.remove_component("darkness") - with pytest.raises(ValueError, match="does not exist"): - manager.get_component("darkness") - - -@pytest.mark.trio -async def test_self_component_handler() -> None: - event_called = False - - async def event_call(event: Event[None]) -> None: - nonlocal event_called - assert event.name == "fish_appears_event" - event_called = True - - manager = ComponentManager("manager", "cat") - manager.register_handler("fish_appears_event", event_call) - - assert manager.has_handler("fish_appears_event") - - await manager.raise_event(Event("fish_appears_event", None)) - assert event_called - - -@pytest.mark.trio -async def test_raise_event_register_handlers_double_call() -> None: - event_called_count = 0 - - async def event_call(event: Event[int]) -> None: - nonlocal event_called_count - assert event.data == 27 - event_called_count += 1 - - manager = ComponentManager("manager") - assert not manager.has_handler("event_name") - - manager.register_component_handler("event_name", event_call, manager.name) - assert manager.has_handler("event_name") - await manager.raise_event(Event("event_name", 27)) - assert event_called_count == 1 - - event_called_count = 0 - - with pytest.raises(ValueError, match="is not registered!"): - manager.register_component_handler( - "event_name", - event_call, - "2nd name", - ) - manager.add_component(Component("2nd name")) - manager.register_component_handler("event_name", event_call, "2nd name") - - await manager.raise_event(Event("event_name", 27)) - assert event_called_count == 2 - - -@pytest.mark.trio -async def test_raise_event_register_handlers() -> None: - event_called = False - - async def event_call(event: Event[int]) -> None: - nonlocal event_called - assert event.data == 27 - event_called = True - - manager = ComponentManager("manager") - sound_effect = Component("sound_effect") - manager.add_component(sound_effect) - sound_effect.register_handlers({"event_name": event_call}) - - assert sound_effect.has_handler("event_name") - - await sound_effect.raise_event(Event("event_name", 27)) - assert event_called - - event_called = False - await manager.raise_event(Event("event_name", 27)) - assert event_called - - event_called = False - manager.remove_component("sound_effect") - with pytest.raises(AttributeError, match="No component manager bound for"): - await sound_effect.raise_event(Event("event_name", 27)) - await manager.raise_event(Event("event_name", 27)) - assert not event_called - - -@pytest.mark.trio -async def test_raise_leveled_comes_back() -> None: - event_called = False - - async def event_call(event: Event[int]) -> None: - nonlocal event_called - assert event.level == 0 - event_called = True - - event_called_two = False - - async def event_call_two(event: Event[int]) -> None: - nonlocal event_called_two - assert event.level == 0 - event_called_two = True - - super_manager = ComponentManager("super_manager") - manager = ComponentManager("manager") - - super_manager.add_component(manager) - assert super_manager.component_exists("manager") - - super_manager.register_handler("leveled_event", event_call) - manager.register_handler("leveled_event", event_call_two) - - await manager.raise_event(Event("leveled_event", None, 1)) - assert event_called - assert event_called_two - - -@pytest.mark.trio -async def test_raise_event_register_handler() -> None: - event_called = False - - async def event_call(event: Event[int]) -> None: - nonlocal event_called - assert event.data == 27 - event_called = True - - manager = ComponentManager("manager") - sound_effect = Component("sound_effect") - manager.add_component(sound_effect) - sound_effect.register_handler("event_name", event_call) - - await sound_effect.raise_event(Event("event_name", 27)) - assert event_called - - -@pytest.mark.trio -async def test_raises_event_in_nursery() -> None: - nursery_called = False - event_called = False - - async def call_bean(event: Event[None]) -> None: - nonlocal event_called - assert event.name == "bean_event" - event_called = True - - async with trio.open_nursery() as nursery: - original = nursery.start_soon - - def replacement(*args: object, **kwargs: object) -> object: - nonlocal nursery_called - nursery_called = True - return original(*args, **kwargs) - - nursery.start_soon = replacement - - manager = ExternalRaiseManager("manager", nursery) - manager.register_handler("bean_event", call_bean) - await manager.raise_event(Event("bean_event", None)) - assert nursery_called - assert event_called - - -@pytest.mark.trio -async def test_internal_does_not_raise_event_in_nursery() -> None: - nursery_called = False - event_called = False - - async def call_bean(event: Event[None]) -> None: - nonlocal event_called - assert event.name == "bean_event" - event_called = True - - async with trio.open_nursery() as nursery: - original = nursery.start_soon - - def replacement(*args: object, **kwargs: object) -> object: - nonlocal nursery_called - nursery_called = True - return original(*args, **kwargs) - - nursery.start_soon = replacement - - manager = ExternalRaiseManager("manager", nursery) - manager.register_handler("bean_event", call_bean) - await manager.raise_event_internal(Event("bean_event", None)) - assert not nursery_called - assert event_called - - -@pytest.mark.trio -async def test_temporary_component() -> None: - event_called = False - - async def event_call(event: Event[int]) -> None: - nonlocal event_called - assert event.data == 27 - event_called = True - - manager = ComponentManager("manager") - with manager.temporary_component( - Component("sound_effect"), - ) as sound_effect: - assert manager.component_exists("sound_effect") - sound_effect.register_handler("event_name", event_call) - - await sound_effect.raise_event(Event("event_name", 27)) - assert event_called - assert not manager.component_exists("sound_effect") - with manager.temporary_component( - Component("sound_effect"), - ) as sound_effect: - manager.remove_component("sound_effect") - - -@pytest.mark.trio -async def test_remove_component() -> None: - event_called = False - - async def event_call(event: Event[int]) -> None: - nonlocal event_called - assert event.data == 27 - event_called = True - - manager = ComponentManager("manager") - sound_effect = Component("sound_effect") - manager.add_component(sound_effect) - assert manager.component_exists("sound_effect") - sound_effect.register_handler("event_name", event_call) - sound_effect.register_handler("waffle_name", event_call) - - await sound_effect.raise_event(Event("event_name", 27)) - assert event_called - manager.add_component(Component("jerald")) - manager.register_handler("event_name", event_call) - manager.remove_component("jerald") - manager.remove_component("sound_effect") - assert not manager.component_exists("sound_effect") diff --git a/tests/test_encrypted_event.py b/tests/test_encrypted_event.py deleted file mode 100644 index aaa837d..0000000 --- a/tests/test_encrypted_event.py +++ /dev/null @@ -1,79 +0,0 @@ -from __future__ import annotations - -import pytest -import trio -import trio.testing - -from azul.component import Event -from azul.encrypted_event import EncryptedNetworkEventComponent - - -@pytest.mark.trio -async def test_event_transmission() -> None: - one, two = trio.testing.memory_stream_pair() - client_one = EncryptedNetworkEventComponent.from_stream("one", stream=one) - client_two = EncryptedNetworkEventComponent.from_stream("two", stream=two) - - client_one.register_network_write_event("echo_event", 0) - client_two.register_read_network_event(0, "reposted_event") - - event = Event( - "echo_event", - bytearray("I will give my cat food to bob", "utf-8"), - 3, - ) - - await client_one.write_event(event) - read_event = await client_two.read_event() - assert read_event.name == "reposted_event" - assert read_event.data == event.data - - await client_one.close() - await client_two.close() - - -@pytest.mark.trio -async def test_event_encrypted_transmission() -> None: - verification_token = bytes.fromhex("da053623dd3dcd441e105ee5ce212ac8") - shared_secret = bytes.fromhex( - "95a883358f09cd5698b3cf8a414a8a659a35c4eb877e9b0228b7f64df85b0f26", - ) - - one, two = trio.testing.memory_stream_pair() - client_one = EncryptedNetworkEventComponent.from_stream("one", stream=one) - client_two = EncryptedNetworkEventComponent.from_stream("two", stream=two) - - client_one.register_network_write_event("echo_event", 0) - client_two.register_read_network_event(0, "reposted_event") - - event = Event( - "echo_event", - bytearray("I will give my cat food to bob", "utf-8"), - 3, - ) - - await client_one.write_event(event) - read_event = await client_two.read_event() - assert read_event.name == "reposted_event" - assert read_event.data == event.data - - await client_one.write_event(event) - assert ( - await two.receive_some() == b"\x00\x1eI will give my cat food to bob" - ) - - client_one.enable_encryption(shared_secret, verification_token) - client_two.enable_encryption(shared_secret, verification_token) - - await client_one.write_event(event) - read_event = await client_two.read_event() - assert read_event.name == "reposted_event" - assert read_event.data == event.data - - await client_one.write_event(event) - assert await two.receive_some() == bytearray.fromhex( - "2bb572309dfb71d22eb5f0442c5347f2d666ed16c97093190a8101c3e59f2beb", - ) - - await client_one.close() - await client_two.close() diff --git a/tests/test_encryption.py b/tests/test_encryption.py deleted file mode 100644 index 92538ad..0000000 --- a/tests/test_encryption.py +++ /dev/null @@ -1,110 +0,0 @@ -# This is the buffer module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -from typing import TYPE_CHECKING, cast - -from cryptography.hazmat.primitives.asymmetric.padding import MGF1, OAEP -from cryptography.hazmat.primitives.hashes import SHA256 -from cryptography.hazmat.primitives.serialization import load_pem_private_key - -from azul.encryption import ( - decrypt_token_and_secret, - deserialize_public_key, - encrypt_token_and_secret, - serialize_public_key, -) - -if TYPE_CHECKING: - from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey - -_SERIALIZED_RSA_PRIVATE_KEY = b""" ------BEGIN PRIVATE KEY----- -MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMtRUQmRHqPkdA2K -F6fM2c8ibIPHYV5KVQXNEkVx7iEKS6JsfELhX1H8t/qQ3Ob4Pr4OFjgXx9n7GvfZ -gekNoswG6lnQH/n7t2sYA 6D+WvSix1FF2J6wPmpKriHS59TDk4opjaV14S4K4XjW -Gmm8DqCzgXkPGC2dunFb+1A8mdkrAgMBAAECgYAWj2dWkGu989OMzQ3i6LAic8dm -t/Dt7YGRqzejzQiHUgUieLcxFKDnEAu6GejpGBKeNCHzB3B9l4deiRwJKCIwHqMN -LKMKoayinA8mj/Y/ O/ELDofkEyeXOhFyM642sPpaxQJoNWc9QEsYbxpG2zeB3sPf -l3eIhkYTKVdxB+o8AQJBAPiddMjU8fuHyjKT6VCL2ZQbwnrRe1AaLLE6VLwEZuZC -wlbx5Lcszi77PkMRTvltQW39VN6MEjiYFSPtRJleA+sCQQDRW2e3BX6uiil2IZ08 -tPFMnltFJpa 8YvW50N6mySd8Zg1oQJpzP2fC0n0+K4j3EiA/Zli8jBt45cJ4dMGX -km/BAkEAtkYy5j+BvolbDGP3Ti+KcRU9K/DD+QGHvNRoZYTQsIdHlpk4t7eo3zci -+ecJwMOCkhKHE7cccNPHxBRkFBGiywJAJBt2pMsu0R2FDxm3C6xNXaCGL0P7hVwv -8y9B51 QUGlFjiJJz0OKjm6c/8IQDqFEY/LZDIamsZ0qBItNIPEMGQQJALZV0GD5Y -zmnkw1hek/JcfQBlVYo3gFmWBh6Hl1Lb7p3TKUViJCA1k2f0aGv7+d9aFS0fRq6u -/sETkem8Jc1s3g== ------END PRIVATE KEY----- -""" -RSA_PRIVATE_KEY = cast( - "RSAPrivateKey", - load_pem_private_key(_SERIALIZED_RSA_PRIVATE_KEY, password=None), -) -RSA_PUBLIC_KEY = RSA_PRIVATE_KEY.public_key() -SERIALIZED_RSA_PUBLIC_KEY = bytes.fromhex( - "30819f300d06092a864886f70d010101050003818d0030818902818100cb515109911ea3e4740d8a17a7ccd9cf226c83c7615e4a5505cd124571ee210a4ba26c7c42e15f51fcb7fa90dce6f83ebe0e163817c7d9fb1af7d981e90da2cc06ea59d01ff9fbb76b1803a0fe5af4a2c75145d89eb03e6a4aae21d2e7d4c3938a298da575e12e0ae178d61a69bc0ea0b381790f182d9dba715bfb503c99d92b0203010001", -) - - -def test_encrypt_token_and_secret() -> None: - """Test encryption returns properly encrypted (decryptable) values.""" - verification_token = bytes.fromhex("da053623dd3dcd441e105ee5ce212ac8") - shared_secret = bytes.fromhex( - "95a883358f09cd5698b3cf8a414a8a659a35c4eb877e9b0228b7f64df85b0f26", - ) - - encrypted_token, encrypted_secret = encrypt_token_and_secret( - RSA_PUBLIC_KEY, - verification_token, - shared_secret, - ) - - assert ( - RSA_PRIVATE_KEY.decrypt( - encrypted_token, - OAEP(MGF1(SHA256()), SHA256(), None), - ) - == verification_token - ) - assert ( - RSA_PRIVATE_KEY.decrypt( - encrypted_secret, - OAEP(MGF1(SHA256()), SHA256(), None), - ) - == shared_secret - ) - - -def test_decrypt_token_and_secret() -> None: - """Test decryption returns properly decrypted values.""" - encrypted_token = bytes.fromhex( - "5541c0c0fc99d8908ed428b20c260795bec7b4041a4f98d26fbed383e8dba077eb53fb5cf905e722e2ceb341843e875508134817bcd3a909ac279e77ed94fd98c428bbe00db630a5ad3df310380d9274ed369cc6a011e7edd45cbe44ae8ad2575ef793b23057e4b15f1b6e3e195ff0921e46370773218517922fbb8b96092d88", - ) - encrypted_secret = bytes.fromhex( - "1a43782ca17f71e87e6ef98f9be66050ecf5d185da81445d26ceb5941f95d69d61b726d27b5ca62aed4cbe27b40fd4bd6b16b5be154a7b6a24ae31c705bc47d9397589b448fb72b14572ea2a9d843c6a3c674b7454cef97e2d65be36e0d0a8cc9f1093a19a8d52a5633a5317d19779bb46146dfaea7a690a7f080fb77d59c7f9", - ) - - assert decrypt_token_and_secret( - RSA_PRIVATE_KEY, - encrypted_token, - encrypted_secret, - ) == ( - bytes.fromhex("da053623dd3dcd441e105ee5ce212ac8"), - bytes.fromhex( - "95a883358f09cd5698b3cf8a414a8a659a35c4eb877e9b0228b7f64df85b0f26", - ), - ) - - -def test_serialize_public_key() -> None: - """Test serialize_public_key.""" - assert serialize_public_key(RSA_PUBLIC_KEY) == SERIALIZED_RSA_PUBLIC_KEY - - -def test_deserialize_public_key() -> None: - """Test deserialize_public_key.""" - assert deserialize_public_key(SERIALIZED_RSA_PUBLIC_KEY) == RSA_PUBLIC_KEY diff --git a/tests/test_network.py b/tests/test_network.py deleted file mode 100644 index 0273bff..0000000 --- a/tests/test_network.py +++ /dev/null @@ -1,178 +0,0 @@ -from __future__ import annotations - -from typing import TYPE_CHECKING - -import pytest -import trio -import trio.testing - -from azul.component import ComponentManager, Event -from azul.network import ( - NetworkComponent, - NetworkEventComponent, - NetworkStreamNotConnectedError, - NetworkTimeoutError, - Server, -) - -if TYPE_CHECKING: - from collections.abc import Callable - - -@pytest.mark.trio -async def client_connect(port: int, stop_server: Callable[[], None]) -> None: - await trio.sleep(0.05) - # manager = ComponentManager("manager") - - client = NetworkEventComponent("client") - # manager.add_component(client) - - await client.connect("127.0.0.1", port) - - client.register_network_write_event("echo_event", 0) - client.register_read_network_event(1, "reposted_event") - - event = Event( - "echo_event", - bytearray("I will give my cat food to bob", "utf-8"), - 3, - ) - - # await client.raise_event(event) - await client.write_event(event) - print(f"{await client.read_event() = }") - - await client.close() - stop_server() - - -@pytest.mark.trio -async def run_async() -> None: - class TestServer(Server): - async def handler(self, stream: trio.SocketStream) -> None: - client = NetworkEventComponent.from_stream("client", stream=stream) - - client.register_read_network_event(0, "repost_event") - client.register_network_write_event("repost_event", 1) - - await client.write_event(await client.read_event()) - await stream.aclose() - - server = TestServer("server") - port = 3004 - async with trio.open_nursery() as nursery: - nursery.start_soon(server.serve, port) - nursery.start_soon(client_connect, port, server.stop_serving) - nursery.start_soon(client_connect, port, server.stop_serving) - - -def test_not_connected() -> None: - client = NetworkComponent("name") - assert client.not_connected - with pytest.raises(NetworkStreamNotConnectedError): - print(client.stream) - - -@pytest.mark.trio -async def test_from_stream() -> None: - stream = trio.testing.MemorySendStream() - - named = NetworkComponent.from_stream( - kwargs={"name": "name"}, - stream=stream, - ) - with pytest.raises(RuntimeError, match="Already connected!"): - await named.connect("example.com", 80) - await named.close() - - -@pytest.mark.trio -async def test_register_network_write_event() -> None: - client = NetworkEventComponent("client") - client.register_network_write_event("echo_event", 0) - with pytest.raises(ValueError, match="event already registered"): - client.register_network_write_event("echo_event", 0) - client.register_read_network_event(0, "reposted_event") - with pytest.raises(ValueError, match="events are also being received"): - client.register_network_write_events({"reposted_event": 0}) - with pytest.raises(RuntimeError, match="Unhandled network event name"): - await client.write_event(Event("jerald event", bytearray())) - client.register_network_write_events({}) - - -@pytest.mark.trio -async def test_register_network_read_event() -> None: - one, two = trio.testing.memory_stream_pair() - client_one = NetworkEventComponent.from_stream("one", stream=one) - client_two = NetworkEventComponent.from_stream("two", stream=two) - client_one.register_network_write_event("echo_event", 0) - await client_one.write_event( - Event( - "echo_event", - bytearray("I will give my cat food to bob", "utf-8"), - ), - ) - with pytest.raises(RuntimeError, match="Unhandled packet ID 0"): - await client_two.read_event() - with pytest.raises(ValueError, match="Packet id 0 packets are also"): - client_one.register_read_network_event(0, "echo_event") - client_two.register_read_network_event(0, "reposted_event") - with pytest.raises(ValueError, match="Packet ID 0 already registered!"): - client_two.register_read_network_events({0: "type_two"}) - client_two.register_read_network_events({}) - - -@pytest.mark.trio -async def test_event_transmission() -> None: - one, two = trio.testing.memory_stream_pair() - client_one = NetworkEventComponent.from_stream("one", stream=one) - manager = ComponentManager("manager") - async with NetworkEventComponent.from_stream( - "two", - stream=two, - ) as client_two: - manager.add_component(client_one) - - assert not client_one.not_connected - - client_one.register_network_write_event("echo_event", 0) - client_two.register_read_network_event(0, "reposted_event") - - event = Event( - "echo_event", - bytearray("I will give my cat food to bob", "utf-8"), - 3, - ) - - await client_one.write_event(event) - read_event = await client_two.read_event() - assert read_event.name == "reposted_event" - assert read_event.data == event.data - - await client_one.write_event(event) - assert ( - await two.receive_some() - == b"\x00\x1eI will give my cat food to bob" - ) - - await client_one.wait_write_might_not_block() - await one.send_all(b"") - client_two.timeout = 0.05 - with pytest.raises(NetworkTimeoutError): - await client_two.read_event() - await one.send_all(b"cat") - with pytest.raises(OSError, match="Server stopped responding"): - await client_two.read(4) - - await client_one.send_eof() - await client_one.send_eof() - - await client_one.close() - await client_one.close() - - -def test_server() -> None: - server = Server("name") - server.stop_serving() - server.serve_cancel_scope = trio.CancelScope() - server.stop_serving() diff --git a/tests/test_objects.py b/tests/test_objects.py new file mode 100644 index 0000000..12e2b56 --- /dev/null +++ b/tests/test_objects.py @@ -0,0 +1,81 @@ +from __future__ import annotations + +import pytest +from pygame.surface import Surface + +from azul.objects import Button, OutlinedText, Text + + +class MockSurface(Surface): + """Mocking a pygame surface for testing.""" + + __slots__ = ("text_data",) + + def __init__(self, text_data: str = "") -> None: + super().__init__((0, 0)) + self.text_data = text_data + + +class MockFont: + """Mocking a pygame font for testing.""" + + __slots__ = () + + def render( + self, + text: str, + antialias: bool, + color: tuple[int, int, int], + ) -> str: + """Fake render method.""" + return MockSurface(text) + + +@pytest.fixture +def font() -> MockFont: + return MockFont() + + +def test_text_initialization(font: MockFont) -> None: + text = Text("TestText", font) + assert text.text == "None" + assert text.color == (255, 255, 255) + assert text.font == font + + +def test_text_rendering(font: MockFont) -> None: + text = Text("TestText", font) + assert text.image is None + + +def test_text_rendering_blank(font: MockFont) -> None: + text = Text("TestText", font) + text.text = "" + text.text = "" + assert text.image.text_data == "" + + +def test_outlined_text_initialization(font: MockFont) -> None: + outlined_text = OutlinedText("TestOutlinedText", font) + assert outlined_text.outline == (0, 0, 0) + assert outlined_text.inside == (255, 255, 255) + + +def test_outlined_text_rendering(font: MockFont) -> None: + outlined_text = OutlinedText("TestOutlinedText", font) + outlined_text.text = "Outlined Text" + assert outlined_text.text == "Outlined Text" + + +def test_outlined_text_rendering_zero_border(font: MockFont) -> None: + outlined_text = OutlinedText("TestOutlinedText", font) + outlined_text.border_width = 0 + outlined_text.text = "Outlined Text" + assert isinstance(outlined_text.image, Surface) + + +def test_button_initialization(font: MockFont) -> None: + button = Button("TestButton", font) + assert button.text == "None" + assert button.color == (0, 0, 0, 255) + assert button.border_width == 3 diff --git a/tests/test_sprite.py b/tests/test_sprite.py new file mode 100644 index 0000000..1344fc2 --- /dev/null +++ b/tests/test_sprite.py @@ -0,0 +1,327 @@ +from __future__ import annotations + +from typing import cast + +import pytest +import trio +from libcomponent.component import Event +from pygame.mask import Mask +from pygame.rect import Rect +from pygame.surface import Surface + +from azul.sprite import ( + AnimationComponent, + DragClickEventComponent, + GroupProcessor, + ImageComponent, + MovementComponent, + OutlineComponent, + Sprite, + TargetingComponent, + TickEventData, +) +from azul.vector import Vector2 + + +@pytest.fixture +def sprite() -> Sprite: + return Sprite("test_sprite") + + +@pytest.fixture +def image_component(sprite: Sprite) -> ImageComponent: + sprite.add_component(ImageComponent()) + return sprite.get_component("image") + + +@pytest.fixture +def animation_component(image_component: ImageComponent) -> AnimationComponent: + image_component.add_component(AnimationComponent()) + return image_component.get_component("animation") + + +@pytest.fixture +def outline_component(image_component: ImageComponent) -> OutlineComponent: + return image_component.get_component("outline") + + +@pytest.fixture +def movement_component(sprite: Sprite) -> MovementComponent: + sprite.add_component(MovementComponent()) + return sprite.get_component("movement") + + +@pytest.fixture +def targeting_component( + movement_component: MovementComponent, +) -> TargetingComponent: + sprite = movement_component.manager + sprite.add_component(TargetingComponent()) + return sprite.get_component("targeting") + + +@pytest.fixture +def drag_click_event_component() -> DragClickEventComponent: + return DragClickEventComponent() + + +@pytest.fixture +def group_processor() -> GroupProcessor: + return GroupProcessor() + + +def test_sprite_init(sprite: Sprite) -> None: + assert sprite.name == "test_sprite" + assert not sprite.visible + assert sprite.rect == Rect(0, 0, 0, 0) + + +def test_sprite_location(sprite: Sprite) -> None: + sprite.location = (10, 20) + assert sprite.rect.center == (10, 20) + + +def test_sprite_repr(sprite: Sprite) -> None: + assert repr(sprite) == "" + + +def test_sprite_image(sprite: Sprite) -> None: + sprite.dirty = 0 + assert sprite.image is None + assert not sprite.dirty + sprite.image = Surface((10, 10)) + assert isinstance(sprite.image, Surface) + assert sprite.dirty + assert sprite.rect.size == (10, 10) + + +def test_sprite_image_set_none(sprite: Sprite) -> None: + sprite.dirty = 0 + assert sprite.image is None + assert not sprite.dirty + sprite.image = None + assert sprite.dirty + + +def test_sprite_image_no_set_location_change(sprite: Sprite) -> None: + sprite.update_location_on_resize = False + sprite.location = (100, 100) + sprite.image = Surface((50, 25)) + assert sprite.location == (125, 112) + + +def test_sprite_image_set_location_change(sprite: Sprite) -> None: + sprite.update_location_on_resize = True + sprite.location = (100, 100) + sprite.image = Surface((50, 25)) + assert sprite.location == (100, 100) + + +def test_sprite_selected_invisible(sprite: Sprite) -> None: + assert not sprite.visible + sprite.rect.size = (100, 100) + assert not sprite.is_selected((20, 20)) + + +def test_sprite_selected(sprite: Sprite) -> None: + sprite.visible = True + sprite.rect.size = (100, 100) + assert sprite.is_selected((20, 20)) + + +def test_image_component_init(image_component: ImageComponent) -> None: + assert image_component.mask_threshold == 127 + + +def test_image_component_add_image(image_component: ImageComponent) -> None: + image = Surface((10, 10)) + image_component.add_image("test_image", image) + assert "test_image" in image_component.list_images() + + +def test_image_component_add_images(image_component: ImageComponent) -> None: + image = Surface((10, 10)) + image_component.add_images({"test_image": image}) + assert "test_image" in image_component.list_images() + + +def test_image_component_get_image_fail( + image_component: ImageComponent, +) -> None: + with pytest.raises( + ValueError, + match='^No image saved for identifier "test_image"$', + ): + image_component.get_image("test_image") + + +def test_image_component_get_mask_fail( + image_component: ImageComponent, +) -> None: + with pytest.raises( + ValueError, + match='^No mask saved for identifier "test_mask"$', + ): + image_component.get_mask("test_mask") + + +def test_image_component_get_mask_success( + image_component: ImageComponent, +) -> None: + image = Surface((10, 10)) + image_component.add_image("test_image", image) + assert isinstance(image_component.get_mask("test_image"), Mask) + + +def test_image_component_add_image_and_mask_invalid_image( + image_component: ImageComponent, +) -> None: + with pytest.raises( + ValueError, + match="^Expected surface to be a valid identifier$", + ): + image_component.add_image_and_mask("test_image", None, None) # type: ignore[arg-type] + with pytest.raises( + ValueError, + match="^Expected surface to be a valid identifier$", + ): + image_component.add_image_and_mask("test_image", "copy_from", None) # type: ignore[arg-type] + + +def test_image_component_add_image_and_mask_invalid_mask( + image_component: ImageComponent, +) -> None: + image = Surface((1, 1)) + with pytest.raises( + ValueError, + match="^Expected mask to be a valid identifier$", + ): + image_component.add_image_and_mask("test_image", image, None) # type: ignore[arg-type] + with pytest.raises( + ValueError, + match="^Expected mask to be a valid identifier$", + ): + image_component.add_image_and_mask("test_image", image, "copy_from") # type: ignore[arg-type] + + +def test_image_component_get_image(image_component: ImageComponent) -> None: + image = Surface((1, 1)) + image_component.add_image("test_image", image) + assert image_component.get_image("test_image") is image + + +def test_image_component_add_image_duplication( + image_component: ImageComponent, +) -> None: + image = Surface((1, 1)) + image_component.add_image("test_image", image) + image_component.add_image("duplicate", "test_image") + assert image_component.get_image("duplicate") is image + + +def test_image_component_get_duplicate_mask( + image_component: ImageComponent, +) -> None: + image = Surface((1, 1)) + image_component.add_image("test_image", image) + image_component.add_image("duplicate", "test_image") + assert isinstance(image_component.get_mask("duplicate"), Mask) + + +def test_image_component_set_image_affects_sprite( + image_component: ImageComponent, +) -> None: + image = Surface((1, 1)) + sprite = cast("Sprite", image_component.manager.get_component("sprite")) + image_component.add_image("test_image", image) + assert sprite.image is None + image_component.set_image("test_image") + assert sprite.image is image + image_component.set_image("test_image") + assert sprite.image is image + + +def test_movement_component_init( + movement_component: MovementComponent, +) -> None: + assert movement_component.heading == Vector2(0, 0) + assert movement_component.speed == 0 + + +def test_movement_component_point_toward( + movement_component: MovementComponent, +) -> None: + movement_component.point_toward((10, 20)) + assert ( + movement_component.heading + == Vector2.from_points((0, 0), (10, 20)).normalized() + ) + + +def test_movement_component_move_heading_time( + movement_component: MovementComponent, +) -> None: + movement_component.speed = 5 + movement_component.move_heading_time(1) + assert movement_component.heading * 5 == movement_component.heading + + +def test_targeting_component_init( + targeting_component: TargetingComponent, +) -> None: + assert targeting_component.destination == Vector2(0, 0) + assert targeting_component.event_raise_name == "reached_destination" + + +def test_targeting_component_update_heading( + targeting_component: TargetingComponent, +) -> None: + targeting_component.destination = Vector2(10, 20) + targeting_component.update_heading() + assert targeting_component.to_destination() == Vector2.from_points( + (0, 0), + (10, 20), + ) + + +@pytest.mark.trio +async def test_targeting_component_move_destination_time( + targeting_component: TargetingComponent, +) -> None: + movement_component = targeting_component.get_component("movement") + movement_component.speed = 1 + targeting_component.destination = Vector2(10, 20) + current_distance = targeting_component.to_destination().magnitude() + await targeting_component.move_destination_time(1) + assert targeting_component.to_destination().magnitude() < current_distance + + +def test_drag_click_event_component_init( + drag_click_event_component: DragClickEventComponent, +) -> None: + assert drag_click_event_component.pressed == {} + + +def test_group_processor_init(group_processor: GroupProcessor) -> None: + assert group_processor.groups == {} + assert group_processor.group_names == {} + assert group_processor.new_gid == 0 + + +def test_group_processor_new_group(group_processor: GroupProcessor) -> None: + gid = group_processor.new_group("test_group") + assert gid in group_processor.groups + assert "test_group" in group_processor.group_names + + +@pytest.mark.trio +async def test_animation_component_tick( + animation_component: AnimationComponent, +) -> None: + async with trio.open_nursery() as nursery: + nursery.start_soon( + animation_component.tick, + Event("tick", TickEventData(time_passed=1, fps=60)), + ) + await trio.lowlevel.checkpoint() + # Assert that the animation component has updated correctly diff --git a/tests/test_utils.py b/tests/test_utils.py deleted file mode 100644 index 37b40b4..0000000 --- a/tests/test_utils.py +++ /dev/null @@ -1,121 +0,0 @@ -# This is the base_io module from https://github.com/py-mine/mcproto v0.5.0, -# which is licensed under the GNU LESSER GENERAL PUBLIC LICENSE v3.0 - -from __future__ import annotations - -__author__ = "ItsDrike" -__license__ = "LGPL-3.0-only" - -import pytest - -from azul.utils import from_twos_complement, to_twos_complement - -# TODO: Consider adding tests for enforce_range - - -@pytest.mark.parametrize( - ("number", "bits", "expected_out"), - [ - (0, 8, 0), - (1, 8, 1), - (10, 8, 10), - (127, 8, 127), - ], -) -def test_to_twos_complement_positive( - number: int, - bits: int, - expected_out: int, -): - """Test conversion to two's complement format from positive numbers gives expected result.""" - assert to_twos_complement(number, bits) == expected_out - - -@pytest.mark.parametrize( - ("number", "bits", "expected_out"), - [ - (-1, 8, 255), - (-10, 8, 246), - (-128, 8, 128), - ], -) -def test_to_twos_complement_negative( - number: int, - bits: int, - expected_out: int, -): - """Test conversion to two's complement format of negative numbers gives expected result.""" - assert to_twos_complement(number, bits) == expected_out - - -@pytest.mark.parametrize( - ("number", "bits"), - [ - (128, 8), - (-129, 8), - (32768, 16), - (-32769, 16), - (2147483648, 32), - (-2147483649, 32), - (9223372036854775808, 64), - (-9223372036854775809, 64), - ], -) -def test_to_twos_complement_range(number: int, bits: int): - """Test conversion to two's complement format for out of range numbers raises :exc:`ValueError`.""" - with pytest.raises(ValueError, match="out of range"): - to_twos_complement(number, bits) - - -@pytest.mark.parametrize( - ("number", "bits", "expected_out"), - [ - (0, 8, 0), - (1, 8, 1), - (10, 8, 10), - (127, 8, 127), - ], -) -def test_from_twos_complement_positive( - number: int, - bits: int, - expected_out: int, -): - """Test conversion from two's complement format of positive numbers give expected result.""" - assert from_twos_complement(number, bits) == expected_out - - -@pytest.mark.parametrize( - ("number", "bits", "expected_out"), - [ - (255, 8, -1), - (246, 8, -10), - (128, 8, -128), - ], -) -def test_from_twos_complement_negative( - number: int, - bits: int, - expected_out: int, -): - """Test conversion from two's complement format of negative numbers give expected result.""" - assert from_twos_complement(number, bits) == expected_out - - -@pytest.mark.parametrize( - ("number", "bits"), - [ - (256, 8), - (-1, 8), - (65536, 16), - (-1, 16), - (4294967296, 32), - (-1, 32), - (18446744073709551616, 64), - (-1, 64), - ], -) -def test_from_twos_complement_range(number: int, bits: int): - """Test conversion from two's complement format for out of range numbers raises :exc:`ValueError`.""" - with pytest.raises(ValueError, match="out of range"): - from_twos_complement(number, bits) diff --git a/tools/project_requirements.py b/tools/project_requirements.py deleted file mode 100755 index edcfbad..0000000 --- a/tools/project_requirements.py +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/env python3 - -"""Project Requirements - Write test-requirements.in based on pyproject.toml.""" - -# Programmed by CoolCat467 - -from __future__ import annotations - -# Project Requirements - Write test-requirements.in based on pyproject.toml. -# Copyright (C) 2024 CoolCat467 -# -# This program 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. -# -# This program 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. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -__title__ = "Project Requirements" -__author__ = "CoolCat467" -__version__ = "0.0.0" -__license__ = "GNU General Public License Version 3" - -import sys -from pathlib import Path -from typing import Final - -import tomllib - -# Key to start replacing inside of contents -KEY: Final = "TOML_DEPENDENCIES" - - -def run() -> None: - """Run program.""" - # Find root folder - this = Path(__file__).absolute() - tools = this.parent - root = tools.parent - # Make sure it's right - assert (root / "LICENSE").exists(), "Not in correct directory!" - - # Read pyproject.toml - pyproject = root / "pyproject.toml" - with pyproject.open("rb") as fp: - data = tomllib.load(fp) - - # Get dependencies list - assert isinstance(data, dict) - project = data["project"] - assert isinstance(project, dict) - dependencies = project["dependencies"] - assert isinstance(dependencies, list) - - # Read requirements file - requirements_list = root / "test-requirements.in" - assert requirements_list.exists(), f"{requirements_list} does not exist!" - requirements_data = requirements_list.read_text("utf-8") - - # Find out what start and end should be based on key. - key_start = f"<{KEY}>" - key_end = f"" - - # Try to find start and end triggers in requirements data - start_char = requirements_data.find(key_start) - end_char = requirements_data.find(key_end) - if -1 in {start_char, end_char}: - raise ValueError( - f"{key_start!r} or {key_end!r} not found in {requirements_list}", - ) - - # Create overwrite text - dependencies_text = "\n".join(sorted(dependencies)) - overwrite_text = "\n".join( - ( - key_start, - dependencies_text, - f"#{key_end}", - ), - ) - # Create new file contents - end = end_char + len(key_end) - new_text = ( - requirements_data[:start_char] - + overwrite_text - + requirements_data[end:] - ) - - # If new text differs, overwrite and alert - if new_text != requirements_data: - print("Requirements file is outdated...") - requirements_list.write_text(new_text, "utf-8") - print("Requirements file updated successfully.") - return 1 - print("Requirements file is up to date.") - return 0 - - -if __name__ == "__main__": - sys.exit(run()) diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..caf9687 --- /dev/null +++ b/uv.lock @@ -0,0 +1,782 @@ +version = 1 +revision = 1 +requires-python = ">=3.10" + +[[package]] +name = "attrs" +version = "25.3.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/5a/b0/1367933a8532ee6ff8d63537de4f1177af4bff9f3e829baf7331f595bb24/attrs-25.3.0.tar.gz", hash = "sha256:75d7cefc7fb576747b2c81b4442d4d4a1ce0900973527c011d1030fd3bf4af1b", size = 812032 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/77/06/bb80f5f86020c4551da315d78b3ab75e8228f89f0162f2c3a819e407941a/attrs-25.3.0-py3-none-any.whl", hash = "sha256:427318ce031701fea540783410126f03899a97ffc6f61596ad581ac2e40e3bc3", size = 63815 }, +] + +[[package]] +name = "azul" +source = { editable = "." } +dependencies = [ + { name = "exceptiongroup", marker = "python_full_version < '3.11'" }, + { name = "libcomponent" }, + { name = "mypy-extensions" }, + { name = "numpy" }, + { name = "orjson" }, + { name = "pygame" }, + { name = "trio" }, +] + +[package.optional-dependencies] +tests = [ + { name = "coverage" }, + { name = "mypy" }, + { name = "pytest" }, + { name = "pytest-cov" }, + { name = "pytest-trio" }, + { name = "uv" }, +] +tools = [ + { name = "black", marker = "implementation_name == 'cpython'" }, + { name = "codespell" }, + { name = "ruff" }, +] + +[package.metadata] +requires-dist = [ + { name = "black", marker = "implementation_name == 'cpython' and extra == 'tools'", specifier = ">=24.10.0" }, + { name = "codespell", marker = "extra == 'tools'", specifier = ">=2.3.0" }, + { name = "coverage", marker = "extra == 'tests'", specifier = ">=7.2.5" }, + { name = "exceptiongroup", marker = "python_full_version < '3.11'", specifier = ">=1.2.2" }, + { name = "libcomponent", specifier = "~=0.0.4" }, + { name = "mypy", marker = "extra == 'tests'", specifier = ">=1.14.1" }, + { name = "mypy-extensions", specifier = ">=1.0.0" }, + { name = "numpy", specifier = "~=2.1.3" }, + { name = "orjson", specifier = ">=3.10,<4" }, + { name = "pygame", specifier = "~=2.6.0" }, + { name = "pytest", marker = "extra == 'tests'", specifier = ">=5.0" }, + { name = "pytest-cov", marker = "extra == 'tests'", specifier = ">=6.0.0" }, + { name = "pytest-trio", marker = "extra == 'tests'", specifier = ">=0.8.0" }, + { name = "ruff", marker = "extra == 'tools'", specifier = ">=0.9.2" }, + { name = "trio", specifier = "~=0.30.0" }, + { name = "uv", marker = "extra == 'tests'", specifier = ">=0.5.21" }, +] +provides-extras = ["tests", "tools"] + +[[package]] +name = "black" +version = "25.1.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "click" }, + { name = "mypy-extensions" }, + { name = "packaging" }, + { name = "pathspec" }, + { name = "platformdirs" }, + { name = "tomli", marker = "python_full_version < '3.11'" }, + { name = "typing-extensions", marker = "python_full_version < '3.11'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/94/49/26a7b0f3f35da4b5a65f081943b7bcd22d7002f5f0fb8098ec1ff21cb6ef/black-25.1.0.tar.gz", hash = "sha256:33496d5cd1222ad73391352b4ae8da15253c5de89b93a80b3e2c8d9a19ec2666", size = 649449 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4d/3b/4ba3f93ac8d90410423fdd31d7541ada9bcee1df32fb90d26de41ed40e1d/black-25.1.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:759e7ec1e050a15f89b770cefbf91ebee8917aac5c20483bc2d80a6c3a04df32", size = 1629419 }, + { url = "https://files.pythonhosted.org/packages/b4/02/0bde0485146a8a5e694daed47561785e8b77a0466ccc1f3e485d5ef2925e/black-25.1.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:0e519ecf93120f34243e6b0054db49c00a35f84f195d5bce7e9f5cfc578fc2da", size = 1461080 }, + { url = "https://files.pythonhosted.org/packages/52/0e/abdf75183c830eaca7589144ff96d49bce73d7ec6ad12ef62185cc0f79a2/black-25.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:055e59b198df7ac0b7efca5ad7ff2516bca343276c466be72eb04a3bcc1f82d7", size = 1766886 }, + { url = "https://files.pythonhosted.org/packages/dc/a6/97d8bb65b1d8a41f8a6736222ba0a334db7b7b77b8023ab4568288f23973/black-25.1.0-cp310-cp310-win_amd64.whl", hash = "sha256:db8ea9917d6f8fc62abd90d944920d95e73c83a5ee3383493e35d271aca872e9", size = 1419404 }, + { url = "https://files.pythonhosted.org/packages/7e/4f/87f596aca05c3ce5b94b8663dbfe242a12843caaa82dd3f85f1ffdc3f177/black-25.1.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:a39337598244de4bae26475f77dda852ea00a93bd4c728e09eacd827ec929df0", size = 1614372 }, + { url = "https://files.pythonhosted.org/packages/e7/d0/2c34c36190b741c59c901e56ab7f6e54dad8df05a6272a9747ecef7c6036/black-25.1.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:96c1c7cd856bba8e20094e36e0f948718dc688dba4a9d78c3adde52b9e6c2299", size = 1442865 }, + { url = "https://files.pythonhosted.org/packages/21/d4/7518c72262468430ead45cf22bd86c883a6448b9eb43672765d69a8f1248/black-25.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:bce2e264d59c91e52d8000d507eb20a9aca4a778731a08cfff7e5ac4a4bb7096", size = 1749699 }, + { url = "https://files.pythonhosted.org/packages/58/db/4f5beb989b547f79096e035c4981ceb36ac2b552d0ac5f2620e941501c99/black-25.1.0-cp311-cp311-win_amd64.whl", hash = "sha256:172b1dbff09f86ce6f4eb8edf9dede08b1fce58ba194c87d7a4f1a5aa2f5b3c2", size = 1428028 }, + { url = "https://files.pythonhosted.org/packages/83/71/3fe4741df7adf015ad8dfa082dd36c94ca86bb21f25608eb247b4afb15b2/black-25.1.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:4b60580e829091e6f9238c848ea6750efed72140b91b048770b64e74fe04908b", size = 1650988 }, + { url = "https://files.pythonhosted.org/packages/13/f3/89aac8a83d73937ccd39bbe8fc6ac8860c11cfa0af5b1c96d081facac844/black-25.1.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:1e2978f6df243b155ef5fa7e558a43037c3079093ed5d10fd84c43900f2d8ecc", size = 1453985 }, + { url = "https://files.pythonhosted.org/packages/6f/22/b99efca33f1f3a1d2552c714b1e1b5ae92efac6c43e790ad539a163d1754/black-25.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:3b48735872ec535027d979e8dcb20bf4f70b5ac75a8ea99f127c106a7d7aba9f", size = 1783816 }, + { url = "https://files.pythonhosted.org/packages/18/7e/a27c3ad3822b6f2e0e00d63d58ff6299a99a5b3aee69fa77cd4b0076b261/black-25.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:ea0213189960bda9cf99be5b8c8ce66bb054af5e9e861249cd23471bd7b0b3ba", size = 1440860 }, + { url = "https://files.pythonhosted.org/packages/98/87/0edf98916640efa5d0696e1abb0a8357b52e69e82322628f25bf14d263d1/black-25.1.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:8f0b18a02996a836cc9c9c78e5babec10930862827b1b724ddfe98ccf2f2fe4f", size = 1650673 }, + { url = "https://files.pythonhosted.org/packages/52/e5/f7bf17207cf87fa6e9b676576749c6b6ed0d70f179a3d812c997870291c3/black-25.1.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:afebb7098bfbc70037a053b91ae8437c3857482d3a690fefc03e9ff7aa9a5fd3", size = 1453190 }, + { url = "https://files.pythonhosted.org/packages/e3/ee/adda3d46d4a9120772fae6de454c8495603c37c4c3b9c60f25b1ab6401fe/black-25.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:030b9759066a4ee5e5aca28c3c77f9c64789cdd4de8ac1df642c40b708be6171", size = 1782926 }, + { url = "https://files.pythonhosted.org/packages/cc/64/94eb5f45dcb997d2082f097a3944cfc7fe87e071907f677e80788a2d7b7a/black-25.1.0-cp313-cp313-win_amd64.whl", hash = "sha256:a22f402b410566e2d1c950708c77ebf5ebd5d0d88a6a2e87c86d9fb48afa0d18", size = 1442613 }, + { url = "https://files.pythonhosted.org/packages/09/71/54e999902aed72baf26bca0d50781b01838251a462612966e9fc4891eadd/black-25.1.0-py3-none-any.whl", hash = "sha256:95e8176dae143ba9097f351d174fdaf0ccd29efb414b362ae3fd72bf0f710717", size = 207646 }, +] + +[[package]] +name = "cffi" +version = "1.17.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pycparser" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/fc/97/c783634659c2920c3fc70419e3af40972dbaf758daa229a7d6ea6135c90d/cffi-1.17.1.tar.gz", hash = "sha256:1c39c6016c32bc48dd54561950ebd6836e1670f2ae46128f67cf49e789c52824", size = 516621 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/90/07/f44ca684db4e4f08a3fdc6eeb9a0d15dc6883efc7b8c90357fdbf74e186c/cffi-1.17.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:df8b1c11f177bc2313ec4b2d46baec87a5f3e71fc8b45dab2ee7cae86d9aba14", size = 182191 }, + { url = "https://files.pythonhosted.org/packages/08/fd/cc2fedbd887223f9f5d170c96e57cbf655df9831a6546c1727ae13fa977a/cffi-1.17.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8f2cdc858323644ab277e9bb925ad72ae0e67f69e804f4898c070998d50b1a67", size = 178592 }, + { url = "https://files.pythonhosted.org/packages/de/cc/4635c320081c78d6ffc2cab0a76025b691a91204f4aa317d568ff9280a2d/cffi-1.17.1-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:edae79245293e15384b51f88b00613ba9f7198016a5948b5dddf4917d4d26382", size = 426024 }, + { url = "https://files.pythonhosted.org/packages/b6/7b/3b2b250f3aab91abe5f8a51ada1b717935fdaec53f790ad4100fe2ec64d1/cffi-1.17.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:45398b671ac6d70e67da8e4224a065cec6a93541bb7aebe1b198a61b58c7b702", size = 448188 }, + { url = "https://files.pythonhosted.org/packages/d3/48/1b9283ebbf0ec065148d8de05d647a986c5f22586b18120020452fff8f5d/cffi-1.17.1-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ad9413ccdeda48c5afdae7e4fa2192157e991ff761e7ab8fdd8926f40b160cc3", size = 455571 }, + { url = "https://files.pythonhosted.org/packages/40/87/3b8452525437b40f39ca7ff70276679772ee7e8b394934ff60e63b7b090c/cffi-1.17.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:5da5719280082ac6bd9aa7becb3938dc9f9cbd57fac7d2871717b1feb0902ab6", size = 436687 }, + { url = "https://files.pythonhosted.org/packages/8d/fb/4da72871d177d63649ac449aec2e8a29efe0274035880c7af59101ca2232/cffi-1.17.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2bb1a08b8008b281856e5971307cc386a8e9c5b625ac297e853d36da6efe9c17", size = 446211 }, + { url = "https://files.pythonhosted.org/packages/ab/a0/62f00bcb411332106c02b663b26f3545a9ef136f80d5df746c05878f8c4b/cffi-1.17.1-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:045d61c734659cc045141be4bae381a41d89b741f795af1dd018bfb532fd0df8", size = 461325 }, + { url = "https://files.pythonhosted.org/packages/36/83/76127035ed2e7e27b0787604d99da630ac3123bfb02d8e80c633f218a11d/cffi-1.17.1-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:6883e737d7d9e4899a8a695e00ec36bd4e5e4f18fabe0aca0efe0a4b44cdb13e", size = 438784 }, + { url = "https://files.pythonhosted.org/packages/21/81/a6cd025db2f08ac88b901b745c163d884641909641f9b826e8cb87645942/cffi-1.17.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:6b8b4a92e1c65048ff98cfe1f735ef8f1ceb72e3d5f0c25fdb12087a23da22be", size = 461564 }, + { url = "https://files.pythonhosted.org/packages/f8/fe/4d41c2f200c4a457933dbd98d3cf4e911870877bd94d9656cc0fcb390681/cffi-1.17.1-cp310-cp310-win32.whl", hash = "sha256:c9c3d058ebabb74db66e431095118094d06abf53284d9c81f27300d0e0d8bc7c", size = 171804 }, + { url = "https://files.pythonhosted.org/packages/d1/b6/0b0f5ab93b0df4acc49cae758c81fe4e5ef26c3ae2e10cc69249dfd8b3ab/cffi-1.17.1-cp310-cp310-win_amd64.whl", hash = "sha256:0f048dcf80db46f0098ccac01132761580d28e28bc0f78ae0d58048063317e15", size = 181299 }, + { url = "https://files.pythonhosted.org/packages/6b/f4/927e3a8899e52a27fa57a48607ff7dc91a9ebe97399b357b85a0c7892e00/cffi-1.17.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:a45e3c6913c5b87b3ff120dcdc03f6131fa0065027d0ed7ee6190736a74cd401", size = 182264 }, + { url = "https://files.pythonhosted.org/packages/6c/f5/6c3a8efe5f503175aaddcbea6ad0d2c96dad6f5abb205750d1b3df44ef29/cffi-1.17.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:30c5e0cb5ae493c04c8b42916e52ca38079f1b235c2f8ae5f4527b963c401caf", size = 178651 }, + { url = "https://files.pythonhosted.org/packages/94/dd/a3f0118e688d1b1a57553da23b16bdade96d2f9bcda4d32e7d2838047ff7/cffi-1.17.1-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f75c7ab1f9e4aca5414ed4d8e5c0e303a34f4421f8a0d47a4d019ceff0ab6af4", size = 445259 }, + { url = "https://files.pythonhosted.org/packages/2e/ea/70ce63780f096e16ce8588efe039d3c4f91deb1dc01e9c73a287939c79a6/cffi-1.17.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a1ed2dd2972641495a3ec98445e09766f077aee98a1c896dcb4ad0d303628e41", size = 469200 }, + { url = "https://files.pythonhosted.org/packages/1c/a0/a4fa9f4f781bda074c3ddd57a572b060fa0df7655d2a4247bbe277200146/cffi-1.17.1-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:46bf43160c1a35f7ec506d254e5c890f3c03648a4dbac12d624e4490a7046cd1", size = 477235 }, + { url = "https://files.pythonhosted.org/packages/62/12/ce8710b5b8affbcdd5c6e367217c242524ad17a02fe5beec3ee339f69f85/cffi-1.17.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:a24ed04c8ffd54b0729c07cee15a81d964e6fee0e3d4d342a27b020d22959dc6", size = 459721 }, + { url = "https://files.pythonhosted.org/packages/ff/6b/d45873c5e0242196f042d555526f92aa9e0c32355a1be1ff8c27f077fd37/cffi-1.17.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:610faea79c43e44c71e1ec53a554553fa22321b65fae24889706c0a84d4ad86d", size = 467242 }, + { url = "https://files.pythonhosted.org/packages/1a/52/d9a0e523a572fbccf2955f5abe883cfa8bcc570d7faeee06336fbd50c9fc/cffi-1.17.1-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:a9b15d491f3ad5d692e11f6b71f7857e7835eb677955c00cc0aefcd0669adaf6", size = 477999 }, + { url = "https://files.pythonhosted.org/packages/44/74/f2a2460684a1a2d00ca799ad880d54652841a780c4c97b87754f660c7603/cffi-1.17.1-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:de2ea4b5833625383e464549fec1bc395c1bdeeb5f25c4a3a82b5a8c756ec22f", size = 454242 }, + { url = "https://files.pythonhosted.org/packages/f8/4a/34599cac7dfcd888ff54e801afe06a19c17787dfd94495ab0c8d35fe99fb/cffi-1.17.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:fc48c783f9c87e60831201f2cce7f3b2e4846bf4d8728eabe54d60700b318a0b", size = 478604 }, + { url = "https://files.pythonhosted.org/packages/34/33/e1b8a1ba29025adbdcda5fb3a36f94c03d771c1b7b12f726ff7fef2ebe36/cffi-1.17.1-cp311-cp311-win32.whl", hash = "sha256:85a950a4ac9c359340d5963966e3e0a94a676bd6245a4b55bc43949eee26a655", size = 171727 }, + { url = "https://files.pythonhosted.org/packages/3d/97/50228be003bb2802627d28ec0627837ac0bf35c90cf769812056f235b2d1/cffi-1.17.1-cp311-cp311-win_amd64.whl", hash = "sha256:caaf0640ef5f5517f49bc275eca1406b0ffa6aa184892812030f04c2abf589a0", size = 181400 }, + { url = "https://files.pythonhosted.org/packages/5a/84/e94227139ee5fb4d600a7a4927f322e1d4aea6fdc50bd3fca8493caba23f/cffi-1.17.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:805b4371bf7197c329fcb3ead37e710d1bca9da5d583f5073b799d5c5bd1eee4", size = 183178 }, + { url = "https://files.pythonhosted.org/packages/da/ee/fb72c2b48656111c4ef27f0f91da355e130a923473bf5ee75c5643d00cca/cffi-1.17.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:733e99bc2df47476e3848417c5a4540522f234dfd4ef3ab7fafdf555b082ec0c", size = 178840 }, + { url = "https://files.pythonhosted.org/packages/cc/b6/db007700f67d151abadf508cbfd6a1884f57eab90b1bb985c4c8c02b0f28/cffi-1.17.1-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1257bdabf294dceb59f5e70c64a3e2f462c30c7ad68092d01bbbfb1c16b1ba36", size = 454803 }, + { url = "https://files.pythonhosted.org/packages/1a/df/f8d151540d8c200eb1c6fba8cd0dfd40904f1b0682ea705c36e6c2e97ab3/cffi-1.17.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:da95af8214998d77a98cc14e3a3bd00aa191526343078b530ceb0bd710fb48a5", size = 478850 }, + { url = "https://files.pythonhosted.org/packages/28/c0/b31116332a547fd2677ae5b78a2ef662dfc8023d67f41b2a83f7c2aa78b1/cffi-1.17.1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:d63afe322132c194cf832bfec0dc69a99fb9bb6bbd550f161a49e9e855cc78ff", size = 485729 }, + { url = "https://files.pythonhosted.org/packages/91/2b/9a1ddfa5c7f13cab007a2c9cc295b70fbbda7cb10a286aa6810338e60ea1/cffi-1.17.1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:f79fc4fc25f1c8698ff97788206bb3c2598949bfe0fef03d299eb1b5356ada99", size = 471256 }, + { url = "https://files.pythonhosted.org/packages/b2/d5/da47df7004cb17e4955df6a43d14b3b4ae77737dff8bf7f8f333196717bf/cffi-1.17.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b62ce867176a75d03a665bad002af8e6d54644fad99a3c70905c543130e39d93", size = 479424 }, + { url = "https://files.pythonhosted.org/packages/0b/ac/2a28bcf513e93a219c8a4e8e125534f4f6db03e3179ba1c45e949b76212c/cffi-1.17.1-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:386c8bf53c502fff58903061338ce4f4950cbdcb23e2902d86c0f722b786bbe3", size = 484568 }, + { url = "https://files.pythonhosted.org/packages/d4/38/ca8a4f639065f14ae0f1d9751e70447a261f1a30fa7547a828ae08142465/cffi-1.17.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:4ceb10419a9adf4460ea14cfd6bc43d08701f0835e979bf821052f1805850fe8", size = 488736 }, + { url = "https://files.pythonhosted.org/packages/86/c5/28b2d6f799ec0bdecf44dced2ec5ed43e0eb63097b0f58c293583b406582/cffi-1.17.1-cp312-cp312-win32.whl", hash = "sha256:a08d7e755f8ed21095a310a693525137cfe756ce62d066e53f502a83dc550f65", size = 172448 }, + { url = "https://files.pythonhosted.org/packages/50/b9/db34c4755a7bd1cb2d1603ac3863f22bcecbd1ba29e5ee841a4bc510b294/cffi-1.17.1-cp312-cp312-win_amd64.whl", hash = "sha256:51392eae71afec0d0c8fb1a53b204dbb3bcabcb3c9b807eedf3e1e6ccf2de903", size = 181976 }, + { url = "https://files.pythonhosted.org/packages/8d/f8/dd6c246b148639254dad4d6803eb6a54e8c85c6e11ec9df2cffa87571dbe/cffi-1.17.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f3a2b4222ce6b60e2e8b337bb9596923045681d71e5a082783484d845390938e", size = 182989 }, + { url = "https://files.pythonhosted.org/packages/8b/f1/672d303ddf17c24fc83afd712316fda78dc6fce1cd53011b839483e1ecc8/cffi-1.17.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:0984a4925a435b1da406122d4d7968dd861c1385afe3b45ba82b750f229811e2", size = 178802 }, + { url = "https://files.pythonhosted.org/packages/0e/2d/eab2e858a91fdff70533cab61dcff4a1f55ec60425832ddfdc9cd36bc8af/cffi-1.17.1-cp313-cp313-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d01b12eeeb4427d3110de311e1774046ad344f5b1a7403101878976ecd7a10f3", size = 454792 }, + { url = "https://files.pythonhosted.org/packages/75/b2/fbaec7c4455c604e29388d55599b99ebcc250a60050610fadde58932b7ee/cffi-1.17.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:706510fe141c86a69c8ddc029c7910003a17353970cff3b904ff0686a5927683", size = 478893 }, + { url = "https://files.pythonhosted.org/packages/4f/b7/6e4a2162178bf1935c336d4da8a9352cccab4d3a5d7914065490f08c0690/cffi-1.17.1-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:de55b766c7aa2e2a3092c51e0483d700341182f08e67c63630d5b6f200bb28e5", size = 485810 }, + { url = "https://files.pythonhosted.org/packages/c7/8a/1d0e4a9c26e54746dc08c2c6c037889124d4f59dffd853a659fa545f1b40/cffi-1.17.1-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c59d6e989d07460165cc5ad3c61f9fd8f1b4796eacbd81cee78957842b834af4", size = 471200 }, + { url = "https://files.pythonhosted.org/packages/26/9f/1aab65a6c0db35f43c4d1b4f580e8df53914310afc10ae0397d29d697af4/cffi-1.17.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dd398dbc6773384a17fe0d3e7eeb8d1a21c2200473ee6806bb5e6a8e62bb73dd", size = 479447 }, + { url = "https://files.pythonhosted.org/packages/5f/e4/fb8b3dd8dc0e98edf1135ff067ae070bb32ef9d509d6cb0f538cd6f7483f/cffi-1.17.1-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:3edc8d958eb099c634dace3c7e16560ae474aa3803a5df240542b305d14e14ed", size = 484358 }, + { url = "https://files.pythonhosted.org/packages/f1/47/d7145bf2dc04684935d57d67dff9d6d795b2ba2796806bb109864be3a151/cffi-1.17.1-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:72e72408cad3d5419375fc87d289076ee319835bdfa2caad331e377589aebba9", size = 488469 }, + { url = "https://files.pythonhosted.org/packages/bf/ee/f94057fa6426481d663b88637a9a10e859e492c73d0384514a17d78ee205/cffi-1.17.1-cp313-cp313-win32.whl", hash = "sha256:e03eab0a8677fa80d646b5ddece1cbeaf556c313dcfac435ba11f107ba117b5d", size = 172475 }, + { url = "https://files.pythonhosted.org/packages/7c/fc/6a8cb64e5f0324877d503c854da15d76c1e50eb722e320b15345c4d0c6de/cffi-1.17.1-cp313-cp313-win_amd64.whl", hash = "sha256:f6a16c31041f09ead72d69f583767292f750d24913dadacf5756b966aacb3f1a", size = 182009 }, +] + +[[package]] +name = "click" +version = "8.1.8" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "colorama", marker = "sys_platform == 'win32'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b9/2e/0090cbf739cee7d23781ad4b89a9894a41538e4fcf4c31dcdd705b78eb8b/click-8.1.8.tar.gz", hash = "sha256:ed53c9d8990d83c2a27deae68e4ee337473f6330c040a31d4225c9574d16096a", size = 226593 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7e/d4/7ebdbd03970677812aac39c869717059dbb71a4cfc033ca6e5221787892c/click-8.1.8-py3-none-any.whl", hash = "sha256:63c132bbbed01578a06712a2d1f497bb62d9c1c0d329b7903a866228027263b2", size = 98188 }, +] + +[[package]] +name = "codespell" +version = "2.4.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/15/e0/709453393c0ea77d007d907dd436b3ee262e28b30995ea1aa36c6ffbccaf/codespell-2.4.1.tar.gz", hash = "sha256:299fcdcb09d23e81e35a671bbe746d5ad7e8385972e65dbb833a2eaac33c01e5", size = 344740 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/20/01/b394922252051e97aab231d416c86da3d8a6d781eeadcdca1082867de64e/codespell-2.4.1-py3-none-any.whl", hash = "sha256:3dadafa67df7e4a3dbf51e0d7315061b80d265f9552ebd699b3dd6834b47e425", size = 344501 }, +] + +[[package]] +name = "colorama" +version = "0.4.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335 }, +] + +[[package]] +name = "coverage" +version = "7.8.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/19/4f/2251e65033ed2ce1e68f00f91a0294e0f80c80ae8c3ebbe2f12828c4cd53/coverage-7.8.0.tar.gz", hash = "sha256:7a3d62b3b03b4b6fd41a085f3574874cf946cb4604d2b4d3e8dca8cd570ca501", size = 811872 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/78/01/1c5e6ee4ebaaa5e079db933a9a45f61172048c7efa06648445821a201084/coverage-7.8.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:2931f66991175369859b5fd58529cd4b73582461877ecfd859b6549869287ffe", size = 211379 }, + { url = "https://files.pythonhosted.org/packages/e9/16/a463389f5ff916963471f7c13585e5f38c6814607306b3cb4d6b4cf13384/coverage-7.8.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:52a523153c568d2c0ef8826f6cc23031dc86cffb8c6aeab92c4ff776e7951b28", size = 211814 }, + { url = "https://files.pythonhosted.org/packages/b8/b1/77062b0393f54d79064dfb72d2da402657d7c569cfbc724d56ac0f9c67ed/coverage-7.8.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5c8a5c139aae4c35cbd7cadca1df02ea8cf28a911534fc1b0456acb0b14234f3", size = 240937 }, + { url = "https://files.pythonhosted.org/packages/d7/54/c7b00a23150083c124e908c352db03bcd33375494a4beb0c6d79b35448b9/coverage-7.8.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:5a26c0c795c3e0b63ec7da6efded5f0bc856d7c0b24b2ac84b4d1d7bc578d676", size = 238849 }, + { url = "https://files.pythonhosted.org/packages/f7/ec/a6b7cfebd34e7b49f844788fda94713035372b5200c23088e3bbafb30970/coverage-7.8.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:821f7bcbaa84318287115d54becb1915eece6918136c6f91045bb84e2f88739d", size = 239986 }, + { url = "https://files.pythonhosted.org/packages/21/8c/c965ecef8af54e6d9b11bfbba85d4f6a319399f5f724798498387f3209eb/coverage-7.8.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:a321c61477ff8ee705b8a5fed370b5710c56b3a52d17b983d9215861e37b642a", size = 239896 }, + { url = "https://files.pythonhosted.org/packages/40/83/070550273fb4c480efa8381735969cb403fa8fd1626d74865bfaf9e4d903/coverage-7.8.0-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:ed2144b8a78f9d94d9515963ed273d620e07846acd5d4b0a642d4849e8d91a0c", size = 238613 }, + { url = "https://files.pythonhosted.org/packages/07/76/fbb2540495b01d996d38e9f8897b861afed356be01160ab4e25471f4fed1/coverage-7.8.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:042e7841a26498fff7a37d6fda770d17519982f5b7d8bf5278d140b67b61095f", size = 238909 }, + { url = "https://files.pythonhosted.org/packages/a3/7e/76d604db640b7d4a86e5dd730b73e96e12a8185f22b5d0799025121f4dcb/coverage-7.8.0-cp310-cp310-win32.whl", hash = "sha256:f9983d01d7705b2d1f7a95e10bbe4091fabc03a46881a256c2787637b087003f", size = 213948 }, + { url = "https://files.pythonhosted.org/packages/5c/a7/f8ce4aafb4a12ab475b56c76a71a40f427740cf496c14e943ade72e25023/coverage-7.8.0-cp310-cp310-win_amd64.whl", hash = "sha256:5a570cd9bd20b85d1a0d7b009aaf6c110b52b5755c17be6962f8ccd65d1dbd23", size = 214844 }, + { url = "https://files.pythonhosted.org/packages/2b/77/074d201adb8383addae5784cb8e2dac60bb62bfdf28b2b10f3a3af2fda47/coverage-7.8.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:e7ac22a0bb2c7c49f441f7a6d46c9c80d96e56f5a8bc6972529ed43c8b694e27", size = 211493 }, + { url = "https://files.pythonhosted.org/packages/a9/89/7a8efe585750fe59b48d09f871f0e0c028a7b10722b2172dfe021fa2fdd4/coverage-7.8.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:bf13d564d310c156d1c8e53877baf2993fb3073b2fc9f69790ca6a732eb4bfea", size = 211921 }, + { url = "https://files.pythonhosted.org/packages/e9/ef/96a90c31d08a3f40c49dbe897df4f1fd51fb6583821a1a1c5ee30cc8f680/coverage-7.8.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a5761c70c017c1b0d21b0815a920ffb94a670c8d5d409d9b38857874c21f70d7", size = 244556 }, + { url = "https://files.pythonhosted.org/packages/89/97/dcd5c2ce72cee9d7b0ee8c89162c24972fb987a111b92d1a3d1d19100c61/coverage-7.8.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e5ff52d790c7e1628241ffbcaeb33e07d14b007b6eb00a19320c7b8a7024c040", size = 242245 }, + { url = "https://files.pythonhosted.org/packages/b2/7b/b63cbb44096141ed435843bbb251558c8e05cc835c8da31ca6ffb26d44c0/coverage-7.8.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d39fc4817fd67b3915256af5dda75fd4ee10621a3d484524487e33416c6f3543", size = 244032 }, + { url = "https://files.pythonhosted.org/packages/97/e3/7fa8c2c00a1ef530c2a42fa5df25a6971391f92739d83d67a4ee6dcf7a02/coverage-7.8.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:b44674870709017e4b4036e3d0d6c17f06a0e6d4436422e0ad29b882c40697d2", size = 243679 }, + { url = "https://files.pythonhosted.org/packages/4f/b3/e0a59d8df9150c8a0c0841d55d6568f0a9195692136c44f3d21f1842c8f6/coverage-7.8.0-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:8f99eb72bf27cbb167b636eb1726f590c00e1ad375002230607a844d9e9a2318", size = 241852 }, + { url = "https://files.pythonhosted.org/packages/9b/82/db347ccd57bcef150c173df2ade97976a8367a3be7160e303e43dd0c795f/coverage-7.8.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:b571bf5341ba8c6bc02e0baeaf3b061ab993bf372d982ae509807e7f112554e9", size = 242389 }, + { url = "https://files.pythonhosted.org/packages/21/f6/3f7d7879ceb03923195d9ff294456241ed05815281f5254bc16ef71d6a20/coverage-7.8.0-cp311-cp311-win32.whl", hash = "sha256:e75a2ad7b647fd8046d58c3132d7eaf31b12d8a53c0e4b21fa9c4d23d6ee6d3c", size = 213997 }, + { url = "https://files.pythonhosted.org/packages/28/87/021189643e18ecf045dbe1e2071b2747901f229df302de01c998eeadf146/coverage-7.8.0-cp311-cp311-win_amd64.whl", hash = "sha256:3043ba1c88b2139126fc72cb48574b90e2e0546d4c78b5299317f61b7f718b78", size = 214911 }, + { url = "https://files.pythonhosted.org/packages/aa/12/4792669473297f7973518bec373a955e267deb4339286f882439b8535b39/coverage-7.8.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:bbb5cc845a0292e0c520656d19d7ce40e18d0e19b22cb3e0409135a575bf79fc", size = 211684 }, + { url = "https://files.pythonhosted.org/packages/be/e1/2a4ec273894000ebedd789e8f2fc3813fcaf486074f87fd1c5b2cb1c0a2b/coverage-7.8.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:4dfd9a93db9e78666d178d4f08a5408aa3f2474ad4d0e0378ed5f2ef71640cb6", size = 211935 }, + { url = "https://files.pythonhosted.org/packages/f8/3a/7b14f6e4372786709a361729164125f6b7caf4024ce02e596c4a69bccb89/coverage-7.8.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f017a61399f13aa6d1039f75cd467be388d157cd81f1a119b9d9a68ba6f2830d", size = 245994 }, + { url = "https://files.pythonhosted.org/packages/54/80/039cc7f1f81dcbd01ea796d36d3797e60c106077e31fd1f526b85337d6a1/coverage-7.8.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:0915742f4c82208ebf47a2b154a5334155ed9ef9fe6190674b8a46c2fb89cb05", size = 242885 }, + { url = "https://files.pythonhosted.org/packages/10/e0/dc8355f992b6cc2f9dcd5ef6242b62a3f73264893bc09fbb08bfcab18eb4/coverage-7.8.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8a40fcf208e021eb14b0fac6bdb045c0e0cab53105f93ba0d03fd934c956143a", size = 245142 }, + { url = "https://files.pythonhosted.org/packages/43/1b/33e313b22cf50f652becb94c6e7dae25d8f02e52e44db37a82de9ac357e8/coverage-7.8.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:a1f406a8e0995d654b2ad87c62caf6befa767885301f3b8f6f73e6f3c31ec3a6", size = 244906 }, + { url = "https://files.pythonhosted.org/packages/05/08/c0a8048e942e7f918764ccc99503e2bccffba1c42568693ce6955860365e/coverage-7.8.0-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:77af0f6447a582fdc7de5e06fa3757a3ef87769fbb0fdbdeba78c23049140a47", size = 243124 }, + { url = "https://files.pythonhosted.org/packages/5b/62/ea625b30623083c2aad645c9a6288ad9fc83d570f9adb913a2abdba562dd/coverage-7.8.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:f2d32f95922927186c6dbc8bc60df0d186b6edb828d299ab10898ef3f40052fe", size = 244317 }, + { url = "https://files.pythonhosted.org/packages/62/cb/3871f13ee1130a6c8f020e2f71d9ed269e1e2124aa3374d2180ee451cee9/coverage-7.8.0-cp312-cp312-win32.whl", hash = "sha256:769773614e676f9d8e8a0980dd7740f09a6ea386d0f383db6821df07d0f08545", size = 214170 }, + { url = "https://files.pythonhosted.org/packages/88/26/69fe1193ab0bfa1eb7a7c0149a066123611baba029ebb448500abd8143f9/coverage-7.8.0-cp312-cp312-win_amd64.whl", hash = "sha256:e5d2b9be5b0693cf21eb4ce0ec8d211efb43966f6657807f6859aab3814f946b", size = 214969 }, + { url = "https://files.pythonhosted.org/packages/f3/21/87e9b97b568e223f3438d93072479c2f36cc9b3f6b9f7094b9d50232acc0/coverage-7.8.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:5ac46d0c2dd5820ce93943a501ac5f6548ea81594777ca585bf002aa8854cacd", size = 211708 }, + { url = "https://files.pythonhosted.org/packages/75/be/882d08b28a0d19c9c4c2e8a1c6ebe1f79c9c839eb46d4fca3bd3b34562b9/coverage-7.8.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:771eb7587a0563ca5bb6f622b9ed7f9d07bd08900f7589b4febff05f469bea00", size = 211981 }, + { url = "https://files.pythonhosted.org/packages/7a/1d/ce99612ebd58082fbe3f8c66f6d8d5694976c76a0d474503fa70633ec77f/coverage-7.8.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42421e04069fb2cbcbca5a696c4050b84a43b05392679d4068acbe65449b5c64", size = 245495 }, + { url = "https://files.pythonhosted.org/packages/dc/8d/6115abe97df98db6b2bd76aae395fcc941d039a7acd25f741312ced9a78f/coverage-7.8.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:554fec1199d93ab30adaa751db68acec2b41c5602ac944bb19187cb9a41a8067", size = 242538 }, + { url = "https://files.pythonhosted.org/packages/cb/74/2f8cc196643b15bc096d60e073691dadb3dca48418f08bc78dd6e899383e/coverage-7.8.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5aaeb00761f985007b38cf463b1d160a14a22c34eb3f6a39d9ad6fc27cb73008", size = 244561 }, + { url = "https://files.pythonhosted.org/packages/22/70/c10c77cd77970ac965734fe3419f2c98665f6e982744a9bfb0e749d298f4/coverage-7.8.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:581a40c7b94921fffd6457ffe532259813fc68eb2bdda60fa8cc343414ce3733", size = 244633 }, + { url = "https://files.pythonhosted.org/packages/38/5a/4f7569d946a07c952688debee18c2bb9ab24f88027e3d71fd25dbc2f9dca/coverage-7.8.0-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:f319bae0321bc838e205bf9e5bc28f0a3165f30c203b610f17ab5552cff90323", size = 242712 }, + { url = "https://files.pythonhosted.org/packages/bb/a1/03a43b33f50475a632a91ea8c127f7e35e53786dbe6781c25f19fd5a65f8/coverage-7.8.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:04bfec25a8ef1c5f41f5e7e5c842f6b615599ca8ba8391ec33a9290d9d2db3a3", size = 244000 }, + { url = "https://files.pythonhosted.org/packages/6a/89/ab6c43b1788a3128e4d1b7b54214548dcad75a621f9d277b14d16a80d8a1/coverage-7.8.0-cp313-cp313-win32.whl", hash = "sha256:dd19608788b50eed889e13a5d71d832edc34fc9dfce606f66e8f9f917eef910d", size = 214195 }, + { url = "https://files.pythonhosted.org/packages/12/12/6bf5f9a8b063d116bac536a7fb594fc35cb04981654cccb4bbfea5dcdfa0/coverage-7.8.0-cp313-cp313-win_amd64.whl", hash = "sha256:a9abbccd778d98e9c7e85038e35e91e67f5b520776781d9a1e2ee9d400869487", size = 214998 }, + { url = "https://files.pythonhosted.org/packages/2a/e6/1e9df74ef7a1c983a9c7443dac8aac37a46f1939ae3499424622e72a6f78/coverage-7.8.0-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:18c5ae6d061ad5b3e7eef4363fb27a0576012a7447af48be6c75b88494c6cf25", size = 212541 }, + { url = "https://files.pythonhosted.org/packages/04/51/c32174edb7ee49744e2e81c4b1414ac9df3dacfcb5b5f273b7f285ad43f6/coverage-7.8.0-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:95aa6ae391a22bbbce1b77ddac846c98c5473de0372ba5c463480043a07bff42", size = 212767 }, + { url = "https://files.pythonhosted.org/packages/e9/8f/f454cbdb5212f13f29d4a7983db69169f1937e869a5142bce983ded52162/coverage-7.8.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e013b07ba1c748dacc2a80e69a46286ff145935f260eb8c72df7185bf048f502", size = 256997 }, + { url = "https://files.pythonhosted.org/packages/e6/74/2bf9e78b321216d6ee90a81e5c22f912fc428442c830c4077b4a071db66f/coverage-7.8.0-cp313-cp313t-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d766a4f0e5aa1ba056ec3496243150698dc0481902e2b8559314368717be82b1", size = 252708 }, + { url = "https://files.pythonhosted.org/packages/92/4d/50d7eb1e9a6062bee6e2f92e78b0998848a972e9afad349b6cdde6fa9e32/coverage-7.8.0-cp313-cp313t-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ad80e6b4a0c3cb6f10f29ae4c60e991f424e6b14219d46f1e7d442b938ee68a4", size = 255046 }, + { url = "https://files.pythonhosted.org/packages/40/9e/71fb4e7402a07c4198ab44fc564d09d7d0ffca46a9fb7b0a7b929e7641bd/coverage-7.8.0-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:b87eb6fc9e1bb8f98892a2458781348fa37e6925f35bb6ceb9d4afd54ba36c73", size = 256139 }, + { url = "https://files.pythonhosted.org/packages/49/1a/78d37f7a42b5beff027e807c2843185961fdae7fe23aad5a4837c93f9d25/coverage-7.8.0-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:d1ba00ae33be84066cfbe7361d4e04dec78445b2b88bdb734d0d1cbab916025a", size = 254307 }, + { url = "https://files.pythonhosted.org/packages/58/e9/8fb8e0ff6bef5e170ee19d59ca694f9001b2ec085dc99b4f65c128bb3f9a/coverage-7.8.0-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:f3c38e4e5ccbdc9198aecc766cedbb134b2d89bf64533973678dfcf07effd883", size = 255116 }, + { url = "https://files.pythonhosted.org/packages/56/b0/d968ecdbe6fe0a863de7169bbe9e8a476868959f3af24981f6a10d2b6924/coverage-7.8.0-cp313-cp313t-win32.whl", hash = "sha256:379fe315e206b14e21db5240f89dc0774bdd3e25c3c58c2c733c99eca96f1ada", size = 214909 }, + { url = "https://files.pythonhosted.org/packages/87/e9/d6b7ef9fecf42dfb418d93544af47c940aa83056c49e6021a564aafbc91f/coverage-7.8.0-cp313-cp313t-win_amd64.whl", hash = "sha256:2e4b6b87bb0c846a9315e3ab4be2d52fac905100565f4b92f02c445c8799e257", size = 216068 }, + { url = "https://files.pythonhosted.org/packages/c4/f1/1da77bb4c920aa30e82fa9b6ea065da3467977c2e5e032e38e66f1c57ffd/coverage-7.8.0-pp39.pp310.pp311-none-any.whl", hash = "sha256:b8194fb8e50d556d5849753de991d390c5a1edeeba50f68e3a9253fbd8bf8ccd", size = 203443 }, + { url = "https://files.pythonhosted.org/packages/59/f1/4da7717f0063a222db253e7121bd6a56f6fb1ba439dcc36659088793347c/coverage-7.8.0-py3-none-any.whl", hash = "sha256:dbf364b4c5e7bae9250528167dfe40219b62e2d573c854d74be213e1e52069f7", size = 203435 }, +] + +[package.optional-dependencies] +toml = [ + { name = "tomli", marker = "python_full_version <= '3.11'" }, +] + +[[package]] +name = "cryptography" +version = "44.0.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "cffi", marker = "platform_python_implementation != 'PyPy'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/53/d6/1411ab4d6108ab167d06254c5be517681f1e331f90edf1379895bcb87020/cryptography-44.0.3.tar.gz", hash = "sha256:fe19d8bc5536a91a24a8133328880a41831b6c5df54599a8417b62fe015d3053", size = 711096 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/08/53/c776d80e9d26441bb3868457909b4e74dd9ccabd182e10b2b0ae7a07e265/cryptography-44.0.3-cp37-abi3-macosx_10_9_universal2.whl", hash = "sha256:962bc30480a08d133e631e8dfd4783ab71cc9e33d5d7c1e192f0b7c06397bb88", size = 6670281 }, + { url = "https://files.pythonhosted.org/packages/6a/06/af2cf8d56ef87c77319e9086601bef621bedf40f6f59069e1b6d1ec498c5/cryptography-44.0.3-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4ffc61e8f3bf5b60346d89cd3d37231019c17a081208dfbbd6e1605ba03fa137", size = 3959305 }, + { url = "https://files.pythonhosted.org/packages/ae/01/80de3bec64627207d030f47bf3536889efee8913cd363e78ca9a09b13c8e/cryptography-44.0.3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:58968d331425a6f9eedcee087f77fd3c927c88f55368f43ff7e0a19891f2642c", size = 4171040 }, + { url = "https://files.pythonhosted.org/packages/bd/48/bb16b7541d207a19d9ae8b541c70037a05e473ddc72ccb1386524d4f023c/cryptography-44.0.3-cp37-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:e28d62e59a4dbd1d22e747f57d4f00c459af22181f0b2f787ea83f5a876d7c76", size = 3963411 }, + { url = "https://files.pythonhosted.org/packages/42/b2/7d31f2af5591d217d71d37d044ef5412945a8a8e98d5a2a8ae4fd9cd4489/cryptography-44.0.3-cp37-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:af653022a0c25ef2e3ffb2c673a50e5a0d02fecc41608f4954176f1933b12359", size = 3689263 }, + { url = "https://files.pythonhosted.org/packages/25/50/c0dfb9d87ae88ccc01aad8eb93e23cfbcea6a6a106a9b63a7b14c1f93c75/cryptography-44.0.3-cp37-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:157f1f3b8d941c2bd8f3ffee0af9b049c9665c39d3da9db2dc338feca5e98a43", size = 4196198 }, + { url = "https://files.pythonhosted.org/packages/66/c9/55c6b8794a74da652690c898cb43906310a3e4e4f6ee0b5f8b3b3e70c441/cryptography-44.0.3-cp37-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:c6cd67722619e4d55fdb42ead64ed8843d64638e9c07f4011163e46bc512cf01", size = 3966502 }, + { url = "https://files.pythonhosted.org/packages/b6/f7/7cb5488c682ca59a02a32ec5f975074084db4c983f849d47b7b67cc8697a/cryptography-44.0.3-cp37-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:b424563394c369a804ecbee9b06dfb34997f19d00b3518e39f83a5642618397d", size = 4196173 }, + { url = "https://files.pythonhosted.org/packages/d2/0b/2f789a8403ae089b0b121f8f54f4a3e5228df756e2146efdf4a09a3d5083/cryptography-44.0.3-cp37-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:c91fc8e8fd78af553f98bc7f2a1d8db977334e4eea302a4bfd75b9461c2d8904", size = 4087713 }, + { url = "https://files.pythonhosted.org/packages/1d/aa/330c13655f1af398fc154089295cf259252f0ba5df93b4bc9d9c7d7f843e/cryptography-44.0.3-cp37-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:25cd194c39fa5a0aa4169125ee27d1172097857b27109a45fadc59653ec06f44", size = 4299064 }, + { url = "https://files.pythonhosted.org/packages/10/a8/8c540a421b44fd267a7d58a1fd5f072a552d72204a3f08194f98889de76d/cryptography-44.0.3-cp37-abi3-win32.whl", hash = "sha256:3be3f649d91cb182c3a6bd336de8b61a0a71965bd13d1a04a0e15b39c3d5809d", size = 2773887 }, + { url = "https://files.pythonhosted.org/packages/b9/0d/c4b1657c39ead18d76bbd122da86bd95bdc4095413460d09544000a17d56/cryptography-44.0.3-cp37-abi3-win_amd64.whl", hash = "sha256:3883076d5c4cc56dbef0b898a74eb6992fdac29a7b9013870b34efe4ddb39a0d", size = 3209737 }, + { url = "https://files.pythonhosted.org/packages/34/a3/ad08e0bcc34ad436013458d7528e83ac29910943cea42ad7dd4141a27bbb/cryptography-44.0.3-cp39-abi3-macosx_10_9_universal2.whl", hash = "sha256:5639c2b16764c6f76eedf722dbad9a0914960d3489c0cc38694ddf9464f1bb2f", size = 6673501 }, + { url = "https://files.pythonhosted.org/packages/b1/f0/7491d44bba8d28b464a5bc8cc709f25a51e3eac54c0a4444cf2473a57c37/cryptography-44.0.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f3ffef566ac88f75967d7abd852ed5f182da252d23fac11b4766da3957766759", size = 3960307 }, + { url = "https://files.pythonhosted.org/packages/f7/c8/e5c5d0e1364d3346a5747cdcd7ecbb23ca87e6dea4f942a44e88be349f06/cryptography-44.0.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:192ed30fac1728f7587c6f4613c29c584abdc565d7417c13904708db10206645", size = 4170876 }, + { url = "https://files.pythonhosted.org/packages/73/96/025cb26fc351d8c7d3a1c44e20cf9a01e9f7cf740353c9c7a17072e4b264/cryptography-44.0.3-cp39-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:7d5fe7195c27c32a64955740b949070f21cba664604291c298518d2e255931d2", size = 3964127 }, + { url = "https://files.pythonhosted.org/packages/01/44/eb6522db7d9f84e8833ba3bf63313f8e257729cf3a8917379473fcfd6601/cryptography-44.0.3-cp39-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:3f07943aa4d7dad689e3bb1638ddc4944cc5e0921e3c227486daae0e31a05e54", size = 3689164 }, + { url = "https://files.pythonhosted.org/packages/68/fb/d61a4defd0d6cee20b1b8a1ea8f5e25007e26aeb413ca53835f0cae2bcd1/cryptography-44.0.3-cp39-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:cb90f60e03d563ca2445099edf605c16ed1d5b15182d21831f58460c48bffb93", size = 4198081 }, + { url = "https://files.pythonhosted.org/packages/1b/50/457f6911d36432a8811c3ab8bd5a6090e8d18ce655c22820994913dd06ea/cryptography-44.0.3-cp39-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:ab0b005721cc0039e885ac3503825661bd9810b15d4f374e473f8c89b7d5460c", size = 3967716 }, + { url = "https://files.pythonhosted.org/packages/35/6e/dca39d553075980ccb631955c47b93d87d27f3596da8d48b1ae81463d915/cryptography-44.0.3-cp39-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:3bb0847e6363c037df8f6ede57d88eaf3410ca2267fb12275370a76f85786a6f", size = 4197398 }, + { url = "https://files.pythonhosted.org/packages/9b/9d/d1f2fe681eabc682067c66a74addd46c887ebacf39038ba01f8860338d3d/cryptography-44.0.3-cp39-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:b0cc66c74c797e1db750aaa842ad5b8b78e14805a9b5d1348dc603612d3e3ff5", size = 4087900 }, + { url = "https://files.pythonhosted.org/packages/c4/f5/3599e48c5464580b73b236aafb20973b953cd2e7b44c7c2533de1d888446/cryptography-44.0.3-cp39-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:6866df152b581f9429020320e5eb9794c8780e90f7ccb021940d7f50ee00ae0b", size = 4301067 }, + { url = "https://files.pythonhosted.org/packages/a7/6c/d2c48c8137eb39d0c193274db5c04a75dab20d2f7c3f81a7dcc3a8897701/cryptography-44.0.3-cp39-abi3-win32.whl", hash = "sha256:c138abae3a12a94c75c10499f1cbae81294a6f983b3af066390adee73f433028", size = 2775467 }, + { url = "https://files.pythonhosted.org/packages/c9/ad/51f212198681ea7b0deaaf8846ee10af99fba4e894f67b353524eab2bbe5/cryptography-44.0.3-cp39-abi3-win_amd64.whl", hash = "sha256:5d186f32e52e66994dce4f766884bcb9c68b8da62d61d9d215bfe5fb56d21334", size = 3210375 }, + { url = "https://files.pythonhosted.org/packages/7f/10/abcf7418536df1eaba70e2cfc5c8a0ab07aa7aa02a5cbc6a78b9d8b4f121/cryptography-44.0.3-pp310-pypy310_pp73-macosx_10_9_x86_64.whl", hash = "sha256:cad399780053fb383dc067475135e41c9fe7d901a97dd5d9c5dfb5611afc0d7d", size = 3393192 }, + { url = "https://files.pythonhosted.org/packages/06/59/ecb3ef380f5891978f92a7f9120e2852b1df6f0a849c277b8ea45b865db2/cryptography-44.0.3-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:21a83f6f35b9cc656d71b5de8d519f566df01e660ac2578805ab245ffd8523f8", size = 3898419 }, + { url = "https://files.pythonhosted.org/packages/bb/d0/35e2313dbb38cf793aa242182ad5bc5ef5c8fd4e5dbdc380b936c7d51169/cryptography-44.0.3-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:fc3c9babc1e1faefd62704bb46a69f359a9819eb0292e40df3fb6e3574715cd4", size = 4117892 }, + { url = "https://files.pythonhosted.org/packages/dc/c8/31fb6e33b56c2c2100d76de3fd820afaa9d4d0b6aea1ccaf9aaf35dc7ce3/cryptography-44.0.3-pp310-pypy310_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:e909df4053064a97f1e6565153ff8bb389af12c5c8d29c343308760890560aff", size = 3900855 }, + { url = "https://files.pythonhosted.org/packages/43/2a/08cc2ec19e77f2a3cfa2337b429676406d4bb78ddd130a05c458e7b91d73/cryptography-44.0.3-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:dad80b45c22e05b259e33ddd458e9e2ba099c86ccf4e88db7bbab4b747b18d06", size = 4117619 }, + { url = "https://files.pythonhosted.org/packages/02/68/fc3d3f84022a75f2ac4b1a1c0e5d6a0c2ea259e14cd4aae3e0e68e56483c/cryptography-44.0.3-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:479d92908277bed6e1a1c69b277734a7771c2b78633c224445b5c60a9f4bc1d9", size = 3136570 }, + { url = "https://files.pythonhosted.org/packages/8d/4b/c11ad0b6c061902de5223892d680e89c06c7c4d606305eb8de56c5427ae6/cryptography-44.0.3-pp311-pypy311_pp73-macosx_10_9_x86_64.whl", hash = "sha256:896530bc9107b226f265effa7ef3f21270f18a2026bc09fed1ebd7b66ddf6375", size = 3390230 }, + { url = "https://files.pythonhosted.org/packages/58/11/0a6bf45d53b9b2290ea3cec30e78b78e6ca29dc101e2e296872a0ffe1335/cryptography-44.0.3-pp311-pypy311_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:9b4d4a5dbee05a2c390bf212e78b99434efec37b17a4bff42f50285c5c8c9647", size = 3895216 }, + { url = "https://files.pythonhosted.org/packages/0a/27/b28cdeb7270e957f0077a2c2bfad1b38f72f1f6d699679f97b816ca33642/cryptography-44.0.3-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:02f55fb4f8b79c1221b0961488eaae21015b69b210e18c386b69de182ebb1259", size = 4115044 }, + { url = "https://files.pythonhosted.org/packages/35/b0/ec4082d3793f03cb248881fecefc26015813199b88f33e3e990a43f79835/cryptography-44.0.3-pp311-pypy311_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:dd3db61b8fe5be220eee484a17233287d0be6932d056cf5738225b9c05ef4fff", size = 3898034 }, + { url = "https://files.pythonhosted.org/packages/0b/7f/adf62e0b8e8d04d50c9a91282a57628c00c54d4ae75e2b02a223bd1f2613/cryptography-44.0.3-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:978631ec51a6bbc0b7e58f23b68a8ce9e5f09721940933e9c217068388789fe5", size = 4114449 }, + { url = "https://files.pythonhosted.org/packages/87/62/d69eb4a8ee231f4bf733a92caf9da13f1c81a44e874b1d4080c25ecbb723/cryptography-44.0.3-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:5d20cc348cca3a8aa7312f42ab953a56e15323800ca3ab0706b8cd452a3a056c", size = 3134369 }, +] + +[[package]] +name = "exceptiongroup" +version = "1.2.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/09/35/2495c4ac46b980e4ca1f6ad6db102322ef3ad2410b79fdde159a4b0f3b92/exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc", size = 28883 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/02/cc/b7e31358aac6ed1ef2bb790a9746ac2c69bcb3c8588b41616914eb106eaf/exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b", size = 16453 }, +] + +[[package]] +name = "idna" +version = "3.10" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f1/70/7703c29685631f5a7590aa73f1f1d3fa9a380e654b86af429e0934a32f7d/idna-3.10.tar.gz", hash = "sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9", size = 190490 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442 }, +] + +[[package]] +name = "iniconfig" +version = "2.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f2/97/ebf4da567aa6827c909642694d71c9fcf53e5b504f2d96afea02718862f3/iniconfig-2.1.0.tar.gz", hash = "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7", size = 4793 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2c/e1/e6716421ea10d38022b952c159d5161ca1193197fb744506875fbb87ea7b/iniconfig-2.1.0-py3-none-any.whl", hash = "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760", size = 6050 }, +] + +[[package]] +name = "libcomponent" +version = "0.0.4" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "cryptography" }, + { name = "mypy-extensions" }, + { name = "trio" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/9a/0a/5f3d65ed7bb368baa69c067a1ef478a994b7326c6747a45fc834e8443890/libcomponent-0.0.4.tar.gz", hash = "sha256:c1daa699d748be0e0132c4062e0527a89a6a8dff32c291efa9880c0715d49c1b", size = 76346 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/c4/ab/c804e77e769a14bace0ff7d536a2c2b9fb08289a0cab7f76840c92a270b6/libcomponent-0.0.4-py3-none-any.whl", hash = "sha256:c26edf821564da1400ede72d1414bc754cd693199d436e3cde7b46b9950d7587", size = 59380 }, +] + +[[package]] +name = "mypy" +version = "1.15.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "mypy-extensions" }, + { name = "tomli", marker = "python_full_version < '3.11'" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/ce/43/d5e49a86afa64bd3839ea0d5b9c7103487007d728e1293f52525d6d5486a/mypy-1.15.0.tar.gz", hash = "sha256:404534629d51d3efea5c800ee7c42b72a6554d6c400e6a79eafe15d11341fd43", size = 3239717 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/68/f8/65a7ce8d0e09b6329ad0c8d40330d100ea343bd4dd04c4f8ae26462d0a17/mypy-1.15.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:979e4e1a006511dacf628e36fadfecbcc0160a8af6ca7dad2f5025529e082c13", size = 10738433 }, + { url = "https://files.pythonhosted.org/packages/b4/95/9c0ecb8eacfe048583706249439ff52105b3f552ea9c4024166c03224270/mypy-1.15.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:c4bb0e1bd29f7d34efcccd71cf733580191e9a264a2202b0239da95984c5b559", size = 9861472 }, + { url = "https://files.pythonhosted.org/packages/84/09/9ec95e982e282e20c0d5407bc65031dfd0f0f8ecc66b69538296e06fcbee/mypy-1.15.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:be68172e9fd9ad8fb876c6389f16d1c1b5f100ffa779f77b1fb2176fcc9ab95b", size = 11611424 }, + { url = "https://files.pythonhosted.org/packages/78/13/f7d14e55865036a1e6a0a69580c240f43bc1f37407fe9235c0d4ef25ffb0/mypy-1.15.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c7be1e46525adfa0d97681432ee9fcd61a3964c2446795714699a998d193f1a3", size = 12365450 }, + { url = "https://files.pythonhosted.org/packages/48/e1/301a73852d40c241e915ac6d7bcd7fedd47d519246db2d7b86b9d7e7a0cb/mypy-1.15.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:2e2c2e6d3593f6451b18588848e66260ff62ccca522dd231cd4dd59b0160668b", size = 12551765 }, + { url = "https://files.pythonhosted.org/packages/77/ba/c37bc323ae5fe7f3f15a28e06ab012cd0b7552886118943e90b15af31195/mypy-1.15.0-cp310-cp310-win_amd64.whl", hash = "sha256:6983aae8b2f653e098edb77f893f7b6aca69f6cffb19b2cc7443f23cce5f4828", size = 9274701 }, + { url = "https://files.pythonhosted.org/packages/03/bc/f6339726c627bd7ca1ce0fa56c9ae2d0144604a319e0e339bdadafbbb599/mypy-1.15.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:2922d42e16d6de288022e5ca321cd0618b238cfc5570e0263e5ba0a77dbef56f", size = 10662338 }, + { url = "https://files.pythonhosted.org/packages/e2/90/8dcf506ca1a09b0d17555cc00cd69aee402c203911410136cd716559efe7/mypy-1.15.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:2ee2d57e01a7c35de00f4634ba1bbf015185b219e4dc5909e281016df43f5ee5", size = 9787540 }, + { url = "https://files.pythonhosted.org/packages/05/05/a10f9479681e5da09ef2f9426f650d7b550d4bafbef683b69aad1ba87457/mypy-1.15.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:973500e0774b85d9689715feeffcc980193086551110fd678ebe1f4342fb7c5e", size = 11538051 }, + { url = "https://files.pythonhosted.org/packages/e9/9a/1f7d18b30edd57441a6411fcbc0c6869448d1a4bacbaee60656ac0fc29c8/mypy-1.15.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5a95fb17c13e29d2d5195869262f8125dfdb5c134dc8d9a9d0aecf7525b10c2c", size = 12286751 }, + { url = "https://files.pythonhosted.org/packages/72/af/19ff499b6f1dafcaf56f9881f7a965ac2f474f69f6f618b5175b044299f5/mypy-1.15.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:1905f494bfd7d85a23a88c5d97840888a7bd516545fc5aaedff0267e0bb54e2f", size = 12421783 }, + { url = "https://files.pythonhosted.org/packages/96/39/11b57431a1f686c1aed54bf794870efe0f6aeca11aca281a0bd87a5ad42c/mypy-1.15.0-cp311-cp311-win_amd64.whl", hash = "sha256:c9817fa23833ff189db061e6d2eff49b2f3b6ed9856b4a0a73046e41932d744f", size = 9265618 }, + { url = "https://files.pythonhosted.org/packages/98/3a/03c74331c5eb8bd025734e04c9840532226775c47a2c39b56a0c8d4f128d/mypy-1.15.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:aea39e0583d05124836ea645f412e88a5c7d0fd77a6d694b60d9b6b2d9f184fd", size = 10793981 }, + { url = "https://files.pythonhosted.org/packages/f0/1a/41759b18f2cfd568848a37c89030aeb03534411eef981df621d8fad08a1d/mypy-1.15.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:2f2147ab812b75e5b5499b01ade1f4a81489a147c01585cda36019102538615f", size = 9749175 }, + { url = "https://files.pythonhosted.org/packages/12/7e/873481abf1ef112c582db832740f4c11b2bfa510e829d6da29b0ab8c3f9c/mypy-1.15.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ce436f4c6d218a070048ed6a44c0bbb10cd2cc5e272b29e7845f6a2f57ee4464", size = 11455675 }, + { url = "https://files.pythonhosted.org/packages/b3/d0/92ae4cde706923a2d3f2d6c39629134063ff64b9dedca9c1388363da072d/mypy-1.15.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8023ff13985661b50a5928fc7a5ca15f3d1affb41e5f0a9952cb68ef090b31ee", size = 12410020 }, + { url = "https://files.pythonhosted.org/packages/46/8b/df49974b337cce35f828ba6fda228152d6db45fed4c86ba56ffe442434fd/mypy-1.15.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:1124a18bc11a6a62887e3e137f37f53fbae476dc36c185d549d4f837a2a6a14e", size = 12498582 }, + { url = "https://files.pythonhosted.org/packages/13/50/da5203fcf6c53044a0b699939f31075c45ae8a4cadf538a9069b165c1050/mypy-1.15.0-cp312-cp312-win_amd64.whl", hash = "sha256:171a9ca9a40cd1843abeca0e405bc1940cd9b305eaeea2dda769ba096932bb22", size = 9366614 }, + { url = "https://files.pythonhosted.org/packages/6a/9b/fd2e05d6ffff24d912f150b87db9e364fa8282045c875654ce7e32fffa66/mypy-1.15.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:93faf3fdb04768d44bf28693293f3904bbb555d076b781ad2530214ee53e3445", size = 10788592 }, + { url = "https://files.pythonhosted.org/packages/74/37/b246d711c28a03ead1fd906bbc7106659aed7c089d55fe40dd58db812628/mypy-1.15.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:811aeccadfb730024c5d3e326b2fbe9249bb7413553f15499a4050f7c30e801d", size = 9753611 }, + { url = "https://files.pythonhosted.org/packages/a6/ac/395808a92e10cfdac8003c3de9a2ab6dc7cde6c0d2a4df3df1b815ffd067/mypy-1.15.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:98b7b9b9aedb65fe628c62a6dc57f6d5088ef2dfca37903a7d9ee374d03acca5", size = 11438443 }, + { url = "https://files.pythonhosted.org/packages/d2/8b/801aa06445d2de3895f59e476f38f3f8d610ef5d6908245f07d002676cbf/mypy-1.15.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c43a7682e24b4f576d93072216bf56eeff70d9140241f9edec0c104d0c515036", size = 12402541 }, + { url = "https://files.pythonhosted.org/packages/c7/67/5a4268782eb77344cc613a4cf23540928e41f018a9a1ec4c6882baf20ab8/mypy-1.15.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:baefc32840a9f00babd83251560e0ae1573e2f9d1b067719479bfb0e987c6357", size = 12494348 }, + { url = "https://files.pythonhosted.org/packages/83/3e/57bb447f7bbbfaabf1712d96f9df142624a386d98fb026a761532526057e/mypy-1.15.0-cp313-cp313-win_amd64.whl", hash = "sha256:b9378e2c00146c44793c98b8d5a61039a048e31f429fb0eb546d93f4b000bedf", size = 9373648 }, + { url = "https://files.pythonhosted.org/packages/09/4e/a7d65c7322c510de2c409ff3828b03354a7c43f5a8ed458a7a131b41c7b9/mypy-1.15.0-py3-none-any.whl", hash = "sha256:5469affef548bd1895d86d3bf10ce2b44e33d86923c29e4d675b3e323437ea3e", size = 2221777 }, +] + +[[package]] +name = "mypy-extensions" +version = "1.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a2/6e/371856a3fb9d31ca8dac321cda606860fa4548858c0cc45d9d1d4ca2628b/mypy_extensions-1.1.0.tar.gz", hash = "sha256:52e68efc3284861e772bbcd66823fde5ae21fd2fdb51c62a211403730b916558", size = 6343 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/79/7b/2c79738432f5c924bef5071f933bcc9efd0473bac3b4aa584a6f7c1c8df8/mypy_extensions-1.1.0-py3-none-any.whl", hash = "sha256:1be4cccdb0f2482337c4743e60421de3a356cd97508abadd57d47403e94f5505", size = 4963 }, +] + +[[package]] +name = "numpy" +version = "2.1.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/25/ca/1166b75c21abd1da445b97bf1fa2f14f423c6cfb4fc7c4ef31dccf9f6a94/numpy-2.1.3.tar.gz", hash = "sha256:aa08e04e08aaf974d4458def539dece0d28146d866a39da5639596f4921fd761", size = 20166090 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f1/80/d572a4737626372915bca41c3afbfec9d173561a39a0a61bacbbfd1dafd4/numpy-2.1.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:c894b4305373b9c5576d7a12b473702afdf48ce5369c074ba304cc5ad8730dff", size = 21152472 }, + { url = "https://files.pythonhosted.org/packages/6f/bb/7bfba10c791ae3bb6716da77ad85a82d5fac07fc96fb0023ef0571df9d20/numpy-2.1.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:b47fbb433d3260adcd51eb54f92a2ffbc90a4595f8970ee00e064c644ac788f5", size = 13747967 }, + { url = "https://files.pythonhosted.org/packages/da/d6/2df7bde35f0478455f0be5934877b3e5a505f587b00230f54a519a6b55a5/numpy-2.1.3-cp310-cp310-macosx_14_0_arm64.whl", hash = "sha256:825656d0743699c529c5943554d223c021ff0494ff1442152ce887ef4f7561a1", size = 5354921 }, + { url = "https://files.pythonhosted.org/packages/d1/bb/75b945874f931494891eac6ca06a1764d0e8208791f3addadb2963b83527/numpy-2.1.3-cp310-cp310-macosx_14_0_x86_64.whl", hash = "sha256:6a4825252fcc430a182ac4dee5a505053d262c807f8a924603d411f6718b88fd", size = 6888603 }, + { url = "https://files.pythonhosted.org/packages/68/a7/fde73636f6498dbfa6d82fc336164635fe592f1ad0d13285fcb6267fdc1c/numpy-2.1.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e711e02f49e176a01d0349d82cb5f05ba4db7d5e7e0defd026328e5cfb3226d3", size = 13889862 }, + { url = "https://files.pythonhosted.org/packages/05/db/5d9c91b2e1e2e72be1369278f696356d44975befcae830daf2e667dcb54f/numpy-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:78574ac2d1a4a02421f25da9559850d59457bac82f2b8d7a44fe83a64f770098", size = 16328151 }, + { url = "https://files.pythonhosted.org/packages/3e/6a/7eb732109b53ae64a29e25d7e68eb9d6611037f6354875497008a49e74d3/numpy-2.1.3-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:c7662f0e3673fe4e832fe07b65c50342ea27d989f92c80355658c7f888fcc83c", size = 16704107 }, + { url = "https://files.pythonhosted.org/packages/88/cc/278113b66a1141053cbda6f80e4200c6da06b3079c2d27bda1fde41f2c1f/numpy-2.1.3-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:fa2d1337dc61c8dc417fbccf20f6d1e139896a30721b7f1e832b2bb6ef4eb6c4", size = 14385789 }, + { url = "https://files.pythonhosted.org/packages/f5/69/eb20f5e1bfa07449bc67574d2f0f7c1e6b335fb41672e43861a7727d85f2/numpy-2.1.3-cp310-cp310-win32.whl", hash = "sha256:72dcc4a35a8515d83e76b58fdf8113a5c969ccd505c8a946759b24e3182d1f23", size = 6536706 }, + { url = "https://files.pythonhosted.org/packages/8e/8b/1c131ab5a94c1086c289c6e1da1d843de9dbd95fe5f5ee6e61904c9518e2/numpy-2.1.3-cp310-cp310-win_amd64.whl", hash = "sha256:ecc76a9ba2911d8d37ac01de72834d8849e55473457558e12995f4cd53e778e0", size = 12864165 }, + { url = "https://files.pythonhosted.org/packages/ad/81/c8167192eba5247593cd9d305ac236847c2912ff39e11402e72ae28a4985/numpy-2.1.3-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:4d1167c53b93f1f5d8a139a742b3c6f4d429b54e74e6b57d0eff40045187b15d", size = 21156252 }, + { url = "https://files.pythonhosted.org/packages/da/74/5a60003fc3d8a718d830b08b654d0eea2d2db0806bab8f3c2aca7e18e010/numpy-2.1.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:c80e4a09b3d95b4e1cac08643f1152fa71a0a821a2d4277334c88d54b2219a41", size = 13784119 }, + { url = "https://files.pythonhosted.org/packages/47/7c/864cb966b96fce5e63fcf25e1e4d957fe5725a635e5f11fe03f39dd9d6b5/numpy-2.1.3-cp311-cp311-macosx_14_0_arm64.whl", hash = "sha256:576a1c1d25e9e02ed7fa5477f30a127fe56debd53b8d2c89d5578f9857d03ca9", size = 5352978 }, + { url = "https://files.pythonhosted.org/packages/09/ac/61d07930a4993dd9691a6432de16d93bbe6aa4b1c12a5e573d468eefc1ca/numpy-2.1.3-cp311-cp311-macosx_14_0_x86_64.whl", hash = "sha256:973faafebaae4c0aaa1a1ca1ce02434554d67e628b8d805e61f874b84e136b09", size = 6892570 }, + { url = "https://files.pythonhosted.org/packages/27/2f/21b94664f23af2bb52030653697c685022119e0dc93d6097c3cb45bce5f9/numpy-2.1.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:762479be47a4863e261a840e8e01608d124ee1361e48b96916f38b119cfda04a", size = 13896715 }, + { url = "https://files.pythonhosted.org/packages/7a/f0/80811e836484262b236c684a75dfc4ba0424bc670e765afaa911468d9f39/numpy-2.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc6f24b3d1ecc1eebfbf5d6051faa49af40b03be1aaa781ebdadcbc090b4539b", size = 16339644 }, + { url = "https://files.pythonhosted.org/packages/fa/81/ce213159a1ed8eb7d88a2a6ef4fbdb9e4ffd0c76b866c350eb4e3c37e640/numpy-2.1.3-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:17ee83a1f4fef3c94d16dc1802b998668b5419362c8a4f4e8a491de1b41cc3ee", size = 16712217 }, + { url = "https://files.pythonhosted.org/packages/7d/84/4de0b87d5a72f45556b2a8ee9fc8801e8518ec867fc68260c1f5dcb3903f/numpy-2.1.3-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:15cb89f39fa6d0bdfb600ea24b250e5f1a3df23f901f51c8debaa6a5d122b2f0", size = 14399053 }, + { url = "https://files.pythonhosted.org/packages/7e/1c/e5fabb9ad849f9d798b44458fd12a318d27592d4bc1448e269dec070ff04/numpy-2.1.3-cp311-cp311-win32.whl", hash = "sha256:d9beb777a78c331580705326d2367488d5bc473b49a9bc3036c154832520aca9", size = 6534741 }, + { url = "https://files.pythonhosted.org/packages/1e/48/a9a4b538e28f854bfb62e1dea3c8fea12e90216a276c7777ae5345ff29a7/numpy-2.1.3-cp311-cp311-win_amd64.whl", hash = "sha256:d89dd2b6da69c4fff5e39c28a382199ddedc3a5be5390115608345dec660b9e2", size = 12869487 }, + { url = "https://files.pythonhosted.org/packages/8a/f0/385eb9970309643cbca4fc6eebc8bb16e560de129c91258dfaa18498da8b/numpy-2.1.3-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:f55ba01150f52b1027829b50d70ef1dafd9821ea82905b63936668403c3b471e", size = 20849658 }, + { url = "https://files.pythonhosted.org/packages/54/4a/765b4607f0fecbb239638d610d04ec0a0ded9b4951c56dc68cef79026abf/numpy-2.1.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:13138eadd4f4da03074851a698ffa7e405f41a0845a6b1ad135b81596e4e9958", size = 13492258 }, + { url = "https://files.pythonhosted.org/packages/bd/a7/2332679479c70b68dccbf4a8eb9c9b5ee383164b161bee9284ac141fbd33/numpy-2.1.3-cp312-cp312-macosx_14_0_arm64.whl", hash = "sha256:a6b46587b14b888e95e4a24d7b13ae91fa22386c199ee7b418f449032b2fa3b8", size = 5090249 }, + { url = "https://files.pythonhosted.org/packages/c1/67/4aa00316b3b981a822c7a239d3a8135be2a6945d1fd11d0efb25d361711a/numpy-2.1.3-cp312-cp312-macosx_14_0_x86_64.whl", hash = "sha256:0fa14563cc46422e99daef53d725d0c326e99e468a9320a240affffe87852564", size = 6621704 }, + { url = "https://files.pythonhosted.org/packages/5e/da/1a429ae58b3b6c364eeec93bf044c532f2ff7b48a52e41050896cf15d5b1/numpy-2.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8637dcd2caa676e475503d1f8fdb327bc495554e10838019651b76d17b98e512", size = 13606089 }, + { url = "https://files.pythonhosted.org/packages/9e/3e/3757f304c704f2f0294a6b8340fcf2be244038be07da4cccf390fa678a9f/numpy-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2312b2aa89e1f43ecea6da6ea9a810d06aae08321609d8dc0d0eda6d946a541b", size = 16043185 }, + { url = "https://files.pythonhosted.org/packages/43/97/75329c28fea3113d00c8d2daf9bc5828d58d78ed661d8e05e234f86f0f6d/numpy-2.1.3-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:a38c19106902bb19351b83802531fea19dee18e5b37b36454f27f11ff956f7fc", size = 16410751 }, + { url = "https://files.pythonhosted.org/packages/ad/7a/442965e98b34e0ae9da319f075b387bcb9a1e0658276cc63adb8c9686f7b/numpy-2.1.3-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:02135ade8b8a84011cbb67dc44e07c58f28575cf9ecf8ab304e51c05528c19f0", size = 14082705 }, + { url = "https://files.pythonhosted.org/packages/ac/b6/26108cf2cfa5c7e03fb969b595c93131eab4a399762b51ce9ebec2332e80/numpy-2.1.3-cp312-cp312-win32.whl", hash = "sha256:e6988e90fcf617da2b5c78902fe8e668361b43b4fe26dbf2d7b0f8034d4cafb9", size = 6239077 }, + { url = "https://files.pythonhosted.org/packages/a6/84/fa11dad3404b7634aaab50733581ce11e5350383311ea7a7010f464c0170/numpy-2.1.3-cp312-cp312-win_amd64.whl", hash = "sha256:0d30c543f02e84e92c4b1f415b7c6b5326cbe45ee7882b6b77db7195fb971e3a", size = 12566858 }, + { url = "https://files.pythonhosted.org/packages/4d/0b/620591441457e25f3404c8057eb924d04f161244cb8a3680d529419aa86e/numpy-2.1.3-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:96fe52fcdb9345b7cd82ecd34547fca4321f7656d500eca497eb7ea5a926692f", size = 20836263 }, + { url = "https://files.pythonhosted.org/packages/45/e1/210b2d8b31ce9119145433e6ea78046e30771de3fe353f313b2778142f34/numpy-2.1.3-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f653490b33e9c3a4c1c01d41bc2aef08f9475af51146e4a7710c450cf9761598", size = 13507771 }, + { url = "https://files.pythonhosted.org/packages/55/44/aa9ee3caee02fa5a45f2c3b95cafe59c44e4b278fbbf895a93e88b308555/numpy-2.1.3-cp313-cp313-macosx_14_0_arm64.whl", hash = "sha256:dc258a761a16daa791081d026f0ed4399b582712e6fc887a95af09df10c5ca57", size = 5075805 }, + { url = "https://files.pythonhosted.org/packages/78/d6/61de6e7e31915ba4d87bbe1ae859e83e6582ea14c6add07c8f7eefd8488f/numpy-2.1.3-cp313-cp313-macosx_14_0_x86_64.whl", hash = "sha256:016d0f6f5e77b0f0d45d77387ffa4bb89816b57c835580c3ce8e099ef830befe", size = 6608380 }, + { url = "https://files.pythonhosted.org/packages/3e/46/48bdf9b7241e317e6cf94276fe11ba673c06d1fdf115d8b4ebf616affd1a/numpy-2.1.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c181ba05ce8299c7aa3125c27b9c2167bca4a4445b7ce73d5febc411ca692e43", size = 13602451 }, + { url = "https://files.pythonhosted.org/packages/70/50/73f9a5aa0810cdccda9c1d20be3cbe4a4d6ea6bfd6931464a44c95eef731/numpy-2.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5641516794ca9e5f8a4d17bb45446998c6554704d888f86df9b200e66bdcce56", size = 16039822 }, + { url = "https://files.pythonhosted.org/packages/ad/cd/098bc1d5a5bc5307cfc65ee9369d0ca658ed88fbd7307b0d49fab6ca5fa5/numpy-2.1.3-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:ea4dedd6e394a9c180b33c2c872b92f7ce0f8e7ad93e9585312b0c5a04777a4a", size = 16411822 }, + { url = "https://files.pythonhosted.org/packages/83/a2/7d4467a2a6d984549053b37945620209e702cf96a8bc658bc04bba13c9e2/numpy-2.1.3-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:b0df3635b9c8ef48bd3be5f862cf71b0a4716fa0e702155c45067c6b711ddcef", size = 14079598 }, + { url = "https://files.pythonhosted.org/packages/e9/6a/d64514dcecb2ee70bfdfad10c42b76cab657e7ee31944ff7a600f141d9e9/numpy-2.1.3-cp313-cp313-win32.whl", hash = "sha256:50ca6aba6e163363f132b5c101ba078b8cbd3fa92c7865fd7d4d62d9779ac29f", size = 6236021 }, + { url = "https://files.pythonhosted.org/packages/bb/f9/12297ed8d8301a401e7d8eb6b418d32547f1d700ed3c038d325a605421a4/numpy-2.1.3-cp313-cp313-win_amd64.whl", hash = "sha256:747641635d3d44bcb380d950679462fae44f54b131be347d5ec2bce47d3df9ed", size = 12560405 }, + { url = "https://files.pythonhosted.org/packages/a7/45/7f9244cd792e163b334e3a7f02dff1239d2890b6f37ebf9e82cbe17debc0/numpy-2.1.3-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:996bb9399059c5b82f76b53ff8bb686069c05acc94656bb259b1d63d04a9506f", size = 20859062 }, + { url = "https://files.pythonhosted.org/packages/b1/b4/a084218e7e92b506d634105b13e27a3a6645312b93e1c699cc9025adb0e1/numpy-2.1.3-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:45966d859916ad02b779706bb43b954281db43e185015df6eb3323120188f9e4", size = 13515839 }, + { url = "https://files.pythonhosted.org/packages/27/45/58ed3f88028dcf80e6ea580311dc3edefdd94248f5770deb980500ef85dd/numpy-2.1.3-cp313-cp313t-macosx_14_0_arm64.whl", hash = "sha256:baed7e8d7481bfe0874b566850cb0b85243e982388b7b23348c6db2ee2b2ae8e", size = 5116031 }, + { url = "https://files.pythonhosted.org/packages/37/a8/eb689432eb977d83229094b58b0f53249d2209742f7de529c49d61a124a0/numpy-2.1.3-cp313-cp313t-macosx_14_0_x86_64.whl", hash = "sha256:a9f7f672a3388133335589cfca93ed468509cb7b93ba3105fce780d04a6576a0", size = 6629977 }, + { url = "https://files.pythonhosted.org/packages/42/a3/5355ad51ac73c23334c7caaed01adadfda49544f646fcbfbb4331deb267b/numpy-2.1.3-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d7aac50327da5d208db2eec22eb11e491e3fe13d22653dce51b0f4109101b408", size = 13575951 }, + { url = "https://files.pythonhosted.org/packages/c4/70/ea9646d203104e647988cb7d7279f135257a6b7e3354ea6c56f8bafdb095/numpy-2.1.3-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4394bc0dbd074b7f9b52024832d16e019decebf86caf909d94f6b3f77a8ee3b6", size = 16022655 }, + { url = "https://files.pythonhosted.org/packages/14/ce/7fc0612903e91ff9d0b3f2eda4e18ef9904814afcae5b0f08edb7f637883/numpy-2.1.3-cp313-cp313t-musllinux_1_1_x86_64.whl", hash = "sha256:50d18c4358a0a8a53f12a8ba9d772ab2d460321e6a93d6064fc22443d189853f", size = 16399902 }, + { url = "https://files.pythonhosted.org/packages/ef/62/1d3204313357591c913c32132a28f09a26357e33ea3c4e2fe81269e0dca1/numpy-2.1.3-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:14e253bd43fc6b37af4921b10f6add6925878a42a0c5fe83daee390bca80bc17", size = 14067180 }, + { url = "https://files.pythonhosted.org/packages/24/d7/78a40ed1d80e23a774cb8a34ae8a9493ba1b4271dde96e56ccdbab1620ef/numpy-2.1.3-cp313-cp313t-win32.whl", hash = "sha256:08788d27a5fd867a663f6fc753fd7c3ad7e92747efc73c53bca2f19f8bc06f48", size = 6291907 }, + { url = "https://files.pythonhosted.org/packages/86/09/a5ab407bd7f5f5599e6a9261f964ace03a73e7c6928de906981c31c38082/numpy-2.1.3-cp313-cp313t-win_amd64.whl", hash = "sha256:2564fbdf2b99b3f815f2107c1bbc93e2de8ee655a69c261363a1172a79a257d4", size = 12644098 }, + { url = "https://files.pythonhosted.org/packages/00/e7/8d8bb791b62586cc432ecbb70632b4f23b7b7c88df41878de7528264f6d7/numpy-2.1.3-pp310-pypy310_pp73-macosx_10_15_x86_64.whl", hash = "sha256:4f2015dfe437dfebbfce7c85c7b53d81ba49e71ba7eadbf1df40c915af75979f", size = 20983893 }, + { url = "https://files.pythonhosted.org/packages/5e/f3/cb8118a044b5007586245a650360c9f5915b2f4232dd7658bb7a63dd1d02/numpy-2.1.3-pp310-pypy310_pp73-macosx_14_0_x86_64.whl", hash = "sha256:3522b0dfe983a575e6a9ab3a4a4dfe156c3e428468ff08ce582b9bb6bd1d71d4", size = 6752501 }, + { url = "https://files.pythonhosted.org/packages/53/f5/365b46439b518d2ec6ebb880cc0edf90f225145dfd4db7958334f7164530/numpy-2.1.3-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c006b607a865b07cd981ccb218a04fc86b600411d83d6fc261357f1c0966755d", size = 16142601 }, + { url = "https://files.pythonhosted.org/packages/03/c2/d1fee6ba999aa7cd41ca6856937f2baaf604c3eec1565eae63451ec31e5e/numpy-2.1.3-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:e14e26956e6f1696070788252dcdff11b4aca4c3e8bd166e0df1bb8f315a67cb", size = 12771397 }, +] + +[[package]] +name = "orjson" +version = "3.10.18" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/81/0b/fea456a3ffe74e70ba30e01ec183a9b26bec4d497f61dcfce1b601059c60/orjson-3.10.18.tar.gz", hash = "sha256:e8da3947d92123eda795b68228cafe2724815621fe35e8e320a9e9593a4bcd53", size = 5422810 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/27/16/2ceb9fb7bc2b11b1e4a3ea27794256e93dee2309ebe297fd131a778cd150/orjson-3.10.18-cp310-cp310-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:a45e5d68066b408e4bc383b6e4ef05e717c65219a9e1390abc6155a520cac402", size = 248927 }, + { url = "https://files.pythonhosted.org/packages/3d/e1/d3c0a2bba5b9906badd121da449295062b289236c39c3a7801f92c4682b0/orjson-3.10.18-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:be3b9b143e8b9db05368b13b04c84d37544ec85bb97237b3a923f076265ec89c", size = 136995 }, + { url = "https://files.pythonhosted.org/packages/d7/51/698dd65e94f153ee5ecb2586c89702c9e9d12f165a63e74eb9ea1299f4e1/orjson-3.10.18-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9b0aa09745e2c9b3bf779b096fa71d1cc2d801a604ef6dd79c8b1bfef52b2f92", size = 132893 }, + { url = "https://files.pythonhosted.org/packages/b3/e5/155ce5a2c43a85e790fcf8b985400138ce5369f24ee6770378ee6b691036/orjson-3.10.18-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:53a245c104d2792e65c8d225158f2b8262749ffe64bc7755b00024757d957a13", size = 137017 }, + { url = "https://files.pythonhosted.org/packages/46/bb/6141ec3beac3125c0b07375aee01b5124989907d61c72c7636136e4bd03e/orjson-3.10.18-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f9495ab2611b7f8a0a8a505bcb0f0cbdb5469caafe17b0e404c3c746f9900469", size = 138290 }, + { url = "https://files.pythonhosted.org/packages/77/36/6961eca0b66b7809d33c4ca58c6bd4c23a1b914fb23aba2fa2883f791434/orjson-3.10.18-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:73be1cbcebadeabdbc468f82b087df435843c809cd079a565fb16f0f3b23238f", size = 142828 }, + { url = "https://files.pythonhosted.org/packages/8b/2f/0c646d5fd689d3be94f4d83fa9435a6c4322c9b8533edbb3cd4bc8c5f69a/orjson-3.10.18-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fe8936ee2679e38903df158037a2f1c108129dee218975122e37847fb1d4ac68", size = 132806 }, + { url = "https://files.pythonhosted.org/packages/ea/af/65907b40c74ef4c3674ef2bcfa311c695eb934710459841b3c2da212215c/orjson-3.10.18-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:7115fcbc8525c74e4c2b608129bef740198e9a120ae46184dac7683191042056", size = 135005 }, + { url = "https://files.pythonhosted.org/packages/c7/d1/68bd20ac6a32cd1f1b10d23e7cc58ee1e730e80624e3031d77067d7150fc/orjson-3.10.18-cp310-cp310-musllinux_1_2_armv7l.whl", hash = "sha256:771474ad34c66bc4d1c01f645f150048030694ea5b2709b87d3bda273ffe505d", size = 413418 }, + { url = "https://files.pythonhosted.org/packages/31/31/c701ec0bcc3e80e5cb6e319c628ef7b768aaa24b0f3b4c599df2eaacfa24/orjson-3.10.18-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:7c14047dbbea52886dd87169f21939af5d55143dad22d10db6a7514f058156a8", size = 153288 }, + { url = "https://files.pythonhosted.org/packages/d9/31/5e1aa99a10893a43cfc58009f9da840990cc8a9ebb75aa452210ba18587e/orjson-3.10.18-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:641481b73baec8db14fdf58f8967e52dc8bda1f2aba3aa5f5c1b07ed6df50b7f", size = 137181 }, + { url = "https://files.pythonhosted.org/packages/bf/8c/daba0ac1b8690011d9242a0f37235f7d17df6d0ad941021048523b76674e/orjson-3.10.18-cp310-cp310-win32.whl", hash = "sha256:607eb3ae0909d47280c1fc657c4284c34b785bae371d007595633f4b1a2bbe06", size = 142694 }, + { url = "https://files.pythonhosted.org/packages/16/62/8b687724143286b63e1d0fab3ad4214d54566d80b0ba9d67c26aaf28a2f8/orjson-3.10.18-cp310-cp310-win_amd64.whl", hash = "sha256:8770432524ce0eca50b7efc2a9a5f486ee0113a5fbb4231526d414e6254eba92", size = 134600 }, + { url = "https://files.pythonhosted.org/packages/97/c7/c54a948ce9a4278794f669a353551ce7db4ffb656c69a6e1f2264d563e50/orjson-3.10.18-cp311-cp311-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:e0a183ac3b8e40471e8d843105da6fbe7c070faab023be3b08188ee3f85719b8", size = 248929 }, + { url = "https://files.pythonhosted.org/packages/9e/60/a9c674ef1dd8ab22b5b10f9300e7e70444d4e3cda4b8258d6c2488c32143/orjson-3.10.18-cp311-cp311-macosx_15_0_arm64.whl", hash = "sha256:5ef7c164d9174362f85238d0cd4afdeeb89d9e523e4651add6a5d458d6f7d42d", size = 133364 }, + { url = "https://files.pythonhosted.org/packages/c1/4e/f7d1bdd983082216e414e6d7ef897b0c2957f99c545826c06f371d52337e/orjson-3.10.18-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:afd14c5d99cdc7bf93f22b12ec3b294931518aa019e2a147e8aa2f31fd3240f7", size = 136995 }, + { url = "https://files.pythonhosted.org/packages/17/89/46b9181ba0ea251c9243b0c8ce29ff7c9796fa943806a9c8b02592fce8ea/orjson-3.10.18-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7b672502323b6cd133c4af6b79e3bea36bad2d16bca6c1f645903fce83909a7a", size = 132894 }, + { url = "https://files.pythonhosted.org/packages/ca/dd/7bce6fcc5b8c21aef59ba3c67f2166f0a1a9b0317dcca4a9d5bd7934ecfd/orjson-3.10.18-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:51f8c63be6e070ec894c629186b1c0fe798662b8687f3d9fdfa5e401c6bd7679", size = 137016 }, + { url = "https://files.pythonhosted.org/packages/1c/4a/b8aea1c83af805dcd31c1f03c95aabb3e19a016b2a4645dd822c5686e94d/orjson-3.10.18-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3f9478ade5313d724e0495d167083c6f3be0dd2f1c9c8a38db9a9e912cdaf947", size = 138290 }, + { url = "https://files.pythonhosted.org/packages/36/d6/7eb05c85d987b688707f45dcf83c91abc2251e0dd9fb4f7be96514f838b1/orjson-3.10.18-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:187aefa562300a9d382b4b4eb9694806e5848b0cedf52037bb5c228c61bb66d4", size = 142829 }, + { url = "https://files.pythonhosted.org/packages/d2/78/ddd3ee7873f2b5f90f016bc04062713d567435c53ecc8783aab3a4d34915/orjson-3.10.18-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9da552683bc9da222379c7a01779bddd0ad39dd699dd6300abaf43eadee38334", size = 132805 }, + { url = "https://files.pythonhosted.org/packages/8c/09/c8e047f73d2c5d21ead9c180203e111cddeffc0848d5f0f974e346e21c8e/orjson-3.10.18-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:e450885f7b47a0231979d9c49b567ed1c4e9f69240804621be87c40bc9d3cf17", size = 135008 }, + { url = "https://files.pythonhosted.org/packages/0c/4b/dccbf5055ef8fb6eda542ab271955fc1f9bf0b941a058490293f8811122b/orjson-3.10.18-cp311-cp311-musllinux_1_2_armv7l.whl", hash = "sha256:5e3c9cc2ba324187cd06287ca24f65528f16dfc80add48dc99fa6c836bb3137e", size = 413419 }, + { url = "https://files.pythonhosted.org/packages/8a/f3/1eac0c5e2d6d6790bd2025ebfbefcbd37f0d097103d76f9b3f9302af5a17/orjson-3.10.18-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:50ce016233ac4bfd843ac5471e232b865271d7d9d44cf9d33773bcd883ce442b", size = 153292 }, + { url = "https://files.pythonhosted.org/packages/1f/b4/ef0abf64c8f1fabf98791819ab502c2c8c1dc48b786646533a93637d8999/orjson-3.10.18-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:b3ceff74a8f7ffde0b2785ca749fc4e80e4315c0fd887561144059fb1c138aa7", size = 137182 }, + { url = "https://files.pythonhosted.org/packages/a9/a3/6ea878e7b4a0dc5c888d0370d7752dcb23f402747d10e2257478d69b5e63/orjson-3.10.18-cp311-cp311-win32.whl", hash = "sha256:fdba703c722bd868c04702cac4cb8c6b8ff137af2623bc0ddb3b3e6a2c8996c1", size = 142695 }, + { url = "https://files.pythonhosted.org/packages/79/2a/4048700a3233d562f0e90d5572a849baa18ae4e5ce4c3ba6247e4ece57b0/orjson-3.10.18-cp311-cp311-win_amd64.whl", hash = "sha256:c28082933c71ff4bc6ccc82a454a2bffcef6e1d7379756ca567c772e4fb3278a", size = 134603 }, + { url = "https://files.pythonhosted.org/packages/03/45/10d934535a4993d27e1c84f1810e79ccf8b1b7418cef12151a22fe9bb1e1/orjson-3.10.18-cp311-cp311-win_arm64.whl", hash = "sha256:a6c7c391beaedd3fa63206e5c2b7b554196f14debf1ec9deb54b5d279b1b46f5", size = 131400 }, + { url = "https://files.pythonhosted.org/packages/21/1a/67236da0916c1a192d5f4ccbe10ec495367a726996ceb7614eaa687112f2/orjson-3.10.18-cp312-cp312-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:50c15557afb7f6d63bc6d6348e0337a880a04eaa9cd7c9d569bcb4e760a24753", size = 249184 }, + { url = "https://files.pythonhosted.org/packages/b3/bc/c7f1db3b1d094dc0c6c83ed16b161a16c214aaa77f311118a93f647b32dc/orjson-3.10.18-cp312-cp312-macosx_15_0_arm64.whl", hash = "sha256:356b076f1662c9813d5fa56db7d63ccceef4c271b1fb3dd522aca291375fcf17", size = 133279 }, + { url = "https://files.pythonhosted.org/packages/af/84/664657cd14cc11f0d81e80e64766c7ba5c9b7fc1ec304117878cc1b4659c/orjson-3.10.18-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:559eb40a70a7494cd5beab2d73657262a74a2c59aff2068fdba8f0424ec5b39d", size = 136799 }, + { url = "https://files.pythonhosted.org/packages/9a/bb/f50039c5bb05a7ab024ed43ba25d0319e8722a0ac3babb0807e543349978/orjson-3.10.18-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:f3c29eb9a81e2fbc6fd7ddcfba3e101ba92eaff455b8d602bf7511088bbc0eae", size = 132791 }, + { url = "https://files.pythonhosted.org/packages/93/8c/ee74709fc072c3ee219784173ddfe46f699598a1723d9d49cbc78d66df65/orjson-3.10.18-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6612787e5b0756a171c7d81ba245ef63a3533a637c335aa7fcb8e665f4a0966f", size = 137059 }, + { url = "https://files.pythonhosted.org/packages/6a/37/e6d3109ee004296c80426b5a62b47bcadd96a3deab7443e56507823588c5/orjson-3.10.18-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7ac6bd7be0dcab5b702c9d43d25e70eb456dfd2e119d512447468f6405b4a69c", size = 138359 }, + { url = "https://files.pythonhosted.org/packages/4f/5d/387dafae0e4691857c62bd02839a3bf3fa648eebd26185adfac58d09f207/orjson-3.10.18-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:9f72f100cee8dde70100406d5c1abba515a7df926d4ed81e20a9730c062fe9ad", size = 142853 }, + { url = "https://files.pythonhosted.org/packages/27/6f/875e8e282105350b9a5341c0222a13419758545ae32ad6e0fcf5f64d76aa/orjson-3.10.18-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9dca85398d6d093dd41dc0983cbf54ab8e6afd1c547b6b8a311643917fbf4e0c", size = 133131 }, + { url = "https://files.pythonhosted.org/packages/48/b2/73a1f0b4790dcb1e5a45f058f4f5dcadc8a85d90137b50d6bbc6afd0ae50/orjson-3.10.18-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:22748de2a07fcc8781a70edb887abf801bb6142e6236123ff93d12d92db3d406", size = 134834 }, + { url = "https://files.pythonhosted.org/packages/56/f5/7ed133a5525add9c14dbdf17d011dd82206ca6840811d32ac52a35935d19/orjson-3.10.18-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:3a83c9954a4107b9acd10291b7f12a6b29e35e8d43a414799906ea10e75438e6", size = 413368 }, + { url = "https://files.pythonhosted.org/packages/11/7c/439654221ed9c3324bbac7bdf94cf06a971206b7b62327f11a52544e4982/orjson-3.10.18-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:303565c67a6c7b1f194c94632a4a39918e067bd6176a48bec697393865ce4f06", size = 153359 }, + { url = "https://files.pythonhosted.org/packages/48/e7/d58074fa0cc9dd29a8fa2a6c8d5deebdfd82c6cfef72b0e4277c4017563a/orjson-3.10.18-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:86314fdb5053a2f5a5d881f03fca0219bfdf832912aa88d18676a5175c6916b5", size = 137466 }, + { url = "https://files.pythonhosted.org/packages/57/4d/fe17581cf81fb70dfcef44e966aa4003360e4194d15a3f38cbffe873333a/orjson-3.10.18-cp312-cp312-win32.whl", hash = "sha256:187ec33bbec58c76dbd4066340067d9ece6e10067bb0cc074a21ae3300caa84e", size = 142683 }, + { url = "https://files.pythonhosted.org/packages/e6/22/469f62d25ab5f0f3aee256ea732e72dc3aab6d73bac777bd6277955bceef/orjson-3.10.18-cp312-cp312-win_amd64.whl", hash = "sha256:f9f94cf6d3f9cd720d641f8399e390e7411487e493962213390d1ae45c7814fc", size = 134754 }, + { url = "https://files.pythonhosted.org/packages/10/b0/1040c447fac5b91bc1e9c004b69ee50abb0c1ffd0d24406e1350c58a7fcb/orjson-3.10.18-cp312-cp312-win_arm64.whl", hash = "sha256:3d600be83fe4514944500fa8c2a0a77099025ec6482e8087d7659e891f23058a", size = 131218 }, + { url = "https://files.pythonhosted.org/packages/04/f0/8aedb6574b68096f3be8f74c0b56d36fd94bcf47e6c7ed47a7bd1474aaa8/orjson-3.10.18-cp313-cp313-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:69c34b9441b863175cc6a01f2935de994025e773f814412030f269da4f7be147", size = 249087 }, + { url = "https://files.pythonhosted.org/packages/bc/f7/7118f965541aeac6844fcb18d6988e111ac0d349c9b80cda53583e758908/orjson-3.10.18-cp313-cp313-macosx_15_0_arm64.whl", hash = "sha256:1ebeda919725f9dbdb269f59bc94f861afbe2a27dce5608cdba2d92772364d1c", size = 133273 }, + { url = "https://files.pythonhosted.org/packages/fb/d9/839637cc06eaf528dd8127b36004247bf56e064501f68df9ee6fd56a88ee/orjson-3.10.18-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5adf5f4eed520a4959d29ea80192fa626ab9a20b2ea13f8f6dc58644f6927103", size = 136779 }, + { url = "https://files.pythonhosted.org/packages/2b/6d/f226ecfef31a1f0e7d6bf9a31a0bbaf384c7cbe3fce49cc9c2acc51f902a/orjson-3.10.18-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7592bb48a214e18cd670974f289520f12b7aed1fa0b2e2616b8ed9e069e08595", size = 132811 }, + { url = "https://files.pythonhosted.org/packages/73/2d/371513d04143c85b681cf8f3bce743656eb5b640cb1f461dad750ac4b4d4/orjson-3.10.18-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f872bef9f042734110642b7a11937440797ace8c87527de25e0c53558b579ccc", size = 137018 }, + { url = "https://files.pythonhosted.org/packages/69/cb/a4d37a30507b7a59bdc484e4a3253c8141bf756d4e13fcc1da760a0b00cb/orjson-3.10.18-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:0315317601149c244cb3ecef246ef5861a64824ccbcb8018d32c66a60a84ffbc", size = 138368 }, + { url = "https://files.pythonhosted.org/packages/1e/ae/cd10883c48d912d216d541eb3db8b2433415fde67f620afe6f311f5cd2ca/orjson-3.10.18-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e0da26957e77e9e55a6c2ce2e7182a36a6f6b180ab7189315cb0995ec362e049", size = 142840 }, + { url = "https://files.pythonhosted.org/packages/6d/4c/2bda09855c6b5f2c055034c9eda1529967b042ff8d81a05005115c4e6772/orjson-3.10.18-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bb70d489bc79b7519e5803e2cc4c72343c9dc1154258adf2f8925d0b60da7c58", size = 133135 }, + { url = "https://files.pythonhosted.org/packages/13/4a/35971fd809a8896731930a80dfff0b8ff48eeb5d8b57bb4d0d525160017f/orjson-3.10.18-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:e9e86a6af31b92299b00736c89caf63816f70a4001e750bda179e15564d7a034", size = 134810 }, + { url = "https://files.pythonhosted.org/packages/99/70/0fa9e6310cda98365629182486ff37a1c6578e34c33992df271a476ea1cd/orjson-3.10.18-cp313-cp313-musllinux_1_2_armv7l.whl", hash = "sha256:c382a5c0b5931a5fc5405053d36c1ce3fd561694738626c77ae0b1dfc0242ca1", size = 413491 }, + { url = "https://files.pythonhosted.org/packages/32/cb/990a0e88498babddb74fb97855ae4fbd22a82960e9b06eab5775cac435da/orjson-3.10.18-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:8e4b2ae732431127171b875cb2668f883e1234711d3c147ffd69fe5be51a8012", size = 153277 }, + { url = "https://files.pythonhosted.org/packages/92/44/473248c3305bf782a384ed50dd8bc2d3cde1543d107138fd99b707480ca1/orjson-3.10.18-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:2d808e34ddb24fc29a4d4041dcfafbae13e129c93509b847b14432717d94b44f", size = 137367 }, + { url = "https://files.pythonhosted.org/packages/ad/fd/7f1d3edd4ffcd944a6a40e9f88af2197b619c931ac4d3cfba4798d4d3815/orjson-3.10.18-cp313-cp313-win32.whl", hash = "sha256:ad8eacbb5d904d5591f27dee4031e2c1db43d559edb8f91778efd642d70e6bea", size = 142687 }, + { url = "https://files.pythonhosted.org/packages/4b/03/c75c6ad46be41c16f4cfe0352a2d1450546f3c09ad2c9d341110cd87b025/orjson-3.10.18-cp313-cp313-win_amd64.whl", hash = "sha256:aed411bcb68bf62e85588f2a7e03a6082cc42e5a2796e06e72a962d7c6310b52", size = 134794 }, + { url = "https://files.pythonhosted.org/packages/c2/28/f53038a5a72cc4fd0b56c1eafb4ef64aec9685460d5ac34de98ca78b6e29/orjson-3.10.18-cp313-cp313-win_arm64.whl", hash = "sha256:f54c1385a0e6aba2f15a40d703b858bedad36ded0491e55d35d905b2c34a4cc3", size = 131186 }, +] + +[[package]] +name = "outcome" +version = "1.3.0.post0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "attrs" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/98/df/77698abfac98571e65ffeb0c1fba8ffd692ab8458d617a0eed7d9a8d38f2/outcome-1.3.0.post0.tar.gz", hash = "sha256:9dcf02e65f2971b80047b377468e72a268e15c0af3cf1238e6ff14f7f91143b8", size = 21060 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/55/8b/5ab7257531a5d830fc8000c476e63c935488d74609b50f9384a643ec0a62/outcome-1.3.0.post0-py2.py3-none-any.whl", hash = "sha256:e771c5ce06d1415e356078d3bdd68523f284b4ce5419828922b6871e65eda82b", size = 10692 }, +] + +[[package]] +name = "packaging" +version = "25.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a1/d4/1fc4078c65507b51b96ca8f8c3ba19e6a61c8253c72794544580a7b6c24d/packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f", size = 165727 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469 }, +] + +[[package]] +name = "pathspec" +version = "0.12.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ca/bc/f35b8446f4531a7cb215605d100cd88b7ac6f44ab3fc94870c120ab3adbf/pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712", size = 51043 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/cc/20/ff623b09d963f88bfde16306a54e12ee5ea43e9b597108672ff3a408aad6/pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08", size = 31191 }, +] + +[[package]] +name = "platformdirs" +version = "4.3.7" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/b6/2d/7d512a3913d60623e7eb945c6d1b4f0bddf1d0b7ada5225274c87e5b53d1/platformdirs-4.3.7.tar.gz", hash = "sha256:eb437d586b6a0986388f0d6f74aa0cde27b48d0e3d66843640bfb6bdcdb6e351", size = 21291 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/6d/45/59578566b3275b8fd9157885918fcd0c4d74162928a5310926887b856a51/platformdirs-4.3.7-py3-none-any.whl", hash = "sha256:a03875334331946f13c549dbd8f4bac7a13a50a895a0eb1e8c6a8ace80d40a94", size = 18499 }, +] + +[[package]] +name = "pluggy" +version = "1.5.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/96/2d/02d4312c973c6050a18b314a5ad0b3210edb65a906f868e31c111dede4a6/pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1", size = 67955 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/88/5f/e351af9a41f866ac3f1fac4ca0613908d9a41741cfcf2228f4ad853b697d/pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669", size = 20556 }, +] + +[[package]] +name = "pycparser" +version = "2.22" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/1d/b2/31537cf4b1ca988837256c910a668b553fceb8f069bedc4b1c826024b52c/pycparser-2.22.tar.gz", hash = "sha256:491c8be9c040f5390f5bf44a5b07752bd07f56edf992381b05c701439eec10f6", size = 172736 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/13/a3/a812df4e2dd5696d1f351d58b8fe16a405b234ad2886a0dab9183fb78109/pycparser-2.22-py3-none-any.whl", hash = "sha256:c3702b6d3dd8c7abc1afa565d7e63d53a1d0bd86cdc24edd75470f4de499cfcc", size = 117552 }, +] + +[[package]] +name = "pygame" +version = "2.6.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/49/cc/08bba60f00541f62aaa252ce0cfbd60aebd04616c0b9574f755b583e45ae/pygame-2.6.1.tar.gz", hash = "sha256:56fb02ead529cee00d415c3e007f75e0780c655909aaa8e8bf616ee09c9feb1f", size = 14808125 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/54/0b/334c7c50a2979e15f2a027a41d1ca78ee730d5b1c7f7f4b26d7cb899839d/pygame-2.6.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:9beeb647e555afb5657111fa83acb74b99ad88761108eaea66472e8b8547b55b", size = 13109297 }, + { url = "https://files.pythonhosted.org/packages/dc/48/f8b1069788d1bd42e63a960d74d3355242480b750173a42b2749687578ca/pygame-2.6.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:10e3d2a55f001f6c0a6eb44aa79ea7607091c9352b946692acedb2ac1482f1c9", size = 12375837 }, + { url = "https://files.pythonhosted.org/packages/bc/33/a1310386b8913ce1bdb90c33fa536970e299ad57eb35785f1d71ea1e2ad3/pygame-2.6.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:816e85000c5d8b02a42b9834f761a5925ef3377d2924e3a7c4c143d2990ce5b8", size = 13607860 }, + { url = "https://files.pythonhosted.org/packages/88/0f/4e37b115056e43714e7550054dd3cd7f4d552da54d7fc58a2fb1407acda5/pygame-2.6.1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:8a78fd030d98faab4a8e27878536fdff7518d3e062a72761c552f624ebba5a5f", size = 14304696 }, + { url = "https://files.pythonhosted.org/packages/11/b3/de6ed93ae483cf3bac8f950a955e83f7ffe59651fd804d100fff65d66d6c/pygame-2.6.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:da3ad64d685f84a34ebe5daacb39fff14f1251acb34c098d760d63fee768f50c", size = 13977684 }, + { url = "https://files.pythonhosted.org/packages/d3/05/d86440aa879708c41844bafc6b3eb42c6d8cf54082482499b53139133e2a/pygame-2.6.1-cp310-cp310-win32.whl", hash = "sha256:9dd5c054d4bd875a8caf978b82672f02bec332f52a833a76899220c460bb4b58", size = 10251775 }, + { url = "https://files.pythonhosted.org/packages/38/88/8de61324775cf2c844a51d8db14a8a6d2a9092312f27678f6eaa3a460376/pygame-2.6.1-cp310-cp310-win_amd64.whl", hash = "sha256:00827aba089355925902d533f9c41e79a799641f03746c50a374dc5c3362e43d", size = 10618801 }, + { url = "https://files.pythonhosted.org/packages/c4/ca/8f367cb9fe734c4f6f6400e045593beea2635cd736158f9fabf58ee14e3c/pygame-2.6.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:20349195326a5e82a16e351ed93465a7845a7e2a9af55b7bc1b2110ea3e344e1", size = 13113753 }, + { url = "https://files.pythonhosted.org/packages/83/47/6edf2f890139616b3219be9cfcc8f0cb8f42eb15efd59597927e390538cb/pygame-2.6.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:f3935459109da4bb0b3901da9904f0a3e52028a3332a355d298b1673a334cf21", size = 12378146 }, + { url = "https://files.pythonhosted.org/packages/00/9e/0d8aa8cf93db2d2ee38ebaf1c7b61d0df36ded27eb726221719c150c673d/pygame-2.6.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c31dbdb5d0217f32764797d21c2752e258e5fb7e895326538d82b5f75a0cd856", size = 13611760 }, + { url = "https://files.pythonhosted.org/packages/d7/9e/d06adaa5cc65876bcd7a24f59f67e07f7e4194e6298130024ed3fb22c456/pygame-2.6.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:173badf82fa198e6888017bea40f511cb28e69ecdd5a72b214e81e4dcd66c3b1", size = 14298054 }, + { url = "https://files.pythonhosted.org/packages/7a/a1/9ae2852ebd3a7cc7d9ae7ff7919ab983e4a5c1b7a14e840732f23b2b48f6/pygame-2.6.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ce8cc108b92de9b149b344ad2e25eedbe773af0dc41dfb24d1f07f679b558c60", size = 13977107 }, + { url = "https://files.pythonhosted.org/packages/31/df/6788fd2e9a864d0496a77670e44a7c012184b7a5382866ab0e60c55c0f28/pygame-2.6.1-cp311-cp311-win32.whl", hash = "sha256:811e7b925146d8149d79193652cbb83e0eca0aae66476b1cb310f0f4226b8b5c", size = 10250863 }, + { url = "https://files.pythonhosted.org/packages/d2/55/ca3eb851aeef4f6f2e98a360c201f0d00bd1ba2eb98e2c7850d80aabc526/pygame-2.6.1-cp311-cp311-win_amd64.whl", hash = "sha256:91476902426facd4bb0dad4dc3b2573bc82c95c71b135e0daaea072ed528d299", size = 10622016 }, + { url = "https://files.pythonhosted.org/packages/92/16/2c602c332f45ff9526d61f6bd764db5096ff9035433e2172e2d2cadae8db/pygame-2.6.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:4ee7f2771f588c966fa2fa8b829be26698c9b4836f82ede5e4edc1a68594942e", size = 13118279 }, + { url = "https://files.pythonhosted.org/packages/cd/53/77ccbc384b251c6e34bfd2e734c638233922449a7844e3c7a11ef91cee39/pygame-2.6.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:c8040ea2ab18c6b255af706ec01355c8a6b08dc48d77fd4ee783f8fc46a843bf", size = 12384524 }, + { url = "https://files.pythonhosted.org/packages/06/be/3ed337583f010696c3b3435e89a74fb29d0c74d0931e8f33c0a4246307a9/pygame-2.6.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c47a6938de93fa610accd4969e638c2aebcb29b2fca518a84c3a39d91ab47116", size = 13587123 }, + { url = "https://files.pythonhosted.org/packages/fd/ca/b015586a450db59313535662991b34d24c1f0c0dc149cc5f496573900f4e/pygame-2.6.1-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:33006f784e1c7d7e466fcb61d5489da59cc5f7eb098712f792a225df1d4e229d", size = 14275532 }, + { url = "https://files.pythonhosted.org/packages/b9/f2/d31e6ad42d657af07be2ffd779190353f759a07b51232b9e1d724f2cda46/pygame-2.6.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1206125f14cae22c44565c9d333607f1d9f59487b1f1432945dfc809aeaa3e88", size = 13952653 }, + { url = "https://files.pythonhosted.org/packages/f3/42/8ea2a6979e6fa971702fece1747e862e2256d4a8558fe0da6364dd946c53/pygame-2.6.1-cp312-cp312-win32.whl", hash = "sha256:84fc4054e25262140d09d39e094f6880d730199710829902f0d8ceae0213379e", size = 10252421 }, + { url = "https://files.pythonhosted.org/packages/5f/90/7d766d54bb95939725e9a9361f9c06b0cfbe3fe100aa35400f0a461a278a/pygame-2.6.1-cp312-cp312-win_amd64.whl", hash = "sha256:3a9e7396be0d9633831c3f8d5d82dd63ba373ad65599628294b7a4f8a5a01a65", size = 10624591 }, + { url = "https://files.pythonhosted.org/packages/e1/91/718acf3e2a9d08a6ddcc96bd02a6f63c99ee7ba14afeaff2a51c987df0b9/pygame-2.6.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:ae6039f3a55d800db80e8010f387557b528d34d534435e0871326804df2a62f2", size = 13090765 }, + { url = "https://files.pythonhosted.org/packages/0e/c6/9cb315de851a7682d9c7568a41ea042ee98d668cb8deadc1dafcab6116f0/pygame-2.6.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:2a3a1288e2e9b1e5834e425bedd5ba01a3cd4902b5c2bff8ed4a740ccfe98171", size = 12381704 }, + { url = "https://files.pythonhosted.org/packages/9f/8f/617a1196e31ae3b46be6949fbaa95b8c93ce15e0544266198c2266cc1b4d/pygame-2.6.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:27eb17e3dc9640e4b4683074f1890e2e879827447770470c2aba9f125f74510b", size = 13581091 }, + { url = "https://files.pythonhosted.org/packages/3b/87/2851a564e40a2dad353f1c6e143465d445dab18a95281f9ea458b94f3608/pygame-2.6.1-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4c1623180e70a03c4a734deb9bac50fc9c82942ae84a3a220779062128e75f3b", size = 14273844 }, + { url = "https://files.pythonhosted.org/packages/85/b5/aa23aa2e70bcba42c989c02e7228273c30f3b44b9b264abb93eaeff43ad7/pygame-2.6.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef07c0103d79492c21fced9ad68c11c32efa6801ca1920ebfd0f15fb46c78b1c", size = 13951197 }, + { url = "https://files.pythonhosted.org/packages/a6/06/29e939b34d3f1354738c7d201c51c250ad7abefefaf6f8332d962ff67c4b/pygame-2.6.1-cp313-cp313-win32.whl", hash = "sha256:3acd8c009317190c2bfd81db681ecef47d5eb108c2151d09596d9c7ea9df5c0e", size = 10249309 }, + { url = "https://files.pythonhosted.org/packages/7e/11/17f7f319ca91824b86557e9303e3b7a71991ef17fd45286bf47d7f0a38e6/pygame-2.6.1-cp313-cp313-win_amd64.whl", hash = "sha256:813af4fba5d0b2cb8e58f5d95f7910295c34067dcc290d34f1be59c48bd1ea6a", size = 10620084 }, +] + +[[package]] +name = "pytest" +version = "8.3.5" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "colorama", marker = "sys_platform == 'win32'" }, + { name = "exceptiongroup", marker = "python_full_version < '3.11'" }, + { name = "iniconfig" }, + { name = "packaging" }, + { name = "pluggy" }, + { name = "tomli", marker = "python_full_version < '3.11'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/ae/3c/c9d525a414d506893f0cd8a8d0de7706446213181570cdbd766691164e40/pytest-8.3.5.tar.gz", hash = "sha256:f4efe70cc14e511565ac476b57c279e12a855b11f48f212af1080ef2263d3845", size = 1450891 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/30/3d/64ad57c803f1fa1e963a7946b6e0fea4a70df53c1a7fed304586539c2bac/pytest-8.3.5-py3-none-any.whl", hash = "sha256:c69214aa47deac29fad6c2a4f590b9c4a9fdb16a403176fe154b79c0b4d4d820", size = 343634 }, +] + +[[package]] +name = "pytest-cov" +version = "6.1.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "coverage", extra = ["toml"] }, + { name = "pytest" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/25/69/5f1e57f6c5a39f81411b550027bf72842c4567ff5fd572bed1edc9e4b5d9/pytest_cov-6.1.1.tar.gz", hash = "sha256:46935f7aaefba760e716c2ebfbe1c216240b9592966e7da99ea8292d4d3e2a0a", size = 66857 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/28/d0/def53b4a790cfb21483016430ed828f64830dd981ebe1089971cd10cab25/pytest_cov-6.1.1-py3-none-any.whl", hash = "sha256:bddf29ed2d0ab6f4df17b4c55b0a657287db8684af9c42ea546b21b1041b3dde", size = 23841 }, +] + +[[package]] +name = "pytest-trio" +version = "0.8.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "outcome" }, + { name = "pytest" }, + { name = "trio" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/13/08/056279526554c6c6e6ad6d4a479a338d14dc785ac30be8bdc6ca0153c1be/pytest-trio-0.8.0.tar.gz", hash = "sha256:8363db6336a79e6c53375a2123a41ddbeccc4aa93f93788651641789a56fb52e", size = 46525 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e9/22/71953f47e0da5852c899f58cd7a31e6100f37c632b7b9ee52d067613a844/pytest_trio-0.8.0-py3-none-any.whl", hash = "sha256:e6a7e7351ae3e8ec3f4564d30ee77d1ec66e1df611226e5618dbb32f9545c841", size = 27221 }, +] + +[[package]] +name = "ruff" +version = "0.11.8" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/52/f6/adcf73711f31c9f5393862b4281c875a462d9f639f4ccdf69dc368311c20/ruff-0.11.8.tar.gz", hash = "sha256:6d742d10626f9004b781f4558154bb226620a7242080e11caeffab1a40e99df8", size = 4086399 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/9f/60/c6aa9062fa518a9f86cb0b85248245cddcd892a125ca00441df77d79ef88/ruff-0.11.8-py3-none-linux_armv6l.whl", hash = "sha256:896a37516c594805e34020c4a7546c8f8a234b679a7716a3f08197f38913e1a3", size = 10272473 }, + { url = "https://files.pythonhosted.org/packages/a0/e4/0325e50d106dc87c00695f7bcd5044c6d252ed5120ebf423773e00270f50/ruff-0.11.8-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:ab86d22d3d721a40dd3ecbb5e86ab03b2e053bc93c700dc68d1c3346b36ce835", size = 11040862 }, + { url = "https://files.pythonhosted.org/packages/e6/27/b87ea1a7be37fef0adbc7fd987abbf90b6607d96aa3fc67e2c5b858e1e53/ruff-0.11.8-py3-none-macosx_11_0_arm64.whl", hash = "sha256:258f3585057508d317610e8a412788cf726efeefa2fec4dba4001d9e6f90d46c", size = 10385273 }, + { url = "https://files.pythonhosted.org/packages/d3/f7/3346161570d789045ed47a86110183f6ac3af0e94e7fd682772d89f7f1a1/ruff-0.11.8-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:727d01702f7c30baed3fc3a34901a640001a2828c793525043c29f7614994a8c", size = 10578330 }, + { url = "https://files.pythonhosted.org/packages/c6/c3/327fb950b4763c7b3784f91d3038ef10c13b2d42322d4ade5ce13a2f9edb/ruff-0.11.8-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:3dca977cc4fc8f66e89900fa415ffe4dbc2e969da9d7a54bfca81a128c5ac219", size = 10122223 }, + { url = "https://files.pythonhosted.org/packages/de/c7/ba686bce9adfeb6c61cb1bbadc17d58110fe1d602f199d79d4c880170f19/ruff-0.11.8-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c657fa987d60b104d2be8b052d66da0a2a88f9bd1d66b2254333e84ea2720c7f", size = 11697353 }, + { url = "https://files.pythonhosted.org/packages/53/8e/a4fb4a1ddde3c59e73996bb3ac51844ff93384d533629434b1def7a336b0/ruff-0.11.8-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:f2e74b021d0de5eceb8bd32919f6ff8a9b40ee62ed97becd44993ae5b9949474", size = 12375936 }, + { url = "https://files.pythonhosted.org/packages/ad/a1/9529cb1e2936e2479a51aeb011307e7229225df9ac64ae064d91ead54571/ruff-0.11.8-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f9b5ef39820abc0f2c62111f7045009e46b275f5b99d5e59dda113c39b7f4f38", size = 11850083 }, + { url = "https://files.pythonhosted.org/packages/3e/94/8f7eac4c612673ae15a4ad2bc0ee62e03c68a2d4f458daae3de0e47c67ba/ruff-0.11.8-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c1dba3135ca503727aa4648152c0fa67c3b1385d3dc81c75cd8a229c4b2a1458", size = 14005834 }, + { url = "https://files.pythonhosted.org/packages/1e/7c/6f63b46b2be870cbf3f54c9c4154d13fac4b8827f22fa05ac835c10835b2/ruff-0.11.8-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7f024d32e62faad0f76b2d6afd141b8c171515e4fb91ce9fd6464335c81244e5", size = 11503713 }, + { url = "https://files.pythonhosted.org/packages/3a/91/57de411b544b5fe072779678986a021d87c3ee5b89551f2ca41200c5d643/ruff-0.11.8-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:d365618d3ad747432e1ae50d61775b78c055fee5936d77fb4d92c6f559741948", size = 10457182 }, + { url = "https://files.pythonhosted.org/packages/01/49/cfe73e0ce5ecdd3e6f1137bf1f1be03dcc819d1bfe5cff33deb40c5926db/ruff-0.11.8-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:4d9aaa91035bdf612c8ee7266153bcf16005c7c7e2f5878406911c92a31633cb", size = 10101027 }, + { url = "https://files.pythonhosted.org/packages/56/21/a5cfe47c62b3531675795f38a0ef1c52ff8de62eaddf370d46634391a3fb/ruff-0.11.8-py3-none-musllinux_1_2_i686.whl", hash = "sha256:0eba551324733efc76116d9f3a0d52946bc2751f0cd30661564117d6fd60897c", size = 11111298 }, + { url = "https://files.pythonhosted.org/packages/36/98/f76225f87e88f7cb669ae92c062b11c0a1e91f32705f829bd426f8e48b7b/ruff-0.11.8-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:161eb4cff5cfefdb6c9b8b3671d09f7def2f960cee33481dd898caf2bcd02304", size = 11566884 }, + { url = "https://files.pythonhosted.org/packages/de/7e/fff70b02e57852fda17bd43f99dda37b9bcf3e1af3d97c5834ff48d04715/ruff-0.11.8-py3-none-win32.whl", hash = "sha256:5b18caa297a786465cc511d7f8be19226acf9c0a1127e06e736cd4e1878c3ea2", size = 10451102 }, + { url = "https://files.pythonhosted.org/packages/7b/a9/eaa571eb70648c9bde3120a1d5892597de57766e376b831b06e7c1e43945/ruff-0.11.8-py3-none-win_amd64.whl", hash = "sha256:6e70d11043bef637c5617297bdedec9632af15d53ac1e1ba29c448da9341b0c4", size = 11597410 }, + { url = "https://files.pythonhosted.org/packages/cd/be/f6b790d6ae98f1f32c645f8540d5c96248b72343b0a56fab3a07f2941897/ruff-0.11.8-py3-none-win_arm64.whl", hash = "sha256:304432e4c4a792e3da85b7699feb3426a0908ab98bf29df22a31b0cdd098fac2", size = 10713129 }, +] + +[[package]] +name = "sniffio" +version = "1.3.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a2/87/a6771e1546d97e7e041b6ae58d80074f81b7d5121207425c964ddf5cfdbd/sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc", size = 20372 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235 }, +] + +[[package]] +name = "sortedcontainers" +version = "2.4.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/e8/c4/ba2f8066cceb6f23394729afe52f3bf7adec04bf9ed2c820b39e19299111/sortedcontainers-2.4.0.tar.gz", hash = "sha256:25caa5a06cc30b6b83d11423433f65d1f9d76c4c6a0c90e3379eaa43b9bfdb88", size = 30594 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/32/46/9cb0e58b2deb7f82b84065f37f3bffeb12413f947f9388e4cac22c4621ce/sortedcontainers-2.4.0-py2.py3-none-any.whl", hash = "sha256:a163dcaede0f1c021485e957a39245190e74249897e2ae4b2aa38595db237ee0", size = 29575 }, +] + +[[package]] +name = "tomli" +version = "2.2.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/18/87/302344fed471e44a87289cf4967697d07e532f2421fdaf868a303cbae4ff/tomli-2.2.1.tar.gz", hash = "sha256:cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff", size = 17175 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/43/ca/75707e6efa2b37c77dadb324ae7d9571cb424e61ea73fad7c56c2d14527f/tomli-2.2.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:678e4fa69e4575eb77d103de3df8a895e1591b48e740211bd1067378c69e8249", size = 131077 }, + { url = "https://files.pythonhosted.org/packages/c7/16/51ae563a8615d472fdbffc43a3f3d46588c264ac4f024f63f01283becfbb/tomli-2.2.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:023aa114dd824ade0100497eb2318602af309e5a55595f76b626d6d9f3b7b0a6", size = 123429 }, + { url = "https://files.pythonhosted.org/packages/f1/dd/4f6cd1e7b160041db83c694abc78e100473c15d54620083dbd5aae7b990e/tomli-2.2.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ece47d672db52ac607a3d9599a9d48dcb2f2f735c6c2d1f34130085bb12b112a", size = 226067 }, + { url = "https://files.pythonhosted.org/packages/a9/6b/c54ede5dc70d648cc6361eaf429304b02f2871a345bbdd51e993d6cdf550/tomli-2.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6972ca9c9cc9f0acaa56a8ca1ff51e7af152a9f87fb64623e31d5c83700080ee", size = 236030 }, + { url = "https://files.pythonhosted.org/packages/1f/47/999514fa49cfaf7a92c805a86c3c43f4215621855d151b61c602abb38091/tomli-2.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c954d2250168d28797dd4e3ac5cf812a406cd5a92674ee4c8f123c889786aa8e", size = 240898 }, + { url = "https://files.pythonhosted.org/packages/73/41/0a01279a7ae09ee1573b423318e7934674ce06eb33f50936655071d81a24/tomli-2.2.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:8dd28b3e155b80f4d54beb40a441d366adcfe740969820caf156c019fb5c7ec4", size = 229894 }, + { url = "https://files.pythonhosted.org/packages/55/18/5d8bc5b0a0362311ce4d18830a5d28943667599a60d20118074ea1b01bb7/tomli-2.2.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:e59e304978767a54663af13c07b3d1af22ddee3bb2fb0618ca1593e4f593a106", size = 245319 }, + { url = "https://files.pythonhosted.org/packages/92/a3/7ade0576d17f3cdf5ff44d61390d4b3febb8a9fc2b480c75c47ea048c646/tomli-2.2.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:33580bccab0338d00994d7f16f4c4ec25b776af3ffaac1ed74e0b3fc95e885a8", size = 238273 }, + { url = "https://files.pythonhosted.org/packages/72/6f/fa64ef058ac1446a1e51110c375339b3ec6be245af9d14c87c4a6412dd32/tomli-2.2.1-cp311-cp311-win32.whl", hash = "sha256:465af0e0875402f1d226519c9904f37254b3045fc5084697cefb9bdde1ff99ff", size = 98310 }, + { url = "https://files.pythonhosted.org/packages/6a/1c/4a2dcde4a51b81be3530565e92eda625d94dafb46dbeb15069df4caffc34/tomli-2.2.1-cp311-cp311-win_amd64.whl", hash = "sha256:2d0f2fdd22b02c6d81637a3c95f8cd77f995846af7414c5c4b8d0545afa1bc4b", size = 108309 }, + { url = "https://files.pythonhosted.org/packages/52/e1/f8af4c2fcde17500422858155aeb0d7e93477a0d59a98e56cbfe75070fd0/tomli-2.2.1-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:4a8f6e44de52d5e6c657c9fe83b562f5f4256d8ebbfe4ff922c495620a7f6cea", size = 132762 }, + { url = "https://files.pythonhosted.org/packages/03/b8/152c68bb84fc00396b83e7bbddd5ec0bd3dd409db4195e2a9b3e398ad2e3/tomli-2.2.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:8d57ca8095a641b8237d5b079147646153d22552f1c637fd3ba7f4b0b29167a8", size = 123453 }, + { url = "https://files.pythonhosted.org/packages/c8/d6/fc9267af9166f79ac528ff7e8c55c8181ded34eb4b0e93daa767b8841573/tomli-2.2.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4e340144ad7ae1533cb897d406382b4b6fede8890a03738ff1683af800d54192", size = 233486 }, + { url = "https://files.pythonhosted.org/packages/5c/51/51c3f2884d7bab89af25f678447ea7d297b53b5a3b5730a7cb2ef6069f07/tomli-2.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:db2b95f9de79181805df90bedc5a5ab4c165e6ec3fe99f970d0e302f384ad222", size = 242349 }, + { url = "https://files.pythonhosted.org/packages/ab/df/bfa89627d13a5cc22402e441e8a931ef2108403db390ff3345c05253935e/tomli-2.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:40741994320b232529c802f8bc86da4e1aa9f413db394617b9a256ae0f9a7f77", size = 252159 }, + { url = "https://files.pythonhosted.org/packages/9e/6e/fa2b916dced65763a5168c6ccb91066f7639bdc88b48adda990db10c8c0b/tomli-2.2.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:400e720fe168c0f8521520190686ef8ef033fb19fc493da09779e592861b78c6", size = 237243 }, + { url = "https://files.pythonhosted.org/packages/b4/04/885d3b1f650e1153cbb93a6a9782c58a972b94ea4483ae4ac5cedd5e4a09/tomli-2.2.1-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:02abe224de6ae62c19f090f68da4e27b10af2b93213d36cf44e6e1c5abd19fdd", size = 259645 }, + { url = "https://files.pythonhosted.org/packages/9c/de/6b432d66e986e501586da298e28ebeefd3edc2c780f3ad73d22566034239/tomli-2.2.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:b82ebccc8c8a36f2094e969560a1b836758481f3dc360ce9a3277c65f374285e", size = 244584 }, + { url = "https://files.pythonhosted.org/packages/1c/9a/47c0449b98e6e7d1be6cbac02f93dd79003234ddc4aaab6ba07a9a7482e2/tomli-2.2.1-cp312-cp312-win32.whl", hash = "sha256:889f80ef92701b9dbb224e49ec87c645ce5df3fa2cc548664eb8a25e03127a98", size = 98875 }, + { url = "https://files.pythonhosted.org/packages/ef/60/9b9638f081c6f1261e2688bd487625cd1e660d0a85bd469e91d8db969734/tomli-2.2.1-cp312-cp312-win_amd64.whl", hash = "sha256:7fc04e92e1d624a4a63c76474610238576942d6b8950a2d7f908a340494e67e4", size = 109418 }, + { url = "https://files.pythonhosted.org/packages/04/90/2ee5f2e0362cb8a0b6499dc44f4d7d48f8fff06d28ba46e6f1eaa61a1388/tomli-2.2.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f4039b9cbc3048b2416cc57ab3bda989a6fcf9b36cf8937f01a6e731b64f80d7", size = 132708 }, + { url = "https://files.pythonhosted.org/packages/c0/ec/46b4108816de6b385141f082ba99e315501ccd0a2ea23db4a100dd3990ea/tomli-2.2.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:286f0ca2ffeeb5b9bd4fcc8d6c330534323ec51b2f52da063b11c502da16f30c", size = 123582 }, + { url = "https://files.pythonhosted.org/packages/a0/bd/b470466d0137b37b68d24556c38a0cc819e8febe392d5b199dcd7f578365/tomli-2.2.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a92ef1a44547e894e2a17d24e7557a5e85a9e1d0048b0b5e7541f76c5032cb13", size = 232543 }, + { url = "https://files.pythonhosted.org/packages/d9/e5/82e80ff3b751373f7cead2815bcbe2d51c895b3c990686741a8e56ec42ab/tomli-2.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9316dc65bed1684c9a98ee68759ceaed29d229e985297003e494aa825ebb0281", size = 241691 }, + { url = "https://files.pythonhosted.org/packages/05/7e/2a110bc2713557d6a1bfb06af23dd01e7dde52b6ee7dadc589868f9abfac/tomli-2.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e85e99945e688e32d5a35c1ff38ed0b3f41f43fad8df0bdf79f72b2ba7bc5272", size = 251170 }, + { url = "https://files.pythonhosted.org/packages/64/7b/22d713946efe00e0adbcdfd6d1aa119ae03fd0b60ebed51ebb3fa9f5a2e5/tomli-2.2.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:ac065718db92ca818f8d6141b5f66369833d4a80a9d74435a268c52bdfa73140", size = 236530 }, + { url = "https://files.pythonhosted.org/packages/38/31/3a76f67da4b0cf37b742ca76beaf819dca0ebef26d78fc794a576e08accf/tomli-2.2.1-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:d920f33822747519673ee656a4b6ac33e382eca9d331c87770faa3eef562aeb2", size = 258666 }, + { url = "https://files.pythonhosted.org/packages/07/10/5af1293da642aded87e8a988753945d0cf7e00a9452d3911dd3bb354c9e2/tomli-2.2.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:a198f10c4d1b1375d7687bc25294306e551bf1abfa4eace6650070a5c1ae2744", size = 243954 }, + { url = "https://files.pythonhosted.org/packages/5b/b9/1ed31d167be802da0fc95020d04cd27b7d7065cc6fbefdd2f9186f60d7bd/tomli-2.2.1-cp313-cp313-win32.whl", hash = "sha256:d3f5614314d758649ab2ab3a62d4f2004c825922f9e370b29416484086b264ec", size = 98724 }, + { url = "https://files.pythonhosted.org/packages/c7/32/b0963458706accd9afcfeb867c0f9175a741bf7b19cd424230714d722198/tomli-2.2.1-cp313-cp313-win_amd64.whl", hash = "sha256:a38aa0308e754b0e3c67e344754dff64999ff9b513e691d0e786265c93583c69", size = 109383 }, + { url = "https://files.pythonhosted.org/packages/6e/c2/61d3e0f47e2b74ef40a68b9e6ad5984f6241a942f7cd3bbfbdbd03861ea9/tomli-2.2.1-py3-none-any.whl", hash = "sha256:cb55c73c5f4408779d0cf3eef9f762b9c9f147a77de7b258bef0a5628adc85cc", size = 14257 }, +] + +[[package]] +name = "trio" +version = "0.30.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "attrs" }, + { name = "cffi", marker = "implementation_name != 'pypy' and os_name == 'nt'" }, + { name = "exceptiongroup", marker = "python_full_version < '3.11'" }, + { name = "idna" }, + { name = "outcome" }, + { name = "sniffio" }, + { name = "sortedcontainers" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/01/c1/68d582b4d3a1c1f8118e18042464bb12a7c1b75d64d75111b297687041e3/trio-0.30.0.tar.gz", hash = "sha256:0781c857c0c81f8f51e0089929a26b5bb63d57f927728a5586f7e36171f064df", size = 593776 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/69/8e/3f6dfda475ecd940e786defe6df6c500734e686c9cd0a0f8ef6821e9b2f2/trio-0.30.0-py3-none-any.whl", hash = "sha256:3bf4f06b8decf8d3cf00af85f40a89824669e2d033bb32469d34840edcfc22a5", size = 499194 }, +] + +[[package]] +name = "typing-extensions" +version = "4.13.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f6/37/23083fcd6e35492953e8d2aaaa68b860eb422b34627b13f2ce3eb6106061/typing_extensions-4.13.2.tar.gz", hash = "sha256:e6c81219bd689f51865d9e372991c540bda33a0379d5573cddb9a3a23f7caaef", size = 106967 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/8b/54/b1ae86c0973cc6f0210b53d508ca3641fb6d0c56823f288d108bc7ab3cc8/typing_extensions-4.13.2-py3-none-any.whl", hash = "sha256:a439e7c04b49fec3e5d3e2beaa21755cadbbdc391694e28ccdd36ca4a1408f8c", size = 45806 }, +] + +[[package]] +name = "uv" +version = "0.7.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/fd/d4/c1104ee4d8a69e4834888cd850eb4f9327c585e5e60da108fda788d3872d/uv-0.7.2.tar.gz", hash = "sha256:45e619bb076916b79df8c5ecc28d1be04d1ccd0b63b080c44ae973b8deb33b25", size = 3293566 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/22/c3/68291a239dbedc0389fa5ce5b5b6c7c2a54c52bc11e9503276f376faa9e7/uv-0.7.2-py3-none-linux_armv6l.whl", hash = "sha256:e1e4394b54bc387f227ca1b2aa0348d35f6455b6168ca1826c1dc5f4fc3e8d20", size = 16590159 }, + { url = "https://files.pythonhosted.org/packages/6c/ac/3c7e8df1d6bb84a805aa773ea4f6a006682f8241f331c9c359eb5310f042/uv-0.7.2-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:c0edb194c35f1f12c75bec4fe2d7d4d09f0c2cec3a16102217a772620ce1d6e6", size = 16753976 }, + { url = "https://files.pythonhosted.org/packages/42/ca/6a3f3c094794d482e3418f6a46c2753fa4f6ed2fe5b7ecf299db8cfed9ea/uv-0.7.2-py3-none-macosx_11_0_arm64.whl", hash = "sha256:be2e8d033936ba8ed9ccf85eb2d15c7a8db3bb3e9c4960bdf7c3c98034a6dbda", size = 15513631 }, + { url = "https://files.pythonhosted.org/packages/1e/65/6fae29e0eb884fa1cab89b0fa865d409e0e2bcada8316cd50b4c81e8706c/uv-0.7.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl", hash = "sha256:a314a94b42bc6014f18c877f723292306b76c10b455c2b385728e1470e661ced", size = 15972100 }, + { url = "https://files.pythonhosted.org/packages/a6/92/3d8da1efc7f3272ccc65c50cb13abd9e6a32246bb6c258175c68a91d0d80/uv-0.7.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e4d1652fe3608fa564dbeaeb2465208f691ac04b57f655ebef62e9ec6d37103d", size = 16288666 }, + { url = "https://files.pythonhosted.org/packages/2c/5e/7d6a788c45d5e2686d01c4886ebb21149892a59bcfa15b66d0646e73aafa/uv-0.7.2-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:48c115a3c13c3b29748e325093ee04fd48eaf91145bedc68727f78e6a1c34ab8", size = 17165785 }, + { url = "https://files.pythonhosted.org/packages/e4/9e/4d0a947ffa4b377c6e34935c23164c7914d7239154d254aa5938db6a7e83/uv-0.7.2-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:c388172209ca5a47706666d570a45fef3dd39db9258682e10b2f62ca521f0e91", size = 18014800 }, + { url = "https://files.pythonhosted.org/packages/c7/31/781288f9f53e1770128f7830841d7d269097ed70a4afa71578d45721bfa2/uv-0.7.2-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:63c97cc5e8029a8dc0e1fc39f15f746be931345bc0aeae85feceaa1828f0de87", size = 17745484 }, + { url = "https://files.pythonhosted.org/packages/6d/04/030eec46217225b77ccff1f2808e64074873d86fe445be3784649506e65e/uv-0.7.2-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:1fa315366ee36ad1f734734f3153e2f334342900061fc0ed18b06f3b9bb2dfe2", size = 22103174 }, + { url = "https://files.pythonhosted.org/packages/5c/07/9d85d0a9ddd49dbec18bde741ffb33d0c671a153461b094a9c73504e1b92/uv-0.7.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7236ec776c559fbc3ae4389b7cd506a2428ad9dd0402ac3d9446200ea3dc45f6", size = 17369922 }, + { url = "https://files.pythonhosted.org/packages/11/18/cfef0efe3c4ebdd81422f35215bb915fd599fc946b40306186d87e90678b/uv-0.7.2-py3-none-manylinux_2_28_aarch64.whl", hash = "sha256:78ec372b2f5c7ff8a034e16dd04bc579a62561a5eac4b6dfc96af60298a97d31", size = 16209878 }, + { url = "https://files.pythonhosted.org/packages/31/ed/2ddd7547203ddd368b9ec56b245e09931f868daf2d2b0e29c0b69584466d/uv-0.7.2-py3-none-musllinux_1_1_armv7l.whl", hash = "sha256:28fd5d689ae4f8f16533f091a6dd63e1ddf3b7c782003ac8a18584ddb8823cbe", size = 16271878 }, + { url = "https://files.pythonhosted.org/packages/f0/9c/30a48a9d875b91b486286d1a4ccc081dad130acea0dca683c1786ddd7c84/uv-0.7.2-py3-none-musllinux_1_1_i686.whl", hash = "sha256:9aaacb143622cd437a446a4b316a546c02403b438cd7fd7556d62f47a9fd0a99", size = 16742005 }, + { url = "https://files.pythonhosted.org/packages/a5/b3/5550a721a1e8a99117d960f16c05ad8d39aff79a3fc1aadf2ed13da4385f/uv-0.7.2-py3-none-musllinux_1_1_x86_64.whl", hash = "sha256:81b86fff996c302be6aa1c1ac6eb72b97a7277c319e52c0def50d40b1ffaa617", size = 17443927 }, + { url = "https://files.pythonhosted.org/packages/52/1f/71a7c3e9c79718647fea1e6fe85ccc82d2629cd858b437ae2081190045cc/uv-0.7.2-py3-none-win32.whl", hash = "sha256:19a64c38657c4fbe7c945055755500116fdaac8e121381a5245ea66823f8c500", size = 16869579 }, + { url = "https://files.pythonhosted.org/packages/44/f0/4424cf64533b7576610f7de5c94183d810743b08e81072a2bb2d98316947/uv-0.7.2-py3-none-win_amd64.whl", hash = "sha256:dc1ee6114c824f5880c584a96b2947a35817fdd3a0b752d1adbd926ae6872d1c", size = 18287842 }, + { url = "https://files.pythonhosted.org/packages/0a/5c/12ce48cab21fb0f9bde4ea0c19ec2ab88d4aa9a53e148a52cfb9a41578c9/uv-0.7.2-py3-none-win_arm64.whl", hash = "sha256:0445e56d3f9651ad84d5a7f16efabba83bf305b73594f1c1bc0659aeab952040", size = 16929582 }, +] diff --git a/zizmor.yml b/zizmor.yml new file mode 100644 index 0000000..c359223 --- /dev/null +++ b/zizmor.yml @@ -0,0 +1,6 @@ +rules: + unpinned-uses: + config: + policies: + # TODO: use the default policies + "*": any