-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug #140: Add a column to the alliances table to store whether the al…
…liance is active or not. Updated it in a daily cron.
- Loading branch information
Showing
4 changed files
with
200 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
-- Add a flag to indicate whether the alliance is active or not. Inactive alliances are filtered | ||
-- out of search results by default. An inactive alliance is one where nobody has logged in for a | ||
-- while. | ||
ALTER TABLE alliances | ||
ADD COLUMN is_active INTEGER; | ||
|
||
-- Everybody's active by default (until a cron job runs and resets it). | ||
UPDATE alliances SET is_active = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
server/src/au/com/codeka/warworlds/server/cron/UpdateActiveAlliancesCronJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package au.com.codeka.warworlds.server.cron; | ||
|
||
import org.joda.time.DateTime; | ||
import org.joda.time.Days; | ||
import org.joda.time.Duration; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import au.com.codeka.warworlds.server.data.DB; | ||
import au.com.codeka.warworlds.server.data.SqlResult; | ||
import au.com.codeka.warworlds.server.data.SqlStmt; | ||
|
||
/** | ||
* Goes through all the alliances and marks inactive any alliance whose member has not logged in for | ||
* a while. | ||
*/ | ||
public class UpdateActiveAlliancesCronJob extends CronJob { | ||
/** | ||
* If no empire has logged in within this number of days, we'll consider the alliance inactive. | ||
*/ | ||
long ACTIVE_DAYS_SINCE_LOGIN = 60; | ||
|
||
@Override | ||
public void run(String extra) throws Exception { | ||
Map<Integer, DateTime> allianceLastLogins = new TreeMap<>(); | ||
String sql = "SELECT" + | ||
" alliances.id," + | ||
" MAX(empires.last_login) as last_login" + | ||
" FROM alliances alliances" + | ||
" INNER JOIN (" + | ||
" SELECT" + | ||
" empires.id," + | ||
" empires.alliance_id," + | ||
" logins.last_login" + | ||
" FROM empires empires" + | ||
" INNER JOIN (" + | ||
" SELECT" + | ||
" empire_id," + | ||
" MAX(date) AS last_login" + | ||
" FROM beta.empire_logins" + | ||
" GROUP BY empire_id" + | ||
" ) logins ON logins.empire_id = empires.id" + | ||
" ) empires" + | ||
" ON alliances.id = empires.alliance_id" + | ||
" GROUP BY alliances.id"; | ||
try (SqlStmt stmt = DB.prepare(sql)) { | ||
SqlResult res = stmt.select(); | ||
while (res.next()) { | ||
int allianceID = res.getInt(1); | ||
DateTime lastLogin = res.getDateTime(2); | ||
allianceLastLogins.put(allianceID, lastLogin); | ||
} | ||
} | ||
|
||
try (SqlStmt stmt = DB.prepare("UPDATE alliances SET is_active = ? WHERE id = ?")) { | ||
for (Map.Entry<Integer, DateTime> entry : allianceLastLogins.entrySet()) { | ||
DateTime lastLogin = entry.getValue(); | ||
Days daysSinceLogin = Days.daysBetween(lastLogin, DateTime.now()); | ||
if (daysSinceLogin.getDays() > ACTIVE_DAYS_SINCE_LOGIN) { | ||
stmt.setInt(1, 0); | ||
} else { | ||
stmt.setInt(1, 1); | ||
} | ||
stmt.setInt(2, entry.getKey()); | ||
stmt.update(); | ||
} | ||
} | ||
} | ||
} |