JoshButner

Crea Anomaly - system.py

Mar 10th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.45 KB | None | 0 0
  1. import os
  2.  
  3. from siege import game
  4. from siege.util import minutes, Timer
  5. from siege.network import NetworkManager, Message, MessageHandler
  6. from siege.io import DataStream, File, FileOnCompleteHandler
  7. from siege.world import World
  8.  
  9. from siege import Locale
  10. from siege.log import Log
  11.  
  12.  
  13. class AnomalySystem(object):
  14.     UPDATE_FREQUENCY = minutes(5)  # Only the update frequency for starting anomalies, everything else updates on update.
  15.     FILE_VERSION = 3
  16.     NAME = 'anomaly'
  17.  
  18.     def getName(self):
  19.         return self.NAME
  20.  
  21.     def __init__(self):
  22.         self.anomalies = dict()
  23.         self.updateTimer = Timer(self.UPDATE_FREQUENCY)
  24.         self.world = None
  25.  
  26.     def add(self, anomaly):
  27.         self.anomalies[anomaly.name] = anomaly
  28.  
  29.     def getAnomalies(self):
  30.         return self.anomalies
  31.  
  32.     def getAnomaly(self, anomalyName):
  33.         return self.anomalies[anomalyName]
  34.  
  35.     def update(self, frametime):
  36.         if World.get() is not None:  # Make sure the game is being played, not that a world is being generated
  37.             if World.get().getPlayer() is not None or NetworkManager.isStandalone():  # Make sure that the player isn't idling on the character select screen
  38.                 if NetworkManager.isHost():
  39.                     self.updateTimer.update(frametime)
  40.                     for anomalyName, anomaly in self.anomalies.iteritems():
  41.                         if anomaly.active:
  42.                             anomaly.update(frametime)
  43.                     if self.updateTimer.expired():
  44.                         self.updateTimer.reset(self.UPDATE_FREQUENCY)
  45.                         for anomalyName, anomaly in self.anomalies.iteritems():
  46.                             if anomaly.shouldOccur(self.UPDATE_FREQUENCY):
  47.                                 anomaly.occur()
  48.                 else:
  49.                     for anomalyName, anomaly in self.anomalies.iteritems():
  50.                         if anomaly.active:
  51.                             anomaly.clientUpdate(frametime)
  52.  
  53.     def handleClientSync(self, client, packet):
  54.         packetAnomalyKey = packet.readString()
  55.         if packetAnomalyKey in self.anomalies.keys():
  56.             self.anomalies[packetAnomalyKey].recieveSync(client, packet)
  57.         else:
  58.             Log.error("No anomaly with name {} registered on client".format(packetAnomalyKey))
  59.  
  60.     def save(self, world, asynchronous):
  61.         stream = DataStream()
  62.         stream.writeUint16(self.FILE_VERSION)
  63.         stream.writeUint8(len(self.anomalies))
  64.         anomalySaveOrder = []
  65.         for anomalyName in self.anomalies.keys():
  66.             stream.writeString(anomalyName)
  67.             anomalySaveOrder.append(anomalyName)
  68.         for i in range(len(anomalySaveOrder)):
  69.             stream.diverge()
  70.             anomaly = self.getAnomaly(anomalySaveOrder[i])
  71.             stream.writeUint8(anomaly.VERSION)
  72.             anomaly.save(stream)
  73.             stream.converge()
  74.         if asynchronous:
  75.             game.file.asyncWrite(self.getPath(world), FileOnCompleteHandler.create(), stream, useCompression=False)
  76.         else:
  77.             File.save(self.getPath(world), stream)
  78.  
  79.     def load(self, world):
  80.         if File.exists(self.getPath(world)):
  81.             stream = DataStream()
  82.             File.load(self.getPath(world), stream)
  83.             systemVersion = stream.readUint16()  # Read anomaly file systemVersion
  84.             if systemVersion >= 2:
  85.                 anomalyAmount = stream.readUint8()
  86.                 anomalySaveOrder = []
  87.  
  88.                 for i in range(anomalyAmount):
  89.                     anomalySaveOrder.append(stream.readString())
  90.                 for i in range(anomalyAmount):
  91.                     stream.begin()
  92.                     try:
  93.                         anomaly = self.getAnomaly(anomalySaveOrder[i])
  94.                         anomalyVersion = stream.readUint8()
  95.                         anomaly.load(stream, systemVersion, anomalyVersion)
  96.                         stream.end()
  97.                     except Exception as error:
  98.                         Log.warn("Anomaly of type {} failed to load".format(type(anomaly).__name__))
  99.                         Log.warn(str(error))
  100.                         anomaly.loadFailed()
  101.                         stream.skip()
  102.                 if systemVersion == 2:
  103.                     for anomaly in self.anomalies.values():
  104.                         if anomaly.realm:
  105.                             anomaly.removeMarker(anomaly.realm)
  106.                             markerName = Locale.get(anomaly.markerName)
  107.                             anomaly.purgeMarkers(anomaly.realm, markerName)
  108.                             anomaly.addMarker(anomaly.realm, markerName)
  109.             elif systemVersion == 1:
  110.                 self.loadV1(stream)
  111.             else:
  112.                 for anomalyName, anomaly in self.anomalies.iteritems():
  113.                     anomaly.loadFailed()
  114.                 Log.warn("Resetting Anomaly Timers due to file version change")
  115.  
  116.     def loadV1(self, stream):
  117.         Log.info("Anomaly data is V1, attempting to load from V1.")
  118.         for anomalyName, anomaly in self.anomalies.iteritems():
  119.             Log.debug("Attempting to load {} from V1".format(anomalyName))
  120.             if anomalyName not in ["apex", "darkness", "silvan", "swarm", "chaos_crystal"]:
  121.                 continue
  122.             stream.begin()
  123.             try:
  124.                 anomaly.load(stream, 1, 1)
  125.                 stream.end()
  126.                 Log.debug("Succeeded in loading {} from V1".format(anomalyName))
  127.             except Exception as error:
  128.                 Log.warn("Anomaly of type {} failed to load from V1".format(type(anomaly).__name__))
  129.                 Log.warn(str(error))
  130.                 anomaly.loadFailed()
  131.                 stream.skip()
  132.  
  133.     def getPath(self, world):
  134.         return os.path.join(world.path, "anomaly")
  135.  
  136.     @staticmethod
  137.     def playerJoined(player=None):
  138.         if player is None and not NetworkManager.isStandalone():
  139.             player = World.get().getPlayer()
  140.         if player is not None:
  141.             player.entity.legend.enableMarker("Anomaly")
  142.  
  143.     @staticmethod
  144.     def worldLoaded(world):
  145.         system = game.anomaly
  146.         system.load(world)
  147.         game.events['world_saved'].listen(system.save)
  148.  
  149.     @staticmethod
  150.     def worldUnloaded(world):
  151.         if game.hasSystem(AnomalySystem.NAME):
  152.             system = game.anomaly
  153.             game.events["world_saved"].remove(system.save)
  154.  
  155.     @staticmethod
  156.     def register():
  157.         game.events['player_joined'].listen(AnomalySystem.playerJoined)
  158.         if not NetworkManager.isStandalone():
  159.             # Need to do this because player_joined isn't called in time for the host.
  160.             game.events['map_loaded'].listen(AnomalySystem.playerJoined)
  161.         system = AnomalySystem()
  162.         game.registerSystem(AnomalySystem.NAME, system)
  163.         if NetworkManager.isHost():
  164.             game.events['world_loaded'].listen(AnomalySystem.worldLoaded)
  165.             game.events['world_unloaded'].listen(AnomalySystem.worldUnloaded)
  166.         else:
  167.             # Check if the client exists since the first system initialization occurs while in the menu
  168.             if game.network.getClient() is not None:
  169.                 game.network.getClient().register(Message.ANOMALY, MessageHandler.create(system.handleClientSync))
  170.  
  171.     @staticmethod
  172.     def unregister():
  173.         game.events['player_joined'].remove(AnomalySystem.playerJoined)
  174.         game.events['map_loaded'].remove(AnomalySystem.playerJoined)
  175.  
Advertisement
Add Comment
Please, Sign In to add comment