Last active
          August 29, 2015 14:13 
        
      - 
      
- 
        Save nickgartmann/3fe8401613ccf5b8cd93 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
    
  
  
    
  | var socket = new Phoenix.Socket(PewMiddle.config.socketPath); | |
| PewMiddle.requestGame = function(username, callback) { | |
| socket.join(PewMiddle.config.channel, "lobby", {username: username}, function(channel) { | |
| var player = null; | |
| channel.on("player:created", function(message) { | |
| player = message; | |
| }); | |
| channel.on("game:created", function(game) { | |
| callback(game.id, player); | |
| channel.leave(); | |
| }); | |
| }); | |
| }; | |
| PewMiddle.joinGame = function(gameId, player, eventReceiver, callback) { | |
| console.log(player); | |
| socket.join(PewMiddle.config.channel, gameId, player, function(channel) { | |
| channel.send("ship:target", {thin: "one"}); | |
| channel.send("ship:move", {thin: "two"}); | |
| channel.send("ship", {message: "also ship"}); | |
| channel.on("ship:move", function(message) { | |
| eventReceiver.shipMove(message.shipId, message.coordinates); | |
| }); | |
| channel.on("ship:targeted", function(message) { | |
| eventReceiver.shipTarget(message.shipId, message.targetShipId); | |
| }); | |
| channel.on("ship:create", function(message) { | |
| eventReceiver.shipCreate(message.ship); | |
| }); | |
| channel.on("ship:destroy", function(message) { | |
| eventReceiver.shipDestroy(message.shipId); | |
| }); | |
| callback({ | |
| moveShip: function(id, coordinates) { | |
| channel.send("ship:move", { | |
| shipId: id, | |
| coordinates: coordinates | |
| }); | |
| }, | |
| targetShip: function(id, targetShipId) { | |
| channel.send("ship:target", { | |
| shipId: id, | |
| targetShipId: targetShipId | |
| }); | |
| } | |
| }); | |
| }); | |
| }; | 
  
    
      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 Spacegame.GameChannel do | |
| use Phoenix.Channel | |
| def join(socket, "lobby", %{"username" => username}) do | |
| opponent = List.first(Players.lobby) | |
| player = Player.new(username, socket) | |
| Players.put(player) | |
| reply socket, "player:created", Player.json(player) | |
| # Find you an opponent | |
| if (opponent != nil) do | |
| game = Game.new([player, opponent]) | |
| Enum.each(game.players, fn(p) -> | |
| Players.put(Map.put(p, :in_game, true)) | |
| end) | |
| Games.put(game) | |
| reply socket, "game:created", Game.json(game) | |
| reply opponent.socket, "game:created", Game.json(game) | |
| end | |
| {:ok, socket} | |
| end | |
| def join(socket, topic, %{"id" => id, "username" => username}) do | |
| game = Games.get(topic) | |
| me = List.first(Enum.filter(game.players, fn(player) -> player.id == id end)) | |
| me = Map.put(me, :connected, true) | |
| me = Map.put(me, :socket, socket) | |
| Players.put(me) | |
| if(Enum.all?(game.players, fn(player) -> Players.get(player.id).connected end)) do | |
| Enum.each(game.players, fn(player) -> | |
| reply Players.get(player.id).socket, "start", %{} | |
| end) | |
| end | |
| {:ok, socket} | |
| end | |
| def event(socket, "ship", message) do | |
| IO.puts "OMG HERE" | |
| IO.inspect socket | |
| end | |
| def event(socket, "ship:target", message) do | |
| end | |
| end | 
  
    
      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 Spacegame.Router do | |
| use Phoenix.Router | |
| use Phoenix.Router.Socket, mount: "/ws" | |
| channel "game", Spacegame.GameChannel | |
| end | 
        
      
            chrismccord
  
      
      
      commented 
        Jan 10, 2015 
      
    
  
  def leave(socket, _reason) do
    Logger.debug "Leaving #{socket.topic}"
    socket
  end
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment