1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
using Microsoft.Extensions.DependencyInjection;
using PUSG.Web.Extensions;
using PUSG.Web.Models;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace PUSG.Web.Services
{
public class GameService : IGameService
{
private ConcurrentDictionary<string, GameSession> sessions;
private ConcurrentDictionary<string, GameLoopService> loops;
private ConcurrentDictionary<string, CancellationToken> loopCancellationTokens;
private IServiceProvider serviceProvider;
public GameService(IServiceProvider serviceProvider)
{
// Key - Socket Id (client), Value - Game Session
this.sessions = new ConcurrentDictionary<string, GameSession>();
this.loops = new ConcurrentDictionary<string, GameLoopService>();
this.loopCancellationTokens = new ConcurrentDictionary<string, CancellationToken>();
this.Waiting = new List<string>();
this.PlayerUsernames = new ConcurrentDictionary<string, string>();
this.OtherPlayer = new ConcurrentDictionary<string, string>();
this.PlayerColor = new ConcurrentDictionary<string, string>();
this.serviceProvider = serviceProvider;
}
public List<string> Waiting { get; }
public ConcurrentDictionary<string, string> PlayerUsernames { get; }
public ConcurrentDictionary<string, string> OtherPlayer { get; }
public ConcurrentDictionary<string, string> PlayerColor { get; }
public async Task CreateGameSession(string blueClientId, string redClientId)
{
GameSession gameSession = new GameSession();
gameSession.BluePlayer = new Player
{
X = 50,
Y = 384,
Direction = "right",
Color = "blue",
Type = "basic"
};
gameSession.RedPlayer = new Player
{
X = 1266,
Y = 384,
Direction = "left",
Color = "red",
Type = "basic"
};
this.sessions[blueClientId] = gameSession;
this.sessions[redClientId] = gameSession;
this.OtherPlayer[blueClientId] = redClientId;
this.OtherPlayer[redClientId] = blueClientId;
this.PlayerColor[blueClientId] = "blue";
this.PlayerColor[redClientId] = "red";
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
GameLoopService gameLoop = this.serviceProvider.CreateScope().ServiceProvider.GetRequiredService<GameLoopService>();
this.loops[blueClientId] = gameLoop;
this.loops[redClientId] = gameLoop;
this.loopCancellationTokens[blueClientId] = token;
this.loopCancellationTokens[redClientId] = token;
gameLoop.GameSession = gameSession;
await gameLoop.StartAsync(token);
}
public async Task DestroyGameSession(string clientId)
{
string otherClientId = this.OtherPlayer.ContainsKey(clientId) ? this.OtherPlayer[clientId] : null;
await this.loops[clientId].StopAsync(this.loopCancellationTokens[clientId]);
this.loops[clientId].Dispose();
this.loops.Delete(clientId);
this.sessions.Delete(clientId);
this.PlayerUsernames.Delete(clientId);
this.PlayerColor.Delete(clientId);
this.loopCancellationTokens.Delete(clientId);
this.OtherPlayer.Delete(clientId);
if (otherClientId != null)
{
this.loops.Delete(otherClientId);
this.sessions.Delete(otherClientId);
this.PlayerUsernames.Delete(otherClientId);
this.PlayerColor.Delete(otherClientId);
this.loopCancellationTokens.Delete(otherClientId);
this.OtherPlayer.Delete(otherClientId);
}
}
public GameSession this[string clientId]
{
get
{
return this.sessions.ContainsKey(clientId) ? this.sessions[clientId] : null;
}
set
{
this.sessions[clientId] = value;
}
}
public void Shoot(string clientId)
{
this.sessions[clientId].Shoot(this.PlayerColor[clientId]);
}
public void MoveUp(string clientId)
{
this.sessions[clientId][this.PlayerColor[clientId]].MoveUp();
}
public void MoveRight(string clientId)
{
this.sessions[clientId][this.PlayerColor[clientId]].MoveRight();
}
public void MoveDown(string clientId)
{
this.sessions[clientId][this.PlayerColor[clientId]].MoveDown();
}
public void MoveLeft(string clientId)
{
this.sessions[clientId][this.PlayerColor[clientId]].MoveLeft();
}
public void RotateLeft(string clientId)
{
this.sessions[clientId][this.PlayerColor[clientId]].RotateLeft();
}
public void RotateRight(string clientId)
{
this.sessions[clientId][this.PlayerColor[clientId]].RotateRight();
}
}
}
|