-
Notifications
You must be signed in to change notification settings - Fork 28
Send raid lobbies to webhook #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a7d55b3
812d336
518e959
d9b8c68
ad3b092
0bc02ef
137f4bb
ba4419d
f1e6b64
563b0fb
3b8e4e3
94c1408
686f2ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,8 @@ type Gym struct { | |
| PowerUpPoints null.Int `db:"power_up_points"` | ||
| PowerUpEndTimestamp null.Int `db:"power_up_end_timestamp"` | ||
| Description null.String `db:"description"` | ||
| LobbyPlayerCount int32 | ||
| LobbyJoinEndMs int64 | ||
| //`id` varchar(35) NOT NULL, | ||
| //`lat` double(18,14) NOT NULL, | ||
| //`lon` double(18,14) NOT NULL, | ||
|
|
@@ -379,11 +381,75 @@ func createGymFortWebhooks(oldGym *Gym, gym *Gym) { | |
| } | ||
| } | ||
|
|
||
| func makeRaidWebhook(gym *Gym) (payload map[string]interface{}) { | ||
| payload = map[string]interface{}{ | ||
| "gym_id": gym.Id, | ||
| "gym_name": func() string { | ||
| if !gym.Name.Valid { | ||
| return "Unknown" | ||
| } else { | ||
| return gym.Name.String | ||
| } | ||
| }(), | ||
| "gym_url": gym.Url.ValueOrZero(), | ||
| "latitude": gym.Lat, | ||
| "longitude": gym.Lon, | ||
| "team_id": gym.TeamId.ValueOrZero(), | ||
| "spawn": gym.RaidSpawnTimestamp.ValueOrZero(), | ||
| "start": gym.RaidBattleTimestamp.ValueOrZero(), | ||
| "end": gym.RaidEndTimestamp.ValueOrZero(), | ||
| "level": gym.RaidLevel.ValueOrZero(), | ||
| "pokemon_id": gym.RaidPokemonId.ValueOrZero(), | ||
| "cp": gym.RaidPokemonCp.ValueOrZero(), | ||
| "gender": gym.RaidPokemonGender.ValueOrZero(), | ||
| "form": gym.RaidPokemonForm.ValueOrZero(), | ||
| "alignment": gym.RaidPokemonAlignment.ValueOrZero(), | ||
| "costume": gym.RaidPokemonCostume.ValueOrZero(), | ||
| "evolution": gym.RaidPokemonEvolution.ValueOrZero(), | ||
| "move_1": gym.RaidPokemonMove1.ValueOrZero(), | ||
| "move_2": gym.RaidPokemonMove2.ValueOrZero(), | ||
| "ex_raid_eligible": gym.ExRaidEligible.ValueOrZero(), | ||
| "is_exclusive": gym.RaidIsExclusive.ValueOrZero(), | ||
| "sponsor_id": gym.SponsorId.ValueOrZero(), | ||
| "partner_id": gym.PartnerId.ValueOrZero(), | ||
| "power_up_points": gym.PowerUpPoints.ValueOrZero(), | ||
| "power_up_level": gym.PowerUpLevel.ValueOrZero(), | ||
| "power_up_end_timestamp": gym.PowerUpEndTimestamp.ValueOrZero(), | ||
| "ar_scan_eligible": gym.ArScanEligible.ValueOrZero(), | ||
| "lobby_player_count": gym.LobbyPlayerCount, | ||
| "lobby_join_end_ms": gym.LobbyJoinEndMs, | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func CreateRaidLobbyPlayerCountWebhooks(ctx context.Context, db db.DbDetails, lobby *pogo.RaidLobbyPlayerCountProto) bool { | ||
| gymMutex, _ := gymStripedMutex.GetLock(lobby.GymId) | ||
| gymMutex.Lock() | ||
| defer gymMutex.Unlock() | ||
|
|
||
| gym, _ := getGymRecord(ctx, db, lobby.GymId) | ||
| if gym == nil { // skip reporting for unseen gyms | ||
| return false | ||
| } | ||
| if gym.LobbyPlayerCount == lobby.PlayerCount && | ||
| (lobby.PlayerCount == 0 || gym.LobbyJoinEndMs == lobby.LobbyJoinUntilMs) { | ||
| // skip unchanged lobbies or empty lobby updates | ||
| return false | ||
| } | ||
| gym.LobbyPlayerCount = lobby.PlayerCount | ||
| gym.LobbyJoinEndMs = lobby.LobbyJoinUntilMs | ||
| gymCache.Set(gym.Id, *gym, ttlcache.DefaultTTL) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you're doing here - reproducing the change logic in save. I wonder if this is the clearest way (I'm not objecting here, just musing). The other alternative would be to call saveRecord as normal and have it decide there is no need to commit to database (ie there are no record changes), but still worth sending a webhook since there are raid lobby changes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like the best option for now. |
||
| payload := makeRaidWebhook(gym) | ||
| areas := MatchStatsGeofence(gym.Lat, gym.Lon) | ||
| webhooks.AddMessage(webhooks.Raid, payload, areas) | ||
| return true | ||
| } | ||
|
|
||
| func createGymWebhooks(oldGym *Gym, gym *Gym) { | ||
| areas := MatchStatsGeofence(gym.Lat, gym.Lon) | ||
| if oldGym == nil || | ||
| (oldGym.AvailableSlots != gym.AvailableSlots || oldGym.TeamId != gym.TeamId || oldGym.InBattle != gym.InBattle) { | ||
| gymDetails := GymDetailsWebhook{ | ||
| webhooks.AddMessage(webhooks.GymDetails, GymDetailsWebhook{ | ||
| Id: gym.Id, | ||
| Name: gym.Name.ValueOrZero(), | ||
| Url: gym.Url.ValueOrZero(), | ||
|
|
@@ -400,9 +466,7 @@ func createGymWebhooks(oldGym *Gym, gym *Gym) { | |
| }(), | ||
| ExRaidEligible: gym.ExRaidEligible.ValueOrZero(), | ||
| InBattle: func() bool { return gym.InBattle.ValueOrZero() != 0 }(), | ||
| } | ||
|
|
||
| webhooks.AddMessage(webhooks.GymDetails, gymDetails, areas) | ||
| }, areas) | ||
| } | ||
|
|
||
| if gym.RaidSpawnTimestamp.ValueOrZero() > 0 && | ||
|
|
@@ -415,43 +479,7 @@ func createGymWebhooks(oldGym *Gym, gym *Gym) { | |
|
|
||
| if (raidBattleTime > now && gym.RaidLevel.ValueOrZero() > 0) || | ||
| (raidEndTime > now && gym.RaidPokemonId.ValueOrZero() > 0) { | ||
| raidHook := map[string]interface{}{ | ||
| "gym_id": gym.Id, | ||
| "gym_name": func() string { | ||
| if !gym.Name.Valid { | ||
| return "Unknown" | ||
| } else { | ||
| return gym.Name.String | ||
| } | ||
| }(), | ||
| "gym_url": gym.Url.ValueOrZero(), | ||
| "latitude": gym.Lat, | ||
| "longitude": gym.Lon, | ||
| "team_id": gym.TeamId.ValueOrZero(), | ||
| "spawn": gym.RaidSpawnTimestamp.ValueOrZero(), | ||
| "start": gym.RaidBattleTimestamp.ValueOrZero(), | ||
| "end": gym.RaidEndTimestamp.ValueOrZero(), | ||
| "level": gym.RaidLevel.ValueOrZero(), | ||
| "pokemon_id": gym.RaidPokemonId.ValueOrZero(), | ||
| "cp": gym.RaidPokemonCp.ValueOrZero(), | ||
| "gender": gym.RaidPokemonGender.ValueOrZero(), | ||
| "form": gym.RaidPokemonForm.ValueOrZero(), | ||
| "alignment": gym.RaidPokemonAlignment.ValueOrZero(), | ||
| "costume": gym.RaidPokemonCostume.ValueOrZero(), | ||
| "evolution": gym.RaidPokemonEvolution.ValueOrZero(), | ||
| "move_1": gym.RaidPokemonMove1.ValueOrZero(), | ||
| "move_2": gym.RaidPokemonMove2.ValueOrZero(), | ||
| "ex_raid_eligible": gym.ExRaidEligible.ValueOrZero(), | ||
| "is_exclusive": gym.RaidIsExclusive.ValueOrZero(), | ||
| "sponsor_id": gym.SponsorId.ValueOrZero(), | ||
| "partner_id": gym.PartnerId.ValueOrZero(), | ||
| "power_up_points": gym.PowerUpPoints.ValueOrZero(), | ||
| "power_up_level": gym.PowerUpLevel.ValueOrZero(), | ||
| "power_up_end_timestamp": gym.PowerUpEndTimestamp.ValueOrZero(), | ||
| "ar_scan_eligible": gym.ArScanEligible.ValueOrZero(), | ||
| } | ||
|
|
||
| webhooks.AddMessage(webhooks.Raid, raidHook, areas) | ||
| webhooks.AddMessage(webhooks.Raid, makeRaidWebhook(gym), areas) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main advantage (as I understand it) of using named returns is that you have immediately created the return type. You then go on and re-assign it, which gives rise to an additional allocation rather than having pre-made one.
Whilst this is not the world's biggest thing, I haven't wrapped my head around when it is good stylistically to use this, vs not recommended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if that is not already optimized by the compiler already, a map allocation should still be virtually for free.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect you're right, it was more a style discussion. It seems the go language recommendations are only to use it on short functions where you can see the definition and the return on a single page, and warning against re-using the variable because it can be shadowed.