//Send founded match info public string SendMatch(string msg, int clientID) { int matchID; string playerName; lock (players) { matchID = players[clientID].matchID; playerName = players[clientID].name; } Player p1, p2; lock (games) { p1 = games[matchID].p1; p2 = games[matchID].p2; } lock (players) { if (p1.name == playerName) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.SEARCH_GAME, p2.name, p2.elo.ToString(), p1.elo.ToString())); } return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.SEARCH_GAME, p1.name, p1.elo.ToString(), p2.elo.ToString())); } }
static void Main(string[] args) { DbMethods m = new DbMethods(); Console.WriteLine(TransmisionProtocol.CreateServerMessage(0, 1, m.GetMatchHistoryData("test"))); //ServerConnection server = new ServerConnection(); //server.RunServer(); }
private string Rank(string msg, int clientID) { lock (players) if (players[clientID].sessionId == null) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NOT_LOGGED_IN, (int)Options.RANK)); } try { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.RANK, dbConnection.GetRankData())); } catch (Exception e) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.DB_CONNECTION_ERROR, (int)Options.RANK, e.Message)); } }
public string CheckUserName(string msg, int clientID) { string[] fields = msg.Split("$$"); string username = fields[0].Split(':')[1]; if (!dbConnection.CheckIfNameExist(username)) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.CHECK_USER_NAME)); } return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.USER_ALREADY_EXISTS, (int)Options.CHECK_USER_NAME)); }
private string SearchGame(string msg, int clientID) { lock (players) if (players[clientID].sessionId == null) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NOT_LOGGED_IN, (int)Options.SEARCH_GAME)); } lock (playersWaitingForGame) { if (playersWaitingForGame.Contains(clientID)) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.GAME_IS_ALREADY_SEARCHED, (int)Options.SEARCH_GAME)); } playersWaitingForGame.Add(clientID); } //lock (players) players[clientID].matchID = -1; return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.SEARCH_GAME)); }
private string SendMove(string msg, int clientID) { int matchID; lock (players) { if (players[clientID].sessionId == null) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NOT_LOGGED_IN, (int)Options.SEND_MOVE)); } if (players[clientID].matchID < 0) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NOT_IN_GAME, (int)Options.SEND_MOVE)); } matchID = players[clientID].matchID; } string[] fields = msg.Split("$$", StringSplitOptions.RemoveEmptyEntries); string f = fields[1].Split(":", StringSplitOptions.RemoveEmptyEntries)[1]; int move = Int32.Parse(f); lock (games[matchID]) { lock (players) { bool check_move = games[matchID].move(move, players[clientID]); if (check_move) { if (players[clientID].name == games[matchID].p1.name) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.SEND_MOVE, String.Format("{0}-{1}", games[matchID].p1Points, games[matchID].p2Points))); } else { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.SEND_MOVE, String.Format("{1}-{0}", games[matchID].p1Points, games[matchID].p2Points))); } } return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.MOVE_NOT_ALLOWED, (int)Options.SEND_MOVE, "Niedozwolony ruch")); } } }
//TODO //End game and set this client to lose private string Logout(string msg, int clientID) { if (players[clientID].sessionId == null) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NOT_LOGGED_IN, (int)Options.LOGOUT)); } lock (players) { if (players[clientID].matchID > 0) { lock (games) { //End game and set this client to lose } } players[clientID].sessionId = null; } return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.LOGOUT)); }
// Try to create new user public string CreateUser(string msg, int clientID) { string[] fields = msg.Split("$$", StringSplitOptions.RemoveEmptyEntries); string username = fields[0].Split(':', StringSplitOptions.RemoveEmptyEntries)[1]; string password = fields[1].Split(':', StringSplitOptions.RemoveEmptyEntries)[1]; if (dbConnection.CheckIfNameExist(username)) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.USER_ALREADY_EXISTS, (int)Options.CREATE_USER)); } password = security.HashPassword(password); if (dbConnection.AddNewUser(username, password)) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.CREATE_USER)); } else { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.CREATE_USER)); } }
//Create answer with client match history private string MatchHistory(string msg, int clientID) { string playerName = ""; lock (players) { if (players[clientID].sessionId == null) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NOT_LOGGED_IN, (int)Options.MATCH_HISTORY)); } try { playerName = players[clientID].name; } catch { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.USER_NOT_FOUND, (int)Options.MATCH_HISTORY)); } } try { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.MATCH_HISTORY, dbConnection.GetMatchHistoryData(playerName))); } catch (Exception e) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.DB_CONNECTION_ERROR, (int)Options.MATCH_HISTORY)); } }
// Try to login client //TODO Add username to user public string Login(string msg, int clientID) { string[] fields = msg.Split("$$", StringSplitOptions.RemoveEmptyEntries); string username = fields[0].Split(':', StringSplitOptions.RemoveEmptyEntries)[1]; string password = fields[1].Split(':', StringSplitOptions.RemoveEmptyEntries)[1]; string sessionId = ""; string passwordHash = ""; try { passwordHash = dbConnection.GetUserPassword(username); } catch (Exception e) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.USER_NOT_FOUND, (int)Options.LOGIN)); } if (security.VerifyPassword(passwordHash, password)) { string elo; lock (players) { if (players[clientID].sessionId == null) { players[clientID].GenerateSessionId(); players[clientID].elo = dbConnection.GetClientElo(username); elo = players[clientID].elo.ToString(); players[clientID].name = username; } else { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.USER_ALREADY_LOGGED_IN, (int)Options.LOGIN)); } sessionId = players[clientID].sessionId; } return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.LOGIN, sessionId, elo)); } else { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.INCORRECT_PASSWORD, (int)Options.LOGIN)); } }
private string EndGame(string msg, int clientID) { return(TransmisionProtocol.CreateServerMessage((int)ErrorCodes.NO_ERROR, (int)Options.END_GAME)); }