You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.5 KiB
157 lines
4.5 KiB
2 months ago
|
package socket
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"matchmaking-system/internal/data"
|
||
|
"matchmaking-system/internal/data/memory"
|
||
|
"matchmaking-system/internal/data/socket/virtualData"
|
||
|
"matchmaking-system/internal/data/tradedeal/virtual"
|
||
|
"matchmaking-system/internal/pkg/flags"
|
||
|
"matchmaking-system/internal/pkg/logging/applogger"
|
||
|
"matchmaking-system/internal/pkg/setting"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// orderSubAdminContractSubscribeByOrder
|
||
|
//
|
||
|
// @Description: 管理员合约|持仓订单订阅|持仓浮动盈亏订阅
|
||
|
// @receiver u
|
||
|
// @param psgMsg
|
||
|
func (u *Client) orderSubAdminContractSubscribeByOrder(psgMsg *SymbolMessage) {
|
||
|
for {
|
||
|
if psgMsg.Symbol == setting.AdminContractSubscribe {
|
||
|
var hashList []virtual.ContractTallyCache
|
||
|
for _, field := range psgMsg.Order {
|
||
|
fieldStr, err := data.Reds.HGet(context.Background(), setting.AdminContractSubscribe, field).Result()
|
||
|
if err != nil {
|
||
|
time.Sleep(5 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
var msg virtual.ContractTallyCache
|
||
|
if err = json.Unmarshal([]byte(fieldStr), &msg); err != nil {
|
||
|
time.Sleep(5 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
hashList = append(hashList, msg)
|
||
|
}
|
||
|
|
||
|
for _, value := range hashList {
|
||
|
orderModel := virtualData.ContractOrderProcessing(setting.AdminContractSubscribe, value)
|
||
|
if orderModel != nil {
|
||
|
_, ok := u.symbol.Load(psgMsg.Symbol)
|
||
|
if ok {
|
||
|
// 清理[撤单|平仓]缓存队列
|
||
|
if orderModel.Status == flags.Cancel || orderModel.Status == flags.Close {
|
||
|
if err := data.Reds.HDel(context.Background(), setting.AdminContractSubscribe, orderModel.OrderId).Err(); err != nil {
|
||
|
applogger.Error("AdminContractSubscribe.HDel:%v", err)
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
orderStr, err := json.Marshal(orderModel)
|
||
|
if err != nil {
|
||
|
applogger.Error("Administrator contract subscription cache order error:%v", err)
|
||
|
time.Sleep(5 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
u.msg <- orderStr // 用户(挂单|持仓)订阅
|
||
|
} else {
|
||
|
applogger.Info("Administrator cancels contract order subscription.")
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
time.Sleep(600 * time.Millisecond)
|
||
|
}
|
||
|
}
|
||
|
func (u *Client) orderSubAdminContractSubscribeBySum(psgMsg *SymbolMessage) {
|
||
|
for {
|
||
|
if psgMsg.Symbol == setting.AdminContractSumSubscribe {
|
||
|
priceSum, err := memory.ContractFloating.Get(flags.FloatingHy)
|
||
|
if err != nil {
|
||
|
time.Sleep(5 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
result := &SymbolSumResult{
|
||
|
Symbol: psgMsg.Symbol,
|
||
|
Price: string(priceSum),
|
||
|
}
|
||
|
|
||
|
priceByte, err := json.Marshal(result)
|
||
|
if err != nil {
|
||
|
time.Sleep(3 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
_, ok := u.symbol.Load(psgMsg.Symbol)
|
||
|
if ok {
|
||
|
u.msg <- priceByte
|
||
|
} else {
|
||
|
applogger.Info("Administrator cancels contract order subscription.")
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
time.Sleep(2 * time.Second)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// orderSubAdminSecondSubscribeByOrder
|
||
|
//
|
||
|
// @Description: 管理员秒合约持仓订单订阅
|
||
|
// @receiver u
|
||
|
// @param psgMsg
|
||
|
func (u *Client) orderSubAdminSecondSubscribeByOrder(psgMsg *SymbolMessage) {
|
||
|
for {
|
||
|
if psgMsg.Symbol == setting.AdminSecondSubscribe {
|
||
|
var hashList []virtual.ContractTallyCache
|
||
|
for _, field := range psgMsg.Order {
|
||
|
fieldStr, err := data.Reds.HGet(context.Background(), setting.AdminSecondSubscribe, field).Result()
|
||
|
if err != nil {
|
||
|
time.Sleep(5 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
var msg virtual.ContractTallyCache
|
||
|
if err = json.Unmarshal([]byte(fieldStr), &msg); err != nil {
|
||
|
time.Sleep(5 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
hashList = append(hashList, msg)
|
||
|
}
|
||
|
|
||
|
for _, value := range hashList {
|
||
|
orderModel := virtualData.SecondOrderProcessing(setting.AdminSecondSubscribe, value)
|
||
|
if orderModel != nil {
|
||
|
_, ok := u.symbol.Load(psgMsg.Symbol)
|
||
|
if ok {
|
||
|
// 清理[撤单|平仓]缓存队列
|
||
|
if orderModel.Status == flags.Cancel || orderModel.Status == flags.Close {
|
||
|
if err := data.Reds.HDel(context.Background(), setting.AdminSecondSubscribe, orderModel.OrderId).Err(); err != nil {
|
||
|
applogger.Error("AdminSecondSubscribe.HDel:%v", err)
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
orderStr, err := json.Marshal(orderModel)
|
||
|
if err != nil {
|
||
|
applogger.Error("Administrator contract subscription cache order error:%v", err)
|
||
|
time.Sleep(5 * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
u.msg <- orderStr // 用户(挂单|持仓)订阅
|
||
|
} else {
|
||
|
applogger.Info("Administrator cancels contract order subscription.")
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
time.Sleep(600 * time.Millisecond)
|
||
|
}
|
||
|
}
|