/// <summary> /// This is used for retrieving a player from the database /// </summary> /// <param name="reader"></param> /// <returns></returns> public static TwodokuPlayer FromSqlReader(SqlDataReader reader) { TwodokuPlayer player = new TwodokuPlayer(); player.PlayerId = (int)reader[DBHelper.ColumnPlayerId]; player.Name = (string)reader[DBHelper.ColumnName]; player.Password = (string)reader[DBHelper.ColumnPassword]; player.GcmId = (string)reader[DBHelper.ColumnGcmId]; player.Wins = (int)reader[DBHelper.ColumnWins]; player.Losses = (int)reader[DBHelper.ColumnLosses]; player.Streak = (int)reader[DBHelper.ColumnStreak]; return player; }
/// <summary> /// This is used for importing the database from a text file /// </summary> /// <param name="input"></param> /// <returns></returns> public static TwodokuPlayer FromString(string input) { string[] parts = input.Split(','); TwodokuPlayer ret = new TwodokuPlayer(); int.TryParse(parts[0], out ret.PlayerId); ret.Name = parts[1]; ret.Password = parts[2]; ret.GcmId = parts[3]; int.TryParse(parts[4], out ret.Wins); int.TryParse(parts[5], out ret.Losses); int.TryParse(parts[6], out ret.Streak); return ret; }
///////////////////// Game Helpers //////////////////////////////// public bool AddPlayer(TwodokuPlayer player) { string values = ""; values += "'" + player.PlayerId + "',"; values += "'" + player.Name + "',"; values += "'" + player.Password + "',"; values += "'" + player.GcmId + "',"; values += "'" + player.Wins + "',"; values += "'" + player.Losses + "',"; values += "'" + player.Streak + "'"; return Insert(TablePlayers, values); }
public bool UpdatePlayerStats(TwodokuPlayer player, bool won) { if (won) { player.Wins++; if (player.Streak <= 0) player.Streak = 1; else player.Streak++; } else { player.Losses++; if (player.Streak >= 0) player.Streak = -1; else player.Streak--; } //Update: Wins, Losses, and Streak string update = ""; update += string.Format("{0}='{1}',", ColumnWins, player.Wins); update += string.Format("{0}='{1}',", ColumnLosses, player.Losses); update += string.Format("{0}='{1}'", ColumnStreak, player.Streak); string qualifier = string.Format("{0}='{1}'", ColumnName, player.Name); return Update(TablePlayers, update, qualifier); }
public bool UpdatePlayerPassword(TwodokuPlayer player, string newPassword) { player.Password = newPassword; //Update: Password string update = ""; update += string.Format("{0}='{1}'", ColumnPassword, player.Password); string qualifier = string.Format("{0}='{1}'", ColumnName, player.Name); return Update(TablePlayers, update, qualifier); }
public bool UpdatePlayerGcmId(TwodokuPlayer player) { //Retrieve the player and make sure it exists TwodokuPlayer existingPlayer = GetPlayer(player.Name); if (existingPlayer == null) return false; //Update: Scores, PlayDate, Status, Turn, PlayerBoard, LastMove string update = ""; update += string.Format("{0}='{1}'", ColumnGcmId, player.GcmId); string qualifier = string.Format("{0}='{1}'", ColumnName, player.Name); return Update(TablePlayers, update, qualifier); }
private bool LoginUser(TwodokuPlayer player, string password) { if (player == null || password == null) return false; return password.Equals(player.Password); }
private string AddPlayer(string name, string password, string gcmId) { TwodokuPlayer player = mDB.GetPlayer(name); if (player == null) { player = new TwodokuPlayer(-1, name, password, gcmId); player.PlayerId = mDB.GetNextKey(DBHelper.TablePlayers, DBHelper.ColumnPlayerId); if (mDB.AddPlayer(player)) return GenerateSuccessResponse(); } StringBuilder ret = new StringBuilder(); ret.AppendLine("HTTP/1.0 404 Add failed"); ret.AppendLine("Connection: close"); ret.AppendLine(""); return ret.ToString(); }
public void Process() { // we can't use a StreamReader for input, because it buffers up extra data on us inside it's // "processed" view of the world, and we want the data raw after the headers Stream inputStream = new BufferedStream(mSocket.GetStream()); // we probably shouldn't be using a streamwriter for all output from handlers either StreamWriter outputStream = new StreamWriter(new BufferedStream(mSocket.GetStream())); string postPlayer = null; try { //Parse the request String request = StreamReadLine(inputStream); //Console.WriteLine(request); string[] tokens = request.Split(' '); if (tokens.Length != 3) { throw new Exception("invalid http request line"); } HttpMethod = tokens[0].ToUpper(); HttpUrl = tokens[1]; HttpProtocolVersionString = tokens[2]; DateTime now = DateTime.Now; //Console.Write(string.Format("{0} {1}: ", now.ToShortDateString(), now.ToShortTimeString())); ReadHeaders(inputStream); if (HttpMethod.Equals("GET")) { HandleGETRequest(outputStream); } else if (HttpMethod.Equals("POST")) { postPlayer = HandlePOSTRequest(inputStream, outputStream); } } catch (Exception e) { Console.WriteLine("Exception: " + e.ToString()); //Write the failure response outputStream.WriteLine("HTTP/1.0 404 File not found"); outputStream.WriteLine("Connection: close"); outputStream.WriteLine(""); } outputStream.Flush(); outputStream.Close(); inputStream = null; outputStream = null; mSocket.Close(); if (postPlayer != null) { //Send a test ping to the player string url = "https://android.googleapis.com/gcm/send"; TwodokuPlayer player = mDB.GetPlayer(postPlayer); if (player != null) { string data = "registration_id=" + player.GcmId; //Console.WriteLine(string.Format("Pinging user {0}", player.Name)); string response = SendPost(url, data); //Console.WriteLine(response); } else { //Create the new player in the database Console.WriteLine("Creating new player: " + postPlayer); TwodokuPlayer newPlayer = new TwodokuPlayer(-1, postPlayer, "", ""); newPlayer.PlayerId = mDB.GetNextKey(DBHelper.TablePlayers, DBHelper.ColumnPlayerId); mDB.AddPlayer(newPlayer); } } }