A corporation manager and dashboard for EVE Online
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

92 wiersze
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime, timedelta
  3. from pathlib import Path
  4. from threading import Lock
  5. from flask import g
  6. from .database import CampaignDB
  7. from .._provided import app, config, logger
  8. from ...database import Database as MainDB
  9. __all__ = ["get_current", "get_overview", "get_summary", "get_unit"]
  10. _MAX_STALENESS = 60 * 60
  11. CampaignDB.path = str(Path(MainDB.path).parent / "db_campaigns.sqlite3")
  12. app.before_request(CampaignDB.pre_hook)
  13. app.teardown_appcontext(CampaignDB.post_hook)
  14. _lock = Lock()
  15. def _update_operation(cname, opname, new):
  16. """Update a campaign/operation."""
  17. ...
  18. operation = config["campaigns"][cname]["operations"][opname]
  19. optype = operation["type"]
  20. qualifiers = operation["qualifiers"]
  21. show_isk = operation.get("isk", True)
  22. primary = __import__("random").randint(10, 99)
  23. secondary = __import__("random").randint(100000, 50000000)
  24. g.campaign_db.set_overview(cname, opname, primary, secondary)
  25. def get_current():
  26. """Return the name of the currently selected campaign, or None."""
  27. if not config["enabled"]:
  28. return None
  29. setting = g.auth.get_character_modprop("campaigns", "current")
  30. if not setting or setting not in config["enabled"]:
  31. return config["enabled"][0]
  32. return setting
  33. def get_overview(cname, opname):
  34. """Return overview information for the given campaign/operation.
  35. The overview is a 2-tuple of (primary_count, secondary_count). The latter
  36. may be None, in which case it should not be displayed.
  37. Updates the database if necessary, so this can take some time.
  38. """
  39. maxdelta = timedelta(seconds=_MAX_STALENESS)
  40. with _lock:
  41. last_updated = g.campaign_db.check_operation(cname, opname)
  42. if last_updated is None:
  43. logger.debug("Adding campaign=%s operation=%s", cname, opname)
  44. _update_operation(cname, opname, new=True)
  45. g.campaign_db.add_operation(cname, opname)
  46. elif datetime.utcnow() - last_updated > maxdelta:
  47. logger.debug("Updating campaign=%s operation=%s", cname, opname)
  48. _update_operation(cname, opname, new=False)
  49. g.campaign_db.touch_operation(cname, opname)
  50. else:
  51. logger.debug("Using cache for campaign=%s operation=%s",
  52. cname, opname)
  53. return g.campaign_db.get_overview(cname, opname)
  54. def get_summary(name, opname, limit=5):
  55. """Return a sample fraction of results for the given campaign/operation."""
  56. ...
  57. return []
  58. def get_unit(operation, num, primary=True):
  59. """Return the correct form of the unit tracked by the given operation."""
  60. if not primary:
  61. return "ISK"
  62. types = {
  63. "killboard": "ship|ships",
  64. "collection": "item|items"
  65. }
  66. if "unit" in operation:
  67. unit = operation["unit"]
  68. else:
  69. unit = types[operation["type"]]
  70. if "|" in unit:
  71. return unit.split("|")[0 if num == 1 else 1]
  72. return unit