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
157
158
159
160
161
162
163
164
165
166
167
168
|
using Microsoft.AspNetCore.SignalR;
using PUSG.Web.Models;
using PUSG.Web.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PUSG.Web.Hubs
{
public class GameHub : Hub
{
private const string ServerMessageUser = "PUSG";
private ISessionService sessionService;
private IGameService gameService;
private IHubContext<HomeHub> homeHubContext;
public GameHub(ISessionService sessionService, IGameService gameService, IHubContext<HomeHub> homeHubContext)
{
this.sessionService = sessionService;
this.gameService = gameService;
this.homeHubContext = homeHubContext;
}
public override async Task OnConnectedAsync()
{
this.gameService.Waiting.Add(this.Context.ConnectionId);
if(this.gameService.Waiting.Count >= 2)
{
await this.gameService.CreateGameSession(
this.gameService.Waiting[0],
this.gameService.Waiting[1]);
await this.Clients.Client(
this.gameService.Waiting[0]).SendAsync("StartGame", new []{ "blue" });
await this.Clients.Client(
this.gameService.Waiting[1]).SendAsync("StartGame", new[]{ "red" });
this.gameService.Waiting.RemoveAt(0);
this.gameService.Waiting.RemoveAt(0);
}
await base.OnConnectedAsync();
}
public async Task Login(string username)
{
this.gameService.PlayerUsernames[this.Context.ConnectionId] = username;
}
public override async Task OnDisconnectedAsync(Exception exception)
{
string id = this.Context.ConnectionId;
string otherId = this.gameService.OtherPlayer.ContainsKey(id)
? this.gameService.OtherPlayer[id]
: null;
await this.gameService.DestroyGameSession(id);
await this.Clients.Client(otherId).SendAsync("GameEnd");
await base.OnDisconnectedAsync(exception);
}
public async Task Update(string command)
{
switch(command)
{
case "up":
{
this.gameService.MoveUp(this.Context.ConnectionId);
await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
.SendAsync("OtherUp");
break;
}
case "right":
{
this.gameService.MoveRight(this.Context.ConnectionId);
await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
.SendAsync("OtherRight");
break;
}
case "down":
{
this.gameService.MoveDown(this.Context.ConnectionId);
await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
.SendAsync("OtherDown");
break;
}
case "left":
{
this.gameService.MoveLeft(this.Context.ConnectionId);
await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
.SendAsync("OtherLeft");
break;
}
case "rotate-left":
{
this.gameService.RotateLeft(this.Context.ConnectionId);
await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
.SendAsync("OtherRotateLeft");
break;
}
case "rotate-right":
{
this.gameService.RotateRight(this.Context.ConnectionId);
await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
.SendAsync("OtherRotateRight");
break;
}
case "pew":
{
this.gameService.Shoot(this.Context.ConnectionId);
await this.Clients.Client(this.gameService.OtherPlayer[this.Context.ConnectionId])
.SendAsync("OtherShoot");
break;
}
}
}
public async Task GetState()
{
GameSession gameSession = this.gameService[this.Context.ConnectionId];
if (gameSession.IsDone)
{
string id = this.Context.ConnectionId;
string otherId = this.gameService.OtherPlayer.ContainsKey(id)
? this.gameService.OtherPlayer[id]
: null;
string clientUsername = this.gameService.PlayerUsernames[id];
string otherClientUsername = this.gameService.PlayerUsernames[otherId];
List<string> clientSessionIds = this.sessionService.Users.Where(user => user.Value == clientUsername).Select(user => user.Key).ToList();
List<string> otherClientSessionIds = this.sessionService.Users.Where(user => user.Value == otherClientUsername).Select(user => user.Key).ToList();
List<string> totalExcludedIds = clientSessionIds.Concat(otherClientSessionIds).ToList();
string winningMessage =
this.gameService.PlayerColor[id] == gameSession.Winner
? $"{clientUsername} won against {otherClientUsername}!"
: $"{otherClientUsername} won against {clientUsername}!";
await this.homeHubContext.Clients.AllExcept(totalExcludedIds).SendAsync("ReceiveMessage", new[] { ServerMessageUser, winningMessage });
await this.Clients.Clients(id, otherId).SendAsync("GameEnd", new { Result = winningMessage });
}
else
{
GameState gameState = new GameState
{
Blue = gameSession.BluePlayer,
Red = gameSession.RedPlayer,
Shells = gameSession.Shells.Select(shell => shell.Value).ToList(),
};
await this.Clients
.Client(this.Context.ConnectionId)
.SendAsync("GameState", gameState);
}
}
}
}
|