読者です 読者をやめる 読者になる 読者になる

/home/cympfh/

はてなブログはクソなのでやめます => https://hackmd.io/s/SyQBwu6Kg#

CODE RUNNER 2014 本戦

プロコン

Sun Nov 30 17:15:19 JST 2014

test.coffee

fs = require 'fs'
{exec} = require 'child_process'
it = require './it.json'

N = (+process.argv[3] or 60)

ms = []

curl = (url, cont) ->
  exec "curl \'#{url}\' |nkf", (err, out) ->
    throw err if err
    cont out if cont

URL = "https://game.coderunner.jp/"
token = 'CDR2YBS99ZXWOQSQUERT71KL18ORJV6K'
time_lag = 1100

# who am i?
info = {}
ami = (cont) ->
  curl "#{URL}info.json?token=#{token}", (result) ->
    try
      info = JSON.parse result
      cont info if cont
    catch
      #passing

summon = (id, cont) ->
  console.log "summon #{id}"
  curl "#{URL}summon?monster=#{id}&token=#{token}", (result) ->
    console.log "result: #{result}"
    cont (result is "OK")

# monsters scores info
reload = (cont) ->
  exec "curl https://game.coderunner.jp/scorelist", (_, out) ->
    ls = out.split('\r\n').slice 0, -1
    ms = ls.map (x) -> x | 0
    cont ms

ok = (m_id) ->
  ls1 = it[m_id].required
  ls2 = info.stone
  for x, i in ls1
    if x > ls2[i]
      return false
  console.log "ok"
  console.log "req: #{ls1}"
  console.log "pos: #{ls2}"
  return true

loop_summon = ->
  ami ->
    reload (ms) ->

      ms = ms.slice(0, N)
      ls = ms.map((score, idx) -> [idx, score])
        .sort (a,b) -> b[1] - a[1]

      rec = (idx) ->
        console.log "rec : #{idx}"
        if idx < ls.length
          if ok idx
            summon ls[idx][0], (b) ->
              if b
                setTimeout (-> rec idx), time_lag
              else
                setTimeout (-> rec idx + 1), time_lag
          else
            console.log "ng"
            rec idx + 1
        else
          do loop_summon

      rec 0

trade = (take, give, num) ->
  console.warn "#{URL}trade?token=#{token}&in=#{take}&out=#{give}&num=#{num}"
  curl "#{URL}trade?token=#{token}&in=#{take}&out=#{give}&num=#{num}", (out) ->
    console.warn out if out

loop_trade = ->
  base = 20
  cx = 24 - base + 1
  ami (o) ->
    ls = o.stone[base .. 24]
    mx = -1
    mi = -1
    for m, i in ls
      if mx < m
        mx = m
        mi = i
    console.log ls
    console.log "mx, mi = ", mx, mi
    rec = (idx) ->
      if idx >= ls.length or (idx > 10)
        return
      if idx isnt mi
        trade (base + idx), (base + mi), (mx / cx | 0)
      setTimeout rec, time_lag, (idx + 1)
    rec 0

seco_trade = ->
  list = []
  for i in [0 .. 16]
    for j in [0 .. 16]
      if i isnt j
        list.push [i, j]

  rec = (idx) ->
    if idx < list.length
      [t, g] = list[idx]
      trade t, g, 0
      setTimeout rec, time_lag, idx + 1
  rec 0

# ---- main

switch process.argv[2]
  when "-s"
    do loop_summon
  when "-t"
    do loop_trade
  when "-T"
    do seco_trade

ui.coffee

fs = require 'fs'
{exec} = require 'child_process'
ms = require './scores.json'
monsters = require './it.json'

curl = (url, cont) ->
  exec "curl \'#{url}\' |nkf", (err, out) ->
    throw err if err
    cont out if cont

URL = "https://game.coderunner.jp/"
token = 'CDR2YBS99ZXWOQSQUERT71KL18ORJV6K'

ls = ms.map (s, idx) -> [idx, s]
  .sort (a, b) -> b[1] - a[1]
  .slice(0, 10)
  .map (o) -> o[0]

console.warn ms.slice(0,3)
console.warn ls.slice(0,3)

console.log "<div style='padding-top:60px;'>"
console.log "<table>"
for m, i in ms
  console.log "<tr>"
  console.log "<td style='width:60px;'>#{i}-th</td>"
  if i in ls
    console.log "<td style='width:120px'><font color=red>#{m}</font></td>"
  else
    console.log "<td style='width:120px'>#{m}</td>"
  for k in monsters[i].required[17 .. 24]
  #for k in monsters[i].required
    console.log "<td style='width:50px'>#{k}</td>"
  console.log "</tr>"
console.log "</table>"
console.log "</div>"

ami = ->
  curl "#{URL}info.json?token=#{token}", (result) ->
    info = JSON.parse result
    console.log "<div style='position:fixed;left:0px;top:0px;background-color:white'>"
    console.log "<table>"
    console.log "<tr>"
    console.log "<td style='width:180px'>poss</td>"
    for m, i in info.stone[17 .. 24]
    #for m, i in info.stone
      console.log "<td style='width:50px'>#{m}</td>"
    console.log "</tr>"
    console.log "</table>"
    console.log "</div>"

do ami

it.json は、ゲーム開始時のモンスターの情報

https://gist.github.com/cympfh/7aae1d6eaee1d00a86bd

広告を非表示にする