Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class Player
def self.all
REDIS.smembers("player_ids").map do |player_id|
JSON.parse(REDIS.get("player_#{player_id}"))
end
end
def self.add(player_json)
player = JSON.parse(player_json)
raise "Player is blank" if player.blank?
REDIS.sadd("player_ids", player["id"])
REDIS.set("player_#{player["id"]}", player_json)
broadcast
REDIS.incr("count_players")
end
def self.link(player, uuid)
player_id = JSON.parse(player)["id"]
raise "Player is blank" if player_id.blank?
REDIS.set("player_ref_#{uuid}", player_id)
end
def self.delete(uuid)
player_id = id_from_uuid(uuid)
REDIS.srem("player_ids", player_id)
broadcast
end
def self.delete_all
puts "DELETE ALL!"
REDIS.del("players")
broadcast
end
private
def self.id_from_uuid(uuid)
REDIS.get("player_ref_#{uuid}")
end
def self.broadcast
ActionCable.server.broadcast(
"game",
{
type: "player_update",
players: Player.all
}
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment