forked from rnixik/durak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
42 lines (36 loc) · 1.21 KB
/
events.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
package main
import (
"encoding/json"
"reflect"
)
const (
errorNeedOneMorePlayer = "need_one_more_player"
errorNumberOfPlayersExceededLimit = "number_of_players_exceeded_limit"
errorGameHasBeenAlreadyStarted = "game_has_been_already_started"
errorYouCanCreateOneRoomOnly = "you_can_create_one_room_only"
errorRoomDoesNotExist = "room_does_not_exist"
errorCantChangeStatusGameHasBeenStarted = "cant_change_status_game_has_been_started"
errorYouShouldBeOwner = "you_should_be_owner"
errorGameAlreadyDeleted = "game_already_deleted"
)
// JSONEvent represents a message to clients with some event.
type JSONEvent struct {
Name string `json:"name"`
Data interface{} `json:"data"`
}
// ClientCommandError contains info about error on client's command.
type ClientCommandError struct {
Message string `json:"message"`
}
func eventToJSON(e interface{}) ([]byte, error) {
name := getNameOfStruct(e)
jsonEvent := JSONEvent{Name: name, Data: e}
return json.Marshal(jsonEvent)
}
func getNameOfStruct(s interface{}) string {
t := reflect.TypeOf(s)
if t.Kind() == reflect.Ptr {
return t.Elem().Name()
}
return t.Name()
}