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(); } }
public override void OnOpen() { Debug.WriteLine(WebSocketContext.Headers); lock (lockObj) { AllClients.Add(this); // クエリストリングを自力で解析 playerName = this.WebSocketContext.QueryString["name"]; var mc = Regex.Matches(this.WebSocketContext.RequestUri.OriginalString, @"/play/(A|B)/([\-A-Z0-9]*)\?"); if (mc.Count > 0) { rule = mc[0].Groups[1].Success ? mc[0].Groups[1].Value.ToString() : "A"; sessionId = mc[0].Groups[2].Success ? mc[0].Groups[2].Value.ToString() : "1"; } room = PlayRoomsManager.GetOrCreate(sessionId, rule); var key = this.WebSocketContext.CreatePlayerKey(playerName); pAdapter = room.FindRemotePlayer(key) as GamePlayerAdapter; if (pAdapter != null && !pAdapter.IsConnected) { // 再接続とみなす logger.Debug("Reconnect [{0}] sessionId={1}", playerName, sessionId); pAdapter.Reconnect(playerName, (str) => Send(str)); } else { // すでにプレイ中なら切断する logger.Debug("Connect [{0}] sessionId={1}", playerName, sessionId); if (room == null || room.NumOfRounds > 0) { this.Close(); return; } pAdapter = new GamePlayerAdapter(room, playerName, (str) => Send(str)); room.AddPlayer(key, pAdapter); } } }
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/ruletest/([\-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) { // 他のプレイヤーを追加 p1 = new TestcasePlayer("革命マシン", (c) => room.Master.PutCards(p1, c), TestSettings.Player1Hands); p2 = new TestcasePlayer("早あがり太郎", (c) => room.Master.PutCards(p2, c), TestSettings.Player2Hands); room.AddPlayer("1", p1); room.AddPlayer("2", p2); room.Master.CardDistributer = new TestCardDistributer(TestSettings.Player0InitialDeck, p1.GetInitialDeck(), p2.GetInitialDeck()); // ゲーム開始 room.Master.Start(); } }