示例#1
0
        public override void OnOpen()
        {
            // nameパラメータの取り出し
            var playerName = this.WebSocketContext.QueryString["name"];

            if (string.IsNullOrWhiteSpace(playerName))
            {
                SendMessageAsJson("URL Parameter 'name' is not specified.");
                Close();
            }

            var mc = Regex.Matches(this.WebSocketContext.RequestUri.OriginalString, @"/test/practice/([\-A-Z0-9]+)");

            if (mc.Count > 0)
            {
                sessionId = mc[0].Groups[1].Success ? mc[0].Groups[1].Value.ToString() : "";

                if (!string.IsNullOrWhiteSpace(sessionId))
                {
                    room = PlayRoomsManager.Get(sessionId);
                }
            }

            if (room == null || !sessionId.StartsWith(ROOM_ID_PREFIX))
            {
                SendMessageAsJson("Invalid session ID");
                base.Close();
                return;
            }

            _startTimer(PLAYROOM_LIFETIME * 1000);

            var key = this.WebSocketContext.CreatePlayerKey(playerName);

            pAdapter = room.FindRemotePlayer(key) as GamePlayerAdapter;
            if (pAdapter != null && !pAdapter.IsConnected)
            {
                // 再接続とみなす
                pAdapter.Reconnect(playerName, (str) => Send(str));
            }
            else
            {
                // プレイヤー通信ハンドラを作成
                pAdapter = new GamePlayerAdapter(room, playerName, (str) => Send(str));
                pAdapter.TurnTimeoutSec = TIMEOUT_SEC;
                room.AddPlayer(key, pAdapter);
            }
            if (room.Master.NumOfPlayers == 1)
            {
                // 他のプレイヤーを追加
                room.DoComplementPlayers(4);

                // ゲーム開始
                room.Master.Start();
            }
        }
示例#2
0
        /// <summary>
        /// クライアントメッセージの処理
        /// </summary>
        /// <param name="jsonObj"></param>
        /// <returns></returns>
        public ProtocolData ProcMessage(JObject jsonObj)
        {
            string kind = null;

            try
            {
                kind = jsonObj["Kind"].ToString();
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Kindがありません");
            }

            if (kind == "Tweet")
            {
                string message;
                try
                {
                    message = jsonObj["Message"].ToString();
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Messageがありません");
                }
                room.Tweet(playerNum, message);
            }
            else if (kind == "Start")
            {
                // 自動的に不足人数を追加してゲーム開始する
                room.DoComplementPlayers(5);
                room.Master.Start();
            }
            else if (kind == "Put")
            {
                var ret = room.Master.PutCards(this, DeckGenerator.FromCardsetString(jsonObj["Cards"].ToString()));
                if (!(ret is CheckOK))
                {
                    return(new WebDaifugo.Neof5Protocols.ProtocolData(ret));
                }
                myTimer.Enabled = false;
            }
            return(null);
        }