-
Notifications
You must be signed in to change notification settings - Fork 7
/
action.go
114 lines (95 loc) · 4.84 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package message
import (
"github.com/sunist-c/genius-invokation-simulator-backend/enum"
)
// ActionMessageInterface 玩家操作信息的约束接口
type ActionMessageInterface interface {
AttackAction | BurnCardAction | UseCardAction | ReRollAction | SkipRoundAction | SwitchAction | ConcedeAction
}
// AttackAction 玩家的攻击操作信息
type AttackAction struct {
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发起操作的玩家
Target uint64 `json:"target" yaml:"target" xml:"target"` // Target 被攻击的玩家
Skill uint64 `json:"skill" yaml:"skill" xml:"skill"` // Skill 执行攻击的技能
Paid map[enum.ElementType]uint `json:"paid" yaml:"paid" xml:"paid"` // Paid 支付发起攻击的费用
}
// BurnCardAction 玩家的元素转换操作信息
type BurnCardAction struct {
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发起操作的玩家
Card uint64 `json:"card" yaml:"card" xml:"card"` // Card 被操作的卡牌
Paid enum.ElementType `json:"paid" yaml:"paid" xml:"paid"` // Paid 支付元素转换的费用
}
// ReRollAction 玩家的重掷元素骰子操作信息
type ReRollAction struct {
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发起操作的玩家
Dropped map[enum.ElementType]uint `json:"dropped" yaml:"dropped" xml:"dropped"` // Dropped 被舍弃的元素骰子
}
// UseCardAction 玩家的使用卡牌操作信息
type UseCardAction struct {
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发起操作的玩家
Card uint64 `json:"card" yaml:"card" xml:"card"` // Card 玩家打出的卡牌
Paid map[enum.ElementType]uint `json:"paid" yaml:"paid" xml:"paid"` // Paid 玩家打出卡牌支付的费用
}
// SkipRoundAction 玩家的跳过回合操作信息
type SkipRoundAction struct {
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发起操作的玩家
}
// SwitchAction 玩家的切换前台角色操作信息
type SwitchAction struct {
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发起操作的玩家
Target uint64 `json:"target" yaml:"target" xml:"target"` // Target 切换到的目标角色
Paid map[enum.ElementType]uint `json:"paid" yaml:"paid" xml:"paid"` // Paid 玩家切换角色支付的费用
}
// ConcedeAction 玩家的弃权操作信息
type ConcedeAction struct {
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发起操作的玩家
}
// ActionMessage 玩家的操作信息
type ActionMessage struct {
Type enum.ActionType `json:"type" yaml:"type" xml:"type"` // Type 玩家的操作类型
Sender uint64 `json:"sender" yaml:"sender" xml:"sender"` // Sender 发送操作的玩家
Args interface{} `json:"args" yaml:"args" xml:"args"` // Args 玩家的操作信息
}
func exchangeMessage[target ActionMessageInterface](message ActionMessage, conditions ...enum.ActionType) (success bool, result target) {
typeCheck := false
for _, condition := range conditions {
if message.Type == condition {
typeCheck = true
break
}
}
if !typeCheck {
return false, result
} else {
result, success = message.Args.(target)
return success, result
}
}
// ToAttackMessage 将ActionMessage转换为AttackAction
func (a ActionMessage) ToAttackMessage() (success bool, message AttackAction) {
return exchangeMessage[AttackAction](a, enum.ActionNormalAttack, enum.ActionElementalSkill, enum.ActionElementalBurst)
}
// ToBurnCardMessage 将ActionMessage转换为BurnCardAction
func (a ActionMessage) ToBurnCardMessage() (success bool, message BurnCardAction) {
return exchangeMessage[BurnCardAction](a, enum.ActionBurnCard)
}
// ToUesCardMessage 将ActionMessage转换为UseCardAction
func (a ActionMessage) ToUesCardMessage() (success bool, message UseCardAction) {
return exchangeMessage[UseCardAction](a, enum.ActionUseCard)
}
// ToReRollMessage 将ActionMessage转换为ReRollAction
func (a ActionMessage) ToReRollMessage() (success bool, message ReRollAction) {
return exchangeMessage[ReRollAction](a, enum.ActionReRoll)
}
// ToSwitchMessage 将ActionMessage转换为SwitchAction
func (a ActionMessage) ToSwitchMessage() (success bool, message SwitchAction) {
return exchangeMessage[SwitchAction](a, enum.ActionSwitch)
}
// ToConcedeMessage 将ActionMessage转换为ConcedeAction
func (a ActionMessage) ToConcedeMessage() (success bool, message ConcedeAction) {
return exchangeMessage[ConcedeAction](a, enum.ActionConcede)
}
// ToSkipRoundMessage 将ActionMessage转换为SkipRoundAction
func (a ActionMessage) ToSkipRoundMessage() (success bool, message SkipRoundAction) {
return exchangeMessage[SkipRoundAction](a, enum.ActionSkipRound)
}