public void PrepareBattle(ref FightingTupleWrapper fighting, ref MatchmakingListWrapper userIDsForMatchmaking) { if (userIDsForMatchmaking.userIDsForMatchmaking.Count >= 2) { mut.WaitOne(); // user values without other threads interfering int userOneID = userIDsForMatchmaking.userIDsForMatchmaking[0]; // get ids from matchmaking queue int userTwoID = userIDsForMatchmaking.userIDsForMatchmaking[1]; fighting.fighting = Tuple.Create <int, int, bool, bool>(userOneID, userTwoID, true, false); // set status for serverinfo userIDsForMatchmaking.userIDsForMatchmaking.Remove(userOneID); // remove userIDs from the matchmakin queue userIDsForMatchmaking.userIDsForMatchmaking.Remove(userTwoID); mut.ReleaseMutex(); //Coinflip bool turn = Convert.ToBoolean(RandoFunct(0, 1)); if (turn) { int tmp = userOneID; userOneID = userTwoID; userTwoID = tmp; } PostgreSqlClass DB = new PostgreSqlClass(); playerOneHand = DB.GetUserDeckCards(userOneID); playerTwoHand = DB.GetUserDeckCards(userTwoID); playerOne = DB.GetUser(userOneID); playerTwo = DB.GetUser(userTwoID); Tuple <int, int, bool> winLoseDraw = Battle(turn); // winner, loser, draw(true) or not draw(false) mut.WaitOne(); fighting.fighting = Tuple.Create <int, int, bool, bool>(winLoseDraw.Item1, winLoseDraw.Item2, false, winLoseDraw.Item3); mut.ReleaseMutex(); } }
public Tuple <string, string> Context(string data, out int userIDMatchmake, out int instanceUserID) { //processing data string[] messageLines = data.Split('\n'); string[] tokens = messageLines[0].Split(' '); // first row string request = tokens[0]; //first row first token (GET/POST/PUT/DEL) string path = tokens[1]; //first row second token tokens = path.Split('/'); //split path in messages and id int rowcounter = SearchDataRow(messageLines); // find row where Header ends Tuple <string[], string> allData = SplitData(rowcounter, messageLines); // split messageLines in HeaderArray and Payload string Dictionary <string, string> headers = GetHeaders(allData.Item1); //Split Header Array into Dictinary with Attributes and Values Console.WriteLine(data); Tuple <string, string> response = Tuple.Create("{\n" + "\"Connection\": \"Closed\",\n" + "\"Error\": \"Input\"\n" + "}", "EXIT"); instanceUserID = 0; userIDMatchmake = 0; switch (tokens[1]) { case "users": //DONE //register = POST users; show user Info = GET users/username; change info = PUT users/username switch (request) { case "POST": //DONE //Register new user response = UserRegistration(allData.Item2); break; case "GET": //DONE //Send back all user information if (CheckAuthenticity(headers)) { user = DB.GetUser(user.user_id); response = Tuple.Create(GetUserInfo(user), "ALIVE"); } else { response = Tuple.Create("{\n" + "\"Query\": \"Unsuccessful\"\n" + "\"Error\": \"WrongToken\"\n" + "}", "ALIVE"); } break; case "PUT": //DONE //reponse = Change Success / No Success if (CheckAuthenticity(headers)) { if (ChangeUserInfo(user, allData.Item2, tokens[2])) { response = Tuple.Create("{\n" + "\"Change\": \"Success\"\n" + "}", "ALIVE"); user = DB.GetUser(user.username); // update User Object } else { response = Tuple.Create("{\n" + "\"Change\": \"Unsuccessful\"\n" + "}", "ALIVE"); } } else { response = Tuple.Create("{\n" + "\"Change\": \"Unsuccessful\",\n" + "\"Error\": \"WrongToken\"\n" + "}", "EXIT"); } break; } break; case "sessions": //DONE //login = POST sessions, logout = DELETE sessions switch (request) { //DONE //Login = POST sessions -> sets authentication token in RequestContext and DB case "POST": response = LoginUser(allData.Item2); break; //DONE //Logout = DELETE sessions -> delete token in DB, end connection to client, close client, close Handler Thread case "DELETE": response = LogoutUser(headers); break; default: break; } break; case "transactions": //DONE //buy Packages = POST transactions/packages // buy Coins = POST transactions/coins switch (request) { case "POST": if (CheckAuthenticity(headers)) { if (tokens[2] == "packages") //buy = POST transactions/packages { //buy packages response = BuyPackages(user, allData.Item2); user = DB.GetUser(user.username); // update user Object } else if (tokens[2] == "coins") //buy Coins = POST transactions/coins { //buy coin response = BuyCoins(user.user_id, allData.Item2); user = DB.GetUser(user.username); // update user Object } } else { response = Tuple.Create("{\n" + "\"Query\": \"Unsuccessful\"\n" + "\"Error\": \"WrongToken\"\n" + "}", "EXIT"); } break; } break; case "cards": //DONE //show all of user = GET cards switch (request) { case "GET": if (CheckAuthenticity(headers)) { List <Card> userCardStack = DB.GetAllUserCards(user.user_id); string serialCards = SerializeCards(userCardStack); response = Tuple.Create(serialCards, "ALIVE"); } else { response = Tuple.Create("{\n" + "\"CardFetch\": \"Unsuccessful\",\n" + "\"Error\": \"WrongToken\"\n" + "}", "EXIT"); } break; default: break; } break; case "decks": //DONE //show deck of user = GET deck; config deck = PUT deck switch (request) { case "GET": //DONE //response = GetUserDeckCards(); if (CheckAuthenticity(headers)) { List <Card> userCardDeck = DB.GetUserDeckCards(user.user_id); string serialCards = SerializeCards(userCardDeck); response = Tuple.Create(serialCards, "ALIVE"); } else { response = Tuple.Create("{\n" + "\"Query\": \"Unsuccessful\",\n" + "\"Error\": \"WrongToken\"\n" + "}", "EXIT"); } break; case "PUT": //DONE //response = AddCardsToUserDeck(); if (CheckAuthenticity(headers)) { response = AddCardsToUserDeck(user, allData.Item2); } else { response = Tuple.Create("{\n" + "\"DeckChange\": \"Unsuccessful\",\n" + "\"Error\": \"WrongToken\"\n" + "}", "EXIT"); } break; case "DELETE": break; default: break; } break; case "score": //DONE //show scoreboard = GET score switch (request) { case "GET": //DONE if (CheckAuthenticity(headers)) { try { response = Tuple.Create(ScoreBoard(), "ALIVE"); } catch (Exception) { response = Tuple.Create("{\n" + "\"Query\": \"Unsuccessful\"\n" + "\"Error\": \"Could Not Fetch\"\n" + "}", "ALIVE"); } } else { response = Tuple.Create("{\n" + "\"Query\": \"Unsuccessful\"\n" + "\"Error\": \"WrongToken\"\n" + "}", "EXIT"); } break; } break; case "tradings": //TO-DO not so High //show all tradings = GET tradings; trade = POST tradings, delete deal = DELETE tradings/tradeID break; case "battle": //DONE //go to matchmaking and fight = POST battles switch (request) { case "POST": if (CheckAuthenticity(headers)) { if (DB.DeckFull(user.user_id)) { userIDMatchmake = user.user_id; response = Tuple.Create("{\n" + "\"Matchmaking\": \"Started\"\n" + "}", "MATCH"); } else { response = Tuple.Create("{\n" + "\"Matchmaking\": \"Unsuccessful\",\n" + "\"Error\": \"Deck not full\"\n" + "}", "ALIVE"); } } else { response = Tuple.Create("{\n" + "\"Query\": \"Unsuccessful\",\n" + "\"Error\": \"WrongToken\"\n" + "}", "EXIT"); } break; } break; default: break; } if (user != null && user.user_id > 0) { instanceUserID = user.user_id; } return(response); }