Merge branch '1.5.x' into 2.0.x
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,7 +21,7 @@ public class DefaultEchoService implements EchoService {
|
||||
private final String echoFormat;
|
||||
|
||||
public DefaultEchoService(String echoFormat) {
|
||||
this.echoFormat = (echoFormat != null) ? echoFormat : "%s";
|
||||
this.echoFormat = (echoFormat != null ? echoFormat : "%s");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,4 +30,5 @@ public class ReverseWebSocketEndpoint {
|
||||
session.getBasicRemote()
|
||||
.sendText("Reversed: " + new StringBuilder(message).reverse());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
@@ -18,5 +17,7 @@
|
||||
package samples.websocket.undertow.snake;
|
||||
|
||||
public enum Direction {
|
||||
|
||||
NONE, NORTH, SOUTH, EAST, WEST
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
@@ -19,11 +18,15 @@ package samples.websocket.undertow.snake;
|
||||
|
||||
public class Location {
|
||||
|
||||
/**
|
||||
* The X location.
|
||||
*/
|
||||
public int x;
|
||||
|
||||
/**
|
||||
* The Y location.
|
||||
*/
|
||||
public int y;
|
||||
public static final int GRID_SIZE = 10;
|
||||
public static final int PLAYFIELD_HEIGHT = 480;
|
||||
public static final int PLAYFIELD_WIDTH = 640;
|
||||
|
||||
public Location(int x, int y) {
|
||||
this.x = x;
|
||||
@@ -33,13 +36,13 @@ public class Location {
|
||||
public Location getAdjacentLocation(Direction direction) {
|
||||
switch (direction) {
|
||||
case NORTH:
|
||||
return new Location(this.x, this.y - Location.GRID_SIZE);
|
||||
return new Location(this.x, this.y - SnakeUtils.GRID_SIZE);
|
||||
case SOUTH:
|
||||
return new Location(this.x, this.y + Location.GRID_SIZE);
|
||||
return new Location(this.x, this.y + SnakeUtils.GRID_SIZE);
|
||||
case EAST:
|
||||
return new Location(this.x + Location.GRID_SIZE, this.y);
|
||||
return new Location(this.x + SnakeUtils.GRID_SIZE, this.y);
|
||||
case WEST:
|
||||
return new Location(this.x - Location.GRID_SIZE, this.y);
|
||||
return new Location(this.x - SnakeUtils.GRID_SIZE, this.y);
|
||||
case NONE:
|
||||
// fall through
|
||||
default:
|
||||
@@ -55,16 +58,13 @@ public class Location {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Location location = (Location) o;
|
||||
|
||||
if (this.x != location.x) {
|
||||
return false;
|
||||
}
|
||||
if (this.y != location.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -74,4 +74,5 @@ public class Location {
|
||||
result = 31 * result + this.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
@@ -157,4 +156,5 @@ public class Snake {
|
||||
public String getHexColor() {
|
||||
return this.hexColor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
@@ -31,7 +30,7 @@ import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* Sets up the timer for the multi-player snake game WebSocket example.
|
||||
*/
|
||||
public class SnakeTimer {
|
||||
public final class SnakeTimer {
|
||||
|
||||
private static final long TICK_DELAY = 100;
|
||||
|
||||
@@ -43,6 +42,9 @@ public class SnakeTimer {
|
||||
|
||||
private static Timer gameTimer = null;
|
||||
|
||||
private SnakeTimer() {
|
||||
}
|
||||
|
||||
public static void addSnake(Snake snake) {
|
||||
synchronized (MONITOR) {
|
||||
if (snakes.isEmpty()) {
|
||||
@@ -112,4 +114,5 @@ public class SnakeTimer {
|
||||
gameTimer.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
@@ -20,14 +19,28 @@ package samples.websocket.undertow.snake;
|
||||
import java.awt.Color;
|
||||
import java.util.Random;
|
||||
|
||||
public class SnakeUtils {
|
||||
public final class SnakeUtils {
|
||||
|
||||
/**
|
||||
* The width of the playfield.
|
||||
*/
|
||||
public static final int PLAYFIELD_WIDTH = 640;
|
||||
|
||||
/**
|
||||
* The height of the playfield.
|
||||
*/
|
||||
public static final int PLAYFIELD_HEIGHT = 480;
|
||||
|
||||
/**
|
||||
* The grid size.
|
||||
*/
|
||||
public static final int GRID_SIZE = 10;
|
||||
|
||||
private static final Random random = new Random();
|
||||
|
||||
private SnakeUtils() {
|
||||
}
|
||||
|
||||
public static String getRandomHexColor() {
|
||||
float hue = random.nextFloat();
|
||||
// sat between 0.1 and 0.3
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
@@ -29,14 +28,12 @@ import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
|
||||
public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
public static final int PLAYFIELD_WIDTH = 640;
|
||||
public static final int PLAYFIELD_HEIGHT = 480;
|
||||
public static final int GRID_SIZE = 10;
|
||||
|
||||
private static final AtomicInteger snakeIds = new AtomicInteger(0);
|
||||
|
||||
private static final Random random = new Random();
|
||||
|
||||
private final int id;
|
||||
|
||||
private Snake snake;
|
||||
|
||||
public static String getRandomHexColor() {
|
||||
@@ -50,15 +47,15 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
}
|
||||
|
||||
public static Location getRandomLocation() {
|
||||
int x = roundByGridSize(random.nextInt(PLAYFIELD_WIDTH));
|
||||
int y = roundByGridSize(random.nextInt(PLAYFIELD_HEIGHT));
|
||||
int x = roundByGridSize(random.nextInt(SnakeUtils.PLAYFIELD_WIDTH));
|
||||
int y = roundByGridSize(random.nextInt(SnakeUtils.PLAYFIELD_HEIGHT));
|
||||
return new Location(x, y);
|
||||
}
|
||||
|
||||
private static int roundByGridSize(int value) {
|
||||
value = value + (GRID_SIZE / 2);
|
||||
value = value / GRID_SIZE;
|
||||
value = value * GRID_SIZE;
|
||||
value = value + (SnakeUtils.GRID_SIZE / 2);
|
||||
value = value / SnakeUtils.GRID_SIZE;
|
||||
value = value * SnakeUtils.GRID_SIZE;
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -109,4 +106,5 @@ public class SnakeWebSocketHandler extends TextWebSocketHandler {
|
||||
SnakeTimer.broadcast(
|
||||
String.format("{'type': 'leave', 'id': %d}", Integer.valueOf(this.id)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user