public EitherAsync <Error, Player> Add(Player player)
 {
     return(_decoratee.Add(player)
            .Do(p =>
     {
         _versionMap.RecordVersionChange(GetEntityId(p), p.Version);
     }));
 }
Example #2
0
        public async Task <ActionResult> Post([FromBody] PlayerPostRequest request,
                                              [FromServices] LinkGenerator linkGenerator)
        {
            var player = await _playersRepository.Add(request);

            var url = linkGenerator.GetPathByAction(HttpContext,
                                                    nameof(Get),
                                                    values: new { player.Id });

            return(Created(url, player));
        }
Example #3
0
        public CreatedPlayerDTO Create(Guid teamId, string name)
        {
            var player           = new Player(name);
            var playerValidation = player.Validate();

            if (playerValidation.isValid)
            {
                player.AddTeam(teamId);
                _playersRepository.Add(player);

                return(new CreatedPlayerDTO(player.Id));
            }

            return(new CreatedPlayerDTO(playerValidation.errors));
        }
        public ActionResult <IEnumerable <PlayerCreationDto> > CreatePlayerCollection(IEnumerable <PlayerCreationDto> playerCollection)
        {
            var entities = Mapper.Map <IEnumerable <Player> >(playerCollection);

            foreach (var player in entities)
            {
                _playersRepository.Add(player);
            }

            _playersRepository.Save();

            var added = Mapper.Map <IEnumerable <PlayerDto> >(entities);

            return(CreatedAtRoute(
                       PlayerCollectionRouteName,
                       new { ids = string.Join(",", added.Select(a => a.Id)) },
                       added));
        }
Example #5
0
            public EitherAsync <Error, Player> Handle(Request request)
            {
                var id     = new PlayerId(_idGenerator.GetRandomId());
                var player = new Player(_currentUser.Tenant, Version.Initial, id, request.FirstName, request.LastName);

                var addResult = _repository.Add(player);

                return(addResult
                       .Do(createdPlayer =>
                {
                    DomainEvents.Add(new PlayerCreated
                    {
                        TenantId = createdPlayer.Tenant,
                        Version = createdPlayer.Version,
                        PlayerId = createdPlayer.Id,
                        FirstName = createdPlayer.FirstName,
                        LastName = createdPlayer.LastName
                    });
                }));
            }
Example #6
0
        private void SaveCommandHandler()
        {
            PlayerDetails.IsDirty = false;

            if (PlayerDetails.Id == Guid.Empty)
            {
                PlayerDetails.Id = Guid.NewGuid();
                Players.Add(PlayerDetails);
            }
            else
            {
                var playerToChange = Players.FirstOrDefault(m => m.Id == PlayerDetails.Id);

                playerToChange?.CopyFrom(PlayerDetails);
            }

            // Disconnect object before saving to prevent same object being linked to master detail view.
            _playersRepository.Add(PlayerDetails.Clone());

            _playersRepository.SaveAll();
        }
        public override void RegisterScenarioDependencies(Container container)
        {
            base.RegisterScenarioDependencies(container);

            _queryMock = Substitute.For <IQuery <TenantId, List <PlayerDto> > >();
            container.Register(() => _queryMock, Lifestyle.Scoped);

            _repoMock = Substitute.For <IPlayersRepository>();
            _repoMock.Add(Arg.Any <Player>())
            .Returns(ci => ci.Arg <Player>())
            .AndDoes(ci => { _actualPlayer = ci.Arg <Player>(); });

            _repoMock.Update(Arg.Any <Player>(), Arg.Any <Version>())
            .Returns(ci =>
            {
                var original       = ci.Arg <Player>();
                var updatedVersion = new Version($"updated-{original.Version}");

                _versionMap.RecordVersionChange(this.GetEntityId(original), updatedVersion);

                var updated = new Player(original.Tenant, updatedVersion, original.Id, original.FirstName,
                                         original.LastName);

                return(updated);
            })
            .AndDoes(ci =>
            {
                if (_actualPlayer == null)
                {
                    return;
                }
                _actualPlayer = ci.Arg <Player>();
            });

            container.Register(() => _repoMock, Lifestyle.Scoped);
        }
        public async Task <ActionResult <PlayerResponse> > AddPlayer([FromBody] PlayerUpsert playerCreate)
        {
            var newPlayer = await _repo.Add(playerCreate);

            return(CreatedAtRoute(nameof(GetSinglePlayer), new { id = newPlayer.Id }, newPlayer));
        }
