using System; using System.Data; using System.Collections; using System.Collections.Generic; using MySql.Data.MySqlClient; namespace gServer { public enum MessageboxIcon : byte { Asterisk, Error, Exclamation, Hand, Info, None, Question, Stop, Warning } public class User { public MainClass main; public MrAG.Networking.IncomingConnection Connection; public int UserID = 0; public String Username = ""; public String Sessionkey = ""; public String Avatar = ""; public int UpdateCount = 0; public List Friends = new List(); public User(MrAG.Networking.IncomingConnection c, MainClass main) { this.main = main; // 0-2: User related packets c.Connection.Packets[0] = new Action(Packet_Login); c.Connection.Packets[1] = new Action(Packet_Logout); c.Connection.Packets[2] = new Action(Packet_Ping); // 3-7: Friends related packets c.Connection.Packets[3] = new Action(Packet_Friendslist); c.Connection.Packets[5] = new Action(Packet_Chatmessage); c.Connection.Packets[6] = new Action(Packet_FriendrequestConfirmation); c.Connection.Packets[7] = new Action(Packet_Friendrequest); // 10-29: Game related packets c.Connection.Packets[10] = new Action(Packet_Gamelist); c.Connection.Packets[11] = new Action(Packet_Updates); c.Connection.Packets[12] = new Action(Packet_GameAchievements); c.Connection.Packets[13] = new Action(Packet_GameLeaderboards); c.Connection.Packets[14] = new Action(Packet_IngameChange); c.Connection.Packets[15] = new Action(Packet_ServerList); // 30-39: General c.Connection.Packets[31] = new Action(Packet_AvatarRequest); // 40-49: Statistics submissions c.Connection.Packets[40] = new Action(Packet_Achievement_Add); c.Connection.Packets[41] = new Action(Packet_Achievement_Set); c.Connection.Packets[42] = new Action(Packet_Leaderboard_Submit); this.Connection = c; } public Hashtable UserRow { get { return this.main.Query("SELECT * FROM `users` WHERE `ID`=" + this.UserID)[0]; } } public void Messagebox(String Title, String Text, MessageboxIcon Icon) { this.Connection.Connection.Connection.Start_Send(); this.Connection.Connection.Connection.AddByte(30); this.Connection.Connection.Connection.AddString(Title); this.Connection.Connection.Connection.AddString(Text); this.Connection.Connection.Connection.AddByte((byte)Icon); this.Connection.Connection.Connection.Finish_Send(); } public void Disconnected(bool Unexpected) { if(Unexpected) this.main.Log(this.Connection.IP + " (" + this.Username + ") unexpectedly disconnected."); if(this.UserID != 0) this.main.Query("UPDATE `users` SET `API_SessionKey`='' WHERE `ID`=" + this.UserID); foreach (User Friend in this.Friends){ Friend.Connection.Connection.Connection.Start_Send(); Friend.Connection.Connection.Connection.AddByte(4); Friend.Connection.Connection.Connection.AddInt(this.UserID); Friend.Connection.Connection.Connection.AddBool(false); Friend.Connection.Connection.Connection.Finish_Send(); Friend.Friends.Remove(this); } this.main.Users.Remove(this); } public void Update() { try { this.Connection.Connection.Update(); }catch(Exception ex){ if(ex.Message == "The object was used after being disposed.") { this.Disconnected(true); } this.main.Log(this.Connection.IP + " (" + this.Username + ") errored: " + ex.Message); this.Connection.Kick("Socket error"); } UpdateCount++; if(UpdateCount >= 1000) { UpdateCount = 0; try { this.Connection.Connection.Connection.Start_Send(); this.Connection.Connection.Connection.AddByte(2); this.Connection.Connection.Connection.Finish_Send(); }catch{} } } public void InvalidPacket() { this.Connection.Kick("Invalid packet received!"); this.main.Log(this.Connection.IP + " (" + this.Username + ") kicked for an invalid packet."); this.Disconnected(false); } public void Packet_Login(MrAG.Networking.Packet p) { String Username; String Password; try { Username = p.ReadString(); Password = p.ReadString(); }catch{ this.InvalidPacket(); return; } Hashtable[] rows = this.main.Query("SELECT * FROM `users` WHERE `Username`='" + this.main.Safe(Username) + "' AND `Password`='" + this.main.Safe(Password) + "'"); if(rows.Length == 1) { this.UserID = int.Parse((String)rows[0]["ID"]); this.Username = (String)rows[0]["Username"]; this.Sessionkey = this.main.MD5(DateTime.Now.TimeOfDay.TotalMilliseconds.ToString() + "GamerServices :D"); this.Avatar = this.main.MD5((String)rows[0]["Email"]); this.main.Query("UPDATE `users` SET `API_SessionKey`='" + this.Sessionkey + "' WHERE `ID`=" + rows[0]["ID"]); p.Start_Send(); p.AddByte(0); p.AddBool(true); p.AddString(this.Sessionkey); p.Finish_Send(); Hashtable[] FriendConnections = this.main.Query("SELECT * FROM `friends` WHERE `User`=" + this.UserID + " OR `Friend`=" + this.UserID); for(int i=0; i 0 ? 1 : 0; } }else // fuck looperdieloops, we are strong like freaking chuck noris! gamestosend = Rows.Length; p.AddInt(gamestosend); for(int i=0; i 0) { p.AddString((String)Rows[i]["Identifier"]); p.AddString((String)Rows[i]["Name"]); p.AddShort(gameID); p.AddString((String)Rows[i]["Description"]); p.AddString((String)Rows[i]["Executable"]); p.AddBool(isadmin || accesslist[gameID] > 1); } } p.Finish_Send(); } public void Packet_Updates(MrAG.Networking.Packet p) { int AppID = 0; try { AppID = (int)p.ReadShort(); }catch{ this.InvalidPacket(); return; } p.Start_Send(); p.AddByte(11); p.AddString("1.0.0"); //TODO: Change. p.AddShort((short)AppID); p.Finish_Send(); } public void Packet_GameAchievements(MrAG.Networking.Packet p) { short AppID = 0; try { AppID = (short)p.ReadShort(); }catch{ this.InvalidPacket(); return; } p.Start_Send(); p.AddByte(12); p.AddShort(AppID); Hashtable[] Rows = this.main.Query("SELECT * FROM `achievements` WHERE `Game`=" + AppID.ToString()); p.AddInt(Rows.Length); for(int i=0; i int.Parse((String)CurrentEntry["Score"])) this.main.Query("UPDATE `leaderboards_entries` SET `Score`=" + Score + " WHERE `ID`=" + (String)CurrentEntry["ID"]); } // Send back current location int C = 0; Hashtable[] Entries = this.main.Query("SELECT * FROM `leaderboards_entries` WHERE `Leaderboard`=" + (String)Leaderboard["ID"] + " ORDER BY `Score` DESC"); for(int i=0; i this.main.Epoch() - 30) { serverCount++; } } p.AddInt(serverCount); foreach(ServerData server in this.main.Servers) { if(server.Game == gameIdent && server.lastUpdate > this.main.Epoch() - 30) { p.AddString(server.IP); p.AddInt(server.Port); p.AddString(server.Name); p.AddString(server.Meta); p.AddInt(server.CurrentUsers); p.AddInt(server.MaxUsers); } } p.Finish_Send(); this.main.Log("Sent " + this.Username + " " + serverCount.ToString() + "/" + this.main.Servers.Count.ToString() + " servers for " + gameIdent + "."); } } }