generated from HoloTech/holotech-plugin-template
Add rudimentary websocket channel.
This commit is contained in:
+48
-20
@@ -16,17 +16,41 @@ from .data import (
|
||||
CampaignData,
|
||||
EarnedReward,
|
||||
)
|
||||
from .campaign import CampaignRegistry
|
||||
from .campaign import CampaignRegistry, RewardCampaign
|
||||
|
||||
|
||||
# ISO formatted timestamp
|
||||
ISOTimestamp: TypeAlias = str
|
||||
|
||||
|
||||
async def prepare_campaign(
|
||||
profiler: ProfilesRegistry, campaign: Campaign
|
||||
):
|
||||
return {}
|
||||
class CampaignPayload(TypedDict):
|
||||
communityid: int
|
||||
campaign_id: int
|
||||
campaign_name: str
|
||||
|
||||
started_at: ISOTimestamp | None
|
||||
completed_at: ISOTimestamp | None
|
||||
|
||||
reward_progress: int
|
||||
reward_cap: int | None
|
||||
|
||||
|
||||
async def prepare_campaign(campaign: RewardCampaign) -> CampaignPayload:
|
||||
reward_progress = len(await campaign.get_rewards())
|
||||
|
||||
return CampaignPayload(
|
||||
communityid=campaign.row.communityid,
|
||||
campaign_id=campaign.row.campaign_id,
|
||||
campaign_name=campaign.row.campaign_name,
|
||||
started_at=campaign.row.started_at.isoformat()
|
||||
if campaign.row.started_at
|
||||
else None,
|
||||
completed_at=campaign.row.completed_at.isoformat()
|
||||
if campaign.row.completed_at
|
||||
else None,
|
||||
reward_progress=reward_progress,
|
||||
reward_cap=campaign.row.target_rewards,
|
||||
)
|
||||
|
||||
|
||||
class CampaignChannel(Channel):
|
||||
@@ -61,30 +85,36 @@ class CampaignChannel(Channel):
|
||||
await super().on_connection(websocket, event)
|
||||
self.communities[cid].add(websocket)
|
||||
|
||||
# TODO: Prepare campaign for sending
|
||||
if campaign:
|
||||
payload = await prepare_campaign(self.profiler, campaign)
|
||||
# Fetch active campaign for this cid and send if it exists
|
||||
active = await self.campaigns.fetch_campaigns(cid)
|
||||
if active:
|
||||
# TODO: Presumes only one active campaign
|
||||
campaign = active[0]
|
||||
payload = await prepare_campaign(campaign)
|
||||
await self.send_campaign_update(cid, payload, websocket)
|
||||
else:
|
||||
await self.send_no_campaign(cid, websocket)
|
||||
|
||||
async def send_sample(self, websocket):
|
||||
import json
|
||||
import random
|
||||
with open("sample-payload.json") as f:
|
||||
payload = json.load(f)
|
||||
ending = utc_now() + timedelta(seconds=10)
|
||||
payload['args']['end_at'] = ending.isoformat()
|
||||
await self.send_event(payload, websocket=websocket)
|
||||
|
||||
payload = CampaignPayload(
|
||||
communityid=1,
|
||||
campaign_id=1,
|
||||
campaign_name="PartnerPlus2026",
|
||||
started_at=utc_now().isoformat(),
|
||||
completed_at=None,
|
||||
reward_progress=random.randint(0, 105),
|
||||
reward_cap=150,
|
||||
)
|
||||
await self.send_event(payload, websocket=websocket)
|
||||
|
||||
async def del_connection(self, websocket):
|
||||
for wss in self.communities.values():
|
||||
wss.discard(websocket)
|
||||
await super().del_connection(websocket)
|
||||
|
||||
async def send_campaign_update(
|
||||
self, communityid: int, payload, websocket=None
|
||||
):
|
||||
async def send_campaign_update(self, communityid: int, payload, websocket=None):
|
||||
for ws in (websocket,) if websocket else self.communities[communityid]:
|
||||
await self.send_event(
|
||||
{
|
||||
@@ -95,9 +125,7 @@ class CampaignChannel(Channel):
|
||||
websocket=ws,
|
||||
)
|
||||
|
||||
async def send_campaign_ended(
|
||||
self, communityid: int, payload, websocket=None
|
||||
):
|
||||
async def send_campaign_ended(self, communityid: int, payload, websocket=None):
|
||||
for ws in (websocket,) if websocket else self.communities[communityid]:
|
||||
await self.send_event(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user