# How could I count a user's sent chat messages?

**URL:** <https://forum.heroiclabs.com/t/how-could-i-count-a-users-sent-chat-messages/1573>\
**Category:** Game Design\
**Created:** [April 15, 2021, 9:49pm UTC](https://forum.heroiclabs.com/t/how-could-i-count-a-users-sent-chat-messages/1573 "2021-04-15T21:49:33Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Mohammed](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@Mohammed](https://forum.heroiclabs.com/u/Mohammed)\
**Post date:** [April 30, 2021, 9:21pm UTC](https://forum.heroiclabs.com/t/how-could-i-count-a-users-sent-chat-messages/1573/5 "2021-04-30T21:21:09Z")

</div>

@novabyte Sorry for late replay

> I’ve never seen a game which restricts chat messages to some kind of daily limit. I have seen rate limits implemented but never a hard count limit. Are you sure this is right for your game design?

I still did not finalize this approach, but you are right about

> I think if you limit that ability it could dramatically hurt your retention numbers.

I am trying to make a reasonable limit that usually fine for the normal usage.

so I spent sometime reading about jsonb functions and tried it, i found your answer

> [@Update Multiple keys with jsonb\_set](https://forum.heroiclabs.com/t/update-multiple-keys-with-jsonb-set/1509/3):
>
> Based on the structure of your storage object and only if you NEED to perform the update without a simple read before write using optimistic concurrency control as described in the storage engine documentation you can achieve what you want with SQL which looks like: UPDATE storage AS s SET value = value || jsonb\_build\_object( 'Cat\_Advance\_Anim', COALESCE((s."value"-\>\>'Cat\_Advance\_Anim')::INT + $4::INT, $4::INT)) || jsonb\_build\_object( 'Cat\_Simple\_AR', …

the SQL query will look like this for me

> UPDATE storage SET value = value  
> || jsonb\_build\_object(‘messageRemainingCount’, COALESCE((value-\>\> ‘s’)::INT - 1::INT, 0::INT))  
> WHERE collection = ‘PlayerData’ AND user\_id = $1 AND key = ‘profile’;

This will only change the count only  
can you please help if possible to do the below in one call to the databese

> if messageRemainingCount \> 0 {  
> messageRemainingCount–  
> return messageRemainingCount  
> }else{  
> return -1  
> }

I need return value to check if to allow the message to be sent or not

---

_[View the full topic](https://forum.heroiclabs.com/t/how-could-i-count-a-users-sent-chat-messages/1573)._
