Пример #1
0
func (this *NewwarHandler) handle(text []string) (result string, err error) {
	//fmt.Printf("len text:%d\n", len(text))
	if len(text) < 3 {
		err = errors.New("i need more info\n" + this.getHelp())
		return
	}
	cout, err := strconv.Atoi(text[1])
	if err != nil || cout < 0 {
		err = errors.New("arg2 must be number\n" + this.getHelp())
		return
	}
	groupname := mainhandler.getGroupName()
	if groupname == "" {
		err = errors.New("group not found groupid:" + mainhandler.rec.Group_id)
		return
	}
	enemyname := strings.Join(text[2:len(text)], " ")
	id, err := models.AddWarData(groupname, enemyname, cout)
	if err != nil {
		fmt.Println(err.Error())
		err = errors.New("server error")
	} else {
		content, err := models.GetWarData(id)
		if err == nil {
			fmt.Printf("now time:%v\n", time.Now())
			fmt.Printf("default time:%v\n", content.Begintime)
			result = printWarData(content)
		}
	}

	return

}
Пример #2
0
func (this *TimerHandler) handle(text []string) (result string, err error) {

	if len(text) < 2 {
		err = errors.New("i need more info\n" + this.getHelp())
		return
	}
	num, err := strconv.Atoi(text[1])
	if err != nil || num < 0 {
		err = errors.New("arg2 must be number\n" + this.getHelp())
		return
	}
	content, err := models.GetWarData(num)
	if err == nil {
		result = fmt.Sprintf("War #%d \n %s VS %s \n %d vs %d \n  %d hours till war start!", content.Id, content.TeamA, content.TeamB, len(content.Battles), len(content.Battles), int(content.Begintime.Sub(time.Now()).Hours()))
	}
	return

}
Пример #3
0
func (this *EditwarHandler) handle(text []string) (result string, err error) {
	groupname := mainhandler.getGroupName()
	if groupname == "" {
		err = errors.New("group not found groupid:" + mainhandler.rec.Group_id)
		return
	}
	if len(text) < 3 {
		err = errors.New("i need more info\n" + this.getHelp())
		return
	}
	num1, err := strconv.Atoi(text[1])
	if err != nil || num1 < 0 {
		err = errors.New("arg2 must be number\n" + this.getHelp())
		return
	}
	num2, err := strconv.Atoi(text[2])
	isTime := strings.HasSuffix(text[2], "am") || strings.HasSuffix(text[2], "pm")
	if (err != nil || num2 < 0) && !isTime {
		err = errors.New("arg3 must be number or time(endwith am/pm)\n" + this.getHelp())
		return
	}

	if err == nil {
		enemyname := strings.Join(text[3:len(text)], " ")
		battles := make([]models.Battle, num2)
		battlep := &models.Battle{}
		battlep.Init()
		for index := range battles {
			battles[index] = *battlep
		}
		err := models.UpdateWarData(num1, bson.M{"$set": bson.M{"teamb": enemyname, "battles": battles}})
		if err == nil {
			content, err := models.GetWarData(num1)
			if err != nil {
				fmt.Println(err.Error())
				err = errors.New("server error")
			} else if !content.IsEnable {
				result = "no war being"
			} else {
				result = fmt.Sprintf("War #%d Edited \n %s VS %s \n %d vs %d \n War starts %s", content.Id, content.TeamA, content.TeamB, len(content.Battles), len(content.Battles), content.Begintime.Format("3:04PM MST 1/2/2006"))
			}

		}
	} else if isTime {
		now := time.Now()
		var h int
		if strings.HasSuffix(text[2], "am") {
			fmt.Printf("trim time:%s\n", strings.Trim(text[2], "am"))
			h, err = strconv.Atoi(strings.Trim(text[2], "am"))
			if err != nil || h < 0 {
				err = errors.New("wrong time format")
			} else {
				mi := 0
				if h > 12 {
					mi = int(math.Mod(float64(h), 100))
					h = int(h / 100)
				}
				y, m, d := now.Date()
				if now.Hour()*60+now.Minute() > h*60+mi { //if befor now so will be tmw
					d++
				}
				newtime := time.Date(y, m, d, h, mi, 0, 0, time.Local)
				err := models.UpdateWarData(num1, bson.M{"$set": bson.M{"begintime": newtime}})
				if err == nil {
					content, err := models.GetWarData(num1)
					if err != nil {
						fmt.Println(err.Error())
						err = errors.New("server error")
					} else if !content.IsEnable {
						result = "no war being"
					} else {
						result = fmt.Sprintf("War #%d Edited \n %s VS %s \n %d vs %d \n War starts %s", content.Id, content.TeamA, content.TeamB, len(content.Battles), len(content.Battles), content.Begintime.Format("3:04PM MST 1/2/2006"))
					}
				}
			}
		} else {

			h, err = strconv.Atoi(strings.Trim(text[2], "pm"))
			fmt.Printf("trim time:%d\n", h)
			if err != nil || h < 0 {
				err = errors.New("wrong time format")
			} else {
				mi := 0
				if h > 12 {
					mi = int(math.Mod(float64(h), 100))
					h = int(h / 100)
				}
				h = h + 12
				y, m, d := now.Date()
				if now.Hour()*60+now.Minute() > h*60+mi { //if befor now so will be tmw
					d++
				}
				fmt.Printf("trim time mi:%d\n", mi)
				newtime := time.Date(y, m, d, h, mi, 0, 0, time.Local)
				err := models.UpdateWarData(num1, bson.M{"$set": bson.M{"begintime": newtime}})
				if err == nil {
					content, err := models.GetWarData(num1)
					if err != nil {
						fmt.Println(err.Error())
						err = errors.New("server error")
					} else if !content.IsEnable {
						result = "no war being"
					} else {
						result = fmt.Sprintf("War #%d Edited \n %s VS %s \n %d vs %d \n War starts %s", content.Id, content.TeamA, content.TeamB, len(content.Battles), len(content.Battles), content.Begintime.Format("3:04PM MST 1/2/2006"))
					}
				}
			}
		}

	} else {
		err = errors.New("wrong arg \n" + this.getHelp())
	}

	return

}