func (s *AuthTest) TestAuthenticateExcistingUser() { entities.Save(&entities.Planet{ Name: "GOP6720", Position: &vec2d.Vector{2, 2}, }) entities.Save(&entities.Player{ Username: "******", RaceID: 1, TwitterID: "gop", HomePlanet: "planet.GOP6720", ScreenSize: []uint64{1, 1}, ScreenPosition: &vec2d.Vector{2, 2}, }) players_before, err := redis.Strings(s.conn.Do("KEYS", "player.*")) before := len(players_before) assert.Nil(s.T(), err) s.assertSend(&user) s.assertReceive("server_params") s.assertReceive("login_success") players_after, err := redis.Strings(s.conn.Do("KEYS", "player.*")) after := len(players_after) assert.Nil(s.T(), err) assert.Equal(s.T(), before, after) }
// scopeOfView is not finished yet but the purpose of the function is // to call calculateCanvasSize and give the player the information // contained in the given borders. func scopeOfView(request *Request) error { response := response.NewScopeOfView(request.Position, request.Resolution) request.Client.Player.ScreenPosition = request.Position go entities.Save(request.Client.Player) clients.Send(request.Client.Player, response) request.Client.MoveToAreas(response.Areas()) return nil }
// Registration process is quite simple: // // - Gather all twitter friends. // - Create a new sun with GenerateSun. // - Choose home planet from the newly created solar sysitem. // - Create a reccord of the new player and start comunication. func register(setupData *entities.SetupData, nickname, twitterId string, twitterApi *anaconda.TwitterApi) *entities.Player { friendsSuns := fetchFriendsSuns(nickname, twitterApi) sun := entities.GenerateSun(nickname, friendsSuns, setupData) planets, homePlanet := entities.GeneratePlanets(nickname, sun) player := entities.CreatePlayer(nickname, twitterId, homePlanet, setupData) for _, planet := range planets { entities.Save(planet) clients.Broadcast(planet) } entities.Save(player) entities.Save(sun) clients.Broadcast(sun) leaderBoard.Add(&leaderboard.Player{ Username: player.Username, RaceId: player.RaceID, HomePlanet: homePlanet.Name, Planets: 1, }) return player }
func (suite *ResponseTestSuite) SetupTest() { suite.conn = db.Pool.Get() suite.conn.Do("FLUSHDB") entities.Save(&planet1) entities.Save(&planet2) entities.Save(&planet3) entities.Save(&gophie) entities.Save(&panda) suite.request = new(Request) suite.request.Command = "start_mission" suite.request.StartPlanets = []string{"planet.GOP6720"} suite.request.EndPlanet = "planet.PAN6720" suite.request.Position = vec2d.New(2.0, 4.0) suite.request.Resolution = []uint64{1920, 1080} suite.request.Fleet = 32 suite.request.Username = "******" suite.request.TwitterID = "gophie92" suite.request.Race = 4 suite.request.SunTextureId = 0 suite.request.Client = &client suite.request.Type = "Spy" }
func prepareMission(startPlanet string, endPlanet *entities.Planet, request *Request) (*entities.Mission, error) { sourceEntity, err := entities.Get(startPlanet) if err != nil { return nil, err } source := sourceEntity.(*entities.Planet) if source.Owner != request.Client.Player.Username { return nil, errors.New("The mission owner does not own the start planet.") } if startPlanet == request.EndPlanet { return nil, errors.New("Start and end planet are the same.") } mission := request.Client.Player.StartMission( source, endPlanet, request.Path, request.Fleet, request.Type, ) if mission.ShipCount == 0 { return nil, errors.New("Not enough pilots on source planet!") } entities.Save(source) go StartMissionary(mission) entities.Save(mission) clients.Broadcast(mission) clients.Broadcast(source) return mission, nil }
func startExcessMission(mission *entities.Mission, homePlanet *entities.Planet, ships int32) { newTargetKey := fmt.Sprintf("planet.%s", mission.Source.Name) newTargetEntity, err := entities.Get(newTargetKey) if err != nil { log.Print("Error in newTarget planet fetch: ", err.Error()) return } playerEntity, err := entities.Get(fmt.Sprintf("player.%s", mission.Player)) player := playerEntity.(*entities.Player) excessMission := player.StartMission(homePlanet, newTargetEntity.(*entities.Planet), []*vec2d.Vector{}, 100, "Attack") excessMission.ShipCount = ships go StartMissionary(excessMission) entities.Save(excessMission) clients.Broadcast(excessMission) }
// StartMissionary is used when a call to initiate a new mission is rescived. // 1. When the delay ends the thread ends the mission calling EndMission // 2. The end of the mission is bradcasted to all clients and the mission entry is erased from the DB. func StartMissionary(mission *entities.Mission) { var ( err error excessShips int32 ownerHasChanged bool foundStartPoint bool player *entities.Player stateChange *response.StateChange target *entities.Planet timeSlept time.Duration ) initialTimeSlept := time.Duration(time.Now().UnixNano()/1e6 - mission.StartTime) if initialTimeSlept > 0 { timeSlept = initialTimeSlept } else { foundStartPoint = true } entities.Save(mission) targetKey := fmt.Sprintf("planet.%s", mission.Target.Name) for _, transferPoint := range mission.TransferPoints() { if !foundStartPoint { if initialTimeSlept > transferPoint.TravelTime { initialTimeSlept -= transferPoint.TravelTime mission.ChangeAreaSet(transferPoint.CoordinateAxis, transferPoint.Direction) continue } else { foundStartPoint = true } } timeToSleep := transferPoint.TravelTime timeSlept += timeToSleep time.Sleep(timeToSleep * time.Millisecond) mission.ChangeAreaSet(transferPoint.CoordinateAxis, transferPoint.Direction) clients.Broadcast(mission) } time.Sleep((mission.TravelTime - timeSlept) * time.Millisecond) target, stateChange, err = fetchMissionTarget(targetKey) if err != nil { log.Print("fetchMissionTarget fail: ", err.Error()) return } ownerBeforeMission := target.Owner if ownerBeforeMission == "" { player = nil } else { playerEntity, pErr := entities.Get(fmt.Sprintf("player.%s", ownerBeforeMission)) if pErr != nil { log.Println("Error in target planet owner fetch:", pErr.Error()) return } player = playerEntity.(*entities.Player) } switch mission.Type { case "Attack": if err != nil { log.Print("Error in target planet fetch:", err.Error()) } excessShips, ownerHasChanged = mission.EndAttackMission(target) clients.Broadcast(target) case "Supply": if err != nil { log.Print("Error in target planet fetch:", err.Error()) } excessShips, ownerHasChanged = mission.EndSupplyMission(target) if player != nil { clients.Send(player, stateChange) } case "Spy": for { if err != nil { log.Print("Error in target planet fetch:", err.Error()) } // All spy pilots die if planet is overtaken (they are killed) // Other possible solution is to generate a supply mission back (they flee) if target.Owner != mission.Target.Owner { break } mission.EndSpyMission(target) updateSpyReports(mission, stateChange) if mission.ShipCount > 0 { time.Sleep(entities.Settings.SpyReportValidity * time.Second) } else { break } target, stateChange, err = fetchMissionTarget(targetKey) } time.Sleep(entities.Settings.SpyReportValidity * time.Second) updateSpyReports(mission, stateChange) } entities.RemoveFromArea(mission.Key(), mission.AreaSet()) entities.Delete(mission.Key()) entities.Save(target) if ownerHasChanged { go func(owned, owner string) { leaderBoard.Channel <- [2]string{owned, owner} }(ownerBeforeMission, target.Owner) if player != nil { ownerChange := response.NewOwnerChange() ownerChange.RawPlanet = map[string]*entities.Planet{ target.Key(): target, } clients.Send(player, ownerChange) } } if excessShips > 0 { startExcessMission(mission, target, excessShips) } }