Add attribute lock buttons to the switchbot window
Five per-slot toggles next to start/stop, shown only when the server sends the MTSB1 lock line; locked attributes are marked in the current attribute line. Server side: metin2-server docs/FEATURE-ATTR-LOCK.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ffe097ec57
commit
a22c064587
@@ -1,6 +1,7 @@
|
||||
// PORT: AVA2-style automatic attribute change (uiswitchbot.py), driven by the server's /mt_switchbot
|
||||
// command. Every roll runs on the server through the original change-attribute scroll path; this window
|
||||
// only picks the item, scroll and targets and shows progress. Protocol: metin2-server docs/FEATURE-SWITCHBOT.md.
|
||||
// only picks the item, scroll and targets and shows progress. The lock buttons toggle the server-side attribute
|
||||
// lock that every reroll keeps. Protocol: metin2-server docs/FEATURE-SWITCHBOT.md, FEATURE-ATTR-LOCK.md.
|
||||
static const char c_szSwitchbotScript[] = R"PY(# -*- coding: utf-8 -*-
|
||||
import app
|
||||
import chat
|
||||
@@ -21,6 +22,7 @@ g_entry=None
|
||||
USE_CHANGE_ATTRIBUTE=18 # CItemData::EUseSubTypes; the server re-checks the scroll subtype.
|
||||
LIMITS=(10,50,100,200,500,1000)
|
||||
MAX_TARGETS=5
|
||||
LOCK_GROUPS=2
|
||||
SEND_GAP=.3
|
||||
TIMEOUT=4.
|
||||
REASONS={'running':'洗练中','matched':'已获得目标属性','limit':'已达到次数上限','no_scroll':'洗练道具已用完',
|
||||
@@ -28,7 +30,8 @@ REASONS={'running':'洗练中','matched':'已获得目标属性','limit':'已达
|
||||
'stopped':'已手动停止','idle':'未运行'}
|
||||
ERRORS={'rate':'操作过快,请稍后','invalid':'参数无效','item':'物品不存在','equipped':'请先卸下装备或结束交易','no_attr':'该物品不能洗练',
|
||||
'scroll':'没有可用的洗练道具','target':'目标属性超出可洗范围','running':'已有洗练在进行','busy':'当前状态不能洗练',
|
||||
'unsupported':'服务器尚未启用自动洗练'}
|
||||
'unsupported':'服务器尚未启用自动洗练','slot':'该条属性不能锁定','max':'最多锁定 2 条,且至少留 1 条可洗',
|
||||
'db':'锁定保存失败,请稍后再试'}
|
||||
|
||||
|
||||
def Number(text,low=0,high=4294967295):
|
||||
@@ -43,7 +46,7 @@ class Bot(object):
|
||||
def __init__(self,send,now,listener=None):
|
||||
self.send=send;self.now=now;self.listener=listener
|
||||
self.queue=[];self.nextSend=0;self.waiting=None
|
||||
self.allowed={};self.loading=None;self.state=None;self.error=None;self.unsupported=False
|
||||
self.allowed={};self.locks={};self.loading=None;self.state=None;self.error=None;self.unsupported=False
|
||||
|
||||
def Enqueue(self,verb,text):
|
||||
if verb=='attrs' and self.waiting and self.waiting[2]==text:return
|
||||
@@ -67,6 +70,14 @@ class Bot(object):
|
||||
parts.append('%d %d'%(apply,value))
|
||||
self.error=None;self.Enqueue('start',' '.join(parts));return None
|
||||
|
||||
def Lock(self,cell,slot):
|
||||
if not 0<=slot<5 or cell not in self.locks:return
|
||||
self.error=None;self.Enqueue('lock','/mt_switchbot 1 lock %d %d'%(cell,slot))
|
||||
|
||||
def LockMask(self,cell,vnum):
|
||||
lock=self.locks.get(cell)
|
||||
return lock[1] if lock and lock[0]==vnum else None
|
||||
|
||||
def Stop(self):self.Enqueue('stop','/mt_switchbot 1 stop')
|
||||
def Status(self):self.Enqueue('status','/mt_switchbot 1 status')
|
||||
def Running(self):return bool(self.state and self.state['active'])
|
||||
@@ -106,6 +117,10 @@ class Bot(object):
|
||||
if not row or Number(words[1])!=row['cell'] or Number(words[2])!=row['count'] or len(row['rows'])!=row['count']:
|
||||
raise ValueError('partial')
|
||||
self.allowed[row['cell']]=row;self.Answered('attrs');self.Notify('attrs')
|
||||
elif kind=='lock':
|
||||
# Follows attrs_end (and answers lock). Servers without the lock never send it, so no buttons show.
|
||||
if len(words)!=4:raise ValueError('lock')
|
||||
self.locks[Number(words[1])]=(Number(words[2]),Number(words[3],0,31));self.Answered('lock');self.Notify('lock')
|
||||
elif kind=='state':
|
||||
if len(words)!=9 or words[8] not in REASONS:raise ValueError('state')
|
||||
self.state=dict(active=Number(words[1],0,1)==1,cell=Number(words[2]),vnum=Number(words[3]),rolls=Number(words[4]),
|
||||
@@ -155,13 +170,14 @@ class SwitchbotWindow(ui.Window):
|
||||
pick=Action(self,18,y,100,'',lambda i=i:self.Pick(i))))
|
||||
self.pickPrev=Action(self,18,0,80,'上一页',lambda:self.PickPage(-1));self.pickNext=Action(self,0,0,80,'下一页',lambda:self.PickPage(1))
|
||||
self.startButton=Action(self,18,0,120,'开始洗练',self.Start);self.stopButton=Action(self,146,0,120,'停止',self.bot.Stop)
|
||||
self.lockButtons=[Action(self,0,0,40,'',lambda i=i:self.ToggleLock(i)) for i in range(5)]
|
||||
self.status=Text(self,18,0,'',0xffc3b7a1);self.current=Text(self,18,0,'',0xffefdfc3)
|
||||
self.Reflow();self.Hide()
|
||||
|
||||
def Buttons(self):
|
||||
rows=[b for r in self.rows for b in (r['minus'],r['plus'],r['remove'],r['pick'])]
|
||||
return [self.closeButton,self.itemPrev,self.itemNext,self.scrollPrev,self.scrollNext,self.limitButton,self.addButton,
|
||||
self.pickPrev,self.pickNext,self.startButton,self.stopButton]+self.altButtons+rows
|
||||
self.pickPrev,self.pickNext,self.startButton,self.stopButton]+self.altButtons+self.lockButtons+rows
|
||||
|
||||
def Open(self):
|
||||
self.Reflow();self.Scan();self.SetCenterPosition();self.Show();self.SetTop();self.bot.Status();self.RequestAttrs()
|
||||
@@ -181,6 +197,7 @@ class SwitchbotWindow(ui.Window):
|
||||
# The server binds the bot to the item ID and reports its current cell; follow it after a move.
|
||||
if selected and selected[0] in self.bot.allowed and selected[1]==state['vnum']:
|
||||
self.bot.allowed[state['cell']]=dict(self.bot.allowed[selected[0]],cell=state['cell'])
|
||||
if selected[0] in self.bot.locks:self.bot.locks[state['cell']]=self.bot.locks[selected[0]]
|
||||
selected=(state['cell'],state['vnum'])
|
||||
self.items=items;self.scrolls=sorted(scrolls.items())
|
||||
self.itemIndex=next((i for i,v in enumerate(items) if v==selected),
|
||||
@@ -235,6 +252,14 @@ class SwitchbotWindow(ui.Window):
|
||||
target=self.alternatives[self.alt][index];maximum=self.Maximum(target[0])
|
||||
target[1]=max(1,min(maximum,target[1]+direction*Step(maximum)));self.Refresh()
|
||||
|
||||
def Locks(self):
|
||||
selected=self.Selected()
|
||||
return self.bot.LockMask(*selected) if selected else None
|
||||
|
||||
def ToggleLock(self,slot):
|
||||
selected=self.Selected()
|
||||
if selected and not self.bot.Running():self.bot.Lock(selected[0],slot);self.Refresh()
|
||||
|
||||
def Remove(self,index):del self.alternatives[self.alt][index];self.Refresh()
|
||||
|
||||
def Start(self):
|
||||
@@ -280,6 +305,11 @@ class SwitchbotWindow(ui.Window):
|
||||
for b in (self.pickPrev,self.pickNext):
|
||||
if self.picking and pages>1:b.Show()
|
||||
else:b.Hide()
|
||||
locks=self.Locks()
|
||||
for i,b in enumerate(self.lockButtons):
|
||||
filled=selected and player.GetItemAttribute(player.INVENTORY,selected[0],i)[0]
|
||||
if locks is None or self.picking or not filled:b.Hide();continue
|
||||
b.SetText(L(B(u'【锁%d】'%(i+1) if locks>>i&1 else u'锁%d'%(i+1))));b.SetEnabled(not running);b.Show()
|
||||
self.pickPrev.SetEnabled(self.pickPage>0);self.pickNext.SetEnabled(self.pickPage+1<pages)
|
||||
self.startButton.SetEnabled(not running and bool(selected) and any(self.alternatives));self.stopButton.SetEnabled(running)
|
||||
self.status.SetText(L(B(self.StatusText(allowed))));self.current.SetText(self.CurrentText(selected))
|
||||
@@ -290,14 +320,15 @@ class SwitchbotWindow(ui.Window):
|
||||
if self.Selected() and allowed is None:return u'正在读取可洗属性…'
|
||||
if state and (state['active'] or state['reason']!='idle'):
|
||||
return u'%s · %d / %d 次 · 剩余道具 %d'%(U(REASONS[state['reason']]),state['rolls'],state['limit'],state['scrolls'])
|
||||
if self.Locks():return u'已锁定的属性在每次洗练中保持不变'
|
||||
return u'任一方案的属性全部达标即停止;只看前 5 条普通属性'
|
||||
|
||||
def CurrentText(self,selected):
|
||||
if not selected:return ''
|
||||
parts=[]
|
||||
parts=[];locks=self.Locks() or 0
|
||||
for i in range(5):
|
||||
apply,value=player.GetItemAttribute(player.INVENTORY,selected[0],i)
|
||||
if apply:parts.append(AffectText(apply,value))
|
||||
if apply:parts.append((L('[锁]') if locks>>i&1 else '')+AffectText(apply,value))
|
||||
return ' / '.join(parts)
|
||||
|
||||
def Reflow(self):
|
||||
@@ -313,6 +344,8 @@ class SwitchbotWindow(ui.Window):
|
||||
bottom=height-104
|
||||
self.pickPrev.SetPosition(18,bottom);self.pickNext.SetPosition(width-98,bottom)
|
||||
self.startButton.SetPosition(18,bottom);self.stopButton.SetPosition(146,bottom)
|
||||
step=(width-292)//5
|
||||
for i,b in enumerate(self.lockButtons):b.SetPosition(274+i*step,bottom);b.SetSize(step-4,48);b.label.SetPosition((step-4)//2,24)
|
||||
self.status.SetPosition(18,height-46);self.current.SetPosition(18,height-22)
|
||||
self.SetCenterPosition();self.Refresh()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user