Skip to content

Commit

Permalink
ogamed: add jump-gate endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed May 11, 2020
1 parent 32b7c2b commit ab9d419
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/ogamed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func start(c *cli.Context) error {
e.POST("/bot/planets/:planetID/send-fleet", ogame.SendFleetHandler)
e.POST("/bot/planets/:planetID/send-ipm", ogame.SendIPMHandler)
e.GET("/bot/moons/:moonID/phalanx/:galaxy/:system/:position", ogame.PhalanxHandler)
e.GET("/bot/moons/:moonID/jump-gate", ogame.JumpGateHandler)
e.GET("/game/allianceInfo.php", ogame.GetAlliancePageContentHandler) // Example: //game/allianceInfo.php?allianceId=500127

// Get/Post Page Content
Expand Down
42 changes: 42 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,3 +1169,45 @@ func PhalanxHandler(c echo.Context) error {
}
return c.JSON(http.StatusOK, SuccessResp(fleets))
}

// JumpGateHandler ...
func JumpGateHandler(c echo.Context) error {
bot := c.Get("bot").(*OGame)
if err := c.Request().ParseForm(); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResp(400, "invalid form"))
}
moonOriginID, err := strconv.ParseInt(c.Param("moonID"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResp(400, "invalid origin moon id"))
}
moonDestinationID, err := strconv.ParseInt(c.Request().PostFormValue("moonDestination"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResp(400, "invalid destination moon id"))
}
var ships ShipsInfos
for key, values := range c.Request().PostForm {
switch key {
case "ships":
for _, s := range values {
a := strings.Split(s, ",")
shipID, err := strconv.ParseInt(a[0], 10, 64)
if err != nil || !IsShipID(shipID) {
return c.JSON(http.StatusBadRequest, ErrorResp(400, "invalid ship id "+a[0]))
}
nbr, err := strconv.ParseInt(a[1], 10, 64)
if err != nil || nbr < 0 {
return c.JSON(http.StatusBadRequest, ErrorResp(400, "invalid nbr "+a[1]))
}
ships.Set(ID(shipID), nbr)
}
}
}
success, rechargeCountdown, err := bot.JumpGate(MoonID(moonOriginID), MoonID(moonDestinationID), ships)
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResp(400, err.Error()))
}
return c.JSON(http.StatusOK, SuccessResp(map[string]interface{}{
"success": success,
"rechargeCountdown": rechargeCountdown,
}))
}

0 comments on commit ab9d419

Please sign in to comment.