master
Raw Download raw file
  1package main
  2
  3import (
  4	"bytes"
  5	"encoding/json"
  6	"go.uber.org/zap"
  7)
  8
  9const (
 10	GENIO_SETNAME      = "error_set_username"
 11	GENIO_NAMETAKEN    = "This username is already taken."
 12	GENIO_QUEUE        = "queue_update"
 13	GENIO_CHAT         = "chat_message"
 14	GENIO_PRESTART     = "pre_game_start"
 15	GENIO_START        = "game_start"
 16	GENIO_UPDATE       = "game_update"
 17	GENIO_WIN          = "game_won"
 18	GENIO_LOSE         = "game_lost"
 19	GENIO_OVER         = "game_over"
 20	GENIO_EMPTY        = -1
 21	GENIO_MOUNTAIN     = -2
 22	GENIO_FOG          = -3
 23	GENIO_FOG_OBSTACLE = -4
 24)
 25
 26type Update struct {
 27	Scores      []PlayerScore `json:"scores"`
 28	Turn        int           `json:"turn"`
 29	AttackIndex int           `json:"attackIndex"`
 30	Generals    []int         `json:"generals"`
 31	MapDiff     []int         `json:"map_diff"`
 32	CitiesDiff  []int         `json:"cities_diff"`
 33}
 34
 35type PlayerScore struct {
 36	PlayerIndex int  `json:"i"`
 37	Dead        bool `json:"dead"`
 38	Army        int  `json:"total"`
 39	Land        int  `json:"tiles"`
 40}
 41
 42type ChatMessage struct {
 43	Text        string `json:"text"`
 44	Username    string `json:"username"`
 45	Prefix      string `json:"prefix"`
 46	PlayerIndex int    `json:"playerIndex"`
 47}
 48
 49type Game struct {
 50	UserIndex  int      `json:"playerIndex"` // YOU!
 51	ReplayID   string   `json:"replay_id"`
 52	ChatID     string   `json:"chat_room"`
 53	TeamChatID string   `json:"team_chat_room"`
 54	Usernames  []string `json:"usernames"`
 55	Teams      []int    `json:"teams"`
 56	Map
 57}
 58
 59func DecodeEvent(p []byte, updates chan<- Update, games chan<- Game) (err error) {
 60	var (
 61		m []json.RawMessage
 62		t string
 63	)
 64	dec := json.NewDecoder(bytes.NewBuffer(p))
 65	err = dec.Decode(&m)
 66	if err != nil {
 67		l.Error(zap.Error(err))
 68		return err
 69	}
 70	err = json.Unmarshal(m[0], &t)
 71	if err != nil {
 72		l.Error(zap.String("type", t), zap.Error(err))
 73		return err
 74	}
 75
 76	switch t {
 77
 78	case GENIO_SETNAME:
 79		var msg string
 80
 81		err = json.Unmarshal(m[1], &msg)
 82		if err != nil {
 83			l.Error(zap.String("type", t), zap.Error(err))
 84			return err
 85		}
 86		if msg == GENIO_NAMETAKEN {
 87			// This message doesn't really make sense.
 88			// You get this "error" even if you are the owner of the name
 89			l.Info("Set Name Failed (expected) ", zap.String("msg", msg))
 90		} else if msg == "" {
 91			l.Info("Set Name Success")
 92		}
 93
 94	case GENIO_CHAT:
 95		var room string
 96		var chat ChatMessage
 97		err = json.Unmarshal(m[1], &room)
 98		if err != nil {
 99			l.Error(zap.String("type", t), zap.Error(err))
100			return err
101		}
102		err = json.Unmarshal(m[2], &chat)
103		if err != nil {
104			l.Error(zap.String("type", t), zap.Error(err))
105			return err
106		}
107		l.Infof("Chat - %s: %s", chat.Username, chat.Text)
108
109	case GENIO_QUEUE:
110		// TODO if this is important someday
111		l.Info("Queue Update ", string(p))
112
113	case GENIO_PRESTART:
114		l.Info("Game Starting")
115
116	case GENIO_START:
117		var game Game
118		err = json.Unmarshal(m[1], &game)
119		if err != nil {
120			l.Error(zap.String("type", t), zap.Error(err))
121			return err
122		}
123		games <- game
124		l.Debug("Game sent to AI", zap.Any("game", game))
125
126	case GENIO_UPDATE:
127		var u Update
128		err = json.Unmarshal(m[1], &u)
129		if err != nil {
130			l.Error(zap.String("type", t), zap.Error(err))
131			return err
132		}
133		updates <- u
134		l.Debug("Update sent to AI ", zap.Any("update", u))
135
136	case GENIO_WIN:
137		l.Info("Game Won")
138
139	case GENIO_LOSE:
140		l.Info("Game Lost")
141
142	case GENIO_OVER:
143		l.Info("Game Over")
144
145	default:
146		l.Error("Unhandled Event", zap.String("type", t), zap.String("event", string(p)))
147	}
148	return err
149}