public AuthorizePlayerResult AuthorizePlayer(string textEncoded, int gameEventsCounter, out PlayerInfo[] players) { players = null; var client = OperationContext.Current.GetCallbackChannel<IDuplexClient>(); StartAuthorizePlayerInfo info; lock (ourAuthorizingPlayerClients) { if (!ourAuthorizingPlayerClients.TryGetValue(client, out info)) return AuthorizePlayerResult.NoAuthorizationStarted; ourAuthorizingLoginClients.Remove(client); } Game game; Player player; lock (ourGamesAndPlayersLock) { if (!ourGames.TryGetValue(info.GameName, out game)) return AuthorizePlayerResult.NoSuchGame; player = game.PlayerByName(info.PlayerName); if (player == null) return AuthorizePlayerResult.NoSuchPlayer; if (!EncryptionUtil.EncryptStringToString(info.TextToEncode, player.Password).Equals(textEncoded)) return AuthorizePlayerResult.WrongPassword; //TODO: notify others, start game if ready if (player.Client != null) ourPlayerClients.Remove(player.Client); Player oldPlayer; if (ourPlayerClients.TryGetValue(client, out oldPlayer)) oldPlayer.Client = null; player.Client = client; ourPlayerClients.Add(client, player); } lock(game) player.EventsReceived = gameEventsCounter; players = new PlayerInfo[game.Players.Count]; for(int i = 0; i < players.Length; i++) { Player p = game.Players[i]; players[i] = new PlayerInfo(p.Name, p.Color, p.Sex); } //TODO: do it later! lock (ourGamesAndPlayersLock) { if (game.Events.Count == 0 && game.Players.All(p => p.Client != null)) client.StartGame(); } return AuthorizePlayerResult.Ok; }
public CreateNewGameResult CreateNewGame(string gameName, string gamePasswordEncoded, PlayerInfo[] players) { if (players.Length < 2 || players.Length > 4) throw new ArgumentException(); var client = OperationContext.Current.GetCallbackChannel<IDuplexClient>(); string login; lock(ourLoggedInClients) { if (!ourLoggedInClients.TryGetValue(client, out login)) return CreateNewGameResult.LoginRequired; } string password; if (!ourLoginAndPasswords.TryGetValue(login, out password)) return CreateNewGameResult.LoginRequired; //? string gamePassword = EncryptionUtil.DecryptStringFromString(gamePasswordEncoded, password); lock(ourGamesAndPlayersLock) { if (ourGames.ContainsKey(gameName)) return CreateNewGameResult.GameNameInUse; if (ourGamesByOwner.ContainsKey(login)) return CreateNewGameResult.YouHaveActiveGame; var game = new Game(gameName, login); foreach(PlayerInfo playerInfo in players) game.AddPlayer(new Player(game, playerInfo.Name, playerInfo.Color, playerInfo.Sex, gamePassword)); ourGames.Add(gameName, game); ourGamesByOwner.Add(login, game); } return CreateNewGameResult.Ok; }