Using 38001 (7 days) or 38002 (30 days) moves every account.account *_expire column to max(now, end) + days, capped at 400 days left, with a 1 s gap per player. The db core only hands out the login-time premium times (CLoginData), so the game reads the row back after a card and on every enter-game and updates m_aiPremiumTimes and the 500..506 icons. /mt_vip 1 status reports the remaining seconds. docs/FEATURE-VIP.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
9.6 KiB
Python
138 lines
9.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Compile the production VIP card header against stubs of the account table, the ordered async SQL queue and
|
|
the premium affects."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
SRC=ROOT/'src/server/game/src'
|
|
def source(name):return (SRC/name).read_bytes().decode('latin1')
|
|
cmd=source('cmd.cpp')
|
|
assert 'ACMD(do_port_vip);' in cmd and '"mt_vip", do_port_vip, 0, POS_DEAD, GM_PLAYER' in cmd
|
|
general=source('cmd_general.cpp')
|
|
assert '#include "port_premium.h"' in general and 'PortVipCommand(ch, argument);' in general
|
|
item=source('char_item.cpp')
|
|
use=item.index('bool CHARACTER::UseItem(TItemPos Cell, TItemPos DestCell)')
|
|
hook=item.index('if (PortVipUseCard(this, item))',use)
|
|
assert '#endif\n#include "port_premium.h"\n' in item # not inside the __AUCTION__ block
|
|
assert item.index('FN_check_item_sex(this, item)',use)<hook<item.index('IS_SUMMON_ITEM(item->GetVnum())',use)
|
|
login=source('input_login.cpp')
|
|
assert login.index('AFFECT_PREMIUM_START + i')<login.index('PortVipRefresh(ch, false);')
|
|
assert 'void\t\t\t\tPortSetPremiumTime(BYTE bType, int iTime)' in source('char.h')
|
|
code=r'''
|
|
#include <cassert>
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <deque>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
using DWORD=unsigned;using BYTE=unsigned char;
|
|
enum {CHAT_TYPE_INFO=1,CHAT_TYPE_COMMAND=5,POINT_NONE=0,AFFECT_PREMIUM_START=500,PREMIUM_MAX_NUM=9};
|
|
static int now=1700000000;static DWORD clockms=1000;
|
|
int get_global_time(){return now;}DWORD get_dword_time(){return clockms;}
|
|
static std::vector<std::string> logs;
|
|
void sys_log(int,const char* format,...){char out[512];va_list a;va_start(a,format);vsnprintf(out,sizeof(out),format,a);va_end(a);logs.push_back(out);}
|
|
void sys_err(const char* format,...){char out[512];va_list a;va_start(a,format);vsnprintf(out,sizeof(out),format,a);va_end(a);logs.push_back(std::string("ERR ")+out);}
|
|
static std::string format(const char* f,va_list a){char out[4096];vsnprintf(out,sizeof(out),f,a);return out;}
|
|
// Account table: id -> seven end times (0 = never), as UNIX_TIMESTAMP reads them.
|
|
static std::map<DWORD,std::vector<int>> accounts;
|
|
using MYSQL_ROW=char**;
|
|
struct Rows{char* cells[7];char text[7][16];};
|
|
struct SQLResult{unsigned uiNumRows;Rows* pSQLResult;Rows rows;};
|
|
struct SQLMsg{SQLResult result;bool ok=true;SQLResult* Get(){return ok?&result:nullptr;}};
|
|
MYSQL_ROW mysql_fetch_row(Rows* r){return r->cells;}
|
|
struct DBManager{
|
|
std::deque<std::function<void()>> queue;std::vector<std::string> queries;
|
|
static DBManager& instance(){static DBManager m;return m;}
|
|
void Query(const char* f,...){va_list a;va_start(a,f);std::string q=format(f,a);va_end(a);queries.push_back(q);
|
|
queue.push_back([q]{DWORD id;int secs;
|
|
const char* where=strstr(q.c_str()," WHERE id=");assert(where && sscanf(where," WHERE id=%u",&id)==1);
|
|
const char* add=strstr(q.c_str(),"UNIX_TIMESTAMP())+");assert(add && sscanf(add,"UNIX_TIMESTAMP())+%d)",&secs)==1);
|
|
for(int& t:accounts[id])t=(t>now?t:now)+secs;});}
|
|
template<class F> void FuncQuery(F f,const char* fmt,...){va_list a;va_start(a,fmt);std::string q=format(fmt,a);va_end(a);queries.push_back(q);
|
|
queue.push_back([f,q]{DWORD id;assert(sscanf(strstr(q.c_str()," WHERE id="), " WHERE id=%u",&id)==1);
|
|
SQLMsg m{};auto it=accounts.find(id);m.result.uiNumRows=it==accounts.end()?0:1;m.result.pSQLResult=&m.result.rows;
|
|
for(int i=0;i<7;++i){m.result.rows.cells[i]=m.result.rows.text[i];snprintf(m.result.rows.text[i],16,"%d",it==accounts.end()?0:it->second[i]);}
|
|
f(&m);});}
|
|
void Run(){while(!queue.empty()){auto job=queue.front();queue.pop_front();job();}}
|
|
};
|
|
struct CItem{DWORD vnum;int count;DWORD GetVnum(){return vnum;}int GetCount(){return count;}void SetCount(int c){count=c;}};
|
|
using LPITEM=CItem*;
|
|
struct TAccountTable{DWORD id;};
|
|
struct DESC{TAccountTable table;TAccountTable& GetAccountTable(){return table;}};
|
|
struct CHARACTER{
|
|
DESC desc;bool online=true;int premium[PREMIUM_MAX_NUM]={};std::map<DWORD,long> affects;std::vector<std::string> messages;
|
|
const char* GetName(){return "Admin";}bool IsPC(){return true;}DESC* GetDesc(){return online?&desc:nullptr;}DWORD GetPlayerID(){return 7;}
|
|
int GetPremiumRemainSeconds(BYTE t)const{return t<PREMIUM_MAX_NUM?premium[t]-now:0;}
|
|
void PortSetPremiumTime(BYTE t,int v){if(t<PREMIUM_MAX_NUM)premium[t]=v;}
|
|
bool AddAffect(DWORD type,BYTE,long,DWORD,long duration,long,bool override){assert(override);affects[type]=duration;return true;}
|
|
bool RemoveAffect(DWORD type){return affects.erase(type)>0;}
|
|
void ChatPacket(BYTE type,const char* f,...){va_list a;va_start(a,f);std::string s=format(f,a);va_end(a);assert(type==CHAT_TYPE_COMMAND);messages.push_back(s);}
|
|
};
|
|
using LPCHARACTER=CHARACTER*;
|
|
static CHARACTER* player=nullptr;
|
|
struct CHARACTER_MANAGER{static CHARACTER_MANAGER& instance(){static CHARACTER_MANAGER m;return m;}
|
|
LPCHARACTER FindByPID(DWORD pid){return player && pid==7?player:nullptr;}};
|
|
static std::vector<std::string> itemlog;
|
|
struct LogManager{static LogManager& instance(){static LogManager m;return m;}
|
|
void ItemLog(LPCHARACTER,LPITEM item,const char* how,const char* hint){assert(item->count>0);itemlog.push_back(std::string(how)+" "+std::to_string(item->vnum)+" "+hint);}};
|
|
#include "port_premium.h"
|
|
static DBManager& db=DBManager::instance();
|
|
static const std::string& last(){return player->messages.back();}
|
|
static void tick(){clockms+=1100;}
|
|
int main(){
|
|
const int DAY=86400;
|
|
accounts[1]=std::vector<int>(7,0);
|
|
player=new CHARACTER;player->desc.table.id=1;
|
|
// Fresh account: 7 days from now on all seven columns, one card consumed, icons 500..506 for 7 days.
|
|
CItem card{38001,2};
|
|
assert(PortVipUseCard(player,&card) && card.count==1 && itemlog.back()=="MT_VIP_CARD 38001 7");
|
|
assert(db.queries[0].rfind("UPDATE account.account SET silver_expire=FROM_UNIXTIME(GREATEST(UNIX_TIMESTAMP(silver_expire),UNIX_TIMESTAMP())+604800),gold_expire=",0)==0);
|
|
assert(db.queries[0].find("money_drop_rate_expire=FROM_UNIXTIME(GREATEST(UNIX_TIMESTAMP(money_drop_rate_expire),UNIX_TIMESTAMP())+604800) WHERE id=1")!=std::string::npos);
|
|
assert(db.queries[1]=="SELECT UNIX_TIMESTAMP(silver_expire),UNIX_TIMESTAMP(gold_expire),UNIX_TIMESTAMP(safebox_expire),UNIX_TIMESTAMP(autoloot_expire),UNIX_TIMESTAMP(fish_mind_expire),UNIX_TIMESTAMP(marriage_fast_expire),UNIX_TIMESTAMP(money_drop_rate_expire) FROM account.account WHERE id=1");
|
|
assert(last()=="MTVIP1 added 38001 7" && player->premium[0]==0); // memory waits for the read-back
|
|
db.Run();
|
|
assert(last()=="MTVIP1 times 604800 604800 604800 604800 604800 604800 604800");
|
|
for(int i=0;i<7;++i)assert(player->premium[i]==now+7*DAY && player->affects[500+i]==7*DAY && accounts[1][i]==now+7*DAY);
|
|
assert(!player->affects.count(507) && player->premium[7]==0);
|
|
// A second card inside the gap is refused and kept; after it, 30 days add to the current end.
|
|
CItem month{38002,1};
|
|
assert(PortVipUseCard(player,&month) && last()=="MTVIP1 error rate" && month.count==1);
|
|
tick();now+=DAY;assert(PortVipUseCard(player,&month) && month.count==0);db.Run();
|
|
assert(last()=="MTVIP1 times 3110400 3110400 3110400 3110400 3110400 3110400 3110400" && player->premium[3]==now-DAY+37*DAY);
|
|
// Past the 400-day cap: refused before anything is consumed or queried.
|
|
const size_t queries=db.queries.size();CItem many{38002,20};
|
|
for(int k=0;k<13;++k){tick();assert(PortVipUseCard(player,&many));db.Run();} // 36 days left: 12 cards reach 396
|
|
assert(last()=="MTVIP1 error full" && many.count==8 && db.queries.size()==queries+24);
|
|
// Not a card: untouched, nothing queried.
|
|
CItem other{27001,5};assert(!PortVipUseCard(player,&other) && other.count==5 && db.queries.size()==queries+24);
|
|
// Enter-game on another core: the db core still has the login-time ends (here, none); the refresh restores them.
|
|
tick();const auto stale=player->messages.size();
|
|
for(int i=0;i<PREMIUM_MAX_NUM;++i)player->premium[i]=0;player->affects.clear();
|
|
PortVipRefresh(player,false);db.Run();
|
|
assert(player->messages.size()==stale && player->premium[6]==accounts[1][6] && player->affects[506]==accounts[1][6]-now);
|
|
// Expired in the account (edited by hand): state and icon go away; unchanged types keep their icon untouched.
|
|
accounts[1][2]=now-5;player->affects[502]=1;player->affects[503]=12345;
|
|
PortVipRefresh(player,false);db.Run();
|
|
assert(player->GetPremiumRemainSeconds(2)<0 && !player->affects.count(502) && player->affects[503]==12345);
|
|
// Status command: version, validation, throttle, report of remaining seconds (0 when expired).
|
|
tick();PortVipCommand(player,"1 status");db.Run();assert(last().rfind("MTVIP1 times ",0)==0 && last().find(" 0 ")!=std::string::npos);
|
|
PortVipCommand(player,"1 status");assert(last()=="MTVIP1 error rate");
|
|
for(auto bad:{"1 stats","1","1 status x"}){tick();PortVipCommand(player,bad);assert(last()=="MTVIP1 error invalid");}
|
|
const auto quiet=player->messages.size();tick();PortVipCommand(player,"2 status");PortVipCommand(player,"1 status x y z");
|
|
assert(player->messages.size()==quiet);
|
|
// The player left before the read-back: nothing is touched.
|
|
tick();PortVipCommand(player,"1 status");CHARACTER* gone=player;player=nullptr;db.Run();assert(gone->messages.size()==quiet);
|
|
// Missing account row: logged, no state change.
|
|
player=gone;player->desc.table.id=99;tick();PortVipRefresh(player,true);db.Run();assert(logs.back()=="ERR PORT_VIP: account 99 not found");
|
|
puts("PASS: card use and read-back, stacking, gap, cap, other items, enter-game refresh, expiry, status command");
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory(prefix='mt-vip-') as folder:
|
|
path=Path(folder);(path/'test.cpp').write_text(code)
|
|
subprocess.run(['c++','-std=c++17','-Wall','-I'+str(SRC),str(path/'test.cpp'),'-o',str(path/'test')],check=True)
|
|
subprocess.run([str(path/'test')],check=True)
|