def newTopic(request,fid): form = Form() fields = request.getForm() user = users.get_current_user() uid = user.user_id() # sanitize title = utils.toStr(fields.get('title',''),80) imp = utils.toInt(fields.get('importance','0')) author = utils.toStr(fields.get('author',''),40) email = fields.get('email') url = fields.get('url') content= utils.toTxt(fields.get('content',''),9000) # validate ok=True warn=[] if email or url: form.ok=False form.redirect=True form.url='%s/%s'%(app.root,fid) return form if not title: warn.append('Title can not be empty') ok=False if title==title.upper(): warn.append('Title can not be all caps') ok=False if not author: warn.append('You need to identify as the author') ok=False if author==author.upper(): warn.append('Author can not be all caps') ok=False if not content: warn.append('You must enter some content') ok=False if content==content.upper(): warn.append('Content can not be all caps') ok=False if ok: dat1 = {'forumid':fid,'title':title,'userid':str(uid),'author':author,'importance':imp} rec1 = models.newTopic(dat1) tid = rec1.topicid dat2 = {'topicid':tid,'forumid':fid,'userid':str(uid),'author':author,'content':content} rec2 = models.newMessage(dat2) form.ok = True form.url = '%s/%s/%s'%(app.root,fid,tid) else: form.ok = False form.warn = warn form.data = { 'forum' :models.getForum(fid), 'warn' :warn, 'title' :title, 'author' :author, 'content':content } return form
def newForum(request): form = Form() fields = request.getForm() # sanitize title = utils.toStr(fields.get('title', ''), 80) desc = utils.toStr(fields.get('desc', ''), 120) url = utils.idify(fields.get('url', ''), 40) order = utils.toInt(fields.get('order')) # validate ok = True warn = [] if not url: warn.append('Forum needs a permalink') ok = False if not title: warn.append('Title can not be empty') ok = False if title == title.upper(): warn.append('Title can not be all caps') ok = False if not desc: warn.append('You must enter some description') ok = False if desc == desc.upper(): warn.append('Description can not be all caps') ok = False if ok: if not order: order = models.getNextOrder() data = { 'url': url, 'title': title, 'description': desc, 'order': order } rec = models.newForum(data) form.ok = True form.url = '%s/admin/forum' % (app.root) form.redirect = True else: form.ok = False form.warn = warn form.data = { 'forum': models.getForum(fid), 'warn': warn, 'title': title, 'author': author, 'content': content } return form
def cannon_move(self, b): m = self.blocks_map lb = {} rb = {} ub = {} db = {} left = range(b['x'] - 1, -1, -1) right = range(b['x'] + 1, 5) up = range(b['y'] + 1, 6) down = range(b['y'] - 1, -1, -1) for f in left: first = m.get(toStr(f, b['y']), None) if first and first['type'] != 0: for s in range(first['x'] - 1, -1, -1): second = m.get(toStr(s, b['y']), None) if second and second['type'] != 0: lb = second break else: continue break for f in right: first = m.get(toStr(f, b['y']), None) if first and first['type'] != 0: for s in range(first['x'] + 1, 5): second = m.get(toStr(s, b['y']), None) if second and second['type'] != 0: rb = second break else: continue break for f in up: first = m.get(toStr(b['x'], f), None) if first and first['type'] != 0: for s in range(first['y'] + 1, 5): second = m.get(toStr(b['x'], s), None) if second and second['type'] != 0: ub = second break else: continue break for f in down: first = m.get(toStr(b['x'], f), None) if first and first['type'] != 0: for s in range(first['y'] - 1, -1, -1): second = m.get(toStr(b['x'], s), None) if second and second['type'] != 0: db = second break else: continue break if lb or rb or ub or db: return {'left': lb, 'right': rb, 'up': ub, 'down': db} else: return False
def changeManifoldStatus(self, result, sensors): db = conf.db.conn newValue = 0b0 pos = 0 for sensor in sensors: for p in conf.HeatingSensors.mapSensorsToManifold.get(sensor): if result[pos] != 0: newValue |= 1 << p pos += 1 # format binnary to string, cut 0b and reverse newValue = format(newValue, '#011b')[2:][::-1] oldValue = utils.toStr(db.get("heating_manifold_state")) if oldValue != newValue: self.log.info("Changing manifold at <%s> to: %s" % (conf.HeatingSensors.manifoldIp, newValue)) data = self.sendReq(conf.HeatingSensors.manifoldIp, "/" + newValue) data = json.loads(data) newValue = data.get("v") db.set("heating_manifold_state", newValue) else: self.log.debug("Manifold at <%s> is still: %s" % (conf.HeatingSensors.manifoldIp, newValue))
def newForum(request): form = Form() fields = request.getForm() # sanitize title = utils.toStr(fields.get('title',''), 80) desc = utils.toStr(fields.get('desc' ,''),120) url = utils.idify(fields.get('url' ,''), 40) order = utils.toInt(fields.get('order')) # validate ok=True warn=[] if not url: warn.append('Forum needs a permalink') ok=False if not title: warn.append('Title can not be empty') ok=False if title==title.upper(): warn.append('Title can not be all caps') ok=False if not desc: warn.append('You must enter some description') ok=False if desc==desc.upper(): warn.append('Description can not be all caps') ok=False if ok: if not order: order=models.getNextOrder() data = {'url':url,'title':title,'description':desc,'order':order} rec = models.newForum(data) form.ok = True form.url = '%s/admin/forum'%(app.root) form.redirect = True else: form.ok = False form.warn = warn form.data = { 'forum' :models.getForum(fid), 'warn' :warn, 'title' :title, 'author' :author, 'content':content } return form
def messages(request, fid, tid): form = Form() fields = request.getForm() user = users.get_current_user() uid = user.user_id() # sanitize author = utils.toStr(fields.get('author', 'Anonymous'), 40) email = fields.get('email') url = fields.get('url') content = utils.toTxt(fields.get('content', ''), 9000) # validate ok = True warn = [] if email or url or 'http' in content: form.ok = False form.redirect = True form.url = '%s/%s/%s' % (app.root, fid, tid) return form if not author: warn.append('You need to identify yourself as the author') ok = False if author == author.upper(): warn.append('Author can not be all caps') ok = False if not content: warn.append('You must enter some content') ok = False if content == content.upper(): warn.append('Content can not be all caps') ok = False # process if ok: form.ok = True form.url = '%s/%s/%s' % (app.root, fid, tid) data = { 'topicid': tid, 'forumid': fid, 'userid': str(uid), 'author': author, 'content': content } models.newMessage(data) else: form.ok = False form.warn = warn form.data = { 'forum': models.getForum(fid), 'topic': models.getTopic(tid), 'list': models.getMessages(tid), 'warn': warn, 'author': author, 'content': content } return form
def myprofile(request): form = Form() fields = request.getForm() user = users.get_current_user() uid = user.user_id() # sanitize name = utils.toStr(fields.get('username', ''), 80) nick = utils.toStr(fields.get('nickname', ''), 40) email = utils.toStr(fields.get('email', ''), 80) token = utils.toStr(fields.get('token', ''), 80) userid = str(uid) if not name: name = 'Anonymous' # validate ok = True warn = [] if userid != token: warn.append('You need to login to change your profile') ok = False # TODO: check for duplicate nick if not nick: warn.append('Nickname can not be empty') ok = False if ok: data = { 'userid': uid, 'nickname': nick, 'username': name, 'email': email } rec = models.saveUser(data) form.ok = True form.url = '%s/myprofile' % (app.root) form.redirect = True else: form.ok = False form.warn = warn form.data = {'profile': models.getUser(uid), 'warn': warn} return form #---- END ----
def myprofile(request): form = Form() fields = request.getForm() user = users.get_current_user() uid = user.user_id() # sanitize name = utils.toStr(fields.get('username',''),80) nick = utils.toStr(fields.get('nickname',''),40) email = utils.toStr(fields.get('email' ,''),80) token = utils.toStr(fields.get('token' ,''),80) userid= str(uid) if not name: name = 'Anonymous' # validate ok=True warn=[] if userid != token : warn.append('You need to login to change your profile') ok=False # TODO: check for duplicate nick if not nick: warn.append('Nickname can not be empty') ok=False if ok: data = {'userid':uid,'nickname':nick,'username':name,'email':email} rec = models.saveUser(data) form.ok = True form.url = '%s/myprofile'%(app.root) form.redirect = True else: form.ok = False form.warn = warn form.data = { 'profile':models.getUser(uid), 'warn' :warn } return form #---- END ----
def messages(request,fid,tid): form = Form() fields = request.getForm() user = users.get_current_user() uid = user.user_id() # sanitize author = utils.toStr(fields.get('author','Anonymous'),40) email = fields.get('email') url = fields.get('url') content= utils.toTxt(fields.get('content',''),9000) # validate ok=True warn=[] if email or url or 'http' in content: form.ok=False form.redirect=True form.url='%s/%s/%s'%(app.root,fid,tid) return form if not author: warn.append('You need to identify yourself as the author') ok=False if author==author.upper(): warn.append('Author can not be all caps') ok=False if not content: warn.append('You must enter some content') ok=False if content==content.upper(): warn.append('Content can not be all caps') ok=False # process if ok: form.ok = True form.url = '%s/%s/%s'%(app.root,fid,tid) data = {'topicid':tid,'forumid':fid,'userid':str(uid),'author':author,'content':content} models.newMessage(data) else: form.ok = False form.warn = warn form.data = { 'forum' :models.getForum(fid), 'topic' :models.getTopic(tid), 'list' :models.getMessages(tid), 'warn' :warn, 'author' :author, 'content':content } return form
def make_map(self): for b in self.blocks: cood = toStr(b['x'], b['y']) if b['piece']: self.blocks_map[cood] = b['piece'] else: side = 2 if self.side == 1 else 1 empty_piece = { 'hasFlip': True, 'type': 0, 'side': side, 'x': b['x'], 'y': b['y'] } self.blocks_map[cood] = empty_piece
def main(): f = open("data.txt", "r") text = f.read() large = len(text) newLarge = max_bits(large) key = [] data = [] for idx, item in enumerate(text): data.append(item) value = randint(0, 20) key.append(value) data.append(addString(value)) print(desencriptar(toComma(key), toStr(data)))
def checkTemperature(self): db = conf.db.conn data = dict() result = list() sensors = list() for item in db.keys("temp_sensor_*"): item = utils.toStr(item) #self.log.info("Item <%s>" % (item)) sensor = pickle.loads(db.get(item)) data[item] = sensor roomId = conf.HeatingSensors.items[sensor["sensorId"]] room = pickle.loads(db.get("heating_" + roomId)) reqTemperature = room.get("temperature") # if a single room temperature - hysteresis is lower # than requested temperature call set on if self.__heatingCounter > conf.Daemon.Interval: if sensor[ 'temperature'] > reqTemperature + conf.Heating.hysteresis: self.log.debug( "Sensor: [%s] %.2fC > %.2fC = OK" % (sensor.get("sensorId"), sensor.get("temperature"), reqTemperature + conf.Heating.hysteresis)) result.append(0) else: self.log.info( "Sensor: [%s] %.2fC < %.2fC = LOW" % (sensor.get("sensorId"), sensor.get("temperature"), reqTemperature + conf.Heating.hysteresis)) result.append(1) sensors.append(int(sensor.get("sensorId"))) """ This if reduce requests to the switch hardware to one per x second Because permanently check of all actions is every 1s Persistent counter is saved into the db. """ if self.__heatingCounter > conf.Daemon.Interval: # first delete heting counter db.set("__heatingCounter", 0) self.changeManifoldStatus(result, sensors) if sum(result) > 0: self.changeHeatingState(1) else: self.changeHeatingState(0)
def _getGroupList(self): ''' 获取原始的群信息 ''' _groupList = {} url = 'http://s.web2.qq.com/api/get_group_name_list_mask2' data = { 'r': json.dumps({ 'vfwebqq': utils.toStr(self.vfwebqq), 'hash': self._genHashCode(), }), } #不加referer会返回100101,没有hash会返回50 r = self.session.post(url, data=data, headers=REFERER_HEADER) content = json.loads(r.content) #{"retcode":0,"result":{"gmasklist":[],"gnamelist":[{"flag":16778241,"name":"测试专用group-顺手牵羊","gid":2619045161,"code":3009117424},{"flag":16778241,"name":"测试专用group-无懈可击","gid":157462148,"code":3564680573}],"gmarklist":[]}} ]]]} logging.info("获取原始的群信息为: %s", r.content) if content["retcode"] == 0: for ginfo in content["result"]["gnamelist"]: # 要通过gcode来获取QQ号码 _groupList[ginfo["code"]] = ginfo logging.info("加载群信息成功...") return _groupList else: logging.error("加载群信息失败! 错误码: %s", content["retcode"])
def newPoll(request): form = Form() fields = request.getForm() # sanitize title = utils.toStr(fields.get('title',''),120) image = utils.toStr(fields.get('image',''),250) isopen = fields.get('isopen',False) single = fields.get('single',False) block = fields.get('block' ,False) close = fields.get('close' ,False) closeon = utils.toDtm(fields.get('closeon',None)) type = 0 if single else 1 restrict = 1 if block else 0 status = 1 if isopen else 0 closeon = closeon if close else None slots = utils.toInt(fields.get('slots',0)) # poll poll={ 'title' : title, 'image' : image, 'closeon' : closeon, 'type' : type, 'restrict': restrict, 'status' : status } # options pos = 0 options=[] for i in range(1,slots+1): option = fields.get('option'+str(i),'') if option: pos+=1 options.append({'title':option,'position':pos}) # validate warn=[] form.ok=True if not title: form.ok=False warn.append('Title is required') if pos<2: form.ok=False warn.append('At least two options are required') # process if form.ok: models.registerPoll(poll,options) form.url = app.root+'/admin' else: poll['slots'] = pos poll['isopen'] = 'checked' if isopen else '' poll['single'] = 'checked' if single else '' poll['block'] = 'checked' if block else '' poll['close'] = 'checked' if close else '' if pos<5: poll['slots']=5 for i in range(pos,5): options.append({'title':'','position':i+1}) form.data={'warn':warn,'poll':poll,'options':options} return form
def editPoll(request,pollid): form = Form() fields = request.getForm() # sanitize title = utils.toStr(fields.get('title',''),120) image = utils.toStr(fields.get('image',''),250) isopen = fields.get('isopen',False) single = fields.get('single',False) block = fields.get('block' ,False) close = fields.get('close' ,False) closeon = utils.toDtm(fields.get('closeon',None)) type = 0 if single else 1 restrict = 1 if block else 0 status = 1 if isopen else 0 closeon = closeon if close else None slots = utils.toInt(fields.get('slots',0)) # poll poll={ 'pollid' : pollid, 'title' : title, 'image' : image, 'closeon' : closeon, 'type' : type, 'restrict': restrict, 'status' : status } # options pos = 0 options=[] for i in range(1,slots+1): option = fields.get('option'+str(i),'') optionid = fields.get('optionid'+str(i),'') # if has title but not optionid: insert option # if has title and has optionid: modify option # if not title but has optionid: delete option if option and optionid: pos+=1 options.append({'optionid':optionid,'title':option,'position':pos}) elif option and not optionid: pos+=1 options.append({'optionid':'','title':option,'position':pos}) else: options.append({'optionid':optionid,'title':'','position':0}) # validate warn=[] form.ok=True if not title: form.ok=False warn.append('Title is required') if pos<2: form.ok=False warn.append('At least two options are required') # process if form.ok: models.savePoll(poll,options) form.url = app.root+'/admin/viewpoll/'+pollid else: poll['slots'] = pos poll['isopen'] = 'checked' if isopen else '' poll['single'] = 'checked' if single else '' poll['block'] = 'checked' if block else '' poll['close'] = 'checked' if close else '' #remove deleted options clean=[] for item in options: if item['position']>0: clean.append(item) if pos<5: poll['slots']=5 for i in range(pos,5): clean.append({'optionid':'','title':'','position':i+1}) form.data={'warn':warn,'poll':poll,'options':clean} return form
def get_move_score(self): big_score = 0 big_block = {} ai_kill_block = {} current_round = self.blocks_map move_map = {} to_map = {} for b in self.ai_flipped: right_one = self.blocks_map.get(toStr(b['x'] + 1, b['y']), None) right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y']), None) right_three = self.blocks_map.get(toStr(b['x'] + 3, b['y']), None) right_up_two = self.blocks_map.get(toStr(b['x'] + 1, b['y'] + 2), None) right_down_two = self.blocks_map.get(toStr(b['x'] + 1, b['y'] - 2), None) left_one = self.blocks_map.get(toStr(b['x'] - 1, b['y']), None) left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y']), None) left_three = self.blocks_map.get(toStr(b['x'] - 3, b['y']), None) left_up_two = self.blocks_map.get(toStr(b['x'] - 1, b['y'] + 2), None) left_down_two = self.blocks_map.get(toStr(b['x'] - 1, b['y'] - 2), None) up_one = self.blocks_map.get(toStr(b['x'], b['y'] + 1), None) up_two = self.blocks_map.get(toStr(b['x'], b['y'] + 2), None) up_three = self.blocks_map.get(toStr(b['x'], b['y'] + 3), None) down_one = self.blocks_map.get(toStr(b['x'], b['y'] - 1), None) down_two = self.blocks_map.get(toStr(b['x'], b['y'] - 2), None) down_three = self.blocks_map.get(toStr(b['x'], b['y'] - 3), None) up_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] + 1), None) up_right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y'] + 1), None) up_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] + 1), None) up_left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y'] + 1), None) down_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] - 1), None) down_right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y'] - 1), None) down_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] - 1), None) down_left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y'] - 1), None) # can kill, not cannon init_score = -9999 if right_one and right_one['hasFlip'] and self.side != right_one[ 'side'] and b['type'] != 2 and vs(b, right_one): score = POINTS[right_one['type']] if (right_two and right_two['hasFlip'] and self.side != right_two['side'] and vs(b, right_two)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(b, up_right)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(b, down_right)): if vs(b, right_two): score = score + POINTS[right_two['type']] / 10 if vs(b, up_right): score = score + POINTS[up_right['type']] / 10 if vs(b, down_right): score = score + POINTS[down_right['type']] / 10 if [vs(b, right_two), vs(b, up_right), vs(b, down_right)].count(True) > 1: vs1 = POINTS[right_two['type']] if vs(b, right_two) else 0 vs2 = POINTS[up_right['type']] if vs(b, up_right) else 0 vs3 = POINTS[down_right['type']] if vs( b, down_right) else 0 score = score + min([vs1, vs2, vs3]) # kill and then be killed, killed by cannon or others if (right_two and right_two['hasFlip'] and self.side != right_two['side'] and vs(right_two, b)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(up_right, b)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(down_right, b)) or\ (right_three and right_three['hasFlip'] and self.side != right_three['side'] and right_three['type'] == 2) or\ (right_up_two and right_up_two['hasFlip'] and self.side != right_up_two['side'] and right_up_two['type'] == 2) or\ (right_down_two and right_down_two['hasFlip'] and self.side != right_down_two['side'] and right_down_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = right_one elif left_one and left_one['hasFlip'] and self.side != left_one[ 'side'] and b['type'] != 2 and vs(b, left_one): score = POINTS[left_one['type']] if (left_two and left_two['hasFlip'] and self.side != left_two['side'] and vs(b, left_two)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(b, up_left)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(b, down_left)): if vs(b, left_two): score = score + POINTS[left_two['type']] / 10 if vs(b, up_left): score = score + POINTS[up_left['type']] / 10 if vs(b, down_left): score = score + POINTS[down_left['type']] / 10 if [vs(b, left_two), vs(b, up_left), vs(b, down_left)].count(True) > 1: vs1 = POINTS[left_two['type']] if vs(b, left_two) else 0 vs2 = POINTS[up_left['type']] if vs(b, up_left) else 0 vs3 = POINTS[down_left['type']] if vs(b, down_left) else 0 score = score + min([vs1, vs2, vs3]) if (left_two and left_two['hasFlip'] and self.side != left_two['side'] and vs(left_two, b)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(up_left, b)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(down_left, b)) or\ (left_three and left_three['hasFlip'] and self.side != left_three['side'] and left_three['type'] == 2) or\ (left_up_two and left_up_two['hasFlip'] and self.side != left_up_two['side'] and left_up_two['type'] == 2) or\ (left_down_two and left_down_two['hasFlip'] and self.side != left_down_two['side'] and left_down_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = left_one elif up_one and up_one['hasFlip'] and self.side != up_one[ 'side'] and b['type'] != 2 and vs(b, up_one): score = POINTS[up_one['type']] if (up_two and up_two['hasFlip'] and self.side != up_two['side'] and vs(b, up_two)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(b, up_left)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(b, up_right)): if vs(b, up_two): score = score + POINTS[up_two['type']] / 10 if vs(b, up_left): score = score + POINTS[up_left['type']] / 10 if vs(b, up_right): score = score + POINTS[up_right['type']] / 10 if [vs(b, up_two), vs(b, up_left), vs(b, up_right)].count(True) > 1: vs1 = POINTS[up_two['type']] if vs(b, up_two) else 0 vs2 = POINTS[up_left['type']] if vs(b, up_left) else 0 vs3 = POINTS[up_right['type']] if vs(b, up_right) else 0 score = score + min([vs1, vs2, vs3]) if (up_two and up_two['hasFlip'] and self.side != up_two['side'] and vs(up_two, b)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(up_left, b)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(up_right, b)) or\ (up_three and up_three['hasFlip'] and self.side != up_three['side'] and up_three['type'] == 2) or\ (up_right_two and up_right_two['hasFlip'] and self.side != up_right_two['side'] and up_right_two['type'] == 2) or\ (up_left_two and up_left_two['hasFlip'] and self.side != up_left_two['side'] and up_left_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = up_one elif down_one and down_one['hasFlip'] and self.side != down_one[ 'side'] and b['type'] != 2 and vs(b, down_one): score = POINTS[down_one['type']] if (down_two and down_two['hasFlip'] and self.side != down_two['side'] and vs(b, down_two)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(b, down_right)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(b, down_left)): if vs(b, down_two): score = score + POINTS[down_two['type']] / 10 if vs(b, down_right): score = score + POINTS[down_right['type']] / 10 if vs(b, down_left): score = score + POINTS[down_left['type']] / 10 if [vs(b, down_two), vs(b, down_right), vs(b, down_left)].count(True) > 1: vs1 = POINTS[down_two['type']] if vs(b, down_two) else 0 vs2 = POINTS[down_right['type']] if vs( b, down_right) else 0 vs3 = POINTS[down_left['type']] if vs(b, down_left) else 0 score = score + min([vs1, vs2, vs3]) if (down_two and down_two['hasFlip'] and self.side != down_two['side'] and vs(down_two, b)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(down_right, b)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(down_left, b)) or\ (down_three and down_three['hasFlip'] and self.side != down_three['side'] and down_three['type'] == 2) or\ (down_right_two and down_right_two['hasFlip'] and self.side != down_right_two['side'] and down_right_two['type'] == 2) or\ (down_left_two and down_left_two['hasFlip'] and self.side != down_left_two['side'] and down_left_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = down_one # can kill, cannon elif b['type'] == 2: if self.cannon_move(b): ck = self.cannon_move(b) for cb in ck: if ck[cb]: vb = ck[cb] cannon_right_one = self.blocks_map.get( toStr(vb['x'] + 1, vb['y']), None) cannon_right_two = self.blocks_map.get( toStr(vb['x'] + 2, vb['y']), None) cannon_left_one = self.blocks_map.get( toStr(vb['x'] - 1, vb['y']), None) cannon_left_two = self.blocks_map.get( toStr(vb['x'] - 2, vb['y']), None) cannon_up_one = self.blocks_map.get( toStr(vb['x'], vb['y'] + 1), None) cannon_up_two = self.blocks_map.get( toStr(vb['x'], vb['y'] + 2), None) cannon_down_one = self.blocks_map.get( toStr(vb['x'], vb['y'] - 1), None) cannon_down_two = self.blocks_map.get( toStr(vb['x'], vb['y'] - 2), None) if vb['hasFlip'] and self.side != vb['side']: score = POINTS[vb['type']] if not vb['hasFlip']: score = -(self.expect_score) * 3 vs1 = 0 vs2 = 0 vs3 = 0 vs4 = 0 if cannon_right_two and cannon_right_two[ 'hasFlip'] and self.side != cannon_right_two[ 'side']: vs1 = POINTS[cannon_right_two['type']] / 10 if cannon_left_two and cannon_left_two[ 'hasFlip'] and self.side != cannon_left_two[ 'side']: vs2 = POINTS[cannon_left_two['type']] / 10 if cannon_up_two and cannon_up_two[ 'hasFlip'] and self.side != cannon_up_two[ 'side']: vs3 = POINTS[cannon_up_two['type']] / 10 if cannon_down_two and cannon_down_two[ 'hasFlip'] and self.side != cannon_down_two[ 'side']: vs3 = POINTS[cannon_down_two['type']] / 10 if [vs1, vs2, vs3, vs4].count(0) == 3: score = score + vs1 + vs2 + vs3 + vs4 if [vs1, vs2, vs3, vs4].count(0) < 3: l = [vs1, vs2, vs3, vs4] l.remove(0) score = score + min(l) if (cannon_right_one and cannon_right_one['hasFlip'] and self.side != cannon_right_one['side'] and vs(cannon_right_one, b)) or\ (cannon_left_one and cannon_left_one['hasFlip'] and self.side != cannon_left_one['side'] and vs(cannon_left_one, b)) or\ (cannon_up_one and cannon_up_one['hasFlip'] and self.side != cannon_up_one['side'] and vs(cannon_up_one, b)) or\ (cannon_down_one and cannon_down_one['hasFlip'] and self.side != cannon_down_one['side'] and vs(cannon_down_one, b)) or\ (cannon_right_two and cannon_right_two['hasFlip'] and self.side != cannon_right_two['side'] and cannon_right_two['type'] == 2) or\ (cannon_left_two and cannon_left_two['hasFlip'] and self.side != cannon_left_two['side'] and cannon_left_two['type'] == 2) or\ (cannon_up_two and cannon_up_two['hasFlip'] and self.side != cannon_up_two['side'] and cannon_up_two['type'] == 2) or\ (cannon_down_two and cannon_down_two['hasFlip'] and self.side != cannon_down_two['side'] and cannon_down_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = vb else: # TODO continue score = 0 move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = None if move_map: max_block_key = getMax(move_map) big_block = self.blocks_map.get(max_block_key) big_score = max(move_map.values()) ai_kill_block = to_map.get(max_block_key) detail = {} for xy in move_map: detail[xy] = {'score': move_map[xy], 'to': to_map[xy]} return { 'action': 'move', 'score': big_score, 'block': ai_kill_block, 'from': big_block, 'detail': detail } else: return { 'action': 'move', 'score': None, 'block': None, 'from': None, 'detail': None }
def prepareMsgContent(msg): ''' 消息内容的格式转换 ''' msg = [utils.toStr(msg).replace('"', '\\"'), ["font", MSG_FONT]] return json.dumps(msg, ensure_ascii=False, encoding="utf8")
def newTopic(request, fid): form = Form() fields = request.getForm() user = users.get_current_user() uid = user.user_id() # sanitize title = utils.toStr(fields.get('title', ''), 80) imp = utils.toInt(fields.get('importance', '0')) author = utils.toStr(fields.get('author', ''), 40) email = fields.get('email') url = fields.get('url') content = utils.toTxt(fields.get('content', ''), 9000) # validate ok = True warn = [] if email or url: form.ok = False form.redirect = True form.url = '%s/%s' % (app.root, fid) return form if not title: warn.append('Title can not be empty') ok = False if title == title.upper(): warn.append('Title can not be all caps') ok = False if not author: warn.append('You need to identify as the author') ok = False if author == author.upper(): warn.append('Author can not be all caps') ok = False if not content: warn.append('You must enter some content') ok = False if content == content.upper(): warn.append('Content can not be all caps') ok = False if ok: dat1 = { 'forumid': fid, 'title': title, 'userid': str(uid), 'author': author, 'importance': imp } rec1 = models.newTopic(dat1) tid = rec1.topicid dat2 = { 'topicid': tid, 'forumid': fid, 'userid': str(uid), 'author': author, 'content': content } rec2 = models.newMessage(dat2) form.ok = True form.url = '%s/%s/%s' % (app.root, fid, tid) else: form.ok = False form.warn = warn form.data = { 'forum': models.getForum(fid), 'warn': warn, 'title': title, 'author': author, 'content': content } return form
def get_keep_score(self): big_score = 0 big_block = {} current_round = self.blocks_map keep_map = {} for b in self.ai_flipped: right_one = self.blocks_map.get(toStr(b['x'] + 1, b['y']), None) right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y']), None) left_one = self.blocks_map.get(toStr(b['x'] - 1, b['y']), None) left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y']), None) up_one = self.blocks_map.get(toStr(b['x'], b['y'] + 1), None) up_two = self.blocks_map.get(toStr(b['x'], b['y'] + 2), None) down_one = self.blocks_map.get(toStr(b['x'], b['y'] - 1), None) down_two = self.blocks_map.get(toStr(b['x'], b['y'] - 2), None) up_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] + 1), None) up_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] + 1), None) down_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] - 1), None) down_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] - 1), None) # keep, protect ai if right_one and right_one['hasFlip'] and self.side == right_one[ 'side']: if right_two and right_two['hasFlip'] and self.side != right_two['side'] or\ up_right and up_right['hasFlip'] and self.side != up_right['side'] or\ down_right and down_right['hasFlip'] and self.side != down_right['side']: if vs(right_two, right_one) or vs( up_right, right_one) or vs(down_right, right_one): score = 0 if vs(b, right_two): score = POINTS[right_two['type']] - POINTS[ right_one['type']] if vs(b, up_right): score = POINTS[up_right['type']] - POINTS[ right_one['type']] if vs(b, down_right): score = POINTS[down_right['type']] - POINTS[ right_one['type']] if [ vs(b, right_two), vs(b, up_right), vs(b, down_right) ].count(True) > 1: vs1 = POINTS[right_two['type']] if vs( b, right_two) else 0 vs2 = POINTS[up_right['type']] if vs( b, up_right) else 0 vs3 = POINTS[down_right['type']] if vs( b, down_right) else 0 score = score + min([vs1, vs2, vs3]) if score > big_score: big_score = score big_block = b keep_map[toStr(b['x'], b['y'])] = {'score': score} if left_one and left_one['hasFlip'] and self.side == left_one[ 'side']: if left_two and left_two['hasFlip'] and self.side != left_two['side'] or\ up_left and up_left['hasFlip'] and self.side != up_left['side'] or\ down_left and down_left['hasFlip'] and self.side != down_left['side']: if vs(left_two, left_one) or vs(up_left, left_one) or vs( down_left, left_one): score = 0 if vs(b, left_two): score = POINTS[left_two['type']] - POINTS[ left_one['type']] if vs(b, up_left): score = POINTS[up_left['type']] - POINTS[ left_one['type']] if vs(b, down_left): score = POINTS[down_left['type']] - POINTS[ left_one['type']] if [vs(b, left_two), vs(b, up_left), vs(b, down_left)].count(True) > 1: vs1 = POINTS[left_two['type']] if vs( b, right_two) else 0 vs2 = POINTS[up_left['type']] if vs(b, up_left) else 0 vs3 = POINTS[down_left['type']] if vs( b, down_left) else 0 score = score + min([vs1, vs2, vs3]) if score > big_score: big_score = score big_block = b keep_map[toStr(b['x'], b['y'])] = {'score': score} if up_one and up_one['hasFlip'] and self.side == up_one['side']: if up_two and up_two['hasFlip'] and self.side != up_two['side'] or\ up_right and up_right['hasFlip'] and self.side != up_right['side'] or\ up_left and up_left['hasFlip'] and self.side != up_left['side']: if vs(up_two, up_one) or vs(up_right, up_one) or vs( up_left, up_one): score = 0 if vs(b, up_two): score = POINTS[up_two['type']] - POINTS[ up_one['type']] if vs(b, up_right): score = POINTS[up_right['type']] - POINTS[ up_one['type']] if vs(b, up_left): score = POINTS[up_left['type']] - POINTS[ up_one['type']] if [vs(b, up_two), vs(b, up_right), vs(b, up_left)].count(True) > 1: vs1 = POINTS[up_two['type']] if vs(b, up_two) else 0 vs2 = POINTS[up_right['type']] if vs( b, up_right) else 0 vs3 = POINTS[up_left['type']] if vs(b, up_left) else 0 score = score + min([vs1, vs2, vs3]) if score > big_score: big_score = score big_block = b keep_map[toStr(b['x'], b['y'])] = {'score': score} if down_one and down_one['hasFlip'] and self.side == down_one[ 'side']: if down_two and down_two['hasFlip'] and self.side != down_two['side'] or\ down_right and down_right['hasFlip'] and self.side != down_right['side'] or\ down_left and down_left['hasFlip'] and self.side != down_left['side']: if vs(down_two, down_one) or vs( down_right, down_one) or vs(down_left, down_one): score = 0 if vs(b, down_two): score = POINTS[down_two['type']] - POINTS[ down_one['type']] if vs(b, down_right): score = POINTS[down_right['type']] - POINTS[ down_one['type']] if vs(b, down_left): score = POINTS[down_left['type']] - POINTS[ down_one['type']] if [ vs(b, down_two), vs(b, down_right), vs(b, down_left) ].count(True) > 1: vs1 = POINTS[down_two['type']] if vs( b, down_two) else 0 vs2 = POINTS[down_right['type']] if vs( b, down_right) else 0 vs3 = POINTS[down_left['type']] if vs( b, down_left) else 0 score = score + min([vs1, vs2, vs3]) if score > big_score: big_score = score big_block = b keep_map[toStr(b['x'], b['y'])] = {'score': score} # keep, will be killed if (right_one and self.side != right_one['side'] and vs(right_one, b)) \ or (left_one and self.side != left_one['side'] and vs(left_one, b)) \ or (up_one and self.side != up_one['side'] and vs(up_one, b)) \ or (down_one and self.side != down_one['side'] and vs(down_one, b)) \ or (right_two and self.side != right_two['side'] and right_two['type'] == 2) \ or (left_two and self.side != left_two['side'] and left_two['type'] == 2) \ or (up_two and self.side != up_two['side'] and up_two['type'] == 2) \ or (down_two and self.side != down_two['side'] and down_two['type'] == 2): score = -POINTS[b['type']] if score > big_score: big_score = score big_block = b keep_map[toStr(b['x'], b['y'])] = {'score': score} return { 'action': 'keep', 'score': big_score, 'block': big_block, 'from': None, 'detail': keep_map }
def get_block(self, x, y): return self.blocks_map.get(toStr(x, y), None)
def get_flip_score(self): cannon_len = sum(b['type'] == 2 for b in self.ai_unflipped) big_score = 0 big_block = {} current_round = self.blocks_map unflipped = self.ai_unflipped + self.player_unflipped unflipped_map = {} for b in unflipped: right_one = self.blocks_map.get(toStr(b['x'] + 1, b['y']), None) right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y']), None) left_one = self.blocks_map.get(toStr(b['x'] - 1, b['y']), None) left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y']), None) up_one = self.blocks_map.get(toStr(b['x'], b['y'] + 1), None) up_two = self.blocks_map.get(toStr(b['x'], b['y'] + 2), None) down_one = self.blocks_map.get(toStr(b['x'], b['y'] - 1), None) down_two = self.blocks_map.get(toStr(b['x'], b['y'] - 2), None) up_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] + 1), None) up_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] + 1), None) down_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] - 1), None) down_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] - 1), None) # flip, if cannons in unfliped blocks if cannon_len > 0: init_score = -9999 if right_two and right_two[ 'hasFlip'] and self.side != right_two[ 'side'] and right_two['type'] != 0: if right_one and not right_one['hasFlip']: score = POINTS[right_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif left_two and left_two['hasFlip'] and self.side != left_two[ 'side'] and left_two['type'] != 0: if left_one and not left_one['hasFlip']: score = POINTS[left_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_two and up_two['hasFlip'] and self.side != up_two[ 'side'] and up_two['type'] != 0: if up_one and not up_one['hasFlip']: score = POINTS[up_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_two and down_two['hasFlip'] and self.side != down_two[ 'side'] and down_two['type'] != 0: if down_one and not down_one['hasFlip']: score = POINTS[down_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score else: score = self.expect_score / 50 unflipped_map[toStr(b['x'], b['y'])] = score # flip, if no cannon in unfliped blocks else: init_score = -9999 if right_one and right_one[ 'hasFlip'] and self.side != right_one['side']: score = (self.expect_score - POINTS[right_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif left_one and left_one[ 'hasFlip'] and self.side != left_one['side']: score = (self.expect_score - POINTS[left_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_one and up_one[ 'hasFlip'] and self.side != up_one['side']: score = (self.expect_score - POINTS[up_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_one and down_one[ 'hasFlip'] and self.side != down_one['side']: score = (self.expect_score - POINTS[down_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_right and up_right[ 'hasFlip'] and self.side != up_right['side']: score = (self.expect_score - POINTS[up_right['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_left and up_left[ 'hasFlip'] and self.side != up_left['side']: score = (self.expect_score - POINTS[up_left['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_right and down_right[ 'hasFlip'] and self.side != down_right['side']: score = (self.expect_score - POINTS[down_right['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_left and down_left[ 'hasFlip'] and self.side != down_left['side']: score = (self.expect_score - POINTS[down_left['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score else: score = self.expect_score / 50 unflipped_map[toStr(b['x'], b['y'])] = score if unflipped_map: max_block_key = getMax(unflipped_map) big_block = self.blocks_map.get(max_block_key) big_score = max(unflipped_map.values()) detail = {} for xy in unflipped_map: detail[xy] = {'score': unflipped_map[xy]} return { 'action': 'flip', 'score': big_score, 'block': big_block, 'from': None, 'detail': detail } else: return { 'action': 'flip', 'score': None, 'big_block': None, 'from': None, 'detail': None }