Пример #1
0
        public async Task <Server> UpdateConfiguration(int id, ServerConfigurationModel configurationModel)
        {
            var server = await Get(id);

            var existingProperties = ServerPropertiesSerializer.Deserialize(server.SerializedProperties);
            var mergedProperties   = ServerPropertiesSerializer.Merge(existingProperties, configurationModel.Properties);

            server.SerializedProperties = ServerPropertiesSerializer.Serialize(mergedProperties);
            server.MainPort             = configurationModel.ServerPort;
            server.MemoryAllocationMB   = configurationModel.MemoryAllocationMB;
            server.Description          = configurationModel.Description;
            server.World = _context.Worlds.FirstOrDefault(w => w.ID == configurationModel.WorldID);
            server.Image = _context.Images.FirstOrDefault(i => i.ID == configurationModel.ImageID);

            if (server.Image.SupportsMods && configurationModel.ModIDs != null)
            {
                server.Mods = configurationModel.ModIDs.Select(i => _context.Mods.FirstOrDefault(m => m.ID == i)).ToList();
            }

            server.NeedsRecreate = true;

            _context.Update(server);
            await _context.SaveChangesAsync();

            return(server);
        }
Пример #2
0
        public RconConnection GetConnectionForServer(int serverId)
        {
            RconConnection connection = null;

            //lock (_connections)
            //{
            //connection = _connections.FirstOrDefault(c => c.ServerId == serverId);
            if (connection == null)
            {
                using (var scope = _serviceFactory.CreateScope())
                {
                    var server = scope.ServiceProvider.GetService <IServerRepository>().GetWithDockerInfo(serverId).Result;
                    if (!server.IsAlive)
                    {
                        throw new Exception("Server not alive");
                    }

                    var properties = ServerPropertiesSerializer.Deserialize(server.Server.SerializedProperties);

                    connection = new RconConnection(_rconIP, (ushort)server.Server.RconPort, properties.Rcon__Password, serverId);
                    //_connections.Add(connection);
                }
            }
            //}

            return(connection);
        }
Пример #3
0
        public void ModelSerializesCorrectly()
        {
            var serverProperties = new ServerProperties();

            var data = ServerPropertiesSerializer.Serialize(serverProperties);

            Assert.DoesNotContain("_", data);
            Assert.NotEmpty(data);
        }
Пример #4
0
        public void PropertiesAreMergedCorrectly()
        {
            var a = new ServerProperties();
            var b = new ServerProperties();

            b.Server_Port  = 1000;
            b.Allow_Flight = true;
            b.Motd         = "test";

            ServerPropertiesSerializer.Merge(a, b);

            Assert.NotEqual(a.Server_Port, b.Server_Port);
            Assert.Equal(a.Allow_Flight, b.Allow_Flight);
            Assert.Equal(a.Motd, b.Motd);
        }
Пример #5
0
        public void ModelDeserializesCorrectly()
        {
            var data = @"#Minecraft server properties
#(File Modification Datestamp)
max-tick-time=60000
generator-settings=
allow-nether=true
force-gamemode=false
gamemode=0
enable-query=false
player-idle-timeout=0
difficulty=1
spawn-monsters=true
op-permission-level=4
announce-player-achievements=true
pvp=true
snooper-enabled=true
level-type=DEFAULT
hardcore=false
enable-command-block=false
max-players=20
network-compression-threshold=256
resource-pack-sha1=
max-world-size=29999984
server-port=25565
server-ip=
spawn-npcs=true
allow-flight=false
level-name=world
view-distance=10
resource-pack=
spawn-animals=true
white-list=false
generate-structures=true
online-mode=true
max-build-height=256
level-seed=
prevent-proxy-connections=false
motd=A Minecraft Server
enable-rcon=false";

            var serverProperties = ServerPropertiesSerializer.Deserialize(data);

            Assert.Equal("A Minecraft Server", serverProperties.Motd);
            Assert.Equal(25565, serverProperties.Server_Port);
        }
Пример #6
0
        public async Task <IActionResult> Get(int serverId)
        {
            var serverWithDockerInfo = await _serverRepository.GetWithDockerInfo(serverId);

            var properties = ServerPropertiesSerializer.GetUserChangableProperties();
            var mapPaths   = serverWithDockerInfo.Server.World != null ? await _worldRepository.GetMapImagePaths(serverWithDockerInfo.Server.World.ID)
                                                                     : new Service.Models.MapImagePaths
            {
                MapPath = null
            };

            return(Ok(new
            {
                hasMapImage = !string.IsNullOrEmpty(mapPaths.MapPath),
                server = serverWithDockerInfo.Server,
                isAlive = serverWithDockerInfo.IsAlive,
                properties = properties
            }));
        }
Пример #7
0
        public async Task <Server> Add(ServerAddModel serverAddModel)
        {
            var serializedProperties = ServerPropertiesSerializer.Serialize(new ServerProperties());

            var server = new Server
            {
                Description          = serverAddModel.Description,
                Image                = _context.Images.FirstOrDefault(i => i.ID == serverAddModel.ImageID),
                World                = _context.Worlds.FirstOrDefault(w => w.ID == serverAddModel.WorldID),
                SerializedProperties = serializedProperties,
                MainPort             = serverAddModel.ServerPort,
                MemoryAllocationMB   = serverAddModel.MemoryAllocationMB,
                QueryPort            = serverAddModel.ServerPort + 1000,
                RconPort             = serverAddModel.ServerPort + 2000
            };

            if (server.Image == null)
            {
                throw new BadInputException("No such image");
            }

            if (server.World == null)
            {
                throw new BadInputException("No such world");
            }

            if (server.Image.SupportsMods && serverAddModel.ModIDs != null)
            {
                server.Mods = serverAddModel.ModIDs.Select(id => _context.Mods.FirstOrDefault(m => m.ID == id)).ToArray();
            }

            await _context.Servers.AddAsync(server);

            await _context.SaveChangesAsync();

            return(server);
        }