示例#1
0
        private object getPlayers(RestRequestArgs args)
        {
            List <RelevantInfo> relevantPlayers = new List <RelevantInfo>();

            for (int i = 0; i < Main.player.Length; i++)
            {
                Player player = Main.player[i];
                if (!String.IsNullOrEmpty(player?.name))
                {
                    relevantPlayers.Add(RelevantInfo.GetRelevance(player));
                }
            }
            return(new RestObject()
            {
                { "count", relevantPlayers.Count },
                { "players", relevantPlayers }
            });
        }
示例#2
0
        private object getPlayer(RestRequestArgs args)
        {
            string strName = args.Parameters["name"];

            if (string.IsNullOrEmpty(strName))
            {
                return new RestObject()
                       {
                           { "error", "expected player name in 'name'" }
                       }
            }
            ;

            User usr = TShock.Users.GetUserByName(strName);

            if (usr == null)
            {
                return new RestObject()
                       {
                           { "error", "player not found" }
                       }
            }
            ;
            List <TSPlayer> plrs = TShock.Utils.FindPlayer(strName);

            // Player is online, no need to fetch from DB
            if (plrs.Count == 1)
            {
                return new RestObject()
                       {
                           { "player", RelevantInfo.GetRelevance(plrs[0].TPlayer) }
                       }
            }
            ;
            try
            {
                // Fetch from DB
                using (var reader = TShock.DB.QueryReader("SELECT * FROM tsCharacter where account =@0", usr.ID))
                {
                    if (reader.Read())
                    {
                        RelevantInfo plr;
                        foreach (var player in TShock.Players.Where(p => null != p && p.User.Name == usr.Name))
                        {
                            // Some reason we didn't get the idea, lets use the real player if they're online
                            plr      = RelevantInfo.GetRelevance(player.TPlayer);
                            plr.Name = usr.Name;
                            plr.ID   = usr.ID;
                            return(new RestObject()
                            {
                                { "player", plr }
                            });
                        }
                        // Load from DB
                        plr      = RelevantInfo.GetRelevance(reader);
                        plr.Name = usr.Name;
                        plr.ID   = usr.ID;
                        return(new RestObject()
                        {
                            { "player", plr }
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                return(new RestObject()
                {
                    { "error", ex.Message }
                });
            }
            return(new RestObject()
            {
                { "error", "player not found" }
            });
        }