From bc70181b135be09bc6ea486882ef40ee33ba883a Mon Sep 17 00:00:00 2001 From: David Fischer Date: Wed, 16 Oct 2024 21:34:33 +0200 Subject: [PATCH] add update function --- .github/workflows/pyinstaller-build.yml | 19 ++++++++----------- lib/helper.py | 23 +++++++++++++++++++++-- scenes/start_screen/start.py | 6 +++--- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.github/workflows/pyinstaller-build.yml b/.github/workflows/pyinstaller-build.yml index 1ccbc9f..d1e2e95 100644 --- a/.github/workflows/pyinstaller-build.yml +++ b/.github/workflows/pyinstaller-build.yml @@ -92,12 +92,9 @@ jobs: generate_release_notes: true overwrite: true body: | - Pre-release versions are not consistent between tags. This is just used + Pre-release versions are not consistent between tags. This is used as a way to automatically package the game as executables for different - operating systems. Each executable version is suffixed with its github - commit hash to identify versions. Pre-releases might be overwritten with - newer versions (differentiable by the github commit hash in the file - name). + operating systems. - name: Upload Linux executable to Release uses: actions/upload-release-asset@v1 @@ -105,8 +102,8 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./dist/vigilant-ubuntu-latest-${{ github.sha }} - asset_name: vigilant-ubuntu-latest-${{ github.sha }} + asset_path: ./dist/vigilant-ubuntu-latest + asset_name: vigilant-ubuntu-latest asset_content_type: application/octet-stream - name: Upload macOS executable to Release @@ -115,8 +112,8 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./dist/vigilant-macos-latest-${{ github.sha }} - asset_name: vigilant-macos-latest-${{ github.sha }} + asset_path: ./dist/vigilant-macos-latest + asset_name: vigilant-macos-latest asset_content_type: application/octet-stream - name: Upload Windows executable to Release @@ -125,6 +122,6 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./dist/vigilant-windows-latest-${{ github.sha }}.exe - asset_name: vigilant-windows-latest-${{ github.sha }}.exe + asset_path: ./dist/vigilant-windows-latest.exe + asset_name: vigilant-windows-latest.exe asset_content_type: application/octet-stream diff --git a/lib/helper.py b/lib/helper.py index bbe4899..ae3539c 100644 --- a/lib/helper.py +++ b/lib/helper.py @@ -1,4 +1,4 @@ -import math, os, sys, yaml, datetime +import math, os, sys, yaml, datetime, urllib.request, platform from scipy.interpolate import CubicSpline import pickle import numpy as np @@ -219,4 +219,23 @@ def merge_dicts(a: dict, b: dict, path=[], level=1): else: print(f"Savegame key '{key}' not found in loaded savegame, defaulting to template value {b[key]}.") a[key] = b[key] - return a \ No newline at end of file + return a + + +def update_game(): + osname = platform.system() + + if 'Linux' in osname: + osname = 'ubuntu' + elif 'Darwin' in osname: + osname = 'macos' + elif 'Windows' in osname: + osname = 'windows' + else: + print(f"Failed to update game executable. Could not detect operating system since platform.system returns unknown value: {osname}.") + return + + try: + urllib.request.urlretrieve(f"https://github.com/fischer-hub/vigilant-funicular/releases/latest/download/vigilant-{osname}", "vigilant") + except Exception as e: + print(f"Failed to update game executable, download returned: {e}.") diff --git a/scenes/start_screen/start.py b/scenes/start_screen/start.py index 25e3a7c..d897f0d 100644 --- a/scenes/start_screen/start.py +++ b/scenes/start_screen/start.py @@ -1,7 +1,7 @@ from src.scene import Scene, Clickable, ChangeScene, Commentable from src.text import Text import pygame as pg -from lib.helper import path, save_config, check_update +from lib.helper import path, save_config, check_update, update_game from src.animate import StripAnimate from datetime import datetime import os, sys @@ -94,7 +94,7 @@ def __init__(self, player, cursor, collision_file = None, scale_factor = 6, dev save_config(self.config) if 'update_available' not in self.config: - self.config['update_available'] = False + self.config['update_available'] = False self.player_spawn = (-100, -100) @@ -112,7 +112,7 @@ def __init__(self, player, cursor, collision_file = None, scale_factor = 6, dev # clickables start_button_clickable = Btn(pg.Rect(460, 120, 1050, 220), sound = path('sounds', 'button_click.ogg'), animation = start_button, scene = self, id = 'start_button', fct = lambda: (4,(0,0))) new_game_button_clickable = Btn(pg.Rect((460, 440, 1050, 220)), sound = path('sounds', 'button_click.ogg'), animation = new_game, fct = lambda: (0,(0,0)), scene = self, id = 'new_game') - update_button_clickable = Btn(pg.Rect((460, 770, 1050, 220)), sound = path('sounds', 'button_click.ogg'), animation = update_button, scene = self, id = 'update_button') + update_button_clickable = Btn(pg.Rect((460, 770, 1050, 220)), sound = path('sounds', 'button_click.ogg'), animation = update_button, scene = self, id = 'update_button', fct = update_game) caution_msg_clickable = Msg(pg.Rect((524, 708, 800, 150)), sound = path('sounds', 'button_click.ogg'), animation = caution_msg, scene = self) self.clickable_lst = { '1': SizeMeter(pg.Rect(1670, 290, 90, 65), self, 1), '2': SizeMeter(pg.Rect(1670, 395, 90, 65), self, 2), '3': SizeMeter(pg.Rect(1670, 495, 90, 65), self, 3),