Get Touge training reference answers (EduCoder)

Practical training answer query entrance

The answers to the practical training on the Touge EduCoder platform are here. Some answers are collected in it. You can check if there is anything you want to see.

https://edaser.github.io/

Be sure not to copy the answers directly . It is recommended that you do it yourself. If you really don’t know how to do it, you should complete it independently after reading the reference.

Here you can query some answers to practical training. The background database records the answers to hundreds of practical training levels. See below for the implementation method.

Implementation

The EduCoder platform requires gold coins to unlock answers, with an average of 150 gold coins per level. First, automatically sign in every day to collect gold coins . Through these gold coins, you can unlock most of the answers to the training. Then you can get the answers through the interface and save them as a database.

The following code is for nodejs environment

EduCoder interface encapsulation code

const rp = require("request-promise");

class Session{
    
    
  /**
  简单的一个Session会话类,用于记录cookies
  */ 
  constructor(cookies){
    
    
    this.cookies = cookies||""; //记录cookies
  }
  async request({
    
    url, method="GET",header, data, success, fail, complete}){
    
    
    var options = {
    
    
      method,
      json:true,
      uri: url,
      headers:{
    
    
        Cookie: this.cookies, ...header //每次请求带上cookies
      },
      resolveWithFullResponse:true  // 加上这个可以获取到请求头,从而得到新cookies,否则只返回请求得到的数据
    }
    if(method=="GET"){
    
    
      options.qs = data //如果是GET,把data传入querystring
    }else if(method=="POST"){
    
    
      options.body = data //如果是POST,把data传入body
    }
    try{
    
    
      console.debug("request options", options);
      let {
    
    headers, body} = await rp(options); //用request-promise发起网络请求
      console.debug("request_success", headers, body);
      if(headers["set-cookie"]){
    
     //如果有新cookies,则获取
        this.cookies = headers["set-cookie"].map(i=>i.split(/;/g)[0]).join(";") // 简单的记录cookies
      }else if(headers["Set-Cookie"]){
    
    
        this.cookies = headers["set-cookie"].map(i=>i.split(/;/g)[0]).join(";")
      }
      success&&success(body); //成功,回调success函数
      complete&&complete(body);
      return body;  // 返回数据
    }catch(e){
    
    
      fail&&fail(e);
      complete&&complete(e);
      throw e;
    }
  }
}

const apiUrl = "https://www.educoder.net/api/"; //接口地址

async function eduHTTPApi({
     
     session,url,method,data}){
    
    
  // 访问EduCoder的api接口,并处理返回的数据
  url = apiUrl + url;
  let res = await session.request({
    
    
    url,
    method,
    data
  });
  // 抛出调用EduCoder接口时的错误,status<0或status>100时错误
  if(res.status && res.status > 100 || res.status<0){
    
    
    let e =  new Error(res.message);
    e.code = res.status;
    throw e;
  }else
    return res;
}

// 所有已经封装的EduCoder的接口函数合集
const eduApi = {
    
    
  //登录
  async ["accounts.login"]({
     
     session, data}){
    
    
    return eduHTTPApi({
    
    
      session,
      method:"POST",
      url:"accounts/login.json",
      data
    });
  },
  // 获取自己的所有实训
  async ["users.shixuns"]({
     
     session, data}){
    
    
    let url = `users/${
      
      data.login}/shixuns.json`;
    delete data.login;
    return eduHTTPApi({
    
    
      session,
      url,
      data
    })
  },
  // 获取实训的详情
  async ["shixuns"]({
     
     session, data}){
    
    
    let url = `shixuns/${
      
      data.identifier}`;
    delete data.identifier;
    return eduHTTPApi({
    
    
      session,
      url,
      data
    })
  },
  // 获取实训的关卡
  async ["shixuns.challenges"]({
     
     session, data}){
    
    
    let url =`shixuns/${
      
      data.identifier}/challenges.json`;
    delete url.identifier;
    return eduHTTPApi({
    
    
      session, 
      url,
      data
    })
  },
  // 获取已解锁的答案
  async ["tasks.get_answer_info"]({
     
     session, data}){
    
    
    let url =  `tasks/${
      
      data.identifier}/get_answer_info.json`;
    delete data.identifier;
    return eduHTTPApi({
    
    
      session,
      url,
      data
    })
  },
  // 解锁答案
  async ["tasks.unlock_answer"]({
     
     session, data}){
    
    
  	let url = `tasks/${
      
      data.identifier}/unlock_answer.json`;
  	delete data.identifier;
  	return eduHTTPApi({
    
    
      session,
      url,
      data
    })
  }
}

Call the encapsulated function to get the answer

async function main(){
    
    
  let session = new Session() //创建会话对象
  let login = "用户名";
  let password = "对应的密码";
  // 调用登录接口
  let {
    
    login} = await eduApi["accounts.login"]({
    
    session, data:{
    
    login, password}}); // 获取用户的login
  // 获取自己的实训列表
  let {
    
    shixuns} = await eduApi["users.shixuns"]({
    
    session, data:{
    
    login, page:1, per_page:10}});
  // 以获取第一个实训的第一个关卡的答案为例
  let {
    
    identifier} = shixuns[0]; //第一个实训
  // 获取实训的所有关卡
  let {
    
    challenge_list} = await eduApi["shixuns.challenges"]({
    
    session, data:{
    
    identifier}});
  let challenge = challenge_list[0]; // 第一个关卡
  var task_identifier = challenge.open_game.match( /\/tasks\/(.*)/)[1];
  try{
    
    
    var {
    
    message} =  await eduApi["tasks.get_answer_info"]({
    
    session, data:{
    
    identifier: task_identifier}});
    // 如果答案已经解锁了,则成功获取答案
    console.info(message);
    // ...其他函数逻辑
  }catch(e){
    
     // 答案没有解锁的情况
    // 解锁答案 
    var {
    
    contents} = await eduApi["tasks.unlock_answer"]({
    
    session, data:{
    
    identifier: task_identifier}});
    console.info(contents);
    // ...其他函数逻辑
  }
}

Guess you like

Origin blog.csdn.net/weixin_44505587/article/details/129306742