Short database schema:
- users (id)
- games (current_player_id) // has many cards
- cards (author_id, game_id, content, created_at)
- game_views(game_id, user_id) //shows which games user have seen
I need to find game for user, which meets all whose rules:
- game’s current_player is NULL (the game do not played by anybody right now)
- author of the last card in game(order by created_at, I think) is not user
- game have not been seen by user
I use PostgreSQL.
I think this is the most clear solution. With proper indexes, performance should be reasonable but its not going to scale well if the number of games gets into the millions. If that happens, one way to increase performance would be to store the last card_id at the game level.