/// <summary> /// Начало игры /// </summary> /// <param name="c"></param> /// <param name="opponentId"></param> /// <param name="password"></param> private void BeginGameHandler(SBRemoteClient c, Guid opponentId, string password) { SBRemoteClient opponent; if (c.State == KnownClientState.Free || c.State == KnownClientState.Ready) { using (_knownClients.Read()) { if (!_knownClients.Object.TryGetValue(opponentId, out opponent)) { opponent = null; } } if (opponent != null) { if (opponent.Password == password) { c.SetState(KnownClientState.InGame); opponent.SetState(KnownClientState.InGame); var g = new SBGame(c, opponent); g.OnScoreChanged += gc => GameScoreChangedHalder(gc); g.OnGameFinished += () => GameFinishedHandler(g); using (_games.Write()) { _games.Object.Add(g.Id, g); } this.SendToAll(new MsgNewGame() { Id = g.Id, PlayerA = g.PlayerA, PlayerB = g.PlayerB }); } else { c.Send(new MsgChatMessage() { Text = "Неверный пароль!" }); } } } }
/// <summary> /// Регистрация имени /// </summary> /// <param name="clnt"></param> private void RegisterNameHandler(SBRemoteClient clnt) { using (_unnamedClients.Write()) { _unnamedClients.Object.Remove(clnt); } KnownClientInfo[] clients; using (_knownClients.Read()) { clients = _knownClients.Object.Values.Select( c => new KnownClientInfo() { Id = c.Id, Name = c.Name, State = c.State } ).ToArray(); } clnt.Send(new MsgAddKnownClients() { Clients = clients }); Tuple <MsgNewGame, MsgUpdateGame>[] games; using (_games.Read()) { games = _games.Object.Values.Select( g => Tuple.Create( new MsgNewGame() { Id = g.Id, PlayerA = g.PlayerA, PlayerB = g.PlayerB }, new MsgUpdateGame() { Id = g.Id, ScoresA = g.ScoresA, ScoresB = g.ScoresB } ) ).ToArray(); } Array.ForEach(games, g => { clnt.Send(g.Item1); clnt.Send(g.Item2); }); using (_knownClients.Write()) { _knownClients.Object.Add(clnt.Id, clnt); } SendToAll(new MsgAddKnownClients() { Clients = new[] { new KnownClientInfo() { Id = clnt.Id, Name = clnt.Name, State = clnt.State } } }); }