Inheritance: WebSocketSharp.Server.WebSocketBehavior
        /**
         *
         * @param {!Client} client Websocket client that's connected to
         *        the game.
         * @param {!RelayServer} relayserver relayserver the game is
         *        connected to.
         * @param {Game~GameOptions} data Data sent from the game which
         *        includes
         */
        public HFTGame AssignClient(HFTSocket client, HFTRuntimeOptions data)
        {
            // If there are no games make one
            // If multiple games are allowed make one
            // If multiple games are not allowed re-assign
            string newGameId = !String.IsNullOrEmpty(data.id) ? data.id : ("_hft_" + nextGameId_++);
            HFTGame game = new HFTGame(newGameId, this, data);
            // Add it to 'games' immediately because if we remove the old game games would go to 0
            // for a moment and that would trigger this GameGroup getting removed because there'd be no games
            if (masterGame_ == null)
            {
                masterGame_ = game;
            }
            HFTGame oldGame = null;
            // See if there's an old game with the same id then replace it
            games_.TryGetValue(newGameId, out oldGame);
            games_[newGameId] = game;

            if (oldGame != null)
            {
                log_.Info("tell old game to quit");
                oldGame.SendQuit();
                oldGame.Close();
            }

            log_.Info("add game: num games = " + games_.Count);
            game.AssignClient(client, data);

            if (data.master)
            {
                masterGame_ = game;
            }

            return game;
        }
 public void AssignAsClientForGame(HFTRuntimeOptions data, HFTSocket client)
 {
     string gameId = String.IsNullOrEmpty(data.gameId) ? "HFTUnity" : data.gameId;
     HFTGameGroup gameGroup = GetGameGroup(gameId, true);
     if (!gameGroup.HasClient())
     {
         ++gameCount_;
     }
     gameGroup.AssignClient(client, data);
 }
Example #3
0
        public void AssignAsClientForGame(HFTRuntimeOptions data, HFTSocket client)
        {
            string       gameId    = String.IsNullOrEmpty(data.gameId) ? "HFTUnity" : data.gameId;
            HFTGameGroup gameGroup = GetGameGroup(gameId, true);

            if (!gameGroup.HasClient())
            {
                ++gameCount_;
            }
            gameGroup.AssignClient(client, data);
        }
Example #4
0
        public HFTPlayer(HFTSocket client, HFTGameManager server, string id)
        {
            client_      = client;
            gameManager_ = server;
            id_          = id;

            log_ = new HFTLog("HFTPlayer[" + id + "]");
            log_.Info("start player");

            client.OnMessageEvent += HandleMessage;
            client.OnCloseEvent   += HandleDisconnect;

            RegisterCmdHandler <AddPlayerToGameMessage>("join", AddPlayerToGame);
            RegisterCmdHandler <HFTRuntimeOptions>("server", AssignAsServerForGame);
            RegisterCmdHandler <object>("update", PassMessageFromPlayerToGame);
        }
Example #5
0
        public void AssignClient(HFTSocket client, HFTRuntimeOptions data)
        {
            if (client_ != null)
            {
                log_.Error("this game already has a client!");
                client_.OnMessageEvent -= OnMessage;
                client_.OnCloseEvent   -= OnDisconnect;
                client_.Close();
            }

            client_ = client;

            client.OnMessageEvent += OnMessage;
            client.OnCloseEvent   += OnDisconnect;

            RegisterCmdHandler <object>("client", SendMessageToPlayer);
            RegisterCmdHandler <object>("broadcast", Broadcast);
            RegisterCmdHandler <HFTMessageSwitchGame>("switchGame", SwitchGame);
            RegisterCmdHandler <object>("peer", SendMessageToGame);
            RegisterCmdHandler <object>("bcastToGames", BroadcastToGames);
            RegisterCmdHandler <HFTMessageAddFile>("addFile", AddFile);

            // Tell the game it's id
            var gs = new HFTMessageGameStart();

            gs.id     = id_;
            gs.gameId = ""; //FIX!
            client.Send(new HFTRelayToGameMessage("gamestart", "", gs));

            // start each player
            foreach (var player in players_.Values)
            {
                client.Send(new HFTRelayToGameMessage("start", player.id, null));
            }

            // Not sure why I even have a sendQueue
            // as the game should be running before anyone
            // joins but it seems to be useful for debugging
            // since contollers start and often immediately
            // send a name and color cmd.
            foreach (var pair in sendQueue_.ToArray())
            {
                client.Send(pair.Value);
            }
            sendQueue_.Clear();
        }
Example #6
0
        /**
         *
         * @param {!Client} client Websocket client that's connected to
         *        the game.
         * @param {!RelayServer} relayserver relayserver the game is
         *        connected to.
         * @param {Game~GameOptions} data Data sent from the game which
         *        includes
         */
        public HFTGame AssignClient(HFTSocket client, HFTRuntimeOptions data)
        {
            // If there are no games make one
            // If multiple games are allowed make one
            // If multiple games are not allowed re-assign
            string  newGameId = !String.IsNullOrEmpty(data.id) ? data.id : ("_hft_" + nextGameId_++);
            HFTGame game      = new HFTGame(newGameId, this, data);

            // Add it to 'games' immediately because if we remove the old game games would go to 0
            // for a moment and that would trigger this GameGroup getting removed because there'd be no games
            if (masterGame_ == null)
            {
                masterGame_ = game;
            }
            HFTGame oldGame = null;

            // See if there's an old game with the same id then replace it
            games_.TryGetValue(newGameId, out oldGame);
            games_[newGameId] = game;

            if (oldGame != null)
            {
                log_.Info("tell old game to quit");
                oldGame.SendQuit();
                oldGame.Close();
            }

            log_.Info("add game: num games = " + games_.Count);
            game.AssignClient(client, data);

            if (data.master)
            {
                masterGame_ = game;
            }

            return(game);
        }
 public void AddPlayer(HFTSocket client)
 {
     new HFTPlayer(client, this, (++connectCount_).ToString());
 }
Example #8
0
 public void AddPlayer(HFTSocket client)
 {
     new HFTPlayer(client, this, (++connectCount_).ToString());
 }