Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit 0f48deb

Browse files
author
Nikita Fediuchin
committed
Added account, player, database, factory, concurrent collection.
1 parent 7de30a7 commit 0f48deb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2107
-75
lines changed

Containers/ByteArray.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
using System;
1617
using System.IO;
1718

1819
namespace OpenSharedLibrary.Containers
@@ -98,33 +99,27 @@ public byte[] ToBytes()
9899
/// </summary>
99100
public static void ToBytes(IByteArray byteArray, Stream inputStream)
100101
{
101-
using (var binaryWriter = new BinaryWriter(inputStream))
102-
byteArray.ToBytes(binaryWriter);
102+
using var binaryWriter = new BinaryWriter(inputStream);
103+
byteArray.ToBytes(binaryWriter);
103104
}
104105
/// <summary>
105106
/// Converts class data to the byte array
106107
/// </summary>
107108
public static void ToBytes(IByteArray byteArray, byte[] array, int index)
108109
{
109-
using (var memoryStream = new MemoryStream(array, index, byteArray.ByteArraySize))
110-
{
111-
using(var binaryWriter = new BinaryWriter(memoryStream))
112-
byteArray.ToBytes(binaryWriter);
113-
}
110+
using var memoryStream = new MemoryStream(array, index, byteArray.ByteArraySize);
111+
using var binaryWriter = new BinaryWriter(memoryStream);
112+
byteArray.ToBytes(binaryWriter);
114113
}
115114
/// <summary>
116115
/// Converts class data to the byte array
117116
/// </summary>
118117
public static byte[] ToBytes(IByteArray byteArray)
119118
{
120119
var array = new byte[byteArray.ByteArraySize];
121-
122-
using (var memoryStream = new MemoryStream(array))
123-
{
124-
using (var binaryWriter = new BinaryWriter(memoryStream))
125-
byteArray.ToBytes(binaryWriter);
126-
}
127-
120+
using var memoryStream = new MemoryStream(array);
121+
using var binaryWriter = new BinaryWriter(memoryStream);
122+
byteArray.ToBytes(binaryWriter);
128123
return array;
129124
}
130125

Containers/ConcurentCollection.cs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+

2+
// Copyright 2019 Nikita Fediuchin (QuantumBranch)
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System;
17+
using System.Collections.Generic;
18+
19+
namespace OpenSharedLibrary.Containers
20+
{
21+
/// <summary>
22+
/// Concurent collection base class (thread-safe)
23+
/// </summary>
24+
public class ConcurentCollection<TKey, TValue, TCollection> : IConcurentCollection<TKey, TValue> where TCollection : IDictionary<TKey, TValue>
25+
{
26+
/// <summary>
27+
/// Concurent array locker
28+
/// </summary>
29+
protected readonly object locker;
30+
/// <summary>
31+
/// Concurent array instance
32+
/// </summary>
33+
protected readonly TCollection collection;
34+
35+
/// <summary>
36+
/// Creates a new concurent array abstract base class instance
37+
/// </summary>
38+
public ConcurentCollection(TCollection collection)
39+
{
40+
locker = new object();
41+
this.collection = collection;
42+
}
43+
44+
/// <summary>
45+
/// Returns true if collection contains item
46+
/// </summary>
47+
public bool Contains(TKey key)
48+
{
49+
lock (locker)
50+
return collection.ContainsKey(key);
51+
}
52+
/// <summary>
53+
/// Adds a new item to the array
54+
/// </summary>
55+
public bool Add(TKey key, TValue value)
56+
{
57+
lock (locker)
58+
return collection.TryAdd(key, value);
59+
}
60+
/// <summary>
61+
/// Returns item from the collection (null if not exist)
62+
/// </summary>
63+
public TValue Get(TKey key)
64+
{
65+
lock (locker)
66+
{
67+
if (collection.TryGetValue(key, out TValue player))
68+
return player;
69+
}
70+
71+
return default;
72+
}
73+
74+
/// <summary>
75+
/// Removes item from the array
76+
/// </summary>
77+
public bool Remove(TKey key)
78+
{
79+
lock (locker)
80+
return collection.Remove(key);
81+
}
82+
/// <summary>
83+
/// Removes item from the collection
84+
/// </summary>
85+
public bool Remove(TKey key, out TValue value)
86+
{
87+
lock (locker)
88+
{
89+
if (!collection.TryGetValue(key, out value))
90+
return false;
91+
92+
collection.Remove(key);
93+
return true;
94+
}
95+
}
96+
97+
/// <summary>
98+
/// Removes all collection items
99+
/// </summary>
100+
/// <returns></returns>
101+
public void Clear()
102+
{
103+
lock (locker)
104+
collection.Clear();
105+
}
106+
107+
/// <summary>
108+
/// Returns true if action has performed
109+
/// </summary>
110+
public bool Lock(TKey key, Action<TValue> onAction)
111+
{
112+
lock (locker)
113+
{
114+
if (!collection.TryGetValue(key, out TValue value))
115+
return false;
116+
117+
try { onAction(value); }
118+
catch { return false; }
119+
}
120+
121+
return true;
122+
}
123+
/// <summary>
124+
/// Returns true if action has performed (for each room)
125+
/// </summary>
126+
public bool Lock(Action<TValue> onAction)
127+
{
128+
lock (locker)
129+
{
130+
try
131+
{
132+
foreach (var value in collection.Values)
133+
onAction(value);
134+
}
135+
catch
136+
{
137+
return false;
138+
}
139+
}
140+
141+
return true;
142+
}
143+
}
144+
}

