Beispiel #1
0
        private void SendSyncLobby(string roomName, Gambler gambler)
        {
            var newPlayer = new List<SerializableType> { gambler };
            var response = new RemoteInvokeMethod(Command.SyncLobby, newPlayer);
            var bytes = RemoteInvokeMethod.WriteToStream(response);

            foreach (var player in Lobbies[roomName].Players)
            {
                player.Sock.Send(bytes, bytes.Length, 0);
            }

            var playersInLobby = Players.FindAll(x => (x.State == State.Lobby || x.State == State.Connect) && x.Id != gambler.Id);
            var lobbyUpdate = new RemoteInvokeMethod(Command.UpdateLobby, newPlayer);
            var bytesMessage = RemoteInvokeMethod.WriteToStream(lobbyUpdate);
            foreach (var player in playersInLobby)
            {
                player.Sock.Send(bytesMessage, bytesMessage.Length, 0);
            }
        }
Beispiel #2
0
        public RemoteInvokeMethod Login(Authentication authentification)
        {
            var name = authentification.Name;
            var pass = authentification.Pass;
            var user = _userRepository.Get(name, pass);
            var args = new List<SerializableType>();
            var gambler = new Gambler();
            args.Add(gambler);
            if (user == null)
            {
                gambler.Name = "";
                gambler.Response = Response.Fail.ToString();
                return new RemoteInvokeMethod(args);
            }

            gambler.Name = user.Username;
            gambler.Id = user.Id;
            gambler.Wins = user.Histories.Count(x => x.Result == 1);
            gambler.Response = Response.Succed.ToString();
            args.AddRange(Lobbies.Values
                .Where(x => x.IsGameStart == false)
                .Select(lobby => new LobbyInfo()
                {
                    GameType = lobby.GameType,
                    CurrentPlayers = lobby.Players.Count,
                    MaxPlayers = lobby.MaxPlayers,
                    Name = lobby.Name,
                    PlayerId = lobby.CreatorId
                }));
            return new RemoteInvokeMethod(args);
        }
Beispiel #3
0
 public RemoteInvokeMethod Register(Authentication authentification)
 {
     var username = authentification.Name;
     var password = authentification.Pass;
     var id = _userRepository.Add(username, password).Id;
     var args = new List<SerializableType>();
     var gambler = new Gambler
     {
         Name = username,
         Id = id,
         Response = Response.Succed.ToString()
     };
     args.Add(gambler);
     args.AddRange(Lobbies.Values
         .Where(x => x.IsGameStart == false)
         .Select(lobby => new LobbyInfo()
         {
             GameType = lobby.GameType,
             CurrentPlayers = lobby.Players.Count,
             MaxPlayers = lobby.MaxPlayers,
             Name = lobby.Name,
             PlayerId = lobby.CreatorId
         }));
     return new RemoteInvokeMethod(args);
 }
Beispiel #4
0
        public RemoteInvokeMethod Join(LobbyInfo lobby)
        {
            var playerId = lobby.PlayerId;
            var roomName = lobby.Name;
            var index = Players.FindIndex(x => x.Id == playerId);
            var emptySlot = Lobbies[roomName].EmptySlot();
            Players[index].Lobby = lobby.Name;
            var gambler = new Gambler()
            {
                Id = playerId,
                Name = Players[index].Name,
                Slot = emptySlot
            };

            if (emptySlot >= 0)
            {
                gambler.Response = Response.Succed.ToString();
            }
            else
            {
                gambler.Response = Response.Fail.ToString();
                var user = new List<SerializableType> { gambler };
                return new RemoteInvokeMethod(user);
            }

            SendSyncLobby(roomName, gambler);
            SendUnits(Players[index]);

            var args = new List<SerializableType>();
            var lobbyInfo = new LobbyInfo(Lobbies[roomName].CreatorId, Lobbies[roomName].Name, Lobbies[roomName].GameType, Lobbies[roomName].MaxPlayers, Lobbies[roomName].Players.Count);
            args.Add(lobbyInfo);

            args.AddRange(Lobbies[roomName].Players.Select(player => new Gambler
            {
                Name = player.Name,
                Id = player.Id,
                Slot = player.Slot,
                Response = Response.Succed.ToString()
            }));

            Lobbies[lobby.Name].Players.Add(Players[index]);
            Players[index].Slot = gambler.Slot;
            Players[index].Team = Players[index].Slot % 2 == 0 ? Team.Red : Team.Blue;

            Players[index].State = State.Join;
            args.Add(gambler);
            return new RemoteInvokeMethod(args);
        }