Skip to content

Commit

Permalink
Breaking news widget works
Browse files Browse the repository at this point in the history
  • Loading branch information
Aksenov239 committed Mar 20, 2016
1 parent f4318db commit 5104d09
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 41 deletions.
3 changes: 3 additions & 0 deletions icpc-live-v2.iml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.validation:validation-api:sources:1.0.0.GA" level="project" />
<orderEntry type="library" name="Maven: com.vaadin:vaadin-themes:7.5.9" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.4" level="project" />
<orderEntry type="library" name="Maven: org.twitter4j:twitter4j-core:4.0.4" level="project" />
<orderEntry type="library" name="Maven: org.twitter4j:twitter4j-stream:4.0.4" level="project" />
<orderEntry type="library" name="Maven: org.twitter4j:twitter4j-async:4.0.4" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:18.0" level="project" />
<orderEntry type="library" name="Maven: org.imgscalr:imgscalr-lib:4.2" level="project" />
<orderEntry type="library" name="Maven: uk.co.caprica:vlcj:3.9.0" level="project" />
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/ru/ifmo/acm/backend/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ private void run() throws InterruptedException, InvocationTargetException, IOExc
generator.addWidget(new QueueWidget(100));
generator.addWidget(new BigStandingsWidget(64, 64,
Widget.BASE_WIDTH - 128, Widget.BASE_HEIGHT - 128, updateWait, true));
generator.addWidget(new BreakingNewsWidget(
updateWait,
(int)(Widget.BASE_WIDTH * 0.65),
(int)(Widget.BASE_HEIGHT * 0.6),
(int)(Widget.BASE_WIDTH * 0.3),
(int)(Widget.BASE_HEIGHT * 0.2),
16. / 9,
Integer.parseInt(properties.getProperty("sleep.time")),
Integer.parseInt(properties.getProperty("breakingnews.time"))
));
new TickPlayer("Main screen", generator, frameRate).frame.setLocation(0, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public class BreakingNewsWidget extends VideoWidget {
private final int PLATE_WIDTH;
private final int GAP;

public BreakingNewsWidget(int x, int y, int width, int height, double aspectRatio, int sleepTime, int duration) {
public BreakingNewsWidget(long updateWait, int x, int y, int width, int height, double aspectRatio, int sleepTime, int duration) {
super(x, y, width, (int) (height / aspectRatio), sleepTime, 0);
this.updateWait = updateWait;
wVideo = width;
hVideo = (int) (height / aspectRatio);
hVideo = (int) (width / aspectRatio);

PLATE_WIDTH = (int) (1.5 * width);
PLATE_WIDTH = (int) (1.2 * width);
GAP = (int) (0.05 * hVideo);

this.x = x;
Expand Down Expand Up @@ -52,13 +53,19 @@ protected void updateImpl(Data data) {
}
if (isVisible())
return;
setVisible(true);

int teamId = data.breakingNewsData.teamId;
int problemId = data.breakingNewsData.problemId;

team = Preparation.eventsLoader.getContestData().getParticipant(teamId).getSmallTeamInfo();
System.err.println("Get request for " + teamId + " " + problemId);

team = Preparation.eventsLoader.getContestData().getParticipant(teamId);
java.util.List<RunInfo> runs = team.getRuns()[problemId];

if (runs.size() == 0) {
System.err.println("Team " + teamId + " has no submit for problem " + problemId);
return;
}
run = runs.get(runs.size() - 1);
for (RunInfo run1 : runs) {
if (run1.isAccepted()) {
Expand All @@ -70,9 +77,11 @@ protected void updateImpl(Data data) {
if (data.breakingNewsData.isLive) {
url = TeamWidget.getUrl(team, data.breakingNewsData.infoType);
} else {
url = run.toString();
url = TeamWidget.getUrl(run);
}

System.err.println("Change to " + url);

change(url);
isLive = data.breakingNewsData.isLive;

Expand All @@ -89,37 +98,69 @@ protected void updateImpl(Data data) {
}
caption += " problem " + (char) ('A' + problemId);

System.err.println("Caption: " + caption);

currentShow = run.getTeamInfoBefore();
timer = 0;
rankState = 0;
visibilityState = 0;
setVisible(true);
}

lastUpdate = System.currentTimeMillis();
}

private double localVisibility;
private int rankState;
@Override
public void paintImpl(Graphics2D g, int width, int height) {
update();

if (visibilityState == 0) {
int dt = updateVisibilityState();

if (visibilityState == 0 && !isVisible()) {
stop();
return;
}
int dt = updateVisibilityState();

timer += dt;
if (!ready.get()) {
return;
}

if (timer > duration / 2) {
timer += dt;
System.err.println(localVisibility + " " + visibilityState + " " + isVisible());
if (rankState == 0) {
setVisibilityState(0);
rankState = 1;
}
if (timer > duration / 2 - 1000 && rankState == 1) {
rankState = 2;
localVisibility = 1;
}
if (rankState == 2) {
localVisibility -= dt * V;
localVisibility = Math.max(localVisibility, 0);
if (localVisibility == 0) {
rankState = 3;
}
}
if (rankState == 3) {
localVisibility += dt * V;
localVisibility = Math.min(localVisibility, 1);
currentShow = team;
if (localVisibility == 1) {
rankState = 4;
}
}

if (run == null || URL.get() == null) {
if (run == null || URL.get() != null) {
int hh = (int) (hVideo * opacity);
g.drawImage(image.get(), x, y + (hVideo - hh) / 2, wVideo, hh, null);
}

int y = this.y + hVideo + GAP;
int x = this.x + (wVideo - PLATE_WIDTH) / 2;
drawTeamPane(g, currentShow, x, y + hVideo, PLATE_WIDTH, visibilityState);
drawTeamPane(g, currentShow, x, y, PLATE_WIDTH, rankState == 2 || rankState == 3 ? localVisibility : visibilityState);
drawTextInRect(g, caption, (int) (x - 0.005 * PLATE_WIDTH), y, -1, PLATE_WIDTH / 10,
POSITION_RIGHT, ACCENT_COLOR, Color.white, visibilityState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class TeamWidget extends VideoWidget {
String url = properties.getProperty("info." + types[i], "");
urlTemplates.put(types[i], url);
}
if (properties.get("info.record") != null) {
urlTemplates.put("record", properties.getProperty("info.record"));
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -195,4 +198,8 @@ public static String getUrl(TeamInfo team, String infoType) {
}
return null;
}

public static String getUrl(RunInfo run) {
return String.format(urlTemplates.get("record"), run.getId());
}
}
16 changes: 13 additions & 3 deletions src/main/java/ru/ifmo/acm/backend/player/widgets/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void paint(Graphics2D g, int width, int height, double scale) {
}
}

private static final double V = 0.001;
protected static final double V = 0.001;

protected int updateVisibilityState() {
long time = System.currentTimeMillis();
Expand All @@ -98,6 +98,14 @@ public void setVisibilityState(double visibilityState) {
textOpacity = f(visibilityState * 2 - 1);
}

public double getOpacity(double visibilityState) {
return f(visibilityState * 2);
}

public double getTextOpacity(double visibilityState) {
return f(visibilityState * 2 - 1);
}

protected double f(double x) {
if (x < 0) return 0;
if (x > 1) return 1;
Expand Down Expand Up @@ -146,8 +154,10 @@ void drawRect(Graphics2D g, int x, int y, int width, int height, Color color, do
static final int POSITION_RIGHT = 1;
static final int POSITION_CENTER = 2;

void drawTextInRect(Graphics2D g, String text, int x, int y, int width, int height, int position, Color color, Color textColor, double state) {
setVisibilityState(state);
void drawTextInRect(Graphics2D g, String text, int x, int y, int width, int height, int position, Color color, Color textColor, double visibilityState) {
//setVisibilityState(state);
double opacity = getOpacity(visibilityState);
double textOpacity = getTextOpacity(visibilityState);
if (text == null) {
text = "NULL";
}
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/ru/ifmo/acm/datapassing/BreakingNewsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public BreakingNewsData initialize() {
BreakingNewsData data = MainScreenData.getMainScreenData().breakingNewsData;
this.timestamp = data.timestamp;
this.isVisible = data.isVisible;
this.isLive = data.isLive;
this.teamId = data.teamId;
this.problemId = data.problemId;
this.infoType = data.infoType;
return this;
}

Expand All @@ -19,16 +23,17 @@ public void recache() {
}

public synchronized boolean setNewsVisible(boolean visible, String type, boolean isLive, String info) {
if (visible && isVisible) {
return false;
}

this.isVisible = visible;

if (visible) {
String[] zz = info.split(" ");
int teamId = Integer.parseInt(zz[0]);
int teamId = Integer.parseInt(zz[0]) - 1;
int problemId = zz[1].charAt(0) - 'A';

if (timestamp + MainScreenData.getProperties().sleepTime > System.currentTimeMillis() && isVisible) {
return false;
}
TeamInfo teamInfo = MainScreenData.getProperties().contestInfo.getParticipant(teamId);
this.teamId = teamId;
this.problemId = problemId;
Expand All @@ -47,7 +52,8 @@ public void update() {
synchronized (breakingNewsLock) {
//System.err.println(PCMSEventsLoader.getInstance().getContestData().getTeamsNumber());
if (System.currentTimeMillis() > timestamp +
MainScreenData.getProperties().breakingNewsTimeToShow) {
MainScreenData.getProperties().breakingNewsTimeToShow +
MainScreenData.getProperties().sleepTime) {
isVisible = false;
change = true;
}
Expand All @@ -62,12 +68,14 @@ public String toString() {

public String getStatus() {
if (isVisible) {
String status = "Breaking news (%s) are shown for team %d and problem %c for %d seconds";
String status = "Breaking news (%s) are shown for team %s and problem %c for %d seconds";

long time = (timestamp + MainScreenData.getProperties().breakingNewsTimeToShow - System.currentTimeMillis()) / 1000;
String type = isLive ? "Live" : infoType;
long time = (timestamp + MainScreenData.getProperties().breakingNewsTimeToShow
+ MainScreenData.getProperties().sleepTime
- System.currentTimeMillis()) / 1000;
String type = isLive ? infoType : "record";

return String.format(status, type, teamId, (char)('A' + problemId), time);
return String.format(status, type, teamName, (char) ('A' + problemId), time);
} else {
return "Breaking news aren't shown";
}
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/ru/ifmo/acm/datapassing/StandingsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ru.ifmo.acm.backend.player.widgets.StandingsWidget;
import ru.ifmo.acm.events.EventsLoader;
import ru.ifmo.acm.mainscreen.MainScreenData;
import ru.ifmo.acm.mainscreen.MainScreenProperties;

public class StandingsData implements CachedData {
@Override
Expand Down Expand Up @@ -31,10 +32,6 @@ public String toString() {
(isBig() ? " big standings are shown" : " compact standings are shown");
}

public long getLatency() {
return latency;
}

public void recache() {
Data.cache.refresh(StandingsData.class);
}
Expand All @@ -53,8 +50,8 @@ public void setStandingsVisible(boolean visible, StandingsType type, boolean isB

public static long getTotalTime(boolean isBig, StandingsType type) {
return isBig ?
BigStandingsWidget.totalTime(type, EventsLoader.getInstance().getContestData().getTeamsNumber()) + latency :
StandingsWidget.totalTime(type, EventsLoader.getInstance().getContestData().getTeamsNumber()) + latency;
BigStandingsWidget.totalTime(type, EventsLoader.getInstance().getContestData().getTeamsNumber()) + MainScreenData.getProperties().latency :
StandingsWidget.totalTime(type, EventsLoader.getInstance().getContestData().getTeamsNumber()) + MainScreenData.getProperties().latency;
}

public void update() {
Expand Down Expand Up @@ -98,8 +95,6 @@ public void setBig(boolean big) {
public boolean isBig;
public OptimismLevel optimismLevel = OptimismLevel.NORMAL;

public static long latency;

final private Object standingsLock = new Object();

public enum StandingsType {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ru/ifmo/acm/events/PCMS/PCMSRunInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class PCMSRunInfo implements RunInfo {
this.firstToSolve = firstToSolve;
}

public int getId() {
throw new AssertionError("PCMSRunInfo doesn't have id");
}

public boolean isAccepted() {
return "AC".equals(result);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ru/ifmo/acm/events/RunInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.ifmo.acm.events;

public interface RunInfo {
int getId();
boolean isAccepted();
boolean isJudged();
String getResult();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ru/ifmo/acm/events/WF/WFEventsLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ private List<ProblemInfo> problemsInfoRead() throws IOException {
problem.name = val;
break;
case "rgb":
problem.color = Color.decode(val.substring(1, 8));
//System.err.println(val + " " + val.length());
problem.color = Color.decode(val);
break;
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/ru/ifmo/acm/mainscreen/MainScreenStandingsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ public Component getBreakingNewsController() {

isLive = new CheckBox("Is live");
isLive.addValueChangeListener(event -> {
types.setValue(isLive.getValue() ? null: TeamWidget.types[0]);
for (String type : TeamWidget.types) {
types.setItemEnabled(type, isLive.isEmpty());
}
//types.setValue(isLive.getValue() ? null: TeamWidget.types[0]);
// for (String type : TeamWidget.types) {
// types.setItemEnabled(type, !isLive.getValue());
// }
types.setEnabled(isLive.getValue());
breakingNewsStatus.setValue(getBreakingNewsStatus());
});
isLive.setValue(false);

types = new OptionGroup();
types.addItems(TeamWidget.types);
types.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
types.setValue(TeamWidget.types[0]);
types.setEnabled(false);

team = new TextField("Team: ");
team.setSizeFull();
Expand All @@ -85,7 +88,12 @@ public Component getBreakingNewsController() {
Notification.show("Team field requires team id and problem id");
} else {
if (!mainScreenData.breakingNewsData.setNewsVisible(true, (String) types.getValue(), isLive.getValue(), team.getValue())) {
Notification.show("You need to wait 30 seconds first", Notification.Type.WARNING_MESSAGE);
Notification.show(
String.format("You need to wait while current breaking news is shown"),
Notification.Type.WARNING_MESSAGE
);
} else {
team.clear();
}
breakingNewsStatus.setValue(getBreakingNewsStatus());
}
Expand Down
Loading

0 comments on commit 5104d09

Please sign in to comment.