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

Hi,

I implemented chat system in my game. The number of chat messages the player can send is stored in storage

Everytime the player send a message

The register hook func will be called “ChannelMessageSend”

Inside the func i will read storage, then decrease message count by one then write to storage again

Is there a better way?
Is reading/writing storage for each message for players is expensive?

Using Go

@Mohammed The approach you’ve taken I would not recommend at all. It doesn’t scale well and I think its unnecessary because the chat message system is designed so that you can cache the cursor received when you list chat messages.

When the game client starts up you can check whether you’ve got a cursor cached locally (I’d recommend you use PlayerPrefs) and then list chat message history with the cursor. You catch yourself up to the most recent message and then you just take the count of messages you’ve listed up to; it is the count of unread messages for this user to see.

@novabyte, thank you for replaying but I think i did not make my question clear.

I mean each player will have a limited number of messages for chatting.

Lets say a player logged for first time. He will get free 30 messages. If he send to a friend (A) 5 messages. The remaining is 25 messages and so on

As we know we are going to store the number of messages in account metadata or storage or even wallet

How to control the number of chat mesaages the player has send and prevent him if he dont have any messages remaining

my current approach is to use

The register “ChannelMessageSend” hook func will be called whenever the player send a message

My concern is that for each message sent i will have to

read storage => unmarshal json => decrease measages cout by 1 => marshal => store it

This will be done for each chat message

Is this how things normally done? Or there is a better approach.

This will be done for each chat message
Is this how things normally done? Or there is a better approach.

Hmmm :thinking: 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? The ability for players to freely chat between each other creates great retention in a social game. I think if you limit that ability it could dramatically hurt your retention numbers.

In any case if that’s really what you want to achieve then I think the approach you’re taking would work fine.

read storage => unmarshal json => decrease measages cout by 1 => marshal => store it

I think given the unique requirements I’d just use a JSONB query to do the partial update as a single operation. Let me know if you need some pointers on it.

@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

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

@Mohammed I don’t really follow what you need to achieve your use case. Sorry. Can you summarize what you’re stuck on?

@novabyte
Sorry my bad.
I have to review my design and arrange my thoughts. For now things are clear. Thank you.

1 Like