Пример #1
0
func (Global) GetConstant(so *wsevent.Client, args struct {
	Constant string `json:"constant"`
}) interface{} {

	output := simplejson.New()
	switch args.Constant {
	case "lobbySettingsList":
		output = models.LobbySettingsToJSON()
	default:
		return helpers.NewTPError("Unknown constant.", -1)
	}

	return chelpers.NewResponse(output)
}
Пример #2
0
func (Player) PlayerProfile(so *wsevent.Client, args struct {
	Steamid *string `json:"steamid"`
}) interface{} {

	steamid := *args.Steamid
	if steamid == "" {
		steamid = chelpers.GetSteamId(so.ID)
	}

	player, playErr := models.GetPlayerWithStats(steamid)

	if playErr != nil {
		return playErr
	}

	result := models.DecoratePlayerProfileJson(player)
	return chelpers.NewResponse(result)
}
Пример #3
0
func (Player) PlayerSettingsGet(so *wsevent.Client, args struct {
	Key *string `json:"key"`
}) interface{} {

	var err error
	player, _ := models.GetPlayerBySteamID(chelpers.GetSteamId(so.ID))

	var settings []models.PlayerSetting
	var setting models.PlayerSetting
	if *args.Key == "*" {
		settings, err = player.GetSettings()
	} else {
		setting, err = player.GetSetting(*args.Key)
		settings = append(settings, setting)
	}

	if err != nil {
		return helpers.NewTPErrorFromError(err)
	}

	result := models.DecoratePlayerSettingsJson(settings)
	return chelpers.NewResponse(result)
}
Пример #4
0
func (Lobby) LobbyCreate(so *wsevent.Client, args struct {
	Map         *string `json:"map"`
	Type        *string `json:"type" valid:"debug,6s,highlander,4v4,ultiduo,bball"`
	League      *string `json:"league" valid:"ugc,etf2l,esea,asiafortress,ozfortress"`
	Server      *string `json:"server"`
	RconPwd     *string `json:"rconpwd"`
	WhitelistID *string `json:"whitelistID"`
	Mumble      *bool   `json:"mumbleRequired"`

	Password            *string `json:"password" empty:"-"`
	SteamGroupWhitelist *string `json:"steamGroupWhitelist" empty:"-"`

	Requirements *struct {
		Classes map[string]Requirement `json:"classes,omitempty"`
		General Requirement            `json:"general,omitempty"`
	} `json:"requirements" empty:"-"`
}) interface{} {

	player := chelpers.GetPlayerFromSocket(so.ID)
	if banned, until := player.IsBannedWithTime(models.PlayerBanCreate); banned {
		str := fmt.Sprintf("You've been banned from creating lobbies till %s", until.Format(time.RFC822))
		return helpers.NewTPError(str, -1)
	}

	var steamGroup string
	if *args.SteamGroupWhitelist != "" && !rSteamGroup.MatchString(*args.SteamGroupWhitelist) {
		return helpers.NewTPError("Invalid Steam group URL", -1)
	} else if rSteamGroup.MatchString(*args.SteamGroupWhitelist) {
		steamGroup = rSteamGroup.FindStringSubmatch(*args.SteamGroupWhitelist)[1]
	}

	var playermap = map[string]models.LobbyType{
		"debug":      models.LobbyTypeDebug,
		"6s":         models.LobbyTypeSixes,
		"highlander": models.LobbyTypeHighlander,
		"ultiduo":    models.LobbyTypeUltiduo,
		"bball":      models.LobbyTypeBball,
		"4v4":        models.LobbyTypeFours,
	}

	lobbyType := playermap[*args.Type]

	var count int
	db.DB.Table("server_records").Where("host = ?", *args.Server).Count(&count)
	if count != 0 {
		return helpers.NewTPError("A lobby is already using this server.", -1)
	}

	randBytes := make([]byte, 6)
	rand.Read(randBytes)
	serverPwd := base64.URLEncoding.EncodeToString(randBytes)

	//TODO what if playermap[lobbytype] is nil?
	info := models.ServerRecord{
		Host:           *args.Server,
		RconPassword:   *args.RconPwd,
		ServerPassword: serverPwd}
	// err = models.VerifyInfo(info)
	// if err != nil {
	// 	bytes, _ := helpers.NewTPErrorFromError(err).Encode()
	// 	return string(bytes)
	// }

	lob := models.NewLobby(*args.Map, lobbyType, *args.League, info, *args.WhitelistID, *args.Mumble, steamGroup, *args.Password)
	lob.CreatedBySteamID = player.SteamID
	lob.RegionCode, lob.RegionName = chelpers.GetRegion(*args.Server)
	if (lob.RegionCode == "" || lob.RegionName == "") && config.Constants.GeoIP != "" {
		return helpers.NewTPError("Couldn't find region server.", 1)
	}
	lob.Save()

	err := lob.SetupServer()
	if err != nil { //lobby setup failed, delete lobby and corresponding server record
		qerr := db.DB.Where("id = ?", lob.ID).Delete(&models.Lobby{}).Error
		if qerr != nil {
			logrus.Warning(qerr.Error())
		}
		db.DB.Delete(&lob.ServerInfo)
		return helpers.NewTPErrorFromError(err)
	}

	lob.State = models.LobbyStateWaiting
	lob.Save()

	models.FumbleLobbyCreated(lob)

	if args.Requirements != nil {
		for class, requirement := range (*args.Requirements).Classes {
			if requirement.Restricted.Blu {
				err := newRequirement("blu", class, requirement, lob)
				if err != nil {
					return err
				}
			}
			if requirement.Restricted.Red {
				err := newRequirement("red", class, requirement, lob)
				if err != nil {
					return err
				}
			}
		}
		if args.Requirements.General.Hours != 0 || args.Requirements.General.Lobbies != 0 {
			general := &models.Requirement{
				LobbyID: lob.ID,
				Hours:   args.Requirements.General.Hours,
				Lobbies: args.Requirements.General.Lobbies,
				Slot:    -1,
			}
			general.Save()
		}
	}
	return chelpers.NewResponse(
		struct {
			ID uint `json:"id"`
		}{lob.ID})
}