Commit dcc3fdf1 authored by Mikhail Babynichev's avatar Mikhail Babynichev 😊
Browse files

Upload

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Original line number Diff line number Diff line
stats.db
 No newline at end of file

LICENSE

0 → 100644
+674 −0

File added.

Preview size limit exceeded, changes collapsed.

football-cms-v3

0 → 100644
+14.8 MiB

File added.

No diff preview for this file type.

main.go

0 → 100644
+254 −0
Original line number Diff line number Diff line
package main

import (
    "log"
    "encoding/json"
    "strings"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "gopkg.in/macaron.v1"
    "strconv"
)
// Удары по воротам
// Удары в створ
// Фолы
// Угловые
// Офсайды
// Предупрждения
// Удаления

// Событие
// Гол
// Жёлтая карточка
// Красная карточка
// Удар по воротам
// Угловой
// Штрафной
// Опасный момент
// Замена
// Комментарий

type Match struct {
  gorm.Model
  Team1 string
  Team2 string
  Arr1 string `sql:"DEFAULT:'{\"shoot1\": 0,\"shoot2\": 0,\"fols\": 0,\"ugls\": 0,\"offsides\": 0,\"yc\": 0,\"rc\": 0}'"`
  Arr2 string `sql:"DEFAULT:'{\"shoot1\": 0,\"shoot2\": 0,\"fols\": 0,\"ugls\": 0,\"offsides\": 0,\"yc\": 0,\"rc\": 0}'"`
  Score1 uint `sql:"DEFAULT:0"`
  Score2 uint `sql:"DEFAULT:0"`
  Stadium string
  Active uint
}

type errord struct {
	Code string
}

type Match_events struct {
	Shoot1   int `json:"shoot1"`
	Shoot2   int `json:"shoot2"`
	Fols     int `json:"fols"`
	Ugls     int `json:"ugls"`
	Offsides int `json:"offsides"`
	Yc       int `json:"yc"`
	Rc       int `json:"rc"`
}


type Event struct {
	gorm.Model
	Match_id int
	Team int
	Event int
	Comment string `sql:"DEFAULT:''`
	Minute int
}

var db, err_db = gorm.Open("sqlite3", "stats.db")

func main() {
    m := macaron.Classic()
    m.Use(macaron.Renderer())
    m.Use(macaron.Static("static",
        macaron.StaticOptions{
            Prefix: "static",
            SkipLogging: true,
        }))
  	if err_db != nil {
    	panic("failed to connect database")
  	}
  	defer db.Close()

  	db.AutoMigrate(&Match{})
  	db.AutoMigrate(&Event{})

  	m.Get("/", handler_index) // / (GET) - основная страница, шаблон "active.tmpl"
    m.Get("/add", handler_add) // /add(GET) - страница для создания матча, шаблон "add.tmpl"
    m.Get("/error", handler_error) // /error(GET) - страница для ошибок, *разрабатывается*
    m.Get("/end", handler_end) // /end(GET) - мгновенно окончить матч, шаблон отсутствует
    m.Get("/medit", handler_edit_main) // /medit(GET) - редактор матча, шаблон "medit.tmpl"
    m.Get("/events", handler_events_get) // /events(GET) - редактор событий, шаблон "events.tmpl"

    m.Post("/edit", handler_edit) // /edit(POST) - обновляет события в /
    m.Post("/events", handler_events_post) // /events(POST) - добавляет события в /events(GET)

    m.Run("0.0.0.0", 8080)
    log.Print("Server started at *:8080")
}

func handler_index(ctx *macaron.Context) {
	var match Match
	var events_team1 Match_events
	var events_team2 Match_events
	db.First(&match, "Active = ?", 1)
	if match.Active != 0 {
		json.Unmarshal([]byte(match.Arr1), &events_team1)
		json.Unmarshal([]byte(match.Arr2), &events_team2)
		ctx.Data["Matchinfo"] = match
		ctx.Data["Team1"] = events_team1
		ctx.Data["Team2"] = events_team2
		ctx.HTML(200, "active")
	} else {
		ctx.Redirect("/add")
	}
}

func handler_error() string {
	return "Произошла какая-то ошибка!"
}

func handler_add(ctx *macaron.Context) {
	var match Match
	db.First(&match, "Active = ?", 1)
	if match.Active != 0 {
		ctx.Redirect("/error")
	} else {
		key := ctx.Req.Request.URL.Query()
		if len(key) < 3{
			ctx.HTML(200, "add")
		} else if key["team1"] == nil && key["team2"] == nil && key["stadium"] == nil {
			ctx.HTML(200, "add")
		} else {
			team1 := strings.Join(key["team1"], " ")
			team2 := strings.Join(key["team2"], " ")
			stadium := strings.Join(key["stadium"], " ")
			db.Create(&Match{Team1: team1, Team2: team2, Stadium: stadium, Active: 1})
			ctx.Redirect("/")
		}
	}
}

func handler_edit_main(ctx *macaron.Context) {
	var match Match
	db.First(&match, "Active = ?", 1)
	if match.Active != 0 {
		key := ctx.Req.Request.URL.Query()
		if len(key) < 5{
			ctx.Data["Matchinfo"] = match
			ctx.HTML(200, "medit")
		} else if key["team1"] == nil && key["team2"] == nil && key["stadium"] == nil && key["score1"] == nil && key["score2"] == nil {
			ctx.Data["Matchinfo"] = match
			ctx.HTML(200, "medit")
		} else {
			team1 := strings.Join(key["team1"], " ")
			team2 := strings.Join(key["team2"], " ")

			stadium := strings.Join(key["stadium"], " ")

			score1, _ := strconv.Atoi(strings.Join(key["score1"], " "))
			score2, _ := strconv.Atoi(strings.Join(key["score2"], " "))

			db.Model(&match).Update("Team1", team1)
			db.Model(&match).Update("Team2", team2)

			db.Model(&match).Update("Stadium", stadium)

			db.Model(&match).Update("Score1", score1)
			db.Model(&match).Update("Score2", score2)
			ctx.Redirect("/")
		}
	} else {
		ctx.Redirect("/error")
	}
}

func handler_edit(ctx *macaron.Context) {
	var match Match
	db.First(&match, "Active = ?", 1)
	if match.Active != 0 {
		r := ctx.Req.Request
		r.ParseForm()                     // Parses the request body
    	arr1 := r.Form.Get("arr1") // x will be "" if parameter is not set
    	arr2 := r.Form.Get("arr2")
    	if len(arr1) > 0 && len(arr2) > 0 {
			db.Model(&match).Update("Arr1", arr1)
			db.Model(&match).Update("Arr2", arr2)
			ctx.Redirect("/")
		} else {
			ctx.Redirect("/error")
		}
	} else {
		ctx.Redirect("/error")	
	}
}

func handler_end(ctx *macaron.Context) {
	var match Match
	db.First(&match, "Active = ?", 1)
	if match.Active != 0 {
		db.Model(&match).Update("Active", 0)
		ctx.Redirect("/")
	} else {
		ctx.Redirect("/error")
	}
}


func handler_events_get(ctx *macaron.Context) {
	var match Match
	db.First(&match, "Active = ?", 1)
	if match.Active != 0 {
		events := []Event {}
		db.Find(&events, "Match_id = ?", match.ID)
		ctx.Data["Events"] = events
		ctx.Data["Matchinfo"] = match
		ctx.HTML(200, "events")
	} else {
		ctx.Redirect("/error")
	}
}


func handler_events_post(ctx *macaron.Context) {
	var match Match
	db.First(&match, "Active = ?", 1)
	if match.Active != 0 {
		r := ctx.Req.Request
		r.ParseForm()

		Match_idc := r.Form.Get("match_id")
		Teamc := r.Form.Get("team")
		Eventc := r.Form.Get("event")
		Minutec := r.Form.Get("minute")

		if len(Match_idc)>0 && len(Teamc)>0 && len(Eventc)>0 && len(Minutec)>0 {
			Comment := r.Form.Get("comment")
			Midd, _ := strconv.Atoi(Match_idc)
			Teamd, _ := strconv.Atoi(Teamc)
			Eventd, _ := strconv.Atoi(Eventc)
			Minuted, _ := strconv.Atoi(Minutec)
			if len(Comment)>0 {
				db.Create(&Event{Match_id: Midd, Team: Teamd, Event: Eventd, Comment: Comment, Minute: Minuted})
				ctx.Redirect("/events")
			} else {
				db.Create(&Event{Match_id: Midd, Team: Teamd, Event: Eventd, Minute: Minuted})
				ctx.Redirect("/events")
			}		
		} else {
			ctx.Redirect("/error")
		}
	} else {
		ctx.Redirect("/error")
	}

}
 No newline at end of file
+364 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading