Ejemplo n.º 1
0
        public static UserScore[] Rank(DataContext context, int skip, int take, User user = null)
        {
            Table <RawData> user_scores = context.GetTable <RawData>();
            var             query       = (user != null) ? user_scores.Where(item => (item.Id == user.Id)) : user_scores;

            var data = query.OrderByDescending(item => item.Point);

            //  Todo:レコードの取得数を制限し、該当クラスにマッピングしたデータを取得する方法の模索
            //      SQLiteはTake()やFirst()等が使えない(SELECT TOPコマンドに対応していない)ため、
            //      Linqを使う場合は一度リスト化(全取得)してから限定するしかない様子
            var list = data.ToList();

            if (list.Count == 0)
            {
                return(null);
            }
            else
            {
                int left  = list.Count - skip;
                int count = (left <= take) ? left : take;
                int max   = skip + count;
                Debug.Assert(max <= list.Count);

                UserScore[] scores = new UserScore[count];
                for (int i = 0; i < count; i++)
                {
                    int index = skip + i;
                    scores[i] = new UserScore(list[index]);
                }
                return(scores);
            }
        }
Ejemplo n.º 2
0
        public bool StoreUserScore(UserScoreParam param, ref UserRankInfo rankInfo)
        {
            using (var connection = new SQLiteConnection(ConnectionString))// 「DataSource=:memory:」にするとオンメモリのDBとして動作
            {
                // データベースに接続
                connection.Open();

                using (var context = new DataContext(connection))
                {
                    User user = User.Find(context, param.Sereal);

                    if (user == null)
                    {
                        connection.Close();
                        return(false);
                    }
                    UserScore score = new UserScore(user);
                    score.Point = param.Point;
                    bool ret = score.Commit(connection);

                    if (rankInfo != null)
                    {
                        rankInfo.UserId = score.UserId;
                        rankInfo.Name   = user.Name;
                        rankInfo.Point  = score.Point;
                        rankInfo.Rank   = score.Rank(context);
                    }

                    connection.Close();
                    return(ret);
                }
            }
        }
Ejemplo n.º 3
0
        bool CreateDataBase(string dbName)
        {
            ConnectionString = $"DataSource={dbName}.sqlite";
            using (var connection = new SQLiteConnection(ConnectionString))// 「DataSource=:memory:」にするとオンメモリのDBとして動作
            {
                // データベースに接続
                connection.Open();
                // ユーザーテーブルの作成
                User.CreateTable(connection);
                // ランキング用テーブルの作成
                UserScore.CreateTable(connection);

                //  ダミーデータの作成
                int count = 0;
                using (var context = new DataContext(connection))
                {
                    count = User.Count(context);
                }
                const int DATA_MIN = 10;

                Random rand = new Random();
                for (int i = count; i < DATA_MIN; i++)
                {
                    User user = new User();
                    user.Name = "CPU" + i;
                    user.Commit(connection);

                    UserScore score = new UserScore(user);
                    score.Point = rand.Next(10000);
                    score.Commit(connection);
                }

                // 切断
                connection.Close();
            }
            return(true);
        }
Ejemplo n.º 4
0
        public UserRankInfo[] GetRanking(RankingRequest request)
        {
            using (var connection = new SQLiteConnection(ConnectionString))// 「DataSource=:memory:」にするとオンメモリのDBとして動作
            {
                // データベースに接続
                connection.Open();

                using (var context = new DataContext(connection))
                {
                    User target = (request.Sereal != 0)? User.Find(context, request.Sereal): null;

                    var data = UserScore.Rank(context, request.Skip, request.Take, target);


                    UserRankInfo[] ret = new UserRankInfo[data.Length];

//                    foreach (var score in data)
                    for (int i = 0; i < data.Length; i++)
                    {
                        var score = data[i];

                        UserRankInfo rankInfo = new UserRankInfo();
                        rankInfo.Point = score.Point;
                        rankInfo.Rank  = request.Skip + i;

                        User user = User.FindByUserId(context, score.UserId);
                        rankInfo.UserId = score.UserId;
                        rankInfo.Name   = user.Name;

                        ret[i] = rankInfo;
                    }
                    connection.Close();
                    return(ret);
                }
            }
        }