Example #9
0
 public void Create(Player player)
 {
     _playerRepository.Add(player);
 }
Example #10
0
        static void Main()
        {
            Console.Write("Enter server name: ");
            var serverName = Console.ReadLine();

            if (serverName.Contains(' '))
            {
                throw new Exception("Server name cannot contain spaces.");
            }
            Console.Write("Use (T)ext or (J)son? ");
            ConsoleKey keyResponse;

            do
            {
                keyResponse = Console.ReadKey().Key;
            } while (keyResponse != ConsoleKey.T && keyResponse != ConsoleKey.J);
            var json = keyResponse == ConsoleKey.J;

            Console.WriteLine();
            Console.WriteLine("Initiating...");
            Initiator.AutoMapperConfig();
            Console.WriteLine("Creating new game...");
            var world = new WorldMap();

            Console.WriteLine("Starting server...");
            Socket listeningSocket        = null;
            var    clients                = new List <Client>();
            var    responseQueue          = new Queue <Response>();
            var    unconfirmedConnections = new List <Socket>();
            var    currentPlayerIndex     = 0;

            //try
            //{
            listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listeningSocket.Bind(localEndPoint);
            listeningSocket.Listen(LISTENERBACKLOG);
            Console.WriteLine("Informing master server...");
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Connect(IPAddress.Loopback, MASTERPORT);
                NetHelpers.SendString($"WAMP/1.0 ADD {serverName} {PORT}", socket);
                socket.Shutdown(SocketShutdown.Both);
            }

            Console.WriteLine($"WWAServer is listening on {listeningSocket.LocalEndPoint}. Press Q to quit.");
            while (true)
            {
                if (Console.KeyAvailable && Console.ReadKey().Key == ConsoleKey.Q)
                {
                    break;
                }
                // Check for dead connections
                for (int i = 0; i < unconfirmedConnections.Count; i++)
                {
                    if (IsDisconnected(unconfirmedConnections[i]))
                    {
                        Console.WriteLine($"{unconfirmedConnections[i].RemoteEndPoint} disconnected.");
                        unconfirmedConnections[i].Close();
                        unconfirmedConnections.RemoveAt(i--);
                    }
                }

                for (int i = 0; i < clients.Count; i++)
                {
                    if (IsDisconnected(clients[i].Socket))
                    {
                        Console.WriteLine($"{clients[i].Socket.RemoteEndPoint} ({clients[i].Player.Name}) disconnected.");
                        responseQueue.Enqueue(new Response {
                            ResponseType = Response.MessageType.REMOVEPLAYER, IdParam = clients[i].Player.PlayerId
                        });
                        clients[i].Socket.Close();
                        clients.RemoveAt(i--);
                        if (clients.Count <= currentPlayerIndex)
                        {
                            currentPlayerIndex = 0;
                            UpdateMap(world, clients, responseQueue, json);
                        }
                    }
                }

                // Check for new logins
                if (clients.Count < 5)
                {
                    for (int i = 0; i < unconfirmedConnections.Count; i++)
                    {
                        var connection = unconfirmedConnections[i];
                        if (connection.Available > 0)
                        {
                            var buffer        = new byte[BUFFERLENGTH];
                            var bytesReceived = connection.Receive(buffer);
                            var command       = encoding.GetString(buffer, 0, bytesReceived);
                            command = command.TrimEnd(';');
                            string[] parts;
                            // Ignore any command that is not LOGIN
                            if (command.StartsWith("WAP/1.0 LOGIN") && (parts = command.Split(' ')).Length == 4)
                            {
                                var    name     = parts[2];
                                var    password = parts[3];
                                var    model    = _repository.GetByName(parts[2]);
                                Player player   = null;
                                if (model == null) //Finns inte i databasen
                                {
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.WELCOME, Socket = connection
                                    });
                                    player          = new Player(GetFirstFreeId(clients), name, 100, 10, 0, world.GetRandomFreeCoords(clients));
                                    model           = Initiator.Mapper.Map <PlayerModel>(player);
                                    model.Password  = password;
                                    model.LastLogin = DateTime.Now;
                                    _repository.Add(model);
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.NEWPLAYER, IdParam = player.PlayerId
                                    });
                                    clients.Add(new Client {
                                        Player = player, Socket = connection
                                    });
                                    unconfirmedConnections.RemoveAt(i--);
                                    if (clients.Count == 2)
                                    {
                                        responseQueue.Enqueue(new Response {
                                            ResponseType = Response.MessageType.YOURTURN, IdParam = clients[currentPlayerIndex].Player.PlayerId
                                        });
                                    }
                                    Console.WriteLine($"{connection.RemoteEndPoint} created player {player.Name}");
                                }
                                else //Finns i databasen
                                {
                                    if (clients.Select(c => c.Player.Name).Contains(model.Name))
                                    {
                                        Console.WriteLine($"{connection.RemoteEndPoint} tried to log in with {model.Name} already in game");
                                        responseQueue.Enqueue(new Response {
                                            ResponseType = Response.MessageType.DENIED, Socket = connection, StringParam = "LOGIN Player already in game"
                                        });
                                    }
                                    else if (model.Password == password)
                                    {
                                        responseQueue.Enqueue(new Response {
                                            ResponseType = Response.MessageType.WELCOME, Socket = connection
                                        });
                                        model.LastLogin = DateTime.Now;
                                        _repository.Update(model);
                                        player             = Initiator.Mapper.Map <Player>(model);
                                        player.Coordinates = world.GetRandomFreeCoords(clients);
                                        player.PlayerId    = GetFirstFreeId(clients);
                                        responseQueue.Enqueue(new Response {
                                            ResponseType = Response.MessageType.NEWPLAYER, IdParam = player.PlayerId
                                        });
                                        clients.Add(new Client {
                                            Player = player, Socket = connection
                                        });
                                        unconfirmedConnections.RemoveAt(i--);
                                        if (clients.Count == 2)
                                        {
                                            responseQueue.Enqueue(new Response {
                                                ResponseType = Response.MessageType.YOURTURN, IdParam = clients[currentPlayerIndex].Player.PlayerId
                                            });
                                        }
                                        Console.WriteLine($"{connection.RemoteEndPoint} logged in as {player.Name}");
                                    }
                                    else
                                    {
                                        Console.WriteLine($"{connection.RemoteEndPoint} failed password for {model.Name}");
                                        responseQueue.Enqueue(new Response {
                                            ResponseType = Response.MessageType.DENIED, Socket = connection, StringParam = "LOGIN Wrong password"
                                        });
                                    }
                                }
                            }
                        }
                    }
                }

                // Accept new connections
                if (unconfirmedConnections.Count < 20 && listeningSocket.Poll(1000, SelectMode.SelectRead))
                {
                    var newConnection = listeningSocket.Accept();
                    Console.WriteLine($"New connection accepted from {newConnection.RemoteEndPoint}");
                    unconfirmedConnections.Add(newConnection);
                }

                // Get player commands
                foreach (var client in clients)
                {
                    if (client.Socket.Available > 0)
                    {
                        var buffer        = new byte[BUFFERLENGTH];
                        var bytesReceived = client.Socket.Receive(buffer);
                        var command       = encoding.GetString(buffer, 0, bytesReceived);
                        //Console.WriteLine($"{command} <- {client.Socket.RemoteEndPoint}");
                        command = command.TrimEnd(';');
                        string[] parts = command.Split(' ');
                        if (parts.Length < 2 || parts[0] != "WAP/1.0")
                        {
                            continue;
                        }
                        switch (parts[1])
                        {
                        case "MOVE":
                            if (clients.IndexOf(client) != currentPlayerIndex || clients.Count < 2)
                            {
                                responseQueue.Enqueue(new Response {
                                    ResponseType = Response.MessageType.DENIED, Socket = client.Socket, StringParam = "MOVE Not your turn"
                                });
                            }
                            else if (parts.Length < 3)
                            {
                                responseQueue.Enqueue(new Response {
                                    ResponseType = Response.MessageType.DENIED, Socket = client.Socket, StringParam = "MOVE invalid direction"
                                });
                            }
                            else
                            {
                                var result = world.MovePlayer(client.Player, parts[2], clients);
                                switch (result.MoveResult)
                                {
                                case MoveResult.Fail:
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.DENIED, Socket = client.Socket, StringParam = "MOVE You cannot move that way"
                                    });
                                    break;

                                case MoveResult.Success:
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.UPDATEPLAYER, IdParam = client.Player.PlayerId
                                    });
                                    break;

                                case MoveResult.Gold:
                                case MoveResult.Potion:
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.UPDATETILE, StringParam = EncodeTile(world, client.Player.Coordinates, json)
                                    });
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.UPDATEPLAYER, IdParam = client.Player.PlayerId
                                    });
                                    UpdateDbStats(_repository, client.Player);
                                    break;

                                case MoveResult.Player:
                                    if (result.Player.Health <= 0)
                                    {
                                        world.GameMap[result.Player.Coordinates.X, result.Player.Coordinates.Y].Gold = result.Player.Gold;
                                        result.Player.Gold        = 0;
                                        result.Player.Coordinates = world.GetRandomFreeCoords(clients);
                                        result.Player.Health      = 100;
                                        result.Player.IsDead      = false;
                                    }
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.UPDATEPLAYER, IdParam = result.Player.PlayerId
                                    });
                                    UpdateDbStats(_repository, result.Player);
                                    break;
                                }
                                if (result.MoveResult != MoveResult.Fail)
                                {
                                    if (++currentPlayerIndex >= clients.Count)
                                    {
                                        currentPlayerIndex = 0;
                                        UpdateMap(world, clients, responseQueue, json);
                                    }
                                    responseQueue.Enqueue(new Response {
                                        ResponseType = Response.MessageType.YOURTURN, IdParam = clients[currentPlayerIndex].Player.PlayerId
                                    });
                                }
                            }
                            break;

                        case "MESSAGE":
                            if (parts.Length < 3)
                            {
                                responseQueue.Enqueue(new Response {
                                    ResponseType = Response.MessageType.DENIED, Socket = client.Socket, StringParam = "MESSAGE Malformed message"
                                });
                            }
                            else
                            {
                                responseQueue.Enqueue(new Response {
                                    ResponseType = Response.MessageType.MESSAGE, IdParam = client.Player.PlayerId, StringParam = command.Substring("WAP/1.0 MESSAGE ".Length)
                                });
                            }
                            break;
                        }
                    }
                }

                // Fire off responses
                while (responseQueue.Count > 0)
                {
                    var response = responseQueue.Dequeue();
                    switch (response.ResponseType)
                    {
                    case Response.MessageType.NEWPLAYER:
                        Response.SendNewPlayerResponses(clients, world, response.IdParam, json);
                        break;

                    case Response.MessageType.DENIED:
                        Response.SendDeniedResponse(clients, response.Socket, response.StringParam);
                        break;

                    case Response.MessageType.REMOVEPLAYER:
                        Response.SendRemovePlayerResponse(clients, response.IdParam);
                        break;

                    case Response.MessageType.UPDATETILE:
                        Response.SendUpdateTile(clients, response.StringParam);
                        break;

                    case Response.MessageType.YOURTURN:
                        Response.SendYourTurn(clients, response.IdParam);
                        break;

                    case Response.MessageType.UPDATEPLAYER:
                        Response.SendUpdatePlayer(clients, response.IdParam, json);
                        break;

                    case Response.MessageType.MESSAGE:
                        Response.SendMessage(clients, response.IdParam, response.StringParam);
                        break;

                    case Response.MessageType.WELCOME:
                        Response.SendWelcome(response.Socket, json);
                        break;
                    }
                }
            }
            // End while

            //}
            //catch (Exception exception)
            //{
            //    Console.WriteLine(exception.Message);
            //    Console.ReadLine();
            //}
            //finally
            //{
            //    listeningSocket?.Close();
            //}

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Connect(IPAddress.Loopback, MASTERPORT);
                NetHelpers.SendString($"WAMP/1.0 REMOVE {PORT}", socket);
                socket.Shutdown(SocketShutdown.Both);
            }
        }