def twin_mock_timer(self): self.timings = Timings() self.timer_a = Timer() self.timer_b = Timer() self.timer_a.duration = 3 self.timer_b.duration = 5 self.timings.aggregate.append(self.timer_a) self.timings.aggregate.append(self.timer_b)
def test_timer_pause(self): self.timer = Timer() self.timer.start() time.sleep(1) self.timer.pause() time.sleep(1) self.timer.restart() time.sleep(1) self.timer.stop() self.assertGreaterEqual(self.timer.duration, 2, "?")
def __init__(self): Gtk.Window.__init__(self,title='Alarm') self.hbox = Gtk.Box(spacing=6) self.label = Gtk.Label('') self.countdown = Timer(hour=0,min=8,sec=0) # Add to the label a countdown self.hbox.pack_start(self.label, True, True, 0) self.set_name('AlarmWindow') self.label.set_name('Alarm') self.add(self.hbox)
def __init__(self, server_id, raid_boss, default_channel, event_loop): self.server_id = server_id self.raid_boss = raid_boss self.raid_time = raid_boss.get_raid_time() self.raid_state = RaidState.LOBBY self.default_channel = default_channel self.event_loop = event_loop self.players = [] self.current_player = None self.current_player_index = -1 self.timer = Timer(15, event_loop, self.start_raid, default_channel, force=True) self.timer.set_event(10, self.announce_time_left, default_channel) self.timer.start()
def updateCountdown(self): flashTime = Timer(min=1,sec=0) # Time when start to flashing finish = Timer() # Time is end self.countdown.dec() # Decrement the countdown self.label.set_text('- '+str(self.countdown)) if self.countdown < flashTime:# If we are up to 1 minute to the alarm if self.label.get_visible(): # we toggle visible status of the label self.label.hide() else: self.label.show() if self.countdown == finish: # if countdown is zero, we stop cycling self.hide() # make the self disappear return False # and stop the countdown return True # Otherwise we continue to cycle
def start(self): # Start countdown if self.countdown == Timer(): # If the countdown is on zero we do nothing return False # Otherwise ... self.show_all() GLib.timeout_add_seconds(1,self.updateCountdown) return True
class AlarmWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self,title='Alarm') self.hbox = Gtk.Box(spacing=6) self.label = Gtk.Label('') self.countdown = Timer(hour=0,min=8,sec=0) # Add to the label a countdown self.hbox.pack_start(self.label, True, True, 0) self.set_name('AlarmWindow') self.label.set_name('Alarm') self.add(self.hbox) def reset(self): # Reset the counter self.countdown = Timer(hour=0,min=8,sec=0) def start(self): # Start countdown if self.countdown == Timer(): # If the countdown is on zero we do nothing return False # Otherwise ... self.show_all() GLib.timeout_add_seconds(1,self.updateCountdown) return True def updateCountdown(self): flashTime = Timer(min=1,sec=0) # Time when start to flashing finish = Timer() # Time is end self.countdown.dec() # Decrement the countdown self.label.set_text('- '+str(self.countdown)) if self.countdown < flashTime:# If we are up to 1 minute to the alarm if self.label.get_visible(): # we toggle visible status of the label self.label.hide() else: self.label.show() if self.countdown == finish: # if countdown is zero, we stop cycling self.hide() # make the self disappear return False # and stop the countdown return True # Otherwise we continue to cycle
async def start_raid(self, channel, force=False): if len(self.players) == 0: await channel.send(messages.data['raid_not_enough_players']) raid_manager.end_raid(self.server_id) return if force: await channel.send(messages.data['force_raid_start']) # Go to player turn self.raid_state = RaidState.PLAYER_TURN self.timer = Timer(self.raid_time, self.event_loop, self.end_raid, self.default_channel, force=True) self.timer.start() # Show raid info await command_raid_info.show_info(channel) await self.next_player(channel)
def setTimeLabel(self): day = ['lu','ma','me','gi','ve','sa','do'] self.label.set_text(time.strftime('%H:%M:%S')) # Set the label to the current time # Next we control if in five minutes start a new program. now = time.localtime() today = now.tm_wday hour = now.tm_hour minutes = now.tm_min timeNow = Timer(hour=hour,min=minutes) for index in range(0,8): # This is necessary cause changing directly min field in the timer timeNow.incMin() # won't change the hour too. if timeNow in self.schedule[day[today]] and not self.alarm.get_property('visible'): self.alarm.reset() # Reset the countdown of the self self.alarm.start() # start the countdown return True
class Timings(object): """A time aggregation class""" def __init__(self): self.aggregate = [] self.timer = Timer() self.last_time = 0 def timer_start(self): try: self.timer = Timer() self.timer.start() except ArithmeticError: self.timer.start_time = 0 def timer_pause(self): try: self.timer.pause() except RuntimeError: pass def timer_restart(self): try: self.timer.restart() except RuntimeError: pass def timer_stop(self): try: self.last_time = self.timer.stop() self.aggregate.append(self.last_time) except ArithmeticError: pass def remove_timer(self, timer): try: self.aggregate.remove(self.aggregate[timer]) except RuntimeError: pass
class TimeTests(unittest.TestCase): def test_now(self): self.timer = Timer() self.assertGreaterEqual(self.timer.serial_time(), 0, "?") def test_timer_with_wait(self): self.timer = Timer() self.timer.start() time.sleep(1) self.timer.stop() self.assertGreaterEqual(self.timer.end_time, self.timer.start_time, "?") def test_timer_pause(self): self.timer = Timer() self.timer.start() time.sleep(1) self.timer.pause() time.sleep(1) self.timer.restart() time.sleep(1) self.timer.stop() self.assertGreaterEqual(self.timer.duration, 2, "?")
class Raid: def __init__(self, server_id, raid_boss, default_channel, event_loop): self.server_id = server_id self.raid_boss = raid_boss self.raid_time = raid_boss.get_raid_time() self.raid_state = RaidState.LOBBY self.default_channel = default_channel self.event_loop = event_loop self.players = [] self.current_player = None self.current_player_index = -1 self.timer = Timer(15, event_loop, self.start_raid, default_channel, force=True) self.timer.set_event(10, self.announce_time_left, default_channel) self.timer.start() async def start_raid(self, channel, force=False): if len(self.players) == 0: await channel.send(messages.data['raid_not_enough_players']) raid_manager.end_raid(self.server_id) return if force: await channel.send(messages.data['force_raid_start']) # Go to player turn self.raid_state = RaidState.PLAYER_TURN self.timer = Timer(self.raid_time, self.event_loop, self.end_raid, self.default_channel, force=True) self.timer.start() # Show raid info await command_raid_info.show_info(channel) await self.next_player(channel) async def next_player(self, channel, flee=False): # Flee marks if the caller of next_player has fled the battle (removed from player list) if not flee: self.current_player_index += 1 # Set to none so people can't do anything during the wait self.current_player = None await asyncio.sleep(1) # Check if all players gone (no players left) if len(self.get_players()) == 0: # cancel timer self.timer.cancel() del self.timer raid_manager.end_raid(self.server_id) await channel.send(messages.data['raid_failed']) return # If we're still not done with players if self.current_player_index < len(self.get_players()): temp_player = self.get_players()[self.current_player_index] turn_msg = messages.data['raid_player_move'].replace('%name%', '<@{}>'.format(temp_player.get_id())) # Stun check if StatusEffect.STUNNED in temp_player.status_effects: breakout = random.randint(1, 2) if breakout == 1: turn_msg = messages.data['raid_player_breakout_stun'].replace('%name%', '<@{}>' .format(temp_player.get_id())) self.get_players()[self.current_player_index].status_effects.remove(StatusEffect.STUNNED) else: await channel.send(messages.data['raid_player_stunned'].replace('%name%', temp_player.get_name())) await self.next_player(channel) return # Set current player after stun check self.current_player = self.get_players()[self.current_player_index] await channel.send(turn_msg) else: # Reset index counter, go to boss turn self.current_player_index = -1 self.current_player = None self.raid_state = RaidState.RAID_BOSS_TURN await self.handle_boss_turn(channel) async def handle_boss_turn(self, channel): event_log = self.raid_boss.handle_turn(self.players) embed = discord.Embed(title="Boss Event Log", color=0xFFFFFF) embed.set_thumbnail(url=self.raid_boss.get_url()) embed.add_field(name='\u200B', value=event_log) await channel.send(embed=embed) self.raid_state = RaidState.PLAYER_TURN await self.next_player(channel) async def announce_time_left(self, channel, force=False): await channel.send(messages.data['ten_seconds_left']) async def end_raid(self, channel, force=False): if force: raid_manager.end_raid(self.server_id) await channel.send(messages.data['raid_out_of_time']) return # cancel timer self.timer.cancel() del self.timer await channel.send(messages.data['raid_defeated'].replace('%boss_name%', self.get_raid_boss().get_name())) self.current_player = None self.raid_state = RaidState.REWARD await asyncio.sleep(1) rewards = self.get_raid_boss().get_rewards() reward_xp = self.get_raid_boss().get_reward_xp() reward_log = "All raiders gained **{}** XP.\n\n".format(reward_xp) for player in self.players: info = player.get_info() # Gold drop gold = self.get_raid_boss().get_reward_gold(info['level']) reward_log += "**{}** found **{}** {}.\n".format(player.get_name(), gold, messages.data['emoji_gold']) info['gold'] += gold # Exp drop xp_info = player.calculate_raw_xp_to_levels(reward_xp) info['level'] += xp_info[0] if xp_info[0] > 0: info['xp'] = xp_info[1] else: info['xp'] += xp_info[1] info['xp_to_next'] = xp_info[2] info['max_hp'] += xp_info[0] * 5 info['max_mp'] += xp_info[0] * 5 info['atk'] += xp_info[0] # Item drops reward = random.choice(rewards) if reward is not None: item_id = reward[0] if item_id in info['items']: info['items'][item_id] += reward[1] else: info['items'][item_id] = reward[1] player.db.set_player_info(player.id, info) item_obj = item_map.item_map[item_id]() item_name = item_obj.get_name() item_emoji = item_obj.get_emoji() reward_log += "**{}** found: {} {} (x{})\n".format(player.get_name(), item_emoji, item_name, reward[1]) player.db.set_player_info(player.id, info) embed = discord.Embed(title="Reward Log", color=0xF8C300) embed.set_thumbnail(url=self.raid_boss.get_url()) embed.add_field(name='\u200B', value=reward_log) await channel.send(embed=embed) raid_manager.end_raid(self.server_id) def get_raid_boss(self): return self.raid_boss def get_raid_state(self): return self.raid_state def get_server_id(self): return self.server_id # Returns true if successful, false if not def add_player(self, id, name, db): if len(self.players) < 20: self.players.append(Player(id, name, db)) return True return False def get_players(self): return self.players def get_current_player(self): return self.current_player def has_player(self, id): for player in self.players: if player.id == id: return True return False def get_timer(self): return self.timer
def __init__(self): self.aggregate = [] self.timer = Timer() self.last_time = 0
def timer_start(self): try: self.timer = Timer() self.timer.start() except ArithmeticError: self.timer.start_time = 0
def reset(self): # Reset the counter self.countdown = Timer(hour=0,min=8,sec=0)
def test_now(self): self.timer = Timer() self.assertGreaterEqual(self.timer.serial_time(), 0, "?")
def test_timer_with_wait(self): self.timer = Timer() self.timer.start() time.sleep(1) self.timer.stop() self.assertGreaterEqual(self.timer.end_time, self.timer.start_time, "?")