Skip to content

Commit 010a55f

Browse files
committed
[refactoring] Make modify event less complex
1 parent de9e469 commit 010a55f

File tree

8 files changed

+80
-106
lines changed

8 files changed

+80
-106
lines changed

internal/app/controller/engine.go

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -77,54 +77,36 @@ func processModify(m *EventModifyValue) error {
7777
return errors.Errorf("Unknown team: %v", m.ForTeam)
7878
}
7979
teamState := refBox.State.TeamState[m.ForTeam]
80-
switch m.Type {
81-
case ModifyGoalie:
82-
if m.ValueInt != nil {
83-
teamState.Goalie = *m.ValueInt
80+
if m.Goals != nil {
81+
teamState.Goals = *m.Goals
82+
} else if m.Goalie != nil {
83+
teamState.Goalie = *m.Goalie
84+
} else if m.YellowCards != nil {
85+
teamState.YellowCards = *m.YellowCards
86+
} else if m.RedCards != nil {
87+
teamState.RedCards = *m.RedCards
88+
} else if m.TimeoutsLeft != nil {
89+
teamState.TimeoutsLeft = *m.TimeoutsLeft
90+
} else if m.TeamName != nil {
91+
teamState.Name = *m.TeamName
92+
} else if m.OnPositiveHalf != nil {
93+
teamState.OnPositiveHalf = *m.OnPositiveHalf
94+
refBox.State.TeamState[m.ForTeam.Opposite()].OnPositiveHalf = !*m.OnPositiveHalf
95+
} else if m.YellowCardTime != nil {
96+
cardId := m.YellowCardTime.CardID
97+
if cardId < 0 || cardId >= len(teamState.YellowCardTimes) {
98+
return errors.Errorf("Invalid card index: %v", cardId)
8499
}
85-
case ModifyGoals:
86-
if m.ValueInt != nil {
87-
teamState.Goals = *m.ValueInt
100+
if duration, err := strToDuration(m.YellowCardTime.Duration); err == nil {
101+
teamState.YellowCardTimes[cardId] = duration
88102
}
89-
case ModifyOnPositiveHalf:
90-
if m.ValueBool != nil {
91-
teamState.OnPositiveHalf = *m.ValueBool
92-
refBox.State.TeamState[m.ForTeam.Opposite()].OnPositiveHalf = !*m.ValueBool
93-
}
94-
case ModifyRedCards:
95-
if m.ValueInt != nil {
96-
teamState.RedCards = *m.ValueInt
97-
}
98-
case ModifyYellowCards:
99-
if m.ValueInt != nil {
100-
teamState.YellowCards = *m.ValueInt
101-
}
102-
case ModifyTeamName:
103-
if m.ValueStr != nil {
104-
teamState.Name = *m.ValueStr
105-
}
106-
case ModifyTimeoutsLeft:
107-
if m.ValueInt != nil {
108-
teamState.TimeoutsLeft = *m.ValueInt
109-
}
110-
case ModifyTimeoutTimeLeft:
111-
if m.ValueStr != nil {
112-
if duration, err := strToDuration(*m.ValueStr); err == nil {
113-
teamState.TimeoutTimeLeft = duration
114-
} else {
115-
return err
116-
}
117-
}
118-
case ModifyYellowCardTime:
119-
if m.ValueStr != nil && m.ValueInt != nil {
120-
if *m.ValueInt < 0 || *m.ValueInt >= len(teamState.YellowCardTimes) {
121-
return errors.Errorf("Invalid card index: %v", *m.ValueInt)
122-
}
123-
if duration, err := strToDuration(*m.ValueStr); err == nil {
124-
teamState.YellowCardTimes[*m.ValueInt] = duration
125-
}
103+
} else if m.TimeoutTimeLeft != nil {
104+
if duration, err := strToDuration(*m.TimeoutTimeLeft); err == nil {
105+
teamState.TimeoutTimeLeft = duration
106+
} else {
107+
return err
126108
}
127-
default:
109+
} else {
128110
return errors.Errorf("Unknown modify: %v", m)
129111
}
130112
log.Printf("Processed modification %v", m)

internal/app/controller/events.go

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,6 @@ const (
5555
CommandGoal RefCommand = "goal"
5656
)
5757

58-
// ModifyType is something to be modified
59-
type ModifyType string
60-
61-
const (
62-
// ModifyGoals goals
63-
ModifyGoals ModifyType = "goals"
64-
// ModifyGoalie goalie
65-
ModifyGoalie ModifyType = "goalie"
66-
// ModifyYellowCards yellow cards
67-
ModifyYellowCards ModifyType = "yellowCards"
68-
// ModifyYellowCardTime yellow card time
69-
ModifyYellowCardTime ModifyType = "yellowCardTime"
70-
// ModifyRedCards red cards
71-
ModifyRedCards ModifyType = "redCards"
72-
// ModifyTimeoutsLeft number of timeouts left
73-
ModifyTimeoutsLeft ModifyType = "timeoutsLeft"
74-
// ModifyTimeoutTimeLeft timeout time left
75-
ModifyTimeoutTimeLeft ModifyType = "timeoutTimeLeft"
76-
// ModifyOnPositiveHalf on positive half?
77-
ModifyOnPositiveHalf ModifyType = "onPositiveHalf"
78-
// ModifyTeamName name of the team
79-
ModifyTeamName ModifyType = "teamName"
80-
)
81-
8258
// StageOperation to apply on the current stage
8359
type StageOperation string
8460

@@ -128,31 +104,57 @@ func (c EventCommand) String() string {
128104
return fmt.Sprintf("%v for %v", c.Type, *c.ForTeam)
129105
}
130106

107+
// EventModifyCardTime holds the duration for a certain yellow card duration
108+
type EventModifyCardTime struct {
109+
CardID int `json:"cardId"`
110+
Duration string `json:"duration"`
111+
}
112+
131113
// EventModifyValue is an event that can be applied
132114
type EventModifyValue struct {
133-
Type ModifyType `json:"modifyType"`
134-
ForTeam Team `json:"forTeam"`
135-
ValueStr *string `json:"valueStr"`
136-
ValueInt *int `json:"valueInt"`
137-
ValueBool *bool `json:"valueBool"`
115+
ForTeam Team `json:"forTeam"`
116+
117+
Goals *int `json:"goals"`
118+
Goalie *int `json:"goalie"`
119+
YellowCards *int `json:"yellowCards"`
120+
YellowCardTime *EventModifyCardTime `json:"yellowCardTime"`
121+
RedCards *int `json:"redCards"`
122+
TimeoutsLeft *int `json:"timeoutsLeft"`
123+
TimeoutTimeLeft *string `json:"timeoutTimeLeft"`
124+
OnPositiveHalf *bool `json:"onPositiveHalf"`
125+
TeamName *string `json:"teamName"`
138126
}
139127

140128
func (m EventModifyValue) String() string {
141-
str := fmt.Sprintf("%v for %v", m.Type, m.ForTeam)
142-
if m.ValueBool != nil {
143-
if *m.ValueBool {
144-
str += " bool:true"
145-
} else {
146-
str += " bool:false"
147-
}
129+
str := fmt.Sprintf("modify for %v:", m.ForTeam)
130+
if m.Goals != nil {
131+
return fmt.Sprintf("%v Goals=%v", str, *m.Goals)
132+
}
133+
if m.Goalie != nil {
134+
return fmt.Sprintf("%v Goalie=%v", str, *m.Goalie)
135+
}
136+
if m.YellowCards != nil {
137+
return fmt.Sprintf("%v YellowCards=%v", str, *m.YellowCards)
138+
}
139+
if m.YellowCardTime != nil {
140+
return fmt.Sprintf("%v YellowCardTime=%v", str, *m.YellowCardTime)
141+
}
142+
if m.RedCards != nil {
143+
return fmt.Sprintf("%v RedCards=%v", str, *m.RedCards)
144+
}
145+
if m.TimeoutsLeft != nil {
146+
return fmt.Sprintf("%v TimeoutsLeft=%v", str, *m.TimeoutsLeft)
147+
}
148+
if m.TimeoutTimeLeft != nil {
149+
return fmt.Sprintf("%v TimeoutTimeLeft=%v", str, *m.TimeoutTimeLeft)
148150
}
149-
if m.ValueStr != nil {
150-
str += " str:" + *m.ValueStr
151+
if m.OnPositiveHalf != nil {
152+
return fmt.Sprintf("%v OnPositiveHalf=%v", str, *m.OnPositiveHalf)
151153
}
152-
if m.ValueInt != nil {
153-
str += " int:" + string(*m.ValueInt)
154+
if m.TeamName != nil {
155+
return fmt.Sprintf("%v TeamName=%v", str, *m.TeamName)
154156
}
155-
return str
157+
return fmt.Sprintf("%v undefined", str)
156158
}
157159

158160
// EventTrigger is an event that can be applied

ui/src/components/team/TeamCards.vue

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,23 @@
5858
this.$socket.sendObj({
5959
'modify': {
6060
'forTeam': this.teamColor,
61-
'modifyType': 'yellowCards',
62-
'valueInt': Number(v)
61+
'yellowCards': Number(v)
6362
}
6463
})
6564
},
6665
updateRedCards: function (v) {
6766
this.$socket.sendObj({
6867
'modify': {
6968
'forTeam': this.teamColor,
70-
'modifyType': 'redCards',
71-
'valueInt': Number(v)
69+
'redCards': Number(v)
7270
}
7371
})
7472
},
7573
updateCardTime: function (v, index) {
7674
this.$socket.sendObj({
7775
'modify': {
7876
'forTeam': this.teamColor,
79-
'modifyType': 'yellowCardTime',
80-
'valueInt': index,
81-
'valueStr': v
77+
'yellowCardTime': {'cardID': index, 'duration': v}
8278
}
8379
})
8480
},

ui/src/components/team/TeamGoalie.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="team-goalie">
33
<EditableLabelNumber
4-
label="Goalie-ID: "
4+
label="Goalie: "
55
title="The goalie number"
66
:value="goalie"
77
:callback="updateGoalie"
@@ -25,8 +25,7 @@
2525
this.$socket.sendObj({
2626
'modify': {
2727
'forTeam': this.teamColor,
28-
'modifyType': 'goalie',
29-
'valueInt': Number(v)
28+
'goalie': Number(v)
3029
}
3130
})
3231
},

ui/src/components/team/TeamHalf.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
this.$socket.sendObj({
1919
'modify': {
2020
'forTeam': this.teamColor,
21-
'modifyType': 'onPositiveHalf',
22-
'valueBool': !currentValue
21+
'onPositiveHalf': !currentValue
2322
}
2423
})
2524
},

ui/src/components/team/TeamName.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@
5959
this.$socket.sendObj({
6060
'modify': {
6161
'forTeam': this.teamColor,
62-
'modifyType': 'teamName',
63-
'valueStr': v
62+
'teamName': v
6463
}
6564
})
6665
}

ui/src/components/team/TeamScore.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
this.$socket.sendObj({
2626
'modify': {
2727
'forTeam': this.teamColor,
28-
'modifyType': 'goals',
29-
'valueInt': Number(v)
28+
'goals': Number(v)
3029
}
3130
})
3231
}

ui/src/components/team/TeamTimeouts.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,15 @@
3030
this.$socket.sendObj({
3131
'modify': {
3232
'forTeam': this.teamColor,
33-
'modifyType': 'timeoutsLeft',
34-
'valueInt': Number(v)
33+
'timeoutsLeft': Number(v)
3534
}
3635
})
3736
},
3837
updateTimeoutTimeLeft: function (v) {
3938
this.$socket.sendObj({
4039
'modify': {
4140
'forTeam': this.teamColor,
42-
'modifyType': 'timeoutTimeLeft',
43-
'valueStr': v
41+
'timeoutTimeLeft': v
4442
}
4543
})
4644
},

0 commit comments

Comments
 (0)