Containers/Database.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+

2+
// Copyright 2019 Nikita Fediuchin (QuantumBranch)
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace OpenSharedLibrary.Containers
17+
{
18+
/// <summary>
19+
/// Database abstract base class
20+
/// </summary>
21+
public abstract class Database<TKey, TValue> : IDatabase<TKey, TValue>
22+
{
23+
/// <summary>
24+
/// Returns true if the database contains item
25+
/// </summary>
26+
public abstract bool Contains(TKey key);
27+
/// <summary>
28+
/// Returns item readed from the database (if not readed null)
29+
/// </summary>
30+
public abstract TValue Read(TKey key);
31+
/// <summary>
32+
/// Writes item to the database (Returns result)
33+
/// </summary>
34+
public abstract bool Write(TKey key, TValue value);
35+
}
36+
}

Containers/DiskDatabase.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+

2+
// Copyright 2019 Nikita Fediuchin (QuantumBranch)
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System;
17+
using System.IO;
18+
19+
namespace OpenSharedLibrary.Containers
20+
{
21+
/// <summary>
22+
/// Disk database abstract base class (thread-safe)
23+
/// </summary>
24+
public abstract class DiskDatabase<TKey, TValue> : Database<TKey, TValue>, IDiskDatabase<TKey, TValue>
25+
{
26+
/// <summary>
27+
/// Database locker
28+
/// </summary>
29+
protected readonly object locker;
30+
31+
/// <summary>
32+
/// Database directory path
33+
/// </summary>
34+
protected readonly string path;
35+
/// <summary>
36+
/// Database value factory
37+
/// </summary>
38+
protected readonly IDiskFactory<TValue> factory;
39+
40+
/// <summary>
41+
/// Database directory path
42+
/// </summary>
43+
public string Path => path;
44+
/// <summary>
45+
/// Database value factory
46+
/// </summary>
47+
public IDiskFactory<TValue> Factory => factory;
48+
49+
/// <summary>
50+
/// Creates a new disk database abstract class instance
51+
/// </summary>
52+
public DiskDatabase(string path, IDiskFactory<TValue> factory)
53+
{
54+
locker = new object();
55+
56+
this.path = path ?? throw new ArgumentNullException();
57+
this.factory = factory ?? throw new ArgumentNullException();
58+
59+
if (!Directory.Exists(path))
60+
Directory.CreateDirectory(path);
61+
}
62+
63+
/// <summary>
64+
/// Returns true if the database contains item
65+
/// </summary>
66+
public override bool Contains(TKey key)
67+
{
68+
lock(locker)
69+
return File.Exists($"{path}{key}");
70+
}
71+
}
72+
}

Containers/Factory.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+

2+
// Copyright 2019 Nikita Fediuchin (QuantumBranch)
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace OpenSharedLibrary.Containers
17+
{
18+
/// <summary>
19+
/// Factory class
20+
/// </summary>
21+
public class Factory<TValue> : IFactory<TValue> where TValue : new()
22+
{
23+
/// <summary>
24+
/// Creates a new value instance
25+
/// </summary>
26+
public TValue Create()
27+
{
28+
return new TValue();
29+
}
30+
}
31+
}

Containers/IConcurentCollection.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+

2+
// Copyright 2019 Nikita Fediuchin (QuantumBranch)
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System;
17+
18+
namespace OpenSharedLibrary.Containers
19+
{
20+
/// <summary>
21+
/// Thread-safe collection interface
22+
/// </summary>
23+
public interface IConcurentCollection<TKey, TValue>
24+
{
25+
/// <summary>
26+
/// Returns true if collection contains item
27+
/// </summary>
28+
bool Contains(TKey key);
29+
/// <summary>
30+
/// Adds a new item to the collection
31+
/// </summary>
32+
bool Add(TKey key, TValue value);
33+
/// <summary>
34+
/// Returns item from the collection (null if not exist)
35+
/// </summary>
36+
TValue Get(TKey key);
37+
38+
/// <summary>
39+
/// Removes item from the collection
40+
/// </summary>
41+
bool Remove(TKey key);
42+
/// <summary>
43+
/// Removes item from the collection
44+
/// </summary>
45+
bool Remove(TKey key, out TValue value);
46+
47+
/// <summary>
48+
/// Removes all collection items
49+
/// </summary>
50+
void Clear();
51+
52+
/// <summary>
53+
/// Returns true if action has performed
54+
/// </summary>
55+
bool Lock(TKey key, Action<TValue> onAction);
56+
/// <summary>
57+
/// Returns true if action has performed (for each item)
58+
/// </summary>
59+
bool Lock(Action<TValue> onAction);
60+
}
61+
}

0 commit comments

Comments
 (0)