mirror of https://github.com/Askill/r_place.git
added Image and Pixel Obejcts
This commit is contained in:
parent
2057469b2b
commit
30f9d842f7
19
go/server.go
19
go/server.go
|
|
@ -11,17 +11,15 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var addr = flag.String("addr", "localhost:8080", "http service address")
|
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 2048,
|
ReadBufferSize: 2048,
|
||||||
WriteBufferSize: 2048,
|
WriteBufferSize: 2048,
|
||||||
}
|
}
|
||||||
|
var img = GetImage(10, 10)
|
||||||
|
|
||||||
func serve(w http.ResponseWriter, r *http.Request) {
|
func serve(w http.ResponseWriter, r *http.Request) {
|
||||||
c, err := upgrader.Upgrade(w, r, nil)
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
|
@ -31,16 +29,17 @@ func serve(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
for {
|
for {
|
||||||
mt, message, err := c.ReadMessage()
|
mt, msg, err := c.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//log.Println("read:", err)
|
log.Println("read:", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
pixel := JsonToStruct(message)
|
|
||||||
|
|
||||||
tm := time.Unix(pixel.Timestamp, 0)
|
message := Message{}
|
||||||
log.Println(tm)
|
message.JsonToStruct(msg)
|
||||||
err = c.WriteMessage(mt, message)
|
img.SetPixel(message)
|
||||||
|
|
||||||
|
err = c.WriteMessage(mt, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//log.Println("write:", err)
|
//log.Println("write:", err)
|
||||||
break
|
break
|
||||||
|
|
@ -49,6 +48,8 @@ func serve(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var addr = flag.String("addr", "localhost:8080", "http service address")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
http.HandleFunc("/", serve)
|
http.HandleFunc("/", serve)
|
||||||
|
|
|
||||||
56
go/util.go
56
go/util.go
|
|
@ -2,9 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Pixel struct {
|
type Message struct {
|
||||||
X uint16 `json:"x"`
|
X uint16 `json:"x"`
|
||||||
Y uint16 `json:"y"`
|
Y uint16 `json:"y"`
|
||||||
Color uint8 `json:"color"`
|
Color uint8 `json:"color"`
|
||||||
|
|
@ -12,8 +14,52 @@ type Pixel struct {
|
||||||
UserID uint64 `json:"userid"`
|
UserID uint64 `json:"userid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func JsonToStruct(input []byte) Pixel {
|
func (message *Message) JsonToStruct(input []byte) *Message {
|
||||||
pixel := Pixel{}
|
json.Unmarshal(input, message)
|
||||||
json.Unmarshal(input, &pixel)
|
return message
|
||||||
return pixel
|
}
|
||||||
|
|
||||||
|
type pixel struct {
|
||||||
|
Color uint8 `json:"color"`
|
||||||
|
Timestamp int64 `json:"timestamp"`
|
||||||
|
UserID uint64 `json:"userid"`
|
||||||
|
Mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
type image struct {
|
||||||
|
width uint16
|
||||||
|
height uint16
|
||||||
|
pixels []pixel
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetImage(w uint16, h uint16) image {
|
||||||
|
pixels := make([]pixel, w*h)
|
||||||
|
for i := 0; i < int(w*h); i++ {
|
||||||
|
pixels[i] = pixel{Color: 0, Timestamp: 0, UserID: 0, Mutex: sync.Mutex{}}
|
||||||
|
}
|
||||||
|
return image{width: w, height: h, pixels: pixels}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *pixel) setColor(color uint8, timestamp int64, userid uint64) {
|
||||||
|
p.Mutex.Lock()
|
||||||
|
defer p.Mutex.Unlock()
|
||||||
|
if timestamp > p.Timestamp {
|
||||||
|
p.Color = color
|
||||||
|
p.Timestamp = timestamp
|
||||||
|
p.UserID = userid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (img *image) SetPixel(message Message) *image {
|
||||||
|
if message.X >= img.width || message.Y >= img.height || message.X < 0 || message.Y < 0 {
|
||||||
|
fmt.Printf("User %d tried accessing out of bounds \n", message.UserID)
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
if message.Color >= 16 || message.Color < 0 {
|
||||||
|
fmt.Printf("User %d tried setting non existent color \n", message.UserID)
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
pos := uint32(message.X)*uint32(img.width) + uint32(message.Y)
|
||||||
|
img.pixels[pos].setColor(message.Color, message.Timestamp, message.UserID)
|
||||||
|
return img
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,11 @@ class pixel:
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
message = pixel(
|
message = pixel(
|
||||||
x=0,
|
x=11,
|
||||||
y=1,
|
y=0,
|
||||||
color=0,
|
color=15,
|
||||||
timestamp=int(time.time()),
|
timestamp=int(time.time()),
|
||||||
userid=0,
|
userid=1,
|
||||||
)
|
)
|
||||||
async with websockets.connect("ws://localhost:8080") as websocket:
|
async with websockets.connect("ws://localhost:8080") as websocket:
|
||||||
print(message)
|
print(message)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue