Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
support ios
Browse files Browse the repository at this point in the history
  • Loading branch information
faceair committed Dec 31, 2017
1 parent 32eb53b commit 7000b67
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 208 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jump*.png
debugger
youjumpijump-*
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@

## 下载地址

Android [下载地址](https://github.com/faceair/youjumpijump/releases/latest)
请下载 `youjumpijump` 单个文件,不要下载 `Source code`,Windows 用户可以尝试下载 Windows.zip 这个一键运行包(感谢群友 @  ♨Deloz.$ヽ. 和 @MonFig 支持)。
Android [下载地址](https://github.com/faceair/youjumpijump/releases/latest) 请下载 `youjumpijump-android` 单个文件,不要下载 `Source code`,Windows 用户可以尝试下载 Windows.zip 这个一键运行包(感谢群友 @  ♨Deloz.$ヽ. 和 @MonFig 支持)。

iOS [下载地址](https://github.com/faceair/youjumpijump/releases/latest) 下载 `youjumpijump-ios` 单个文件即可。

## 使用须知

Android 设备

1. Android 手机一台,电脑上安装 ADB,连接上电脑后开启 USB 调试模式
2. 进入微信打开微信跳一跳,点击开始游戏
3. 将下载的文件 Push 到手机上 `adb push ./youjumpijump /data/local/tmp/ && adb shell`
4. 跑起来 `cd /data/local/tmp/ && chmod 775 ./youjumpijump || true && ./youjumpijump`
3. 将下载的文件 Push 到手机上 `adb push ./youjumpijump-android /data/local/tmp/ && adb shell`
4. 跑起来 `cd /data/local/tmp/ && chmod 775 ./youjumpijump-android || true && ./youjumpijump-android`

iOS 设备

1. 需要在 Mac 上安装配置 WebDriverAgent,参考[教程](https://testerhome.com/topics/7220)
2. 一切配置弄好后运行 `chmod 775 ./youjumpijump-ios || true && ./youjumpijump-ios`

## 跳跃系数

Expand All @@ -36,7 +44,7 @@ Android [下载地址](https://github.com/faceair/youjumpijump/releases/latest)

3. 怎么编译 Android 版本?

`CGO_ENABLED=1 GOARCH=arm GOOS=linux go build .`
`CGO_ENABLED=0 GOARCH=arm GOOS=linux go build -o youjumpijump-android android/main.go`

4. 其他疑难杂症?

Expand Down
72 changes: 72 additions & 0 deletions android/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"
"image/png"
"log"
"math"
"os"
"os/exec"
"runtime/debug"
"strconv"
"time"

jump "github.com/faceair/youjumpijump"
)

func main() {
defer func() {
jump.Debugger()
if e := recover(); e != nil {
log.Printf("%s: %s", e, debug.Stack())
fmt.Print("the program has crashed, press any key to exit")
var c string
fmt.Scanln(&c)
}
}()

var ratio float64
fmt.Print("input jump ratio (recommend 2.04):")
_, err := fmt.Scanln(&ratio)
if err != nil {
log.Fatal(err)
}
log.Printf("now jump ratio is %f", ratio)

for {
jump.Debugger()

_, err := exec.Command("/system/bin/screencap", "-p", "jump.png").Output()
if err != nil {
panic(err)
}
infile, err := os.Open("jump.png")
if err != nil {
panic(err)
}
src, err := png.Decode(infile)
if err != nil {
panic(err)
}

start, end := jump.Find(src)
if start == nil {
log.Print("can't find the starting point,please export the debugger directory")
break
} else if end == nil {
log.Print("can't find the end point,please export the debugger directory")
break
}

ms := int(math.Pow(math.Pow(float64(start[0]-end[0]), 2)+math.Pow(float64(start[1]-end[1]), 2), 0.5) * ratio)
log.Printf("from:%v to:%v press:%vms", start, end, ms)

_, err = exec.Command("/system/bin/sh", "/system/bin/input", "swipe", "320", "410", "320", "410", strconv.Itoa(ms)).Output()
if err != nil {
panic(err)
}

infile.Close()
time.Sleep(time.Millisecond * 1500)
}
}
103 changes: 103 additions & 0 deletions ios/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"image/png"
"log"
"math"
"os"
"runtime/debug"
"time"

jump "github.com/faceair/youjumpijump"
)

type ScreenshotRes struct {
Value string `json:"value"`
SessionID string `json:"sessionId"`
Status int `json:"status"`
}

func main() {
defer func() {
jump.Debugger()
if e := recover(); e != nil {
log.Printf("%s: %s", e, debug.Stack())
fmt.Print("程序已崩溃,按任意键退出")
var c string
fmt.Scanln(&c)
}
}()

var ip string
fmt.Print("请输入 WebDriverAgentRunner 监听的 IP 和端口 (例如 192.168.9.94:8100):")
_, err := fmt.Scanln(&ip)
if err != nil {
log.Fatal(err)
}

var ratio float64
fmt.Print("请输入跳跃系数(推荐值 2,可适当调整):")
_, err = fmt.Scanln(&ratio)
if err != nil {
log.Fatal(err)
}
log.Printf("现在跳跃系数是 %f", ratio)

r := jump.NewRequest()

for {
jump.Debugger()

_, body, err := r.Get(fmt.Sprintf("http://%s/screenshot", ip))
if err != nil {
panic(err)
}

scr := new(ScreenshotRes)
err = json.Unmarshal(body, scr)
if err != nil {
panic(err)
}

pngValue, err := base64.StdEncoding.DecodeString(scr.Value)
if err != nil {
panic(err)
}

src, err := png.Decode(bytes.NewReader(pngValue))
if err != nil {
panic(err)
}

f, _ := os.OpenFile("jump.png", os.O_WRONLY|os.O_CREATE, 0600)
png.Encode(f, src)
f.Close()

start, end := jump.Find(src)
if start == nil {
log.Print("找不到起点,请把 debugger 目录打包发给开发者检查问题。")
break
} else if end == nil {
log.Print("找不到落脚点,请把 debugger 目录打包发给开发者检查问题。")
break
}

ms := float64(math.Pow(math.Pow(float64(start[0]-end[0]), 2)+math.Pow(float64(start[1]-end[1]), 2), 0.5) * ratio)
log.Printf("from:%v to:%v press:%vms", start, end, ms)

_, _, err = r.PostJSON(fmt.Sprintf("http://%s/session/%s/wda/touchAndHold", ip, scr.SessionID), map[string]interface{}{
"x": 200,
"y": 200,
"duration": ms / 1000,
})
if err != nil {
panic(err)
}

time.Sleep(time.Millisecond * 1500)
}
}
155 changes: 155 additions & 0 deletions jump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package jump

import (
"image"
"image/color"
"image/png"
"io"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"strconv"
"time"

"github.com/nfnt/resize"
)

var basePath string

func init() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
basePath = filepath.Dir(ex)

if ok, _ := Exists(basePath + "/debugger"); !ok {
os.MkdirAll(basePath+"/debugger", os.ModePerm)
}

os.Remove(basePath + "/debugger/debug.log")
logFile, _ := os.OpenFile(basePath+"/debugger/debug.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
}

func Debugger() {
if ok, _ := Exists(basePath + "/jump.png"); ok {
os.Rename(basePath+"/jump.png", basePath+"/debugger/"+strconv.Itoa(TimeStamp())+".png")

files, err := ioutil.ReadDir(basePath + "/debugger/")
if err != nil {
panic(err)
}

for _, f := range files {
fname := f.Name()
ext := filepath.Ext(fname)
name := fname[0 : len(fname)-len(ext)]
if ts, err := strconv.Atoi(name); err == nil {
if TimeStamp()-ts > 10 {
os.Remove(basePath + "/debugger/" + fname)
}
}
}
}
}

func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}

func TimeStamp() int {
return int(time.Now().UnixNano() / int64(time.Second))
}

func getRGB(m color.Model, c color.Color) [3]int {
if m == color.RGBAModel {
return [3]int{int(c.(color.RGBA).R), int(c.(color.RGBA).G), int(c.(color.RGBA).B)}
} else if m == color.RGBA64Model {
return [3]int{int(c.(color.RGBA64).R), int(c.(color.RGBA64).G), int(c.(color.RGBA64).B)}
} else if m == color.NRGBAModel {
return [3]int{int(c.(color.NRGBA).R), int(c.(color.NRGBA).G), int(c.(color.NRGBA).B)}
} else if m == color.NRGBA64Model {
return [3]int{int(c.(color.NRGBA64).R), int(c.(color.NRGBA64).G), int(c.(color.NRGBA64).B)}
}
return [3]int{0, 0, 0}
}

func colorSimilar(a, b [3]int, distance float64) bool {
return (math.Abs(float64(a[0]-b[0])) < distance) && (math.Abs(float64(a[1]-b[1])) < distance) && (math.Abs(float64(a[2]-b[2])) < distance)
}

func Find(src image.Image) ([]int, []int) {
src = resize.Resize(720, 0, src, resize.Lanczos3)
f, _ := os.OpenFile("jump.720.png", os.O_WRONLY|os.O_CREATE, 0600)
png.Encode(f, src)
f.Close()

bounds := src.Bounds()
w, h := bounds.Max.X, bounds.Max.Y

jumpCubeColor := [3]int{54, 52, 92}
points := [][]int{}
for y := 0; y < h; y++ {
line := 0
for x := 0; x < w; x++ {
c := src.At(x, y)
if colorSimilar(getRGB(src.ColorModel(), c), jumpCubeColor, 20) {
line++
} else {
if y > 300 && x-line > 10 && line > 30 {
points = append(points, []int{x - line/2, y, line})
}
line = 0
}
}
}
jumpCube := []int{0, 0, 0}
for _, point := range points {
if point[2] > jumpCube[2] {
jumpCube = point
}
}
jumpCube = []int{jumpCube[0], jumpCube[1]}
if jumpCube[0] == 0 {
return nil, nil
}

possible := [][]int{}
for y := 0; y < h; y++ {
line := 0
bgColor := getRGB(src.ColorModel(), src.At(w-10, y))
for x := 0; x < w; x++ {
c := src.At(x, y)
if !colorSimilar(getRGB(src.ColorModel(), c), bgColor, 5) {
line++
} else {
if y > 300 && x-line > 10 && line > 35 && ((x-line/2) < (jumpCube[0]-20) || (x-line/2) > (jumpCube[0]+20)) {
possible = append(possible, []int{x - line/2, y, line, x})
}
line = 0
}
}
}
if len(possible) == 0 {
return jumpCube, nil
}
target := possible[0]
for _, point := range possible {
if point[3] > target[3] && point[1]-target[1] <= 5 {
target = point
}
}
target = []int{target[0], target[1]}

return jumpCube, target
}
Loading

0 comments on commit 7000b67

Please sign in to comment.