public void ClientRegistered(string userName, string hashedPasswd) { if (Clients.ContainsKey(userName)) { throw new FaultException <UserExistsFault>(new UserExistsFault() { Details = "User name " + userName + " already exists. Try something else" }); } using (var ctx = new FourinrowDBContext()) { var user = (from u in ctx.Users where u.UserName == userName select u).FirstOrDefault(); if (user == null) { User newUser = new User() { UserName = userName, HashedPassword = hashedPasswd }; ctx.Users.Add(newUser); ctx.SaveChanges(); } ++NumClients; IFourRowServiceCallback callbackChannel = OperationContext.Current.GetCallbackChannel <IFourRowServiceCallback>(); Clients.Add(userName, callbackChannel); } }
public Tuple <MoveResult, int> ReportMove( string currentPlayer, string opponent, string whoMoved, int location, double pointX, double pointY) { try { string player = whoMoved == currentPlayer ? currentPlayer : opponent; string player2Notify = whoMoved == currentPlayer ? opponent : currentPlayer; Game theGame = _games[Tuple.Create(currentPlayer, opponent)]; IFourRowServiceCallback callback = ConnectedClient.ContainsKey(player2Notify) ? ConnectedClient[player2Notify] : null; Tuple <MoveResult, int> result = theGame.VerifyMove(currentPlayer, opponent, whoMoved, location); switch (result.Item1) { case MoveResult.NotYourTurn: case MoveResult.IllegalMove: return(result); case MoveResult.YouWon: case MoveResult.Draw: { Thread notifyOppnentWinOrTieThread = new Thread(() => WinOrDraw(currentPlayer, opponent, theGame, result.Item1, player)); notifyOppnentWinOrTieThread.Start(); break; } } Thread notifyOppnentMoveThread = new Thread(() => callback?.OtherPlayerMoved(result, pointX, pointY)); notifyOppnentMoveThread.Start(); return(result); } catch (DbException ex) { throw new FaultException <DbException>(ex); } catch (Exception ex) { throw new FaultException <Exception>(ex); } }
public void ClientConnected(string userName, string hashedPasswd) { if (ConnectedClient.ContainsKey(userName)) { throw new FaultException <UserAlreadyConnectedFault>(new UserAlreadyConnectedFault() { Details = "User name " + userName + " Already Connected" }); } using (var ctx = new FourinrowDBContext()) { var user = (from u in ctx.Users where u.UserName == userName select u).FirstOrDefault(); if (user == null) { throw new FaultException <UserDoesntExistsFault>(new UserDoesntExistsFault() { Details = "User name " + userName + " Doesnt exists. Please register" }); } if (user.HashedPassword != hashedPasswd) { throw new FaultException <WrongPasswordFault>(new WrongPasswordFault() { Details = "Worng PassWord!" }); } if (!Clients.ContainsKey(userName)) { IFourRowServiceCallback callbackChannel = OperationContext.Current.GetCallbackChannel <IFourRowServiceCallback>(); Clients.Add(userName, callbackChannel); } if (!ConnectedClient.ContainsKey(userName)) { ConnectedClient.Add(userName, Clients[userName]); } if (!_clientsThatNotPlay.Contains(userName)) { _clientsThatNotPlay.Add(userName); } ++ConnectedClientsCnt; } }