Created
          October 4, 2014 16:30 
        
      - 
      
- 
        Save nickgartmann/02952dc47a1c40e14b44 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or 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
    
  
  
    
  | defmodule Pew.Ship do | |
| use Phoenix.Channel | |
| :timer.apply_interval(1000, __MODULE__, :gc, []) | |
| def gc() do | |
| IO.puts "stuff" | |
| end | |
| def join(socket, "join", msg) do | |
| reply socket, "connect", %{ships: Ships.get} | |
| {:ok, socket} | |
| end | |
| def event(socket, "new", data) do | |
| ship = Ship.new(data["x"], data["y"], data["rotation"], data["name"]) | |
| Ships.put(ship) | |
| broadcast socket, "new", ship | |
| reply socket, "new:you", ship | |
| socket | |
| end | |
| def event(socket, "update", data) do | |
| ship = Ships.get(data["id"]) | |
| broadcast socket, "update", data | |
| # Merge the new data onto the ship, taking into account | |
| # that data has string keys, and ship has atomic keys | |
| ship = Enum.reduce Map.keys(data), ship, fn(el, acc) -> | |
| Map.put(acc, String.to_atom(el), Map.get(data, el)) | |
| end | |
| Map.put(ship, :last_update, Timex.Time.now(:msecs)) | |
| Ships.put(ship) | |
| socket | |
| end | |
| # Tell everyone to drop the disconnected ship | |
| def event(socket, "disconnect", data) do | |
| Ships.remove(data["id"]) | |
| broadcast socket, "remove", data | |
| socket | |
| end | |
| end | 
        
      
            jeregrine
  
      
      
      commented 
        Oct 4, 2014 
      
    
  
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment