Files
winapp/AIFormUnit.cpp
T
2026-11-18 10:24:38 +08:00

739 lines
28 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//---------------------------------------------------------------------------
#include <vcl.h>
#include <System.JSON.hpp>
#pragma hdrstop
#include "AIFormUnit.h"
#include "PolicyUnit.h"
#include "PolicySave.h" //applyStrategy需要TPolicySaveForm //getPolicyContext需要g_aBetPolicies/g_aChipPolicies等
#include "UtilityUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TAIForm *AIForm = NULL;
const String TAIForm::skills[TAIForm::SKILL_COUNT] = {
"skills", "normalBet", "specialBet", "betWithChip",
"normalChip", "tierChip", "ladderChip",
"winLoseDiffChip", "specialChip", "comboLadderChip"
};
//---------------------------------------------------------------------------
__fastcall TAIForm::TAIForm(TComponent* Owner, String apiKey)
: TForm(Owner)
{
m_chrm = NULL;
m_hV8Sema = CreateSemaphore( NULL, 1, 1, NULL );
m_hV8ValueSema = CreateSemaphore( NULL, 0, 1, NULL );
m_v8Values = new TStringList;
m_bClosing = false;
if(!apiKey.IsEmpty())
m_apiKey = "sk-" + apiKey;
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::FormCreate(TObject *Sender)
{
m_baseUrl = "http://" + (String)g_apiServer + ":3100/";
LoadSkillCache();
InitBrowser();
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
if( m_hV8Sema ){
CloseHandle( m_hV8Sema );
m_hV8Sema = NULL;
}
if( m_hV8ValueSema ){
CloseHandle( m_hV8ValueSema );
m_hV8ValueSema = NULL;
}
if( m_v8Values ){
delete m_v8Values;
m_v8Values = NULL;
}
if( !m_bClosing && m_chrm ){
m_bClosing = true;
if( m_chrm->ChromiumBrowser->IsLoading )
m_chrm->ChromiumBrowser->StopLoad();
m_chrm->CloseBrowser(true);
CanClose = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::FormClose(TObject *Sender, TCloseAction &Action)
{
UnloadSkillCache();
if( m_chrm ){
delete m_chrm;
m_chrm = NULL;
}
delete AIForm;
AIForm = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::InitBrowser()
{
m_chrm = new TChromiumWindow(this);
m_chrm->ChromiumBrowser->OnProcessMessageReceived = WebProcessMessageReceived;
m_chrm->ChromiumBrowser->OnBeforeContextMenu = WebBeforeContextMenu;
m_chrm->ChromiumBrowser->OnPreKeyEvent = WebPreKeyEvent;
m_chrm->ChromiumBrowser->OnLoadEnd = WebLoadEnd;
m_chrm->ChromiumBrowser->OnLoadError = WebLoadError;
m_chrm->ChromiumBrowser->OnAfterCreated = WebAfterCreated;
m_chrm->ChromiumBrowser->OnBeforeClose = WebBeforeClose;
m_chrm->Parent = this;
m_chrm->Align = alClient;
m_chrm->CreateBrowser();
m_chrm->Show();
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::WebAfterCreated(TObject *Sender, const _di_ICefBrowser browser)
{
if( m_bClosing )
return;
m_chrm->LoadURL(m_baseUrl + "init");
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::WebBeforeClose(TObject* Sender, const _di_ICefBrowser browser)
{
if( m_bClosing )
PostMessage( Handle, WM_CLOSE, 0, 0 );
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::WebBeforeContextMenu(TObject* Sender, const _di_ICefBrowser browser,
const _di_ICefFrame frame, const _di_ICefContextMenuParams params,
const _di_ICefMenuModel model)
{
model->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::WebPreKeyEvent(TObject* Sender, const _di_ICefBrowser browser, const PCefKeyEvent event,
TCefEventHandle osEvent, /* out */ bool &isKeyboardShortcut, /* out */ bool &Result)
{
if(!g_bTesting && event->kind==KEYEVENT_RAWKEYDOWN && ( event->windows_key_code==VK_F12 //禁止devtools
|| event->windows_key_code==VK_F5 //禁止F5刷新
|| event->modifiers==EVENTFLAG_ALT_DOWN && (event->windows_key_code==VK_LEFT //禁止回退
|| event->windows_key_code==VK_RIGHT) ) )
Result = true;
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::WebLoadError(TObject* Sender, const _di_ICefBrowser browser, const _di_ICefFrame frame,
TCefErrorCode errorCode, const ustring errorText, const ustring failedUrl)
{
if( m_bClosing )
return;
DebugPrint("load error %s (%s[%d])", ((AnsiString)failedUrl).c_str(), ((AnsiString)errorText).c_str(), errorCode);
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::WebLoadEnd(TObject* Sender, const _di_ICefBrowser browser,
const _di_ICefFrame frame, int httpStatusCode)
{
if( m_bClosing )
return;
if( httpStatusCode==200 ){
String url = frame->GetUrl();
if(url.Pos("/init")){
if(!m_apiKey.IsEmpty()){
frame->ExecuteJavaScript("init('" + m_baseUrl + "', '" + m_apiKey + "');", "", 0 );
}
// else if( g_bTesting ){
// frame->ExecuteJavaScript("init('" + m_baseUrl + "', 'sk-IOQE1ddbYtA92nHX3f42Bd98A3Ab459e80A1Ad3491024aEb');", "", 0 );
// }
}
}
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::WebProcessMessageReceived(TObject* Sender, const _di_ICefBrowser browser,
const _di_ICefFrame frame, TCefProcessId sourceProcess, const _di_ICefProcessMessage message,
/* out */ bool &Result)
{
if( m_bClosing )
return;
if( message->Name=="ReturnJQValue" ){
_di_ICefListValue argList = message->GetArgumentList();
for( int i=0; i<argList->GetSize(); i++ ){
m_v8Values->Append( argList->GetString(i) );
}
ReleaseV8ValueSema();
Result = true;
}
else if( message->Name=="OnJSMessage" ){
_di_ICefListValue argList = message->GetArgumentList();
if( argList->GetSize()==1 ){
}
Result = true;
}
else if( message->Name=="SyncCall" ){
String jsonTxt;
String id; //ExecuteJavaScript在if块外用到id
_di_ICefListValue argList = message->GetArgumentList();
if( argList->GetSize() >= 2 ){
String method = argList->GetString(0);
id = argList->GetString(1);
TJSONObject* env = new TJSONObject();
if( method == "getSkills" ){
env->AddPair( "ok", new TJSONTrue() );
env->AddPair( "md", new TJSONString( m_skillCache[0] ) );
}
else if( method == "loadSkill" && argList->GetSize() >= 3 ){
String skillName = argList->GetString(2);
int idx = -1;
for( int i=0; i<SKILL_COUNT; i++ ){
if( skills[i] == skillName ){
idx = i;
break;
}
}
if( idx >= 0 ){
env->AddPair( "ok", new TJSONTrue() );
env->AddPair( "md", new TJSONString( m_skillCache[idx] ) );
}
else{
env->AddPair( "ok", new TJSONFalse() );
env->AddPair( "message", new TJSONString( "不认识的技能名:" + skillName ) );
}
}
else if( method == "getPolicyContext" ){
env->AddPair( "ok", new TJSONTrue() );
TJSONArray* arr = new TJSONArray();
//遍历全部打法策略(28条内置 + 用户自定义);问路/差均归属「常规打法」
for(int i=0; i<(int)g_aBetPolicies.size(); i++){
TBetPolicy* p = (TBetPolicy*)g_aBetPolicies[i];
TJSONObject* pObj = new TJSONObject();
pObj->AddPair( "id", new TJSONNumber( p->id ) );
String betKindName = p->kind==KIND_BET_SPE ? "特殊打法"
: p->kind==KIND_BET_WCH ? "打注一体" : "常规打法";
pObj->AddPair( "kind", new TJSONString( betKindName ) );
pObj->AddPair( "name", new TJSONString( p->name ) );
pObj->AddPair( "description", new TJSONString( p->desc ) );
TJSONObject* data = new TJSONObject();
TJSONObject* opts = new TJSONObject();
TJSONArray* rows = new TJSONArray();
if( IsCodePolicy(p->kind) ){ //代码策略:data只有code(打注一体另有miniChip)
data->AddPair( "code", new TJSONString( p->code ) );
if( p->kind==KIND_BET_WCH )
data->AddPair( "miniChip", new TJSONNumber( ((TBetChipPolicy*)p)->miniChip ) );
}
else if( p->kind == KIND_BET_ASK ){ //庄闲问路(小强路特例,rows留空)
opts->AddPair( "适用路单", new TJSONString( "小强路" ) );
opts->AddPair( "庄问路全红", new TJSONNumber( (int)g_BPAskBet.bet[0] ) );
opts->AddPair( "闲问路全红", new TJSONNumber( (int)g_BPAskBet.bet[1] ) );
}
else if( p->kind == KIND_BET_DIF ){ //庄闲差(珠盘特例,rows留空)
opts->AddPair( "适用路单", new TJSONString( "珠盘" ) );
opts->AddPair( "庄比闲多", new TJSONNumber( (int)g_BPDiffBet.diff[0] ) );
opts->AddPair( "庄多下注", new TJSONString( g_BPDiffBet.bets[0] ) );
opts->AddPair( "闲比庄多", new TJSONNumber( (int)g_BPDiffBet.diff[1] ) );
opts->AddPair( "闲多下注", new TJSONString( g_BPDiffBet.bets[1] ) );
}
else{ //常规打法:options(适用路单) + rows(序号,切入点,下注)
String roadName = "珠盘";
if( p->road == 1 ) roadName = "大路";
else if( p->road == 2 ) roadName = "大眼路";
else if( p->road == 3 ) roadName = "小路";
else if( p->road == 4 ) roadName = "小强路";
opts->AddPair( "适用路单", new TJSONString( roadName ) );
for(int r=0; r<(int)p->aRows.size(); r++){
TBetRow* row = (TBetRow*)p->aRows[r];
TJSONObject* rObj = new TJSONObject();
rObj->AddPair( "序号", new TJSONNumber( r+1 ) );
//切入点:cond还原ASD串 item&0x3=颜色(1庄/2闲/3和) (item&0x7C)>>2=个数 item&0x80=不定长(大写)
String condStr;
for(int c=0; c<(int)row->cond.size(); c++){
int v = row->cond[c];
int count = (v & 0x7C) >> 2;
char atom = (v&0x3)==1 ? 'a' : (v&0x3)==2 ? 's' : 'd';
condStr += (String)(char)((v & 0x80) ? atom-0x20 : atom);
if( count > 1 )
condStr += IntToStr( count );
}
rObj->AddPair( "切入点", new TJSONString( condStr ) );
//下注:bets存序号 0=x不下注 1=a庄 2=s闲 3=d和 4=z局停
String betStr;
for(int c=0; c<(int)row->bets.size(); c++){
int bet = row->bets[c];
betStr += (String)(char)(bet==0 ? 'x' : bet==1 ? 'a' : bet==2 ? 's' : bet==3 ? 'd' : 'z');
}
rObj->AddPair( "下注", new TJSONString( betStr ) );
rows->Add( rObj );
}
}
if( !IsCodePolicy(p->kind) ){
data->AddPair( "options", opts );
data->AddPair( "rows", rows );
}
pObj->AddPair( "data", data );
arr->Add( pObj );
}
//遍历全部注码策略(10条内置 + 用户自定义)
for(int i=0; i<(int)g_aChipPolicies.size(); i++){
TChipPolicy* p = (TChipPolicy*)g_aChipPolicies[i];
TJSONObject* pObj = new TJSONObject();
pObj->AddPair( "id", new TJSONNumber( p->id ) );
String kindName;
if( p->kind == KIND_CHIP_NOR ) kindName = "常规注码";
else if( p->kind == KIND_CHIP_HAN ) kindName = "层级注码";
else if( p->kind == KIND_CHIP_LEV ) kindName = "阶梯注码";
else if( p->kind == KIND_CHIP_WLD ) kindName = "赢输次差注码";
else if( p->kind == KIND_CHIP_COM ) kindName = "组合阶梯注码";
else if( p->kind == KIND_CHIP_SPE ) kindName = "特殊注码";
else kindName = "常规注码"; //随机注码等归属常规注码
pObj->AddPair( "kind", new TJSONString( kindName ) );
pObj->AddPair( "name", new TJSONString( p->name ) );
pObj->AddPair( "description", new TJSONString( p->desc ) );
TJSONObject* data = new TJSONObject();
TJSONObject* opts = new TJSONObject();
TJSONArray* rows = new TJSONArray();
if( IsCodePolicy(p->kind) ){ //代码策略:data只有code与miniChip
data->AddPair( "code", new TJSONString( p->code ) );
data->AddPair( "miniChip", new TJSONNumber( p->miniChip ) );
pObj->AddPair( "data", data );
arr->Add( pObj );
continue;
}
if( p->kind == KIND_CHIP_RAN ){ //随机注码(特例,rows留空)
opts->AddPair( "随机最小值", new TJSONNumber( g_RandomChip.minChip ) );
opts->AddPair( "随机最大值", new TJSONNumber( g_RandomChip.maxChip ) );
}
else{
opts->AddPair( "爆缆方式", new TJSONNumber( p->dead ) );
opts->AddPair( "暂停分钟", new TJSONNumber( p->deadPause ) );
if( p->kind == KIND_CHIP_HAN ){
TTierPolicy* tp = (TTierPolicy*)p;
opts->AddPair( "高层赢多回基", new TJSONBool( tp->bTierWinRet ) );
opts->AddPair( "净输值累加", new TJSONBool( tp->bTierAccChip ) );
opts->AddPair( "输多打0", new TJSONBool( tp->bTierLos0 ) );
}
else if( p->kind >= KIND_CHIP_LEV ){
TLevelPolicy* lp = (TLevelPolicy*)p;
opts->AddPair( "总体计算", new TJSONBool( lp->bTotalDiff ) );
opts->AddPair( "即时升降", new TJSONBool( lp->bInstant ) );
if( p->kind == KIND_CHIP_WLD )
opts->AddPair( "升降清零", new TJSONBool( ((TWLDiffPolicy*)p)->bIndie ) );
}
}
for(int r=0; r<(int)p->aRows.size(); r++){
TJSONObject* rObj = new TJSONObject();
rObj->AddPair( "序号", new TJSONNumber( r+1 ) );
if( p->kind == KIND_CHIP_COM ){ //常规方案,倍数,赢利,赢走向,亏损,输走向
TComboRow* row = (TComboRow*)p->aRows[r];
rObj->AddPair( "常规方案", new TJSONNumber( row->id ) );
rObj->AddPair( "倍数", new TJSONNumber( row->chip ) );
rObj->AddPair( "赢利", new TJSONNumber( row->nCondWin ) );
rObj->AddPair( "赢走向", new TJSONNumber( row->iWin ) );
rObj->AddPair( "亏损", new TJSONNumber( row->nCondLos ) );
rObj->AddPair( "输走向", new TJSONNumber( row->iLos ) );
}
else if( p->kind == KIND_CHIP_HAN ){ //注码,手数,赢走向,输走向
TTierRow* row = (TTierRow*)p->aRows[r];
rObj->AddPair( "注码", new TJSONNumber( row->chip ) );
rObj->AddPair( "手数", new TJSONNumber( row->hands ) );
rObj->AddPair( "赢走向", new TJSONNumber( row->iWin ) );
rObj->AddPair( "输走向", new TJSONNumber( row->iLos ) );
}
else if( p->kind == KIND_CHIP_LEV || p->kind == KIND_CHIP_WLD ){ //注码,条件,赢走向,条件,输走向
TLevelRow* row = (TLevelRow*)p->aRows[r];
rObj->AddPair( "注码", new TJSONNumber( row->chip ) );
if( p->kind == KIND_CHIP_LEV ){
rObj->AddPair( "赢利", new TJSONNumber( row->nCondWin ) );
rObj->AddPair( "赢走向", new TJSONNumber( row->iWin ) );
rObj->AddPair( "亏损", new TJSONNumber( row->nCondLos ) );
rObj->AddPair( "输走向", new TJSONNumber( row->iLos ) );
}
else{
rObj->AddPair( "大于等于", new TJSONNumber( row->nCondWin ) );
rObj->AddPair( "赢走向", new TJSONNumber( row->iWin ) );
rObj->AddPair( "小于", new TJSONNumber( row->nCondLos ) );
rObj->AddPair( "输走向", new TJSONNumber( row->iLos ) );
}
}
else{ //常规注码:注码,赢走向,输走向
TChipRow* row = (TChipRow*)p->aRows[r];
rObj->AddPair( "注码", new TJSONNumber( row->chip ) );
rObj->AddPair( "赢走向", new TJSONNumber( row->iWin ) );
rObj->AddPair( "输走向", new TJSONNumber( row->iLos ) );
}
rows->Add( rObj );
}
data->AddPair( "options", opts );
data->AddPair( "rows", rows );
pObj->AddPair( "data", data );
arr->Add( pObj );
}
env->AddPair( "strategies", arr );
}
else if( method == "applyStrategy" && argList->GetSize() >= 3 ){
String json = argList->GetString(2);
ApplyResult ar = SaveAIPolicy(json);
if( ar.ok ){
env->AddPair( "ok", new TJSONTrue() );
env->AddPair( "id", new TJSONNumber( ar.id ) );
env->AddPair( "name", new TJSONString( ar.name ) );
}
else{
env->AddPair( "ok", new TJSONFalse() );
String codeStr = ar.code==1 ? "解析失败" : ar.code==3 ? "校验失败" : ar.code==4 ? "用户取消" : "操作失败";
env->AddPair( "code", new TJSONString( codeStr ) );
env->AddPair( "message", new TJSONString( ar.message ) );
}
}
else{
env->AddPair( "ok", new TJSONFalse() );
env->AddPair( "message", new TJSONString( "未知方法:" + method ) );
}
jsonTxt = env->ToJSON();
delete env;
}
else{
jsonTxt = "{ \"ok\": false, \"message\": \"参数不完整\" }";
}
//jsonTxt 是 JSON 文本,JS 代码里的字符串参数要 JS 字符串字面量,不是 JSON 文本,所以,需要再做一次编码
TJSONString *jsArg = new TJSONString(jsonTxt);
frame->ExecuteJavaScript("window.aiResolve('" + id + "', " + jsArg->ToJSON() + ");", "", 0);
delete jsArg;
Result = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::ReleaseV8ValueSema()
{
if(!ReleaseSemaphore( m_hV8ValueSema, 1, NULL)){
DebugPrint("V8ValueSema error: %d", GetLastError());
CloseHandle( m_hV8ValueSema );
m_hV8ValueSema = CreateSemaphore( NULL, 0, 1, NULL );
}
}
//---------------------------------------------------------------------------
//调用该函数的JS语句各分支必须返回ReturnJQValue,且确保延迟不大于1秒
TStringList* __fastcall TAIForm::GetJQValue( String& str, _di_ICefFrame frame )
{
WaitForSingleObject( m_hV8Sema, INFINITE );//60000 );
m_v8Values->Clear();
frame->ExecuteJavaScript( str, "", 0 );
DWORD dwRet = WaitForSingleObject( m_hV8ValueSema, 2000 );
TStringList* values = NULL;
if( dwRet==WAIT_OBJECT_0 ){
values = new TStringList;
for( int i=0; i<m_v8Values->Count; i++ ){
values->Append( m_v8Values->Strings[i] );
}
m_v8Values->Clear();
}
else if(dwRet==WAIT_TIMEOUT){
ReleaseV8ValueSema();
}
ReleaseSemaphore( m_hV8Sema, 1, NULL );
return values;
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::OnLogin(String apiKey)
{
if(!apiKey.IsEmpty())
m_apiKey = "sk-" + apiKey;
String url = m_chrm->ChromiumBrowser->Browser->MainFrame->Url;
if(url.Pos("/init") && !m_apiKey.IsEmpty()){
m_chrm->ChromiumBrowser->ExecuteJavaScript("init('" + m_baseUrl + "', '" + m_apiKey + "');", "", 0 );
}
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::OnLogout()
{
m_apiKey = "";
m_chrm->ChromiumBrowser->ExecuteJavaScript("init('', '');", "", 0 );
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::LoadSkillCache()
{
for( int i=0; i<SKILL_COUNT; i++ ){
TResourceStream* rs = new TResourceStream( int(HInstance),
skills[i].c_str(), RT_RCDATA );
if( rs->Size > 0 ){
char* buf = new char[rs->Size + 1];
rs->Read(buf, rs->Size);
buf[rs->Size] = 0;
m_skillCache[i] = String(buf);
delete[] buf;
}
delete rs;
}
}
//---------------------------------------------------------------------------
void __fastcall TAIForm::UnloadSkillCache()
{
for( int i=0; i<SKILL_COUNT; i++ ){
m_skillCache[i] = "";
}
}
//---------------------------------------------------------------------------
TAIForm::ApplyResult TAIForm::SaveAIPolicy(const String& json)
{
ApplyResult r;
r.ok = false; r.code = 1; r.id = 0; r.name = "";
TJSONObject* obj = (TJSONObject*)TJSONObject::ParseJSONValue(json);
if( !obj ){ r.message = "解析失败: JSON 格式错误"; return r; }
TJSONValue* vId = obj->GetValue("id");
TJSONValue* vKind = obj->GetValue("kind");
TJSONValue* vName = obj->GetValue("name");
TJSONValue* vDesc = obj->GetValue("description");
TJSONValue* vData = obj->GetValue("data");
if( !vId || !vKind || !vName || !vDesc || !vData ){
r.message = "解析失败: 缺少必需字段"; delete obj; return r; }
int inId = (int)((TJSONNumber*)vId)->AsInt;
String kindName = ((TJSONString*)vKind)->Value();
String newName = ((TJSONString*)vName)->Value();
String desc = ((TJSONString*)vDesc)->Value();
int ptype = -1, pkind = -1;
if(kindName == "常规打法"){ ptype = TYPE_P_BET; pkind = KIND_BET_NOR; }
else if(kindName == "常规注码"){ ptype = TYPE_P_CHIP; pkind = KIND_CHIP_NOR; }
else if(kindName == "层级注码"){ ptype = TYPE_P_CHIP; pkind = KIND_CHIP_HAN; }
else if(kindName == "阶梯注码"){ ptype = TYPE_P_CHIP; pkind = KIND_CHIP_LEV; }
else if(kindName == "组合阶梯"){ ptype = TYPE_P_CHIP; pkind = KIND_CHIP_COM; }
else if(kindName == "赢输次差注码"){ ptype = TYPE_P_CHIP; pkind = KIND_CHIP_WLD; }
else if(kindName == "随机注码"){ ptype = TYPE_P_CHIP; pkind = KIND_CHIP_RAN; }
else if(kindName == "特殊打法"){ ptype = TYPE_P_BET; pkind = KIND_BET_SPE; }
else if(kindName == "打注一体"){ ptype = TYPE_P_BET; pkind = KIND_BET_WCH; }
else if(kindName == "特殊注码"){ ptype = TYPE_P_CHIP; pkind = KIND_CHIP_SPE; }
else{ r.message = "解析失败: 未知策略类型"; delete obj; return r; }
TJSONObject* dataObj = (TJSONObject*)vData;
TJSONObject* opts = (TJSONObject*)dataObj->GetValue("options");
TJSONArray* rows = (TJSONArray*)dataObj->GetValue("rows");
//代码策略:data里只有code,没有options/rows
if( IsCodePolicy(pkind) ){
TJSONValue* vCode = dataObj->GetValue("code");
if( !vCode || !(vCode->Value().Length()>0) ){
r.message = "解析失败: 缺少 code 字段"; delete obj; return r; }
}
//代码策略的data中还需要miniChip(最小非0注码):仅打注一体与特殊注码两类需要,
//特殊打法只管下注方向、不输出注码,无miniChip
//注意KIND_BET_SPE与KIND_CHIP_SPE同为101,必须配ptype才能区分
bool needMiniChip = ptype==TYPE_P_BET ? (pkind==KIND_BET_WCH) : IsCodePolicy(pkind);
int codeMiniChip = -1;
if( needMiniChip ){
TJSONValue* vMini = dataObj->GetValue("miniChip");
String kindLabel = pkind==KIND_BET_WCH ? "打注一体" : "特殊注码";
if( !vMini || !vMini->InheritsFrom(__classid(TJSONNumber)) ){
r.message = "解析失败: " + kindLabel + "缺少 miniChip 字段或类型错误";
delete obj; return r; }
codeMiniChip = (int)((TJSONNumber*)vMini)->AsInt;
if( codeMiniChip<=0 ){
r.message = "校验失败: " + kindLabel + "的 miniChip 必须为正整数";
delete obj; return r; }
}
//构造策略对象与行文本
String line;
TBasePolicy* pPolicy = NULL;
if( pkind==KIND_BET_WCH ){
//打注一体:bet+chip代码策略,带miniChip
TBetChipPolicy* pBC = new TBetChipPolicy;
pBC->type = 2; pBC->kind = pkind; pBC->desc = desc;
pBC->code = dataObj->GetValue("code")->Value();
pBC->miniChip = codeMiniChip;
pPolicy = pBC;
}
else if( pkind==KIND_BET_SPE ){
//特殊打法:代码策略,无miniChip
TBetPolicy* pBP = new TBetPolicy;
pBP->type = 2; pBP->kind = pkind; pBP->desc = desc;
pBP->code = dataObj->GetValue("code")->Value();
pPolicy = pBP;
}
else if( ptype == TYPE_P_BET ){
TBetPolicy* pBP = new TBetPolicy;
pBP->type = 2; pBP->kind = pkind; pBP->desc = desc;
String road = opts ? ((TJSONString*)opts->GetValue("适用路单"))->Value() : String("");
if( road == "大路" ) pBP->road = 1;
else if( road == "大眼路" ) pBP->road = 2;
else if( road == "小路" ) pBP->road = 3;
else if( road == "小强路" ) pBP->road = 4;
for(int i=0; i<rows->Count; i++){
TJSONObject* rObj = (TJSONObject*)rows->Items[i];
if( i>0 ) line += ";";
line += ((TJSONString*)rObj->GetValue("切入点"))->Value() + "," + ((TJSONString*)rObj->GetValue("下注"))->Value();
}
int err = pBP->ParsePolicy(((AnsiString)line).c_str(), TYPE_P_BET);
if( err ){
int row = err >> 16;
int col = err & 0xF;
r.code = 3; r.message = "校验失败: 第 " + IntToStr(row) + " 行第 " + IntToStr(col) + " 列格式有误";
delete pBP; delete obj; return r;
}
pPolicy = pBP;
}
else{
TChipPolicy* pCP = NULL;
if( IsCodePolicy(pkind) ){
pCP = new TChipPolicy;
pCP->type = 2; pCP->kind = pkind; pCP->desc = desc;
pCP->code = dataObj->GetValue("code")->Value();
pCP->miniChip = codeMiniChip;
pPolicy = pCP;
}
else{
if( pkind == KIND_CHIP_HAN ) pCP = new TTierPolicy;
else if( pkind == KIND_CHIP_LEV || pkind == KIND_CHIP_COM ) pCP = new TLevelPolicy;
else if( pkind == KIND_CHIP_WLD ) pCP = new TWLDiffPolicy;
else pCP = new TChipPolicy;
pCP->type = 2; pCP->kind = pkind; pCP->desc = desc;
if( pkind != KIND_CHIP_RAN && opts ){
pCP->dead = (int)((TJSONNumber*)opts->GetValue("爆缆方式"))->AsInt;
pCP->deadPause = (int)((TJSONNumber*)opts->GetValue("暂停分钟"))->AsInt;
if( pkind == KIND_CHIP_HAN ){
TTierPolicy* tp = (TTierPolicy*)pCP;
tp->bTierWinRet = ((TJSONBool*)opts->GetValue("高层赢多回基"))->AsBoolean;
tp->bTierAccChip = ((TJSONBool*)opts->GetValue("净输值累加"))->AsBoolean;
tp->bTierLos0 = ((TJSONBool*)opts->GetValue("输多打0"))->AsBoolean;
}
else if( pkind >= KIND_CHIP_LEV ){
TLevelPolicy* lp = (TLevelPolicy*)pCP;
lp->bTotalDiff = ((TJSONBool*)opts->GetValue("总体计算"))->AsBoolean;
lp->bInstant = ((TJSONBool*)opts->GetValue("即时升降"))->AsBoolean;
if( pkind == KIND_CHIP_WLD ) ((TWLDiffPolicy*)pCP)->bIndie = ((TJSONBool*)opts->GetValue("升降清零"))->AsBoolean;
}
}
for(int i=0; i<rows->Count; i++){
TJSONObject* rObj = (TJSONObject*)rows->Items[i];
if( i>0 ) line += ";";
if( pkind == KIND_CHIP_COM )
line += ((TJSONString*)rObj->GetValue("常规方案"))->Value() + "," + ((TJSONString*)rObj->GetValue("倍数"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("赢利"))->Value() + "," + ((TJSONString*)rObj->GetValue("赢走向"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("亏损"))->Value() + "," + ((TJSONString*)rObj->GetValue("输走向"))->Value();
else if( pkind == KIND_CHIP_HAN )
line += ((TJSONString*)rObj->GetValue("注码"))->Value() + "," + ((TJSONString*)rObj->GetValue("手数"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("赢走向"))->Value() + "," + ((TJSONString*)rObj->GetValue("输走向"))->Value();
else if( pkind == KIND_CHIP_LEV )
line += ((TJSONString*)rObj->GetValue("注码"))->Value() + "," + ((TJSONString*)rObj->GetValue("赢利"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("赢走向"))->Value() + "," + ((TJSONString*)rObj->GetValue("亏损"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("输走向"))->Value();
else if( pkind == KIND_CHIP_WLD )
line += ((TJSONString*)rObj->GetValue("注码"))->Value() + "," + ((TJSONString*)rObj->GetValue("大于等于"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("赢走向"))->Value() + "," + ((TJSONString*)rObj->GetValue("小于"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("输走向"))->Value();
else
line += ((TJSONString*)rObj->GetValue("注码"))->Value() + "," + ((TJSONString*)rObj->GetValue("赢走向"))->Value() + ","
+ ((TJSONString*)rObj->GetValue("输走向"))->Value();
}
int err = pCP->ParsePolicy(((AnsiString)line).c_str(), TYPE_P_CHIP);
if( err ){
int row = err >> 16;
int col = err & 0xF;
r.code = 3; r.message = "校验失败: 第 " + IntToStr(row) + " 行第 " + IntToStr(col) + " 列格式有误";
delete pCP; delete obj; return r;
}
pPolicy = pCP;
}
}
//阶段三:命名
int index = -1;
String preName = newName;
if( inId > 0 ){
index = FindPolicy(ptype, inId);
if( index < 0 ){ r.message = "校验失败: 策略 id 不存在"; delete obj; return r; }
}
if( preName.IsEmpty() ){
vector<TBasePolicy*>& ap = ptype==TYPE_P_BET ? g_aBetPolicies : g_aChipPolicies;
preName = index>=0 ? ap[index]->name : AutoNewPolicyName(ptype);
}
//主窗口置前
// if (Application->MainForm->WindowState == wsMinimized)
// Application->MainForm->WindowState = wsNormal;
// Application->MainForm->BringToFront();
TPolicySaveForm* pForm = new TPolicySaveForm(this, ptype, preName);
if( pForm->ShowModal() == mrOk ){
String finalName = pForm->m_name;
int tgtIndex = pForm->m_iPolicy;
pPolicy->name = finalName;
pPolicy->type = 2;
if( tgtIndex>=0 ){
vector<TBasePolicy*>& ap = ptype==TYPE_P_BET ? g_aBetPolicies : g_aChipPolicies;
pPolicy->id = ap[tgtIndex]->id;
}
String msg = (String)"保存" + (ptype==TYPE_P_BET ? "打法" : "注码");
int ret = SavePolicy(ptype, pPolicy, tgtIndex);
if( ret >= 0 ){
r.ok = true; r.code = 0; r.id = pPolicy->id; r.name = finalName;
PostMessage(Application->MainFormHandle, UM_POLICYCHANGED, 0x10000 | ptype, 0);
pPolicy = NULL; // 所有权已移交给 g_aBetPolicies,禁止释放
msg += "成功";
MessageBox(Handle, msg.c_str(), L"保存", MB_ICONINFORMATION | MB_OK);
}
else{
r.code = 5;
r.message = "操作失败: 打开策略文件失败";
msg += "失败";
MessageBox(Handle, msg.c_str(), L"保存", MB_ICONERROR | MB_OK);
}
}
else {
r.code = 4;
r.message = "保存已取消";
}
if(pPolicy) delete pPolicy;
delete pForm;
delete obj;
return r;
}
//---------------------------------------------------------------------------