A corporation manager and dashboard for EVE Online
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

getters.py 2.8 KiB

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