Skip to content

Instantly share code, notes, and snippets.

@shaetads
Last active August 16, 2016 12:29
Show Gist options
  • Save shaetads/3232db84f92b0cab68c4c7a988ad4570 to your computer and use it in GitHub Desktop.
Save shaetads/3232db84f92b0cab68c4c7a988ad4570 to your computer and use it in GitHub Desktop.
library(RJSONIO)
library(plyr)
#modify these to tweek analysis
SHIP_NAME <- "Blackbird"
SHIP_ID <- 632 # shipID (632 = Blackbird)
NUMBER_OF_LOSSES <- 1000 #number of ship losses to download
LOSS_LEVEL <- 2 #minimum losses by single pilot to check further
#solarSystemID
UEDAMA_ID <- 30002768 #https://zkillboard.com/system/30002768/
UEDAMA_NAME <- "Uedama"
uedama_count <- 0
NIARJA_ID <- 30003504 #https://zkillboard.com/system/30003504/
NIARJA_NAME <- "Niarja"
niarja_count <- 0
MADIRMILIRE_ID <- 30003503 #https://zkillboard.com/system/30003503/
MADIRMILIRE_NAME <- "Madirmilire"
madirmilire_count <- 0
SIRPPALA_ID <- 30002791 #https://zkillboard.com/system/30002791/
SIRPPALA_NAME <- "Sirppala"
sirppala_count <- 0
SIVALA_ID <- 30002765
SIVALA_NAME <- "Sivala"
sivala_count <- 0
PERIMETER_ID <- 30000144 #https://zkillboard.com/system/30000144/
PERIMETER_NAME <- "Perimeter"
perimeter_count <- 0
JITA_ID <- 30000142 #https://zkillboard.com/system/30000142/
JITA_NAME <- "Jita"
jita_count <- 0
#shipID
CONCORD_COMMANDER <- 11125 #https://zkillboard.com/ship/11125/
CONCORD_CAPTAIN <- 3885 #https://zkillboard.com/ship/3885/
CONCORD_OFFICER <- 3863 #https://zkillboard.com/ship/3863/
AMARR_SENTRY <- 1194 #https://zkillboard.com/ship/1194/
CALDARI_SENTRY_I <- 3740 #https://zkillboard.com/ship/3740/
CALDARI_SENTRY_II <- 3741 #https://zkillboard.com/ship/3741/
CALDARI_SENTRY_III <- 3739 #https://zkillboard.com/ship/3739/
total_lossmails <- 0 #count of total killmails downloaded
killed_criminal <- 0 #count of Blackbirds with CONCORD/Sentry Gun on killmail
pilot_ids <- c()
pilot_names <- c()
loss_ids <- c() #collection of killIDs in checked systems
#count for one solar system
system_to_count <- UEDAMA_ID
# Counts pilots by number of criminal losses while flying a Blackbird
track.pilots <- function(lossmail) {
pilot_ids <<- append(pilot_ids, lossmail$victim$characterID)
pilot_names <<- append(pilot_names, lossmail$victim$characterName)
if(lossmail$solarSystemID == UEDAMA_ID) {
uedama_count <<- uedama_count + 1
}
else if(lossmail$solarSystemID == NIARJA_ID) {
niarja_count <<- niarja_count + 1
}
else if(lossmail$solarSystemID == MADIRMILIRE_ID) {
madirmilire_count <<- madirmilire_count + 1
}
else if(lossmail$solarSystemID == SIRPPALA_ID) {
sirppala_count <<- sirppala_count + 1
}
else if(lossmail$solarSystemID == SIVALA_ID) {
sivala_count <<- sivala_count + 1
}
else if(lossmail$solarSystemID == PERIMETER_ID) {
perimeter_count <<- perimeter_count + 1
}
else if(lossmail$solarSystemID == JITA_ID) {
jita_count <<- jita_count + 1
}
}
# Checks list of attackers on the killmail and returns 1 if CONCORD
# or Sentry Gun found on the list of attackers.
# Returns 0 if not.
check.gank <- function(lossmail) {
#loop over each attacker on the lossmail
for(i in 1:length(lossmail$attackers)) {
# if the attacker is CONCORD or a Sentry Gun
if(lossmail$attackers[[i]]
$shipTypeID %in% c(CONCORD_COMMANDER,
CONCORD_CAPTAIN,
CONCORD_OFFICER,
AMARR_SENTRY,
CALDARI_SENTRY_I,
CALDARI_SENTRY_II,
CALDARI_SENTRY_III)) {
track.pilots(lossmail)
# criminal found, return 1
return(1)
}
}
# NOT criminal, return 0
return(0)
}
# Download the required number of lossmails
for(x in seq(1:(NUMBER_OF_LOSSES / 200))) {
# zkillboard api call url
# shipID 632 = Blackbird
# losses = filter for lossmails only
url <- paste0("https://zkillboard.com/api/losses/shipID/", SHIP_ID, "/page/", sep="")
#append page number to url
url <- paste(url, x, sep="")
print(url)
#request the data
losses <- fromJSON(url)
#loop over the 200 entries in the response
for (i in seq(1,length(losses))) {
#for each lossmail, add 1 to the total
total_lossmails <- total_lossmails + 1
#check the lossmail to see if it occured in high gank system
if(losses[[i]]$solarSystemID %in% c(UEDAMA_ID,
NIARJA_ID,
MADIRMILIRE_ID,
SIRPPALA_ID,
SIVALA_ID,
PERIMETER_ID,
JITA_ID)) {
loss_ids <- append(loss_ids, losses[[i]]$killID)
#check if criminal loss
killed_criminal <- killed_criminal + check.gank(losses[[i]])
}
}
}
pilots_data = data.frame(id=pilot_ids, name=pilot_names)
print(pilots_data)
pilots_count <- count(pilots_data, c("name", "id"))
final_pilots <- subset(pilots_count, freq >= LOSS_LEVEL)
print(final_pilots)
barplot(final_pilots$freq,
main=paste0("Pilot losses while criminal in a ", SHIP_NAME),
xlab="Pilot Name",
ylab="# of losses",
names.arg=final_pilots$name)
print(total_lossmails) #total lossmails checked
print(losses[[length(losses)]]$killTime) #oldest lossmail checked
print(length(loss_ids)) #total Blackbird losses in target systems
print(loss_ids) #killID in target systems
print(killed_criminal) #total number of criminals
start_date <- as.Date(losses[[length(losses)]]$killTime)
time_diff <- round(difftime(Sys.time(), start_date, units = "days"), 0)
print(time_diff)
system_names <- c(UEDAMA_NAME,
NIARJA_NAME,
MADIRMILIRE_NAME,
SIRPPALA_NAME,
SIVALA_NAME,
PERIMETER_NAME,
JITA_NAME)
system_counts <- c(uedama_count,
niarja_count,
madirmilire_count,
sirppala_count,
sivala_count,
perimeter_count,
jita_count)
systems_table <- data.frame(names=system_names, count=system_counts)
print(systems_table)
barplot(systems_table$count,
names = systems_table$names,
main = paste0("Criminal losses by System (",
time_diff,
" days: ",
NUMBER_OF_LOSSES,
" ",
SHIP_NAME,
" losses)"),
xlab = "System Name",
ylab = "# of losses")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment