def __init__(self, loop, config): self.loop = loop self.config = config base = config.base # game-related self.update_packet = packets.ServerUpdate() self.update_packet.reset() self.connections = set() self.players = MultikeyDict() self.chunks = {} self.updated_chunks = set() self.update_loop = LoopingCall(self.update) self.update_loop.start(1.0 / base.update_fps, now=False) # world self.world = World(self, self.loop, base.seed, base.use_tgen, base.use_entities) # server-related self.git_rev = base.get('git_rev', None) self.passwords = {} for k, v in base.passwords.items(): self.passwords[k.lower()] = v self.scripts = ScriptManager() for script in base.scripts: self.load_script(script) # time self.extra_elapsed_time = 0.0 self.start_time = loop.time() self.set_clock('12:00') # start listening asyncio.Task(self.create_server(self.build_protocol, port=base.port))
import traceback import asyncio import signal import socket import importlib # initialize packet instances for sending join_packet = packets.JoinPacket() seed_packet = packets.SeedData() chat_packet = packets.ServerChatMessage() entity_packet = packets.EntityUpdate() update_finished_packet = packets.UpdateFinished() time_packet = packets.CurrentTime() mismatch_packet = packets.ServerMismatch() server_full_packet = packets.ServerFull() extra_server_update = packets.ServerUpdate() class Entity(world.Entity): connection = None def kill(self): self.damage(self.hp + 100.0) def damage(self, damage=0, stun_duration=0): packet = packets.HitPacket() packet.entity_id = self.entity_id packet.target_id = self.entity_id packet.hit_type = packets.HIT_NORMAL packet.damage = damage packet.critical = 1
def __init__(self, loop, config): self.loop = loop self.config = config base = config.base # game-related self.update_packet = packets.ServerUpdate() self.update_packet.reset() self.connections = set() self.players = MultikeyDict() self.updated_chunks = set() use_same_loop = base.update_fps == base.network_fps if use_same_loop: def update_callback(): self.update() self.send_update() else: update_callback = self.update self.update_loop = LoopingCall(update_callback) self.update_loop.start(1.0 / base.update_fps, now=False) if use_same_loop: self.send_loop = self.update_loop else: self.send_loop = LoopingCall(self.send_update) self.send_loop.start(1.0 / base.network_fps, now=False) self.mission_loop = LoopingCall(self.update_missions) self.mission_loop.start(base.mission_update_rate, now=False) # world self.world = World(self, self.loop, base.seed, use_tgen=base.use_tgen, use_entities=base.use_entities, chunk_retire_time=base.chunk_retire_time, debug=base.world_debug_info) if base.world_debug_file is not None: debug_fp = open(base.world_debug_file, 'wb') self.world.set_debug(debug_fp) # server-related self.git_rev = base.get('git_rev', None) self.passwords = {} for k, v in base.passwords.items(): self.passwords[k.lower()] = v self.scripts = ScriptManager() for script in base.scripts: self.load_script(script) # time self.extra_elapsed_time = 0.0 self.start_time = loop.time() self.set_clock('12:00') # start listening self.loop.set_exception_handler(self.exception_handler) self.loop.create_task( self.create_server(self.build_protocol, port=base.port, family=socket.AF_INET))