Compare commits

...

2 Commits

Author SHA1 Message Date
72275c491a
Added build script 2023-01-27 15:20:10 +01:00
64fe3d69dd
Added score notification 2023-01-27 15:19:50 +01:00
3 changed files with 68 additions and 2 deletions

View File

@ -1,4 +1,6 @@
using System.Drawing;
using System.Reflection;
using MessagePack;
namespace PongGame.Hubs;
@ -107,7 +109,7 @@ public struct PongGameState {
}
public struct PongPaddleState {
public const float PADDLE_LENGTH = HEIGHT / 10;
public const float PADDLE_LENGTH = HEIGHT / 6;
public const float PADDLE_HALF_LENGTH = PADDLE_LENGTH / 2;
public const float PADDLE_WIDTH = PADDLE_LENGTH / 5;
public const float PADDLE_SPEED = 8;
@ -121,7 +123,17 @@ public struct PongGameState {
public float Height;
public PongPaddleDirection Direction;
public int Score;
[IgnoreMember] private int _score;
public int Score {
get => _score; set {
if (_score != value) {
_score = value;
_ = Task.Run(() => ScoreChanged.Invoke(this, value));
}
}
}
public event EventHandler<int> ScoreChanged;
public static void Update(ref PongPaddleState state)
=> state.Height = Math.Clamp(state.Height + ((int)state.Direction) * PADDLE_SPEED, PADDLE_HALF_LENGTH, HEIGHT - PADDLE_HALF_LENGTH);

53
PongGame/build.py Normal file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env python3
from argparse import ArgumentParser
from dataclasses import dataclass
from pathlib import Path
@dataclass
class Version:
major: int = 1
minor: int = 0
patch: int = 0
def __str__(self):
return f"{self.major}.{self.minor}.{self.patch}"
def __init__(self, version_str: str):
try:
match list(map(int, version_str.split("."))):
case [major, minor, patch]:
self.major, self.minor, self.patch = major, minor, patch
return
case _:
raise ValueError("Version file must contain 3 parts (SemVer)!")
except ValueError as e:
raise ValueError(f"Version must contain numbers only!") from e
def parse_args():
parser = ArgumentParser("build.py", description="Docker multi-arch build helper.")
version_group = parser.add_mutually_exclusive_group()
version_group.add_argument("--major", "-m", action="store_true", help="Increase the major version before building.")
version_group.add_argument("--minor", "-n", action="store_true", help="Increase the minor version before building.")
version_group.add_argument("--patch", "-p", action="store_true", help="Increase the patch version before building.")
parser.add_argument("--file", "-f", type=str, default="version.txt", help="File to store the current version in.")
return parser.parse_args()
def save_version(version_file: Path, version: Version):
with version_file.open("w", encoding="utf-8") as f:
f.write(str(version))
def load_version(version_file: Path):
try:
with version_file.open("r", encoding="utf-8") as f:
return Version(f.read().strip())
except FileNotFoundError:
return Version()
def main():
args = parse_args()
version_file = Path(args.file)
version = load_version(version_file)
save_version(version_file, version)
if __name__ == '__main__':
main()

1
PongGame/version.txt Normal file
View File

@ -0,0 +1 @@
1.0.0