View difference between Paste ID: M7TJ4rWN and aisjaBex
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Threading;
3
using System.Threading.Tasks;
4
using PlayerIO.GameLibrary;
5
6
namespace UPIO.Server
7
{
8
    internal class RecourcesEngine : Player
9
    {
10
        private BigDB bigDb;
11
12
        private async Task CalculateRecources(string tableName, string clientName, string clientId, Player client,
13
                                              CancellationToken cancellationToken)
14
        {
15
            var delay = TimeSpan.FromSeconds(30);
16
17
            cancellationToken.ThrowIfCancellationRequested();
18
19
            int aluminium = 0, uranium = 0;
20
            await bigDb.LoadAsync(tableName,clientId,(databaseobject) =>
21
                {
22
                    var recources = databaseobject.GetObject("Recources");
23
                    var aluminium = recources.GetInt("Aluminium");
24
                    var uranium = recources.GetInt("Uranium");
25
26
                    aluminium += 30;
27
                    uranium += 40;
28
29
                    recources.Set("Aluminium", aluminium);
30
                    recources.Set("Uranium", uranium);
31
                }, cancellationToken).ConfigureAwait(false);
32
33
            cancellationToken.ThrowIfCancellationRequested();
34-
            client.Send("Recources", aluminium, uranium);
34+
            await client.SendAsync("Recources", aluminium, uranium);
35
            cancellationToken.ThrowIfCancellationRequested();
36
            await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
37
        }
38
    }
39
}
40
41
namespace UPIO.DB
42
{
43
    class BigDB
44
    {
45-
        private void Load(string tableName, string clientId, Action<DatabaseObject> process)
45+
        private void Load(string tableName, string clientId, Action<DatabaseObject> process, CancellationToken ctx)
46
        {
47
            // ...
48
        }
49
50
        private TaskScheduler scheduler;
51
52
        public Task LoadAsync(
53
                string tableName,
54
                string clientId,
55
                Action<DatabaseObject> process,
56
                CancellationToken ctx)
57
        {
58
            return Task.Factory.StartNew(
59-
                () => Load(tableName, clientId, process),
59+
                () => Load(tableName, clientId, process, ctx),
60
                ctx,
61
                TaskCreationOptions.None,
62
                scheduler);
63
        }
64
    }
65
}