def test_success(self): """ Test a successful run of level 0 """ tlu_globals.init() #in case not called before ;) tlu_hardware_global.init() logging.debug('Test test_success started') game = self.getGame('test_success') logging.debug('game created: ' + str(game)) task = Level00 logging.debug("test_success: b4 start: Game=" + str(game)) game.start(task, 0) logging.debug("test_success: after start: Game=" + str(game)) self.assertTrue(game.is_running(), "Started game has to run (for at least 2.5 seconds)") time.sleep(1.2) #wait a bit to terminate the level internally res = game.result() #wait for level to terminate games = Game.objects.all() for g in games: logging.debug(str(g)) self.assertEqual(res, Level.PASSED, "Expected result is " + str(Level.PASSED)) levelobject = game.current_level self.assertNotEqual(levelobject, None, "The level has to be known") self.assertEqual(levelobject.levelnum, 0, "Level has to be 0") self.assertEqual(levelobject.points, 5, "5 points achieved") self.assertNotEqual(levelobject.level_start, None, "The level should have a starting time") self.assertNotEqual(levelobject.level_ended, None, "The level should have a ending time") self.assertEqual(levelobject.result, Level.PASSED, "results have to match (passed)=" + str(Level.PASSED))
def abortlevel(request, level_id): """ Process the abort request :param request: HTTP-request :param level_id: level-id that shall be aborted """ user = request.user try: tlu_globals.init() tlu_hardware_global.init() game = user.game checkLevel(request, level_id) except OutOfRangeError: template_name = "level_error.html" error = _( "We are sorry, but the provided level-id does not fit your overall progress, you have to restart." ) return render(request, template_name, {'error': error}) except: template_name = "level_error.html" error = _( "We are sorry, but the user has to be known/logged in, you have to restart." ) return render(request, template_name, {'error': error}) game.abort() level = Level00(request.user.id) level.cleanup(True) level.startup() return renderLevel(request, level_id) #redisplay the content
def retrylevel(request, level_id): """ Process the retry-request, shows renderLevel in the end :param request: HTTP-request :param level_id: """ user = request.user try: tlu_globals.init() tlu_hardware_global.init() game = user.game checkLevel(request, level_id) except OutOfRangeError: template_name = "level_error.html" error = _( "We are sorry, but the provided level-id does not fit your overall progress, you have to restart." ) return render(request, template_name, {'error': error}) except: template_name = "level_error.html" error = _( "We are sorry, but the user has to be known/logged in, you have to restart." ) return render(request, template_name, {'error': error}) level = Level00(request.user.id) #use the simplest instance level.startup() #all hardware to restart game.retry() return renderLevel(request, level_id) #redisplay the content to allow for restart
def startlevel(request, level_id): """ Starts the game as per request given and returns the Level via renderLevel :param request: HTTP-request :param level_id: level-number """ user = request.user try: tlu_globals.init() tlu_hardware_global.init() checkLevel(request, level_id) user.game.start(levels[level_id], level_id) logging.debug("Game started: " + str(user.game)) except OutOfRangeError: template_name = "level_error.html" error = _( "We are sorry, but the provided level-id does not fit your overall progress, you have to restart." ) return render(request, template_name, {'error': error}) except Exception as e: template_name = "level_error.html" error = _( "We are sorry, but the user has to be known/logged in, you have to restart." ) + " Error: " + str(e) return render(request, template_name, {'error': error}) return renderLevel(request, level_id)
def renderLevel(request, level_id): """ Show the Level-page where we can start the individual level :param request: HTTP-request :param level_id: level-number to start """ template_name = "level_" + "{:02d}".format(level_id) + ".html" user = request.user game = user.game try: tlu_globals.init() tlu_hardware_global.init() checkLevel(request, level_id) except OutOfRangeError: template_name = "level_error.html" error = _( "We are sorry, but the provided level-id does not fit your overall progress, you have to restart." ) return render(request, template_name, {'error': error}) dips = game.getDipSwitch(level_id) leftDip = _("left:") + " " + tlu_hardwarebase.showleft_dip(dips) rightDip = _("right:") + " " + tlu_hardwarebase.showright_dip(dips) overall_progress = game.getOverallProgress() info = _("Please press start and check the DIP-settings...") button_text = _("Start Level") + " " + str(level_id) button_url = '/tlu_joyit_game/start/' + str(level_id) return render( request, template_name, { 'leftDip': leftDip, 'rightDip': rightDip, 'overall_progress': overall_progress, 'info': info, 'button_text': button_text, 'button_url': button_url })
def test_start_game(self): """ Test to startup the game (level 0) """ tlu_globals.init() #in case not called before ;) tlu_hardware_global.init() logging.debug('Test test_start_game started') game = self.getGame('test_start_game') logging.debug("test_start_game: b4 start: Game=" + str(game)) game.start(Level00, 0) logging.debug("test_start_game: after start: Game=" + str(game)) time.sleep( 1.2 ) #wait until game times out internally after 1 second, then continue here res = game.result() self.assertEqual( res, Level.PASSED, "Expected result of game00-run is " + str(Level.PASSED)) levelobject = game.current_level self.assertNotEqual(levelobject, None, "The level has to be known") self.assertEqual(levelobject.levelnum, 0, "Level has to be 0") self.assertEqual(levelobject.points, 5, "5 points achieved") self.assertEqual(levelobject.result, 1, "results have to match") self.assertNotEqual(levelobject.level_start, None, "The level should have a starting time") self.assertNotEqual(levelobject.level_ended, None, "The level should have a ending time") self.assertEqual(levelobject.prevLevel, None, "The previous level has to be empty")
def __init__(self, *args, **kwargs): tlu_hardware_global.init() self.dipswitch_settings = (tlu_hardwarebase.getDipHex(tlu_buttons), tlu_hardwarebase.getDipHex(tlu_buttons), tlu_hardwarebase.getDipHex(tlu_cursor), tlu_hardwarebase.getDipHex(tlu_buttons), tlu_hardwarebase.getDipHex(tlu_buttons) | tlu_hardwarebase.getDipHex(tlu_touch)) models.Model.__init__(self, *args, **kwargs)
def __init__(self): global global_lcd global global_matrix logging.info("Global_vars: Init") if (global_lcd == None) or (global_matrix == None): tlu_hardware_global.init() if global_lcd == None: global_lcd = tlu_lcd.lcd_panel() if global_matrix == None: global_matrix = tlu_matrix.tlu_matrix()
def restart(request): """ Process the restart request from the user :param request: HTTP-request """ tlu_globals.init() tlu_hardware_global.init() game = request.user.game game.restart() return HttpResponseRedirect( reverse('tlu_joyit_game:level', kwargs={'level_id': 1}))
def setUp(self): tlu_globals.init() tlu_hardware_global.init() if emulatekey and self._testMethodName=="test_checkkey" : obj=tlu_queueobject(tlu_queue.tlu_queue.MSG_KEYPRESSED,1) tlu_globals.kbQueue.put(obj) elif emulatekey and self._testMethodName=="test_checkcursor": obj=tlu_queueobject(tlu_queue.tlu_queue.MSG_KEYPRESSED,100) tlu_globals.cqueue.put(obj) elif emulatekey and self._testMethodName=="test_checktouch": obj=tlu_queueobject(tlu_queue.tlu_queue.MSG_TOUCH_PRESSED) tlu_globals.tqueue.put(obj)
def test_globals(self): """ Test the global-functionality provided by tlu_globals """ tlu_globals.init() #in case not called before ;) tlu_hardware_global.init() glob = tlu_globals.globMgr.tlu_glob() self.assertTrue(glob.test(), "The test has to pass...") if emulatekey: self.assertNotEqual(tlu_globals.kbQueue, None, "Keyboardqueue should not be null on mac") self.assertNotEqual(tlu_globals.cqueue, None, "Cursorqueue should not be null on mac") self.assertNotEqual(tlu_globals.tqueue, None, "Touchqueue should not be null on mac")
def get(self, request, *args, **kwargs): """ Renders the view and starts the hardware by displaying the clock Kills as well all possible running processes :param request: HTTP-request """ logger.info("Index View called") tlu_globals.init() tlu_hardware_global.init() suspendCurrentGame(request) level = Level00(request.user.id) level.cleanup(True) level.startup() tlu_globals.globMgr.tlu_glob().stopClock() return generic.TemplateView.get(self, request, *args, **kwargs)
def test_retry(self): """ Test to simulate a retry-wish of the player """ tlu_globals.init() #in case not called before ;) tlu_hardware_global.init() logging.debug('Test test_retry started') game = self.getGame('test_retry') logging.debug("test_retry: b4 start: Game=" + str(game)) game.start(Level00, 0) time.sleep(0.5) #allow the game top start logging.debug("test_retry: after start: Game=" + str(game)) game.retry() levelobject = game.current_level self.assertNotEqual(levelobject, None, "The level has to be known") self.assertEqual(levelobject.levelnum, 0, "Level has to be 0") self.assertEqual(levelobject.points, 0, "No points achieved") self.assertEqual(levelobject.result, Level.RETRY, "results have to match (retry)=" + str(Level.RETRY))
def LevelView(request, level_id): """ Shows renderLevel for the given level-id :param request: HTTP-request :param level_id: level-id """ level = Level00(request.user.id) #real level does not matter tlu_globals.init() tlu_hardware_global.init() level.startup() #some basic settings try: checkLevel(request, level_id) except OutOfRangeError: template_name = "level_error.html" error = _( "We are sorry, but the provided level-id does not fit your overall progress, you have to restart." ) return render(request, template_name, {'error': error}) if request.method == 'GET': return renderLevel(request, level_id) return HttpResponseNotAllowed(['POST'])
def __init__(self, queue, seconds): """ Defines the queue where the timeout message would be send to and the amount of seconds the level should last :param queue: Message-Queue for Timeout message :param seconds: Seconds to wait until timeout, max 99 seconds allowed """ Thread.__init__(self) self.is_aborted=False self.has_to_restart=False self.paused = seconds<0 if seconds > 999: seconds = 999 if seconds <= 0: seconds = 0.1 self.tenths=int(seconds*10) self.queue=queue self.sevenseg=tlu_led.seven_segment() tlu_globals.init() tlu_hardware_global.init() glob=tlu_globals.globMgr.tlu_glob() glob.stopClock() #terminate Clock in case it is showing
def test_abort(self): """ Test to abort the game during its operation (level 0) """ tlu_globals.init() #in case not called before ;) tlu_hardware_global.init() logging.debug('Test test_abort started') game = self.getGame('test_abort') game.start(Level00, 0) game.abort() logging.debug('Test test_abort ended, analysis starts') levelobject = game.current_level logging.debug('Test test_abort result is: ' + str(levelobject)) self.assertNotEqual(levelobject, None, "The level has to be known") self.assertEqual(levelobject.levelnum, 0, "Level has to be 0") self.assertEqual(levelobject.points, 0, "No points achieved") self.assertNotEqual(levelobject.level_start, None, "The level should have a starting time") self.assertNotEqual(levelobject.level_ended, None, "The level should have a ending time") self.assertEqual(levelobject.result, Level.ABORT, "results have to match (aborted)=" + str(Level.ABORT))
def checkLevel(request, level_id): """ Check that the provided level is within the allowed range. If not raises an OutOfRangeError :param request: HTTP-request :param level_id: level-number (picked from url) """ if level_id == 1: #startup level is ever ok return user = request.user try: tlu_globals.init() tlu_hardware_global.init() game = user.game except: return if game.current_level == None: if (level_id > 1): status = getGameState(user.id) status.msg = _( "You just started from the beginning, so level has to be one, please restart the game!" ) setGameState(user.id, status) raise OutOfRangeError else: return if (level_id > game.current_level.levelnum + 1) or ( level_id < game.current_level.levelnum) or game.completed(level_id): status = getGameState(user.id) status.msg = _( "The requested level is not in line with our expectations, please restart the game!" ) setGameState(user.id, status) raise OutOfRangeError
def LevelOverview(request): """ Render the Level-overwiew which contains a table for the current state and as well another one for the levels played so far. :param request: HTTP-request """ if request.method == 'GET': tlu_globals.init() tlu_hardware_global.init() suspendCurrentGame(request) level = Level00(request.user.id) level.startup() template_name = 'tlu_joyit_game/level_overview.html' user = get_object_or_404(User, pk=request.user.id) game = user.game logging.debug("LevelOverview, current game=" + str(game)) button_text = _("Continue the game now") path = "" try: level_set = game.level_set.all().order_by('-id') except (KeyError, Level.DoesNotExist): level_set = None cur_level = game.current_level if cur_level == None: current_level_num = 0 else: current_level_num = cur_level.levelnum next_level_num = current_level_num if (cur_level != None) and (cur_level.result == Level.PASSED): next_level_num += 1 if game.completed(next_level_num): next_level_num -= 1 button_text = _("Play level again") path = reverse('tlu_joyit_game:retry', kwargs={'level_id': next_level_num}) if len(path) == 0: path = reverse('tlu_joyit_game:level', kwargs={'level_id': next_level_num}) last_played = None if (cur_level != None): last_played = cur_level.level_start if last_played == None: last_played = _("Not yet played") path = '/tlu_joyit_game/restart' if (level_set == None) or (len(level_set) < 1): button_text = _("Start now") game = request.user.game achieved_points = game.getAchievedPoints() minutes_active_game = game.getMinutesPlayedCurrentGame() minutes_played = game.getMinutesPlayedOverall() num_starts = game.getNumStarts() return render( request, template_name, { 'current_level': str(current_level_num), 'last_played': last_played, 'achieved_points': achieved_points, 'minutes_active_game': str(minutes_active_game), 'minutes_played': str(minutes_played), 'num_starts': str(num_starts), 'button_text': button_text, 'next_level_url': path }) return HttpResponseNotAllowed(['POST'])