Skip to content

Add the first of the SSE event broadcasting functionality as originally planned#144

Merged
Bash-09 merged 33 commits intomainfrom
Lilith/Sse-events
May 30, 2024
Merged

Add the first of the SSE event broadcasting functionality as originally planned#144
Bash-09 merged 33 commits intomainfrom
Lilith/Sse-events

Conversation

@lili7h
Copy link
Copy Markdown
Contributor

@lili7h lili7h commented Apr 29, 2024

Way back yonder when things were a lot simpler, we created the footings for SSE (Server-Sent Events) broadcasting, with the intention that this channel would be how the backend alerts the frontend to things, without the frontend have to poll for it. We uh, never used it.... UNTIL NOW

This is a simple implementation, but it lays a lot of ground work that would make copy pasting further SSE broadcasting quite the easier process.

We broadcast 3 subtypes of ConsoleOutput messages - ChatMessage events, Kill events, and DemoStop events. These are broadcast to subscribers as a JSON blob that looks something like the follow:

{
  "type":"ChatMessage",
  "uuid":"c7bcd50a-6f5d-4a84-8fb7-01b906ed15b7",
  "time":"2024-04-29T11:59:56.979901203Z",
  "event": {
    "player_name":"Lilith",
    "steamid":"76561198071482715",
    "message":"this is for the haters"
  }
}

{
  "type":"PlayerKill",
  "uuid":"c486298d-155b-4ccc-9bbd-876e9e4802ac",
  "time":"2024-04-29T12:00:09.269070783Z",
  "event": {
    "killer_name":"WITCH",
    "killer_steamid":null,
    "victim_name":"Lilith",
    "victim_steamid":"76561198071482715",
    "weapon":"tf_projectile_rocket",
    "crit":false
  }
}

{
  "type":"VoteStarted",
  "content": {
    "count":2,
    "option_1":"ctf_doublecross",
    "option_2":"Extend current Map",
    "option_3":"",
    "option_4":"",
    "option_5":"",
    "voteidx":3
  },
  "time":"2024-04-30T09:37:06.325924920Z",
  "uuid":"f8b59643-1724-4f61-98c6-f59d54d70cb1"
}

{
  "type":"VoteCast",
  "content": {
    "voter":76561198071482715,
    "voter_name":"Lilith",
    "choice":"ctf_doublecross",
    "event": {
      "vote_option":0,
      "team":0,
      "entity_id":1,
      "voteidx":3
    }
  },
  "time":"2024-04-30T09:37:06.325977329Z",
  "uuid":"2f2123ff-b274-4b21-a6c0-6b2f710cd80f"
}

Note that in the above PlayerKill example, WITCH is a bot, and thus doesnt have a steamID (so null is perfectly expected).

The intention is to use this same framework to broadcast votes to the frontend so it can display votes, vote options, and who voted for what, etc etc.

Use cases for the current messages? Cute little pop ups in the corner or something that show these things, to display game state and also useful as a meter for whether or not the client is actually properly reading TF2 data. Also, its COOL.

The code is a bit messy, so please eat me alive. Thanks.

FOR TESTING

You can use the python package requests-sse to subscribe to SSE endpoints and display messages as they come in. I have a short demo module (that is literally copy pasted sample code from the PyPI page for requests-sse, wrapped in a poetry project) that you can use: https://github.com/lili7h/http-sse-event-viewer

@lili7h
Copy link
Copy Markdown
Contributor Author

lili7h commented Apr 30, 2024

ISSUES: Windows build fails due to: winapi::ctypes::c_void and std::ffi::c_void have similar names, but are actually distinct types

Copy link
Copy Markdown
Contributor

@Bash-09 Bash-09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty cool so far, I hope we can get something using it soon.

Copy link
Copy Markdown
Contributor

@Bash-09 Bash-09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise everything looks pretty good. I'll be happy to approve and merge once you fix up the conflicts and address the last little bit of feedback.

@Bash-09
Copy link
Copy Markdown
Contributor

Bash-09 commented May 27, 2024

Looks like you just need to run cargo fmt I think

self.votes.insert(options.voteidx, values);

// Replay shunted messages if we have them. This ensures that we don't print VoteCast events for Vote we haven't seen the
// VoteOptions event for yet. Saves
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saves?

Copy link
Copy Markdown
Contributor

@Bash-09 Bash-09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming from your testing it still all behaves as expected. I haven't had a chance to do any in-depth testing but I trust your judgement and as far as I can tell the code looks fine 👍

@Bash-09 Bash-09 merged commit 8c5f30e into main May 30, 2024
@Bash-09 Bash-09 deleted the Lilith/Sse-events branch May 30, 2024 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants