Created
November 20, 2016 13:49
-
-
Save marcgg/1f376b58ddca8ef45f47ee831dbc3c37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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