Skip to content
This repository was archived by the owner on Aug 24, 2020. It is now read-only.

Commit

Permalink
notificacoes com dupla checagem antes do envio
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonata-menezes committed Jun 5, 2017
1 parent d2b1a44 commit dc94e57
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bot/bot_kartolafc.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func getNotificacao(s string) (notification.MessageNotification, error) {
regComando, _ := regexp.Compile("(/[\\w-_]+) ?\n(.*?)\n(.*?)\n(.*?)\n(.*?)$")
if regComando.MatchString(s) {
match := regComando.FindStringSubmatch(s)
return notification.MessageNotification{match[2], match[3], match[4], "", nil, match[5]}, nil
return notification.MessageNotification{match[2], match[3], match[4], "", nil, match[5], "notificacao"}, nil
}
log.Println("errou")
return notification.MessageNotification{}, errors.New("nao foi possivel parsear")
Expand Down
1 change: 1 addition & 0 deletions notification/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ type MessageNotification struct {
Badge string `json:"badge"`
Vibrate []int `json:"vibrate"`
Link string `json:"link"`
Scout string `json:"scout"`
}

2 changes: 1 addition & 1 deletion notification/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func requestEndpoint(ch chan requestBroadcast) {
log.Println(err)
} else {
s, _ := ioutil.ReadAll(res.Body)
log.Printf("vapid: %#v", string(s))
log.Printf("status code: %d body response: %#v", res.StatusCode, string(s))
res.Body.Close()
}
}
Expand Down
51 changes: 32 additions & 19 deletions update_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,70 @@ import (
"time"
"github.com/jhonata-menezes/kartolafc-backend/api"
"log"
"fmt"
)

var scoutsPreparados = map[string]string{"G":"Gol", "CV": "Cartão Vermelho",
"CA": "Cartão Amarelo", "GC": "Gol Contra", "DP": "Defesa de Penalti"}

var scoutsPontuacao = map[string]int{"G":8, "CV": -5, "CA": -2, "GC": -6, "DP": 7}

// notificações duplicadas sendos enviadas, mapear as notificações que foram enviadas para nao duplicar
var atletasNotificados = make(map[int]map[string]int, 1000)
var atletasNotificados = make(map[int]map[string]int, 200000)

func Run(channelMessage chan *notification.MessageNotification) {
time.Sleep(5 * time.Second)
CacheLocal := CachePontuados
CacheLocal := api.Pontuados{}
CopyStructs(CachePontuados, &CacheLocal)

// na inicialiazacao verifica os scouts que tem para nao passar scouts passado
for atletaId, p := range CachePontuados.Atletas {
for scout, v := range p.Scout {
atletasNotificados[atletaId] = map[string]int{scout: v}
}
}
for {
Pontuados := CachePontuados
for atletaId, p := range Pontuados.Atletas {
attCache := false
for atletaId, p := range CachePontuados.Atletas {
for scout, v := range p.Scout {
// apenas os scouts permitidos serão validos
if !(len(scoutsPreparados[scout]) > 0) {
continue
}

if CacheLocal.Atletas[atletaId].Scout[scout] >= 0 {
// se o valor do scout atual for maior do que esta em cache. entao houve atualizacao
if CacheLocal.Atletas[atletaId].Scout[scout] >= 0 && CacheLocal.Atletas[atletaId].Scout[scout] < v {
// scout existe
if CacheLocal.Atletas[atletaId].Scout[scout] != v {
log.Println("notificando por scout att", p.Apelido, scout, v)
dispatchScout(scout, Pontuados.Atletas[atletaId], channelMessage)
CacheLocal = Pontuados
}
} else {
log.Println("notificando por scout novo", p.Apelido, scout, v)
dispatchScout(scout, Pontuados.Atletas[atletaId], channelMessage)
CacheLocal = Pontuados
log.Println("notificando por scout att", p.Apelido, scout, v, CacheLocal.Atletas[atletaId].Scout[scout])
dispatchScout(scout, CachePontuados.Atletas[atletaId], channelMessage, atletaId, v)
attCache = true
}
}
}

if attCache {
CacheLocal = api.Pontuados{}
CopyStructs(CachePontuados, &CacheLocal)
attCache = false
}

for CacheStatus.StatusMercado != 2 {
time.Sleep(60 * time.Minute)
}
time.Sleep(10 * time.Second)
}
}

func dispatchScout(scout string, atleta api.Pontuado, ch chan *notification.MessageNotification) {
if len(scoutsPreparados[scout]) > 0 {
func dispatchScout(scout string, atleta api.Pontuado, ch chan *notification.MessageNotification, atletaId, valorScout int) {
if len(scoutsPreparados[scout]) > 0 && atletasNotificados[atletaId][scout] < valorScout {
log.Printf("notificando scout %#v %#v", scout, atleta)
// existe scout na lista preparada
m := notification.MessageNotification{}
m.Title = scoutsPreparados[scout]
m.Title = atleta.Apelido + " #" + scoutsPreparados[scout]
m.Link = "https://kartolafc.com.br"
m.Icon = atleta.Foto
m.Body = atleta.Apelido
m.Body = fmt.Sprintf("Pontuaçao %.2f", (float32(scoutsPontuacao[scout]) + CachePontuados.Atletas[atletaId].Pontuacao) )
m.Scout = scout
atletasNotificados[atletaId] = map[string]int{scout: valorScout}
ch <- &m
}
}
10 changes: 10 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package kartolafc
import (
"time"
"log"
"bytes"
"encoding/gob"
)

func TemJogoComPartidaRolando() bool {
Expand All @@ -21,3 +23,11 @@ func TemJogoComPartidaRolando() bool {
}
return false
}

func CopyStructs(from, to interface{}) {
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
enc.Encode(from)
dec.Decode(to)
}

0 comments on commit dc94e57

Please sign in to comment.