A corporation manager and dashboard for EVE Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

89 lines
2.8 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. CampaignDB.path = str(Path(MainDB.path).parent / "db_campaigns.sqlite3")
  11. app.before_request(CampaignDB.pre_hook)
  12. app.teardown_appcontext(CampaignDB.post_hook)
  13. _lock = Lock()
  14. def _update_operation(cname, opname, new):
  15. """Update a campaign/operation."""
  16. ...
  17. operation = config["campaigns"][cname]["operations"][opname]
  18. optype = operation["type"]
  19. qualifiers = operation["qualifiers"]
  20. show_isk = operation.get("isk", True)
  21. primary = 42
  22. secondary = 63
  23. g.campaign_db.set_overview(cname, opname, primary, secondary)
  24. def get_current():
  25. """Return the name of the currently selected campaign, or None."""
  26. if not config["enabled"]:
  27. return None
  28. setting = g.auth.get_character_modprop("campaigns", "current")
  29. if not setting or setting not in config["enabled"]:
  30. return config["enabled"][0]
  31. return setting
  32. def get_overview(cname, opname):
  33. """Return overview information for the given campaign/operation.
  34. The overview is a 2-tuple of (primary_count, secondary_count). The latter
  35. may be None, in which case it should not be displayed.
  36. Updates the database if necessary, so this can take some time.
  37. """
  38. with _lock:
  39. last_updated = g.campaign_db.check_operation(cname, opname)
  40. if last_updated is None:
  41. logger.debug("Adding campaign=%s operation=%s", cname, opname)
  42. _update_operation(cname, opname, new=True)
  43. g.campaign_db.add_operation(cname, opname)
  44. elif datetime.utcnow() - last_updated > timedelta(seconds=60 * 60):
  45. logger.debug("Updating campaign=%s operation=%s", cname, opname)
  46. _update_operation(cname, opname, new=False)
  47. g.campaign_db.touch_operation(cname, opname)
  48. else:
  49. logger.debug("Using cache for campaign=%s operation=%s",
  50. cname, opname)
  51. return g.campaign_db.get_overview(cname, opname)
  52. def get_summary(name, opname, limit=5):
  53. """Return a sample fraction of results for the given campaign/operation."""
  54. ...
  55. return []
  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