A corporation manager and dashboard for EVE Online
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

94 lignes
3.1 KiB

  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, sortby=None, limit=5):
  46. """Return some details for the given campaign/operation."""
  47. optype = config["campaigns"][cname]["operations"][opname]["type"]
  48. if optype == "killboard":
  49. sorts = ["new", "old", "value"]
  50. renderer = "killboard_recent"
  51. func = g.campaign_db.get_associated_kills
  52. elif optype == "collection":
  53. sorts = ["value", "quantity", "price"]
  54. renderer = "collection_items"
  55. func = g.campaign_db.get_associated_items
  56. else:
  57. raise RuntimeError("Unknown operation type: %s" % optype)
  58. if sortby not in sorts:
  59. sortby = sorts[0]
  60. data = func(cname, opname, sort=sortby, limit=limit)
  61. return data, renderer
  62. def get_unit(operation, num, primary=True):
  63. """Return the correct form of the unit tracked by the given operation."""
  64. if not primary:
  65. return "ISK"
  66. types = {
  67. "killboard": "ship|ships",
  68. "collection": "item|items"
  69. }
  70. if "unit" in operation:
  71. unit = operation["unit"]
  72. else:
  73. unit = types[operation["type"]]
  74. if "|" in unit:
  75. return unit.split("|")[0 if num == 1 else 1]
  76. return unit