def game_loop(worldstate, current_tick_time, ttl=None): logger.debug("New tick: %d", worldstate.ticks) events_ = itertools.chain( ai.dot_ai(worldstate), events.process_browser_events(worldstate) ) worldstate = events.handle_events(worldstate, events_) # TODO: New variable name, because FP new_worldstate = attr_update( worldstate, ticks=_+1, # Elapsed ticks in game ) next_tick_time, delta = calc_next_tick_time(current_tick_time) # Get times for next frame logger.debug("Sleeping for %s (%s%%)", delta, round(delta * 1000, 2)) time.sleep(delta) # TTL stuff is in place to let us unit test this function, which we can't do if it runs forever if ttl is None: new_ttl = ttl else: new_ttl = ttl - 1 if ttl == 0: continue_ = False else: continue_ = True return continue_, (new_worldstate, next_tick_time, new_ttl)
def test_handle_events(): @events.event("handler") def handler1(old_val, new_val, payload): return new_val * payload @events.event("handler") def handler2(old_val, new_val, payload, bonus=0): return new_val * payload + bonus handled = events.handle_events(1, ( utils.Event(type="handler", payload=dict(payload=2)), utils.Event(type="handler", payload=dict(payload=3, bonus=3)) )) assert handled == 9 # 1 * 2 * 3 + 3