최종 :
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import commands
import sys
import time
import os
import thread
from time import localtime, strftime
from datetime import datetime, timedelta
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# global veriable definition
blockrange = [['B', [-2697555,-2169923,-5915996,-14585436,-11887922,-1641489,-13526322,-3809298,-15716030,-9192722,-15716022,-14590580,-14588532,-11360546,-14589564,-14589556,-4334618,-13532739,-14058059,-15122086,-15121053,-11296042,-14055747,-13532747,-13531459,-10245410,-13533515,-525313,-14582091,-9194794,-14057803,-5913939,-7027994,-13531715,-11888946,-14057035,-11887914,-8601882,-15713974,-15123118,-15713966,-14592653,-15117965,-15124142,-16242374,-13529395,-12413226,-13527346,-15717318,-15717054,-15717310,-15718342,-15717062,-16242630,-15718086,-10244386,-13527338,-13529394,-14054715,-13528370,-13530427,-13529403,-13003050,-13002026,-13002018,-13001002,-12937514,-12936490]],
['G', [-2168907,-526370,-527410,-9204216,-9205240,-7029240,-7034319,-9204208,-6507999,-1642564,-13550328,-9205232,-10256880,-3811701,-10258168,-9731568,-15130624,-9730544,-14605304,-10258160,-5914071,-10783480,-529524,-9796600,-13551352,-10850040,-4335765,-14075640,-10851056,-10259184,-10259192,-10849016,-4339110,-3811477,-7557607,-1051698,-2168907,-2167892,-4341158,-3745949,-1646716,-4338069,-14076672,-9201136,-8676848,-7033319,-8151536,-8675824,-9203184,-7558639,-12957432,-8677880,-12958464,-13548280,-8678896,-6506951,-9203192,-7031247,-13023992,-12958456,-7556600,-6503919,-13549304,-14077688,-7557599,-14077696,-4335773,-13552376,-14076664,-5979615,-3219580,-4861101,-12431096,-12432120,-12432112,-8084976,-6503920,-6504952,-8083952,-8082928,-7573026,-7559664,-7558640,-7557616,-7556592,-7032304,-7031288,-7031280,-7030264,-7030256,-6504944]],
['P', [-3836475,-4888131,-5413451,-1649681,-532555,-2713898,-3835443,-529442,-9220749,-6005340,-8105588,-12441278,-8698493,-12441534,-8106620,-4360771,-3241514,-1123602,-9222789,-9748109,-9222797,-2185506,-2181402,-1656090,-3832371,-2181426,-5411403,-529417,-4885059,-5937747,-4887115,-2711850,-4886091,-11915958,-11916206,-11915182,-11390638,-1126674,-1125650,-4888139,-11915950,-3307050,-530441,-11916214,-8105596,-8171133,-8172156,-8697469,-8697468,-8696444,-8696445,-8697469,-13557702,-1653010,-12442558,-13032390,-2183458,-3832114,-13557702,-4361795,-3242282,-4359483,-3307058,-3240490,-2710818,-13033414,-12966854,-12967878,-3836219,-12967870,-12966846,-3240234,-1058833,-4359739,-3242290,-3766578,-2713890,-3835451,-4887107,-4362819,-4362811,-4361787,-4360763,-4360507,-3835442,-3835186,-3834162,-3833138,-3243314,-3241258,-2715946,-2714922,-2714914]],
['R', [-1144180,-1671556,-1136212,-1673604,-1150597,-535635,-7210,-1671813,-542019,-3255974,-1152653,-1147269,-8637135,-1669525,-8178374,-8177351,-540995,-6012598,-6534838,-11394007,-6539966,-5416614,-2726557,-6538942,-1669492,-6537918,-1672845,-6536886,-6536894,-1676941,-2206374,-2203285,-2201237,-1073507,-544124,-2200981,-5430223,-1079676,-545108,-2200213,-2729629,-2727581,-2728605,-2725525,-3254950,-3844773,-1674885,-3845806,-3845798,-3845798,-2729638,-3845798,-1074539,-1675925,-10867407,-10868439,-1675917,-10868695,-2203293,-10869719,-11919327,-1148549,-11920351,-12443615,-12444639,-10278863,-10277839,-1080965,-1671557,-1672837,-1673613,-1672837,-1676949,-2735790,-2206373,-2205341,-2204317,-2202261,-1680029,-1680021,-1679005,-1678997,-1677973,-1674893,-1673869,-1149581,-1149573,-1147525,-1146501,-1081989]],
['Y', [-3830256,-12438264,-3114,-3238392,-2188784,-1057925,-11913984,-6524664,-1658303,-1651110,-535949,-6523640,-6524656,-532621,-11912952,-2190832,-6525688,-2187759,-1652191,-1124708,-2707952,-2708942,-532861,-2186727,-3238384,-2180592,-1651068,-3304944,-2180600,-5414648,-3239408,-532563,-536973,-3237360,-1657278,-2184679,-2182640,-11915000,-531581,-1129877,-10861304,-529467,-10863352,-10862328,-1127813,-1656279,-1658352,-1651934,-11913976,-12438272,-12439296,-1654255,-12439288,-12439288,-1655280,-2187760,-2186736,-2185712,-2184688,-2183664,-2181616,-1660400,-1659376,-1657328,-1656304,-1654264,-1654256,-1653232]],
['S', [-10276542,-6013630,-9744012,-7585470,-50862,-46749,-4364883,-45717,-44693]],
['F', [-15133680,-11913927,-11912903,-11386558,-14608359,-15133671,-11912902,-14607327,-11910846,-12438214,-11911870]],
['A', [-8625664,-10794744,-7046648,-11914992,-6521592,-7636728,-8689400,-4351480,-11914224,-4350456,-10732024,-11255544,-12965624,-12442352,-12441592,-9153024,-8105703,-5413599]]]
gamepan = [['',72,590,[1,8,7]],['',170,535,[2,8,0]],['',265,590,[3,10,9,8,1]],['',360,535,[4,10,2]],['',457,590,[5,12,11,10,3]],['',552,535,[6,12,4]],['',650,590,[13,12,5]],
['',72,700,[0,8,15,14]],['',170,645,[2,9,15,7,0,1]],['',265,700,[10,17,16,15,8,2]],['',360,645,[4,11,17,9,2,3]],['',457,700,[12,19,18,17,10,4]],['',552,645,[6,13,19,11,4,5]],['',650,700,[20,19,12,6]],
['',72,810,[7,15,22,21]],['',170,755,[9,16,22,14,7,8]],['',265,810,[17,24,23,22,15,9]],['',360,755,[11,18,24,16,9,10]],['',457,810,[19,26,25,24,17,11]],['',552,755,[13,20,26,18,11,12]],['',650,810,[27,26,19,13]],
['',72,920,[14,22,29,28]],['',170,865,[16,23,29,21,14,15]],['',265,920,[24,31,30,29,22,16]],['',360,865,[18,25,31,23,16,17]],['',457,920,[26,33,32,31,24,18]],['',552,865,[20,27,33,25,18,19]],['',650,920,[34,33,26,20]],
['',72,1030,[21,29,36,35]],['',170,975,[23,30,36,28,21,22]],['',265,1030,[31,38,37,36,29,23]],['',360,975,[25,32,38,30,23,24]],['',457,1030,[33,40,39,38,31,25]],['',552,975,[27,34,40,32,25,26]],['',650,1030,[41,40,33,27]],
['',72,1140,[28,36]],['',170,1085,[35,28,29,30,37]],['',265,1140,[36,30,38]],['',360,1085,[37,30,31,32,39]],['',457,1140,[38,32,40]],['',552,1085,[39,32,33,34,41]],['',650,1140,[40,34]]]
try:
# connection to the current device, and return a MonkeyDevice object
print "Please connect your device to your PC!!"
device = MonkeyRunner.waitForConnection()
def ChkAppState():
AppInstall = device.shell('pm path com.nhncorp.SKPOCO')
if AppInstall.startswith('package:'):
print "POKOPANG is already installed."
else:
print "You should have install POKOPANG from google play."
print "Process will be terminated."
sys.exit(0)
ChkAppLaunched = device.shell('ps')
if ChkAppLaunched.find('com.nhncorp.SKPOCO'):
print "POKOPANG is already launched!!"
print "Kill exist process of POKOPANG!!"
device.shell("am force-stop com.nhncorp.SKPOCO")
MonkeyRunner.sleep(2)
print "Launching POKOPANG..."
device.startActivity(component='com.nhncorp.SKPOCO/com.treenod.android.UnityPlayerActivity')
# Launching Pokopang.. in my device!!
# device.touch(270, 600, MonkeyDevice.DOWN_AND_UP)
# MonkeyRunner.sleep(1)
# device.touch(440, 780, MonkeyDevice.DOWN_AND_UP)
# MonkeyRunner.sleep(20)
def TSS():
try:
while True:
os.system('adb shell /system/bin/screencap -p /sdcard/tempscshot.png')
os.system('adb pull /sdcard/tempscshot.png tempscshot.png')
#ResultImage = device.takeSnapshot()
ResultImage = MonkeyRunner.loadImageFromFile("tempscshot.png")
if ResultImage:
return ResultImage
break
else:
pass
except Exception, e:
print e.message, e.duration
print "Error occur in takeSnapshot"
sys.exit(-1)
def IsNumber(s):
try:
float(s)
return True
except ValueError:
return False
def RtAddTime(sec):
tempdt = datetime.now() + timedelta(seconds=sec)
return tempdt.strftime("%Y%m%d.%H%M%S")
def GetTimeFromNTP(ntpserver):
nowtime = device.shell('rdate -sp ' + ntpserver)
date_object = datetime.strptime(str(nowtime), "%a %b %d %I:%M:%S %Y")
date_object = date_object + timedelta(seconds=32400)
return date_object.strftime("%Y%m%d.%H%M%S")
def SetDeviceTime(dtstr):
f = open('tempdate.dat', 'w')
f.write(dtstr)
f.close()
os.system('adb push tempdate.dat /sdcard/tempdate.dat')
retval = device.shell("su -c 'sh ./sdcard/myshell.sh'")
print str(retval)
print "SetDeviceTime to " + str(dtstr)
def ReChargeClover():
# 277,237 ==> -7021766(Green clover) -9222896(No clover)
cnt = 1
while True:
if TSS().getRawPixelInt(277, 237) == -7021766:
print "Recharging complete!!"
break
print "Now Time : " + datetime.now().strftime("%Y%m%d.%H%M%S")
SetDeviceTime(RtAddTime(360*cnt))
print "After add(360sec) time : " + datetime.now().strftime("%Y%m%d.%H%M%S")
cnt += 1
MonkeyRunner.sleep(1)
def ChkCell(id, idx, bidx, dragpaths, dragedpaths):
childcells = gamepan[idx][3]
#print "ID[" + str(idx) + "] childcell : " + str(childcells)
dragpaths[id].append(idx)
for childcell in childcells:
if childcell in dragpaths[id]:
continue
if gamepan[idx][0] == gamepan[childcell][0]:
ChkCell(id, childcell, idx, dragpaths, dragedpaths)
if id == idx:
if len(dragpaths[id]) >= 3:
dragedpaths.append(dragpaths[id])
dragpaths[id] = []
dragpaths[id].append(idx)
else:
dragpaths[id] = []
dragpaths[id].append(idx)
def PlayGame():
while True:
print "Rescan!!"
RstImg = TSS()
dragpaths = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
dragedpaths = []
sumofelement = []
for idx in range(0,len(gamepan)):
gamepan[idx][0] = RstImg.getRawPixelInt(gamepan[idx][1], gamepan[idx][2])
#print "IDX[" + str(idx) + "]:" + str(gamepan[idx][0])
#print gamepan
for idx in range(0,len(gamepan)):
for idx2 in range(0,len(blockrange)):
if gamepan[idx][0] in blockrange[idx2][1]:
gamepan[idx][0] = blockrange[idx2][0]
notfoundcnt = 0
for idx in range(0,len(gamepan)):
if IsNumber(gamepan[idx][0]):
print "IDX[" + str(idx) + "]:" + str(gamepan[idx][0])
#RstImg.getSubImage((gamepan[idx][1]-30,gamepan[idx][2]-30,60,60)).writeToFile(strftime("%Y%M%d_%H%M", localtime()) + "_" + str(idx) + str(gamepan[idx][0]) + ".png")
notfoundcnt += 1
#print gamepan
if notfoundcnt >= 35:
print "Not recognized more then 35 blocks!! maybe the game is over!!"
break
for idx in range(0, len(gamepan)):
thread.start_new_thread(ChkCell,(idx, idx, -1, dragpaths, dragedpaths))
#print "dragedpaths : " + str(dragedpaths)
for idx in range(0, len(dragedpaths)):
if sum(dragedpaths[idx]) in sumofelement:
print "maybe this element(" + str(dragedpaths[idx]) + ") is already dragged so we will skip it!!"
continue
else:
sumofelement.append(sum(dragedpaths[idx]))
device.touch(gamepan[dragedpaths[idx][0]][1], gamepan[dragedpaths[idx][0]][2], MonkeyDevice.DOWN)
MonkeyRunner.sleep(0.04)
print "\nDrag Path : ",
for node in dragedpaths[idx]:
print str(node),
device.touch(gamepan[node][1], gamepan[node][2], MonkeyDevice.MOVE)
MonkeyRunner.sleep(0.04)
device.touch(gamepan[node][1], gamepan[node][2], MonkeyDevice.UP)
MonkeyRunner.sleep(0.04)
print " "
for idx in range(0, len(gamepan)):
device.touch(gamepan[idx][1], gamepan[idx][2], MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(0.03)
def main():
# starting script
print "POKOPANG starting..."
"""
device.touch(185, 1075, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
"""
"""
print TSS().getRawPixelInt(660, 420)
print device.takeSnapshot().getRawPixelInt(355, 1240)
sys.exit(0)
"""
# Check the state of POKOPANG
ChkAppState()
chk_time = 0
while True:
if TSS().getRawPixelInt(60, 240) == -4892416:
print "Launching is complete!!"
MonkeyRunner.sleep(1)
skipnotice = False
break
else:
print "Waiting for Launch..."
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(60, 240) == -4327573:
chk_time = chk_time + 1
if chk_time >= 3:
skipnotice = True
break
if skipnotice == False:
# Check Notice popup and close it!!
print "Check daily notice!! and close it all!"
while True:
if TSS().getRawPixelInt(60, 240) == -4892416:
# Notice
# -4892416
# 600, 250
device.touch(600, 250, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
Chk_Time = 0
else:
if Chk_Time <= 3:
Chk_Time = Chk_Time + 1
MonkeyRunner.sleep(0.5)
else:
#MonkeyRunner.sleep(1)
break
# Start of main cycle
while True:
print "Start of main cycle!!"
while True:
TSS_IMG = TSS()
#getRawPixelInt(277, 237) == -10592064:
#if TSS_IMG.getRawPixelInt(60, 240) == -4327573 and TSS_IMG.getRawPixelInt(670, 1130) == -4892416:
if TSS_IMG.getRawPixelInt(277, 237) == -7021766 and TSS_IMG.getRawPixelInt(670, 1130) == -4892416:
print "It is ok to play game!!"
# To go to play game
# 350, 1070
break
else:
device.touch(355, 1240, MonkeyDevice.DOWN_AND_UP)
print "Wait a moment and Rechanging clover!!"
if TSS_IMG.getRawPixelInt(670, 1130) == -4892416:
ReChargeClover()
SetDeviceTime(GetTimeFromNTP('time.bora.net'))
# Wait For time
MonkeyRunner.sleep(10)
device.touch(350, 1070, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
device.touch(350, 1070, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(2)
Chk_Time = 0
while True:
if TSS().getRawPixelInt(660, 420) == -4892416:
print "Help notice is showed!!"
device.touch(195, 960, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
else:
if Chk_Time <= 3:
Chk_Time = Chk_Time + 1
MonkeyRunner.sleep(0.5)
else:
break
# -56246
while True:
#if TSS().getRawPixelInt(30, 45) == -56246:
#if TSS().getRawPixelInt(660, 420) != -4892416:
#if TSS().getRawPixelInt(678, 1100) not in (-16777024, -4892416, -7590623, -16736410):
if TSS().getRawPixelInt(27, 1144) not in (-9225984, -7058168, -15135744, -7584504, -7585536, -14609400, -4360919, -14083056):
print "Game is playing!!"
PlayGame()
MonkeyRunner.sleep(1)
else:
print "Game over!!"
Chk_Time = 0
while True:
if TSS().getRawPixelInt(27, 1144) != -9225984:
if TSS().getRawPixelInt(27, 1144) == -14609400:
print "Close monster info!"
device.touch(350, 880, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(27, 1144) == -7058168:
print "Close monster list!"
device.touch(630, 230, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(27, 1144) == -15135744:
print "Close mission info!"
device.touch(360, 800, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(27, 1144) == -7584504:
if TSS().getRawPixelInt(150, 630) == -3240407:
print "Close item store!"
device.touch(630, 330, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(27, 1144) == -7585536:
print "Close upgrade shop!"
device.touch(630, 330, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(27, 1144) == -14083056:
if TSS().getRawPixelInt(150, 630) == -10249728:
print "Close send clover info!"
device.touch(360, 800, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(150, 630) == -10249728:
print "Close send clover info!"
device.touch(360, 800, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(150, 630) == -14053715:
print "Close today mission window!"
device.touch(360, 800, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(150, 630) == -7060224:
print "Close profile!"
device.touch(355, 915, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
if TSS().getRawPixelInt(27, 1144) == -4360919:
break
else:
if Chk_Time <= 3:
Chk_Time = Chk_Time + 1
MonkeyRunner.sleep(0.5)
else:
break
MonkeyRunner.sleep(1)
break
# 40, 945 ==> -10859455 ==> 645, 970
while TSS().getRawPixelInt(660, 420) == -4892416:
# Add Time Notice or High score notice!!
device.touch(195, 960, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
while True:
if TSS().getRawPixelInt(40, 945) == -10859455:
print "Notice pop up!!"
device.touch(665, 947, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
else:
print "Notice is not exists!!"
break
# 40, 945 ==> -4892416 ==> 185, 1075
while True:
if TSS().getRawPixelInt(40, 945) == -4892416:
print "Game score pop up!!"
device.touch(185, 1075, MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
else:
print "Game score popup is closed!!"
MonkeyRunner.sleep(1)
break
print "End of main cycle.."
print "Restarting..."
if __name__ == "__main__":
main()
finally:
device.shell("am force-stop com.android.commands.monkey")
2013-11-13
older_version
'Study > python' 카테고리의 다른 글
[펌]Python code coverage for Lib/ctypes/wintypes.py (0) | 2014.10.12 |
---|---|
shutil copyfileobj () (0) | 2014.09.03 |
[펌]python thread 사용예 (0) | 2013.10.16 |
[연습]wxpython rss reader (0) | 2013.10.06 |
wxpython webview test (0) | 2013.10.05 |