Beispiel #1
0
        public void InsertScore(Address from, Address target, string name, BigInteger score)
        {
            Runtime.Expect(_leaderboards.ContainsKey <string>(name), "invalid leaderboard");
            var leaderboard = _leaderboards.Get <string, Leaderboard>(name);

            Runtime.Expect(from == leaderboard.owner, "invalid leaderboard owner");
            Runtime.Expect(Runtime.IsWitness(from), "invalid witness");

            var rows  = _rows.Get <string, StorageList>(name);
            var count = rows.Count();

            var newRow = new LeaderboardRow()
            {
                address = target,
                score   = score
            };

            int bestIndex = 0;

            var lastIndex = (int)(count - 1);

            for (int i = lastIndex; i >= 0; i--)
            {
                var entry = rows.Get <LeaderboardRow>(i);
                if (entry.score >= score)
                {
                    bestIndex = i + 1;
                    break;
                }
            }

            if (bestIndex >= leaderboard.size)
            {
                return;
            }

            for (int i = lastIndex; i > bestIndex; i--)
            {
                var entry = rows.Get <LeaderboardRow>(i - 1);
                rows.Replace <LeaderboardRow>(i, entry);
            }

            if (bestIndex < count)
            {
                rows.Replace(bestIndex, newRow);
            }
            else
            {
                Runtime.Expect(bestIndex == count, "invalid insertion index");
                rows.Add <LeaderboardRow>(newRow);
            }

            Runtime.Notify(EventKind.LeaderboardInsert, from, newRow);
        }
        public void InsertScore(Address from, Address target, string name, BigInteger score)
        {
            Runtime.Expect(_leaderboards.ContainsKey <string>(name), "invalid leaderboard");
            var leaderboard = _leaderboards.Get <string, Leaderboard>(name);

            Runtime.Expect(from == leaderboard.owner, "invalid leaderboard owner");
            Runtime.Expect(Runtime.IsWitness(from), "invalid witness");

            var rows     = _rows.Get <string, StorageList>(name);
            var count    = rows.Count();
            var oldCount = count;

            int oldIndex = -1;

            for (int i = 0; i < count; i++)
            {
                var entry = rows.Get <LeaderboardRow>(i);
                if (entry.address == target)
                {
                    if (entry.score > score)
                    {
                        return;
                    }
                    oldIndex = i;
                    break;
                }
            }

            if (oldIndex >= 0)
            {
                count--;

                for (int i = oldIndex; i <= count - 1; i++)
                {
                    var entry = rows.Get <LeaderboardRow>(i + 1);
                    rows.Replace <LeaderboardRow>(i, entry);
                }

                rows.RemoveAt <LeaderboardRow>(count);
            }

            int bestIndex = 0;

            var lastIndex = (int)(count - 1);

            for (int i = lastIndex; i >= 0; i--)
            {
                var entry = rows.Get <LeaderboardRow>(i);
                if (entry.score >= score)
                {
                    bestIndex = i + 1;
                    break;
                }
            }

            if (bestIndex >= leaderboard.size)
            {
                rows  = _rows.Get <string, StorageList>(name);
                count = rows.Count();
                for (int i = 0; i < count; i++)
                {
                    var entry = rows.Get <LeaderboardRow>(i);
                    Runtime.Expect(entry.score >= score, "leaderboard bug");
                }

                return;
            }

            /*for (int i = lastIndex; i > bestIndex; i--)
             * {
             *  var entry = rows.Get<LeaderboardRow>(i - 1);
             *  rows.Replace<LeaderboardRow>(i, entry);
             * }*/

            var newRow = new LeaderboardRow()
            {
                address = target,
                score   = score
            };

            if (bestIndex < count)
            {
                if (count < leaderboard.size)
                {
                    rows.Add <LeaderboardRow>(newRow);
                    for (int i = (int)count; i > bestIndex; i--)
                    {
                        var entry = rows.Get <LeaderboardRow>(i - 1);
                        rows.Replace <LeaderboardRow>(i, entry);
                    }
                }

                rows.Replace(bestIndex, newRow);
            }
            else
            {
                Runtime.Expect(bestIndex == count, "invalid insertion index");
                rows.Add <LeaderboardRow>(newRow);
            }

            rows  = _rows.Get <string, StorageList>(name);
            count = rows.Count();
            for (int i = 0; i < bestIndex; i++)
            {
                var entry = rows.Get <LeaderboardRow>(i);
                Runtime.Expect(entry.score >= score, "leaderboard bug");
            }

            Runtime.Expect(count >= oldCount, "leaderboard bug");

            Runtime.Notify(EventKind.LeaderboardInsert, target, newRow);
        }