Skip to content

Commit 79b55c0

Browse files
author
Zbynek Novotny
committed
Added check for a valid game type specification when creating a new
match for a player. Also consolidated error types a little bit.
1 parent c63e1f8 commit 79b55c0

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

core/core.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,15 @@ func (c *Core) JoinMatch(mid MatchID, pid PlayerID, gt GameType) (MatchID, error
127127
if mid != InvalidMatchID {
128128
match, ok = c.matches[mid]
129129
if !ok {
130-
return InvalidMatchID, MatchNotFoundError{mid}
130+
return InvalidMatchID, InvalidArgumentError{"Match not found:" + MatchIDToString(mid)}
131131
}
132132

133133
match.Add(pid)
134134
} else {
135+
if gt == InvalidGameType {
136+
return InvalidMatchID, InvalidArgumentError{"Game type specification required if no match ID defined"}
137+
}
138+
135139
ids := PlayerIDs{pid}
136140
match = NewMatchWithPlayers(gt, ids)
137141
c.matches[match.ID] = match

core/errors.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ func (err IntegrityError) Error() string {
1212
return "Integrity error:" + err.Message
1313
}
1414

15-
// MatchNotFoundError indicates that a match
16-
// with the given ID could not be found
17-
type MatchNotFoundError struct {
18-
MID MatchID
15+
// InvalidArgumentError indicates an
16+
// invalid input to a function
17+
type InvalidArgumentError struct {
18+
Message string
1919
}
2020

21-
// Error converts the error object into
22-
// a string presentable to the user
23-
func (err MatchNotFoundError) Error() string {
24-
return "Match not found: " + MatchIDToString(err.MID)
21+
func (err InvalidArgumentError) Error() string {
22+
return "Invalid argument error: " + err.Message
2523
}

0 commit comments

Comments
 (0)