|
"""
|
|
Sample Weather Plugin for MMORPG
|
|
Adds weather effects and seasonal changes to the game world
|
|
"""
|
|
|
|
from src.interfaces.plugin_interfaces import IWeatherPlugin, PluginMetadata, PluginType
|
|
from typing import Dict, List, Any
|
|
import random
|
|
import time
|
|
|
|
class WeatherPlugin(IWeatherPlugin):
|
|
"""Plugin that adds dynamic weather system to the game."""
|
|
|
|
def __init__(self):
|
|
self._metadata = PluginMetadata(
|
|
id="weather_system",
|
|
name="Weather System",
|
|
version="1.0.0",
|
|
author="MMORPG Dev Team",
|
|
description="Adds dynamic weather effects and seasonal changes",
|
|
plugin_type=PluginType.SERVICE,
|
|
dependencies=[],
|
|
config={
|
|
"weather_change_interval": 300,
|
|
"enable_seasons": True,
|
|
"weather_effects_enabled": True
|
|
}
|
|
)
|
|
self._enabled = False
|
|
self._weather_state = {
|
|
"current_weather": "sunny",
|
|
"temperature": 20,
|
|
"season": "spring",
|
|
"last_change": time.time()
|
|
}
|
|
self._weather_types = [
|
|
"sunny", "cloudy", "rainy", "stormy", "foggy", "snowy"
|
|
]
|
|
|
|
@property
|
|
def metadata(self) -> PluginMetadata:
|
|
return self._metadata
|
|
|
|
def initialize(self, context: Dict[str, Any]) -> bool:
|
|
"""Initialize the weather plugin."""
|
|
try:
|
|
self._config = self._metadata.config
|
|
self._enabled = True
|
|
print("Weather Plugin initialized successfully")
|
|
self._update_weather()
|
|
return True
|
|
except Exception as e:
|
|
print(f"Failed to initialize Weather Plugin: {e}")
|
|
return False
|
|
|
|
def shutdown(self) -> bool:
|
|
"""Shutdown the weather plugin."""
|
|
try:
|
|
self._enabled = False
|
|
print("Weather Plugin shut down")
|
|
return True
|
|
except Exception as e:
|
|
print(f"Error shutting down Weather Plugin: {e}")
|
|
return False
|
|
|
|
def get_status(self) -> Dict[str, Any]:
|
|
"""Get weather plugin status."""
|
|
return {
|
|
"enabled": self._enabled,
|
|
"current_weather": self._weather_state["current_weather"],
|
|
"temperature": self._weather_state["temperature"],
|
|
"season": self._weather_state["season"],
|
|
"effects_enabled": self._config.get("weather_effects_enabled", True) if hasattr(self, '_config') else True
|
|
}
|
|
|
|
def get_weather_info(self) -> Dict[str, Any]:
|
|
"""Get current weather information."""
|
|
if not self._enabled:
|
|
return {}
|
|
|
|
return {
|
|
"weather": self._weather_state["current_weather"],
|
|
"temperature": self._weather_state["temperature"],
|
|
"season": self._weather_state["season"],
|
|
"description": self._get_weather_description()
|
|
}
|
|
|
|
def update_weather(self) -> Dict[str, Any]:
|
|
"""Update weather conditions."""
|
|
if not self._enabled:
|
|
return {}
|
|
|
|
current_time = time.time()
|
|
interval = self._config.get("weather_change_interval", 300)
|
|
|
|
if current_time - self._weather_state["last_change"] > interval:
|
|
self._update_weather()
|
|
self._weather_state["last_change"] = current_time
|
|
|
|
return self.get_weather_info()
|
|
|
|
def apply_weather_effects(self, player_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Apply weather effects to player."""
|
|
if not self._enabled or not self._config.get("weather_effects_enabled", True):
|
|
return player_data
|
|
|
|
effects = player_data.copy()
|
|
weather_modifier = self._get_weather_modifier()
|
|
|
|
if "stats" not in effects:
|
|
effects["stats"] = {}
|
|
|
|
effects["stats"]["weather_modifier"] = weather_modifier
|
|
effects["weather_description"] = self._get_weather_description()
|
|
|
|
return effects
|
|
|
|
def _update_weather(self):
|
|
"""Internal method to update weather conditions."""
|
|
self._weather_state["current_weather"] = random.choice(self._weather_types)
|
|
|
|
base_temp = self._get_seasonal_base_temperature()
|
|
weather_modifier = self._get_weather_temperature_modifier()
|
|
self._weather_state["temperature"] = base_temp + weather_modifier + random.randint(-5, 5)
|
|
|
|
if self._config.get("enable_seasons", True):
|
|
seasons = ["spring", "summer", "autumn", "winter"]
|
|
if random.random() < 0.01:
|
|
current_season_idx = seasons.index(self._weather_state["season"])
|
|
self._weather_state["season"] = seasons[(current_season_idx + 1) % len(seasons)]
|
|
|
|
def _get_seasonal_base_temperature(self) -> int:
|
|
"""Get base temperature for current season."""
|
|
season_temps = {
|
|
"spring": 15,
|
|
"summer": 25,
|
|
"autumn": 10,
|
|
"winter": 0
|
|
}
|
|
return season_temps.get(self._weather_state["season"], 15)
|
|
|
|
def _get_weather_temperature_modifier(self) -> int:
|
|
"""Get temperature modifier based on weather."""
|
|
weather_mods = {
|
|
"sunny": 5,
|
|
"cloudy": 0,
|
|
"rainy": -3,
|
|
"stormy": -5,
|
|
"foggy": -2,
|
|
"snowy": -10
|
|
}
|
|
return weather_mods.get(self._weather_state["current_weather"], 0)
|
|
|
|
def _get_weather_description(self) -> str:
|
|
"""Get descriptive text for current weather."""
|
|
weather = self._weather_state["current_weather"]
|
|
temp = self._weather_state["temperature"]
|
|
season = self._weather_state["season"]
|
|
|
|
descriptions = {
|
|
"sunny": f"Bright and sunny {season} day ({temp} degrees C)",
|
|
"cloudy": f"Overcast {season} sky ({temp} degrees C)",
|
|
"rainy": f"Light rain falling in {season} ({temp} degrees C)",
|
|
"stormy": f"Thunderstorm brewing this {season} ({temp} degrees C)",
|
|
"foggy": f"Thick fog blankets the {season} landscape ({temp} degrees C)",
|
|
"snowy": f"Snow falling gently in {season} ({temp} degrees C)"
|
|
}
|
|
return descriptions.get(weather, f"Unknown weather in {season} ({temp} degrees C)")
|
|
|
|
def _get_weather_modifier(self) -> Dict[str, float]:
|
|
"""Get weather-based stat modifiers."""
|
|
weather = self._weather_state["current_weather"]
|
|
|
|
modifiers = {
|
|
"sunny": {"movement_speed": 1.1, "visibility": 1.0, "morale": 1.1},
|
|
"cloudy": {"movement_speed": 1.0, "visibility": 0.95, "morale": 0.95},
|
|
"rainy": {"movement_speed": 0.8, "visibility": 0.9, "morale": 0.9},
|
|
"stormy": {"movement_speed": 0.6, "visibility": 0.7, "morale": 0.8},
|
|
"foggy": {"movement_speed": 0.9, "visibility": 0.5, "morale": 0.85},
|
|
"snowy": {"movement_speed": 0.7, "visibility": 0.8, "morale": 0.9}
|
|
}
|
|
|
|
return modifiers.get(weather, {"movement_speed": 1.0, "visibility": 1.0, "morale": 1.0})
|
|
|
|
def create_plugin():
|
|
"""Factory function to create the plugin instance."""
|
|
return WeatherPlugin()
|
|
|