177 lines
6.5 KiB
C++
177 lines
6.5 KiB
C++
// PORT: VIP panel. The server's VIP cards (38001 7 days, 38002 30 days, sold by 百店老板娘) extend the seven
|
||
// original premium services of the account; this window shows what is left of each, asked with /mt_vip, and
|
||
// reports card use in the chat. The premium icons (affects 500..506) are the original ones.
|
||
// Protocol: metin2-server docs/FEATURE-VIP.md.
|
||
static const char c_szVipScript[] = R"PY(# -*- coding: utf-8 -*-
|
||
import app
|
||
import chat
|
||
import grp
|
||
import net
|
||
import ui
|
||
import game
|
||
import weakref
|
||
from mt_biolog import Action, Text, L
|
||
|
||
g_window=None
|
||
g_entry=None
|
||
SEND_GAP=1.
|
||
TIMEOUT=3.
|
||
MAX_DAYS=400
|
||
CARDS={38001:7,38002:30}
|
||
# PREMIUM_EXP .. PREMIUM_GOLD, the order of MTVIP1 times.
|
||
SERVICES=('经验加成','物品掉落翻倍','仓库扩展到 3 页','金币直接进背包','钓鱼成功率提升','结婚亲密度加快','金币掉落翻倍')
|
||
ERRORS={'rate':'操作过快,请稍后再试','invalid':'参数无效','full':'VIP 剩余时间不能超过 %d 天'%MAX_DAYS}
|
||
|
||
|
||
def Number(text,low=0,high=2**31-1):
|
||
if not text.isdigit() or len(text)>10:raise ValueError(text)
|
||
value=int(text)
|
||
if not low<=value<=high:raise ValueError(text)
|
||
return value
|
||
|
||
|
||
def Remaining(seconds):
|
||
seconds=int(seconds)
|
||
if seconds<=0:return '未开通'
|
||
days,rest=divmod(seconds,86400);hours,rest=divmod(rest,3600);minutes=rest//60
|
||
if days:return '剩余 %d 天 %d 小时'%(days,hours)
|
||
if hours:return '剩余 %d 小时 %d 分'%(hours,minutes)
|
||
return '剩余 %d 分'%max(1,minutes)
|
||
|
||
|
||
class Status(object):
|
||
# Protocol state only. send(text) posts a chat command, now() is seconds.
|
||
def __init__(self,send,now,listener=None):
|
||
self.send=send;self.now=now;self.listener=listener
|
||
self.waiting=None;self.nextSend=0;self.unsupported=False
|
||
self.times=None;self.at=0;self.error=None;self.added=None
|
||
|
||
def Query(self):
|
||
self.Tick()
|
||
now=self.now()
|
||
if self.waiting is not None or now<self.nextSend:return False
|
||
self.send('/mt_vip 1 status');self.waiting=now+TIMEOUT;self.nextSend=now+SEND_GAP
|
||
return True
|
||
|
||
def Tick(self):
|
||
if self.waiting is not None and self.now()>=self.waiting:
|
||
self.waiting=None;self.unsupported=True;self.Notify('unsupported')
|
||
|
||
def Left(self,index):
|
||
if self.times is None:return 0
|
||
return max(0,self.times[index]-(self.now()-self.at))
|
||
|
||
def Notify(self,kind):
|
||
if self.listener:self.listener(kind)
|
||
|
||
def Receive(self,line):
|
||
if not line.startswith('MTVIP1 '):return False
|
||
words=line.split()
|
||
try:
|
||
if words[1]=='times' and len(words)==2+len(SERVICES):
|
||
self.times=[Number(w) for w in words[2:]];self.at=self.now();self.error=None;kind='times'
|
||
elif words[1]=='added' and len(words)==4:
|
||
self.added=(Number(words[2]),Number(words[3],1,MAX_DAYS));kind='added'
|
||
elif words[1]=='error' and len(words)==3 and words[2] in ERRORS:self.error=words[2];kind='error'
|
||
else:return True
|
||
except ValueError:return True
|
||
if kind!='added':self.waiting=None
|
||
self.unsupported=False;self.Notify(kind)
|
||
return True
|
||
|
||
|
||
class VipWindow(ui.Window):
|
||
def __init__(self,status):
|
||
ui.Window.__init__(self);self.SetWindowName('VipWindow');self.AddFlag('float');self.AddFlag('movable')
|
||
self.status=status;self.SetSize(332,118+len(SERVICES)*24)
|
||
self.heading=Text(self,18,18,'VIP 会员',0xffffd68b)
|
||
self.closeButton=Action(self,256,6,60,'关闭',self.Hide)
|
||
self.names=[Text(self,18,64+i*24,name,0xffe8dec8) for i,name in enumerate(SERVICES)]
|
||
self.values=[Text(self,190,64+i*24,'',0xffc3b7a1) for i in range(len(SERVICES))]
|
||
self.note=Text(self,18,self.GetHeight()-44,'',0xffc3b7a1)
|
||
self.hint=Text(self,18,self.GetHeight()-22,'VIP 卡在各村百店老板娘处购买,使用后全部特权延长',0xff8f8676)
|
||
self.second=-1
|
||
self.Hide()
|
||
|
||
def Open(self):
|
||
self.status.Query();self.Refresh();self.SetCenterPosition();self.Show();self.SetTop()
|
||
|
||
def Toggle(self):
|
||
if self.IsShow():self.Hide()
|
||
else:self.Open()
|
||
|
||
def Refresh(self):
|
||
s=self.status
|
||
for i,value in enumerate(self.values):
|
||
left=s.Left(i)
|
||
value.SetText(L(Remaining(left) if s.times is not None else '……'))
|
||
value.SetPackedFontColor(0xff9fe08b if left>0 else 0xff8f8676)
|
||
if s.unsupported:note='!服务器尚未启用 VIP 卡'
|
||
elif s.error:note='!'+ERRORS[s.error]
|
||
elif s.times is None:note='正在查询……'
|
||
elif any(s.Left(i)>0 for i in range(len(SERVICES))):note='VIP 生效中'
|
||
else:note='尚未开通 VIP'
|
||
self.note.SetText(L(note))
|
||
|
||
def OnUpdate(self):
|
||
self.status.Tick()
|
||
second=int(app.GetTime())
|
||
if second!=self.second:self.second=second;self.Refresh()
|
||
|
||
def OnRender(self):
|
||
x,y=self.GetGlobalPosition();grp.SetColor(0xf51b1916);grp.RenderBar(x,y,self.GetWidth(),self.GetHeight());grp.SetColor(0xff9b7b48);grp.RenderBox(x,y,self.GetWidth(),self.GetHeight())
|
||
|
||
def OnPressEscapeKey(self):self.Hide();return True
|
||
|
||
def Destroy(self):
|
||
self.Hide();self.closeButton.event=None
|
||
|
||
|
||
class Entry(Action):
|
||
def __init__(self,window):
|
||
Action.__init__(self,None,0,0,64,'VIP',Toggle)
|
||
self.minimap=weakref.proxy(window.interface.wndMiniMap);self.SetWindowName('VipEntry');self.OnUpdate();self.Hide()
|
||
|
||
def OnUpdate(self):
|
||
x,y=self.minimap.GetGlobalPosition();self.SetPosition(max(4,x-68),y+408)
|
||
|
||
|
||
def OnStatus(kind):
|
||
s=g_status
|
||
if kind=='added':
|
||
vnum,days=s.added
|
||
chat.AppendChat(chat.CHAT_TYPE_INFO,L('VIP 会员已延长 %d 天,全部特权立即生效'%days))
|
||
elif kind=='error':chat.AppendChat(chat.CHAT_TYPE_INFO,L(ERRORS[s.error]))
|
||
if g_window:g_window.Refresh()
|
||
|
||
|
||
g_status=Status(net.SendChatPacket,app.GetTime,OnStatus)
|
||
|
||
|
||
def Toggle():
|
||
if g_window:g_window.Toggle()
|
||
|
||
|
||
_open=game.GameWindow.Open
|
||
_close=game.GameWindow.Close
|
||
_command=game.GameWindow.BINARY_ServerCommand_Run
|
||
|
||
def Open(self):
|
||
global g_window,g_entry
|
||
_open(self);g_status.waiting=None;g_status.times=None;g_window=VipWindow(g_status);g_entry=Entry(self)
|
||
|
||
def Close(self):
|
||
global g_window,g_entry
|
||
if g_entry:g_entry.Hide();g_entry.event=None;g_entry=None
|
||
if g_window:g_window.Destroy();g_window=None
|
||
_close(self)
|
||
|
||
def Command(self,line):
|
||
if g_status.Receive(line):return 1
|
||
return _command(self,line)
|
||
|
||
game.GameWindow.Open=Open
|
||
game.GameWindow.Close=Close
|
||
game.GameWindow.BINARY_ServerCommand_Run=Command
|
||
)PY";
|