Skip to content

Instantly share code, notes, and snippets.

@marcgg
Created November 20, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcgg/1f376b58ddca8ef45f47ee831dbc3c37 to your computer and use it in GitHub Desktop.
Save marcgg/1f376b58ddca8ef45f47ee831dbc3c37 to your computer and use it in GitHub Desktop.
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