exaV / screeps-kotlin-starter

A starting point for a Screeps AI written in Kotlin
MIT License
46 stars 63 forks source link

peer not authenticated when I try to doploy my code #11

Closed Tender2233 closed 3 years ago

Tender2233 commented 3 years ago

This repo might not be updated long time.But these days I'm trying to play screeps by kotlin and I find this repo. If someone still maintain this repo, can anyone help me with that.

I meet this problem when I first try gradlew deploy.

It's work when I try build this project.So It's seems to be wrong when upload js file to screeps.com Have trying add screeps.com.cer to my jdk-home/jre/lib/secutiry and keytool -import -alias screeps -file screeps.com.cer -keystore cacerts -storepass changeit.But it not work. Also trying to downgrade my jdk version to 8,and not work.

THIS PROBLEM MAY BE SILLY,beacause I never use gradle before.

I may upload the js file manually later.

Task :deploy FAILED uploading 3 files to branch 'kotlin' on server https://screeps.com

FAILURE: Build failed with an exception.

Tender2233 commented 3 years ago

I figure it out by write my own deploy task code.

But I add new dependency gson and apache httpclient. So if everyone else is fine with old code,there is no need to change.

tasks.register("deploy") {
    group = "screeps"
    dependsOn(processDceKotlinJs)
    val modules = mutableMapOf<String, String>()
    val minifiedCodeLocation = File(minifiedJsDirectory)

    doFirst {
        val jsFiles = minifiedCodeLocation.listFiles { _, name -> name.endsWith(".js") }.orEmpty()
        val (mainModule, otherModules) = jsFiles.partition { it.nameWithoutExtension == project.name }

        val main = mainModule.firstOrNull()
            ?: throw IllegalStateException("Could not find js file corresponding to main module in ${minifiedCodeLocation.absolutePath}. Was looking for ${project.name}.js")

        modules["main"] = main.readText()
        modules.putAll(otherModules.associate { it.nameWithoutExtension to it.readText() })

        val client =
            de.undercouch.gradle.tasks.download.org.apache.http.impl.client.HttpClients.createDefault()
        val post =
            de.undercouch.gradle.tasks.download.org.apache.http.client.methods.HttpPost("$host/api/user/code");
        if (screepsToken != null) {
            post.setHeader("X-Token", screepsToken)
        } else {
            post.setHeader("Authorization", "Basic " + "$screepsUser:$screepsPassword".encodeBase64())
        }
        post.setHeader("Content-Type", "application/json")
        val body = mapOf("branch" to branch, "modules" to modules)
        val gson = com.google.gson.Gson()
        val entity =
            de.undercouch.gradle.tasks.download.org.apache.http.entity.StringEntity(gson.toJson(body))
        entity.setContentType("application/json")
        println("post body entity: ${entity.content}")
        post.entity = entity

        try {
            val response = client.execute(post)
            println(de.undercouch.gradle.tasks.download.org.apache.http.util.EntityUtils.toString(response.entity))
        } catch (e: Exception) {
            throw GradleException(e.stackTrace.toString(), e)
        }

    }

}