I'm making a simple game engine, which implements room manipulation.
I was thinking a lot and still have doubts that I'm making rooms not in the valid way.
Here's the scenario.
1) There's one static room, where users are able to 'register'.
2) after certain number of users are registered, it should create dynamic room and put these certain number of users in that room and make them quit the static room.
With lua scripting, you can have a script like this:
-- add new user to static room redis.call('lpush', 'static_room', ARGV) -- if the number of static room reaches the limit local num = redis.call('llen', 'static_room') if num == 2 then -- get the room number for a new dynamic room local new_dynamic_room_num = redis.call('incr', 'dynamic_room'); local dynamic_room = 'dynamic_room' .. new_dynamic_room_num -- move all users from static room to dynamic room while true do local num = redis.call('llen', 'static_room') if num == 0 then break end redis.call('rpoplpush', 'static_room', dynamic_room) end end
Since the lua script executes atomically, no other user can join the static room before we finish moving all users from the static room to dynamic room.