Ejemplo n.º 1
0
        public Room(ConnectionsHandler connections, Player owner, SettingsLoader settings, string password = "")
        {
            ConnectionsHandler = connections;
            connections.AddMessageListener(this);

            Settings = settings;
            Settings.OnSettingsChanged += OnSettingsChanged;

            Owner = owner;

            RoomIdPool = new IdPool(MaxPlayers);

            RoomData = new RoomData();
            RoomData.ID = Guid.NewGuid().ToString().Substring(0, GuidSize);
            RoomData.Password = password;
            RoomData.Players = new List<PlayerData>();
            RoomData.Owner = Owner.Data;
            RoomData.MinPlayers = settings.Values.Server.MinPlayers;

            Players = new List<Player>();

            if (string.IsNullOrEmpty(password))
            {
                Logger.Log(this, "Creating room for {0}", Owner.Data.Name);
            }
            else
            {
                Logger.Log(this, "Creating room for {0} with password {1}", Owner.Data.Name, password);
            }

            Join(Owner);
        }
Ejemplo n.º 2
0
        public void TestServer()
        {
            Form1 fm = new Form1();

            Server.Player pl = new Server.Player(1, 2);
            fm.ChangeWay(pl, pl.NextWay);
            Assert.IsTrue(fm.newWaysTest());
        }
Ejemplo n.º 3
0
        public void Join(Player joiningPlayer, string password = "")
        {
            Logger.Log(this, "Player {0} joined room {1}", joiningPlayer.Data.Name, RoomData.ID);

            // Prevent join if password is incorrect
            if (joiningPlayer.Data.ID != Owner.Data.ID && password != RoomData.Password)
            {
                Logger.Log(this, "Player {0} provided incorrect password {1}. (Is {2})", joiningPlayer.Data.Name, password, RoomData.Password);
                joiningPlayer.SendRoomJoinNotice(RoomNotice.InvalidPassword);
                return;
            }

            // Prevent join if room is full
            if (Players.Count == Settings.Values.Server.MaxPlayers)
            {
                Logger.Log(this, "Player {0} attempt to join the room {1} but that room is full", joiningPlayer.Data.Name, RoomData.ID);
                joiningPlayer.SendRoomJoinNotice(RoomNotice.RoomFull);
                return;
            }

            // Prevent join if game has started
            if (RoomData.GameStarted)
            {
                Logger.Log(this, "Player {0} attempt to join the room {1} but the game has already started", joiningPlayer.Data.Name, RoomData.ID);
                joiningPlayer.SendRoomJoinNotice(RoomNotice.GameAlreadyStarted);
                return;
            }

            if (Players.Contains(joiningPlayer))
            {
                // TODO: Handle player rejoining room after disconnection?
                Logger.Log(this, "Already contains player {0}", joiningPlayer.Data.Name);
                joiningPlayer.SendRoomJoinNotice(RoomNotice.AlreadyInRoom);
            }
            else
            {
                joiningPlayer.SendRoomJoinNotice(RoomNotice.None);

                // Assign colour
                var color = RoomIdPool.GetValue();
                joiningPlayer.AssignRoomId(color);

                Players.Add(joiningPlayer);
                RoomData.Players.Add(joiningPlayer.Data);

                // Add callbacks
                joiningPlayer.OnConnectionClosed += OnPlayerConnectionClosed;
                joiningPlayer.OnAwayStatusChanged += OnPlayerAwayStatusChanged;

                // Send message to joining player
                EchoActionToAll(joiningPlayer.Data, PlayerAction.Joined);

                SendUpdateToAll();
            }
        }
Ejemplo n.º 4
0
        void IConnectionMessageHandler.HandleMessage(Player player, string json)
        {
            Message.IsType<ClientMessage.Game.SendAction>(json, (data) =>
            {
                if (player == Owner)
                {
                    switch (data.Action)
                    {
                        case GameAction.Start:
                            Logger.Log(this, "{0} has started the game", Owner.Data.Name);
                            StartCountdown();
                            break;

                        case GameAction.CancelStart:
                            Logger.Log(this, "{0} has cancelled the game from starting", Owner.Data.Name);
                            CancelCountdown();
                            break;

                        case GameAction.Restart:
                            Logger.Log(this, "{0} has restarted the game", Owner.Data.Name);
                            Game.Restart();
                            break;

                        case GameAction.StartNewRound:
                            Logger.Log(this, "{0} has started a new round", Owner.Data.Name);
                            Game.StartNewRound();
                            break;

                        case GameAction.ForceStart:
                            if (player.IsAdmin)
                                StartCountdown();
                            break;

                        default:
                            break;
                    }

                    SendUpdateToAll();
                }
            });

            Message.IsType<ClientMessage.SendChat>(json, (data) =>
            {
                Logger.Log(this, "{0} said something", player.Data.Name);
                // Echo chat to all clients
                Players.ForEach(x => x.SendChat(player.Data, data.Message));
            });
        }
Ejemplo n.º 5
0
        void IConnectionMessageHandler.HandleMessage(Player player, string json)
        {
            Message.IsType<ClientMessage.RequestRoomList>(json, (data) =>
            {
                Logger.Log(this, "Recieved a request from {0} for a list a rooms.", player);
                var rooms = Rooms.Select(x => x.RoomData).ToList();
                player.SendMessage(new ServerMessage.RoomList(rooms));
            });

            Message.IsType<ClientMessage.JoinRoom>(json, (data) =>
            {
                Logger.Log(this, "Recieved a request from {0} to join room {1}.", player.Data, data.RoomId);

                var roomHasPlayer = Rooms.Find(x => x.HasPlayer(player.Data));
                if (roomHasPlayer != null)
                    roomHasPlayer.Leave(player.Data);

                var targetRoom = Rooms.Find(x => x.RoomData.ID == data.RoomId);

                if (targetRoom != null)
                {
                    targetRoom.Join(player, data.Password);
                }
                else
                {
                    player.SendRoomJoinNotice(RoomNotice.RoomDoesNotExist);
                }
            });

            Message.IsType<ClientMessage.LeaveRoom>(json, (data) =>
            {
                var containingRoom = Rooms.Find(x => x.HasPlayer(player.Data));

                if (containingRoom != null)
                {
                    Logger.Log(this, "Remove {0} from room", player);
                    containingRoom.Leave(player.Data);
                }
                else
                {
                    Logger.Warn(this, "Cannot remove {0} from room as they are not in that room", player);
                }
            });

            Message.IsType<ClientMessage.CreateRoom>(json, (data) =>
            {
                if (Rooms.Count != SettingsLoader.Values.Server.MaxRooms)
                {
                    Logger.Log(this, "Create room for {0} with password {1}", player, data.Password);

                    var playerCurrentRoom = FindRoomContainingPlayer(player.Data);
                    if (playerCurrentRoom != null)
                        playerCurrentRoom.Leave(player.Data);

                    var room = new Room(ConnectionsHandler, player, SettingsLoader, data.Password);

                    room.OnEmpty += OnRoomEmpty;

                    Rooms.Add(room);
                }
                else
                {
                    Logger.Log(this, "Cannot create room for {0} as the maximum room limit has been reached", player);
                    player.SendRoomJoinNotice(RoomNotice.MaxRoomsLimitReached);
                }
            });
        }
Ejemplo n.º 6
0
 public void AddPlayer(Player player)
 {
     _dataModelContainer.PlayerSet.Add(player);
     _dataModelContainer.SaveChanges();
 }
Ejemplo n.º 7
0
 private Ship CreateShip(Player player)
 {
     ushort id = 0;
     lock (this)
     {
         id = _currentId++;
     }
     player.ShipId = id;
     var ship = new Ship(this)
     {
         Status = ShipStatus.Waiting,
         team = id,//Deathmatch
         id = id,
         player = player,
         rot = (float)(_rand.NextDouble() * 2 * Math.PI),
         x = X_MIN + (float)(_rand.NextDouble() * (X_MAX - X_MIN)),
         y = Y_MIN + (float)(_rand.NextDouble() * (Y_MAX - Y_MIN)),
         currentPv = 15,
         maxPv = 15,
         weapons = new Weapon[] { new Weapon { id = "canon", damage = 10, precision = 0.6f, coolDown = 1500, range = 200 }/*, new Weapon { id = "missile", damage = 40, precision = 0.6f, coolDown = 3 }*/ }
     };
     return ship;
 }
Ejemplo n.º 8
0
        private async Task OnConnected(IScenePeerClient client)
        {
            var pInfos = PlayerInfos.FromPeer(client);

            var player = new Player(pInfos, client.Id);

            _players.AddOrUpdate(client.Id, player, (id, old) => player);

            Ship ship = null;
            if (!player.IsObserver)
            {
                ship = CreateShip(player);
                player.ShipId = ship.id;
                _ships.AddOrUpdate(ship.id, ship, (id, old) => ship);

                var dto = new ShipCreatedDto { timestamp = _scene.GetComponent<IEnvironment>().Clock, id = ship.id, team = ship.team, x = ship.x, y = ship.y, rot = ship.rot, weapons = ship.weapons, status = ship.Status };
                var data = new[] { dto };

                client.Send("ship.me", s => client.Serializer().Serialize(data, s), PacketPriority.MEDIUM_PRIORITY, PacketReliability.RELIABLE);

                _scene.Broadcast("ship.add", data, PacketPriority.MEDIUM_PRIORITY, PacketReliability.RELIABLE);

            }

            // Send ships to new client
            var shipsToSend = new List<ShipCreatedDto>();
            foreach (var s in _ships.Values.ToArray())
            {
                var dto = new ShipCreatedDto { timestamp = _scene.GetComponent<IEnvironment>().Clock, id = s.id, team = s.team, x = s.x, y = s.y, rot = s.rot, weapons = s.weapons, status = s.Status };
                if (ship == null || ship.id != s.id)
                {
                    shipsToSend.Add(dto);
                }
            }
            client.Send("ship.add", stream =>
            {
                client.Serializer().Serialize(shipsToSend, stream);
            }, PacketPriority.MEDIUM_PRIORITY, PacketReliability.RELIABLE);

            if (ship != null)
            {
                await Task.Delay(1000);
                ship.UpdateStatus(ShipStatus.InGame);
            }

            _scene.GetComponent<ILogger>().Info("gameScene", "Added ship");

            StartUpdateLoop();
        }