@@ -2,6 +2,7 @@ package gateway
22
33import  (
44"fmt" 
5+ "github.com/guregu/null" 
56"math/rand" 
67"sync" 
78"time" 
@@ -192,6 +193,7 @@ func handleClose(c *WebSocketConnection) {
192193// TODO: optimize this 
193194r  :=  room .FindLobbyByWebsocketID (c .ID )
194195if  r  !=  nil  {
196+ // TODO: Store travelled distance in DB if player is not a guest 
195197r .RemovePlayer (r .GetPlayerIndexByWebSocketID (c .ID ))
196198}
197199}
@@ -236,6 +238,10 @@ func (c *WebSocketConnection) HandleAntiCheatFlags(r *room.Room, flags int) bool
236238return  false 
237239}
238240
241+ // sadly we have to define these functions in this package 
242+ // because go doesn't like import cycles 
243+ // (room functions that interact with anything websocket related) 
244+ 
239245func  BroadcastMessage (r  * room.Room , d  AnyMessage ) {
240246for  _ , p  :=  range  r .Players  {
241247conn , ok  :=  connections [p .ID ]
@@ -284,3 +290,71 @@ func StartEliminationRoom(r *room.Room) {
284290},
285291})
286292}
293+ 
294+ func  HandleDeath (r  * room.Room , loser  * player.Player , winner  * player.Player ) {
295+ placement  :=  len (r .Players ) -  1 
296+ 
297+ coinGain  :=  room .GetRewardForPlacement (room .CoinRewardType , placement )
298+ brGain  :=  room .GetRewardForPlacement (room .BRRewardType , placement )
299+ 
300+ err  :=  loser .Update (brGain , coinGain , room .DefaultXPGain )
301+ if  err  !=  nil  {
302+ fmt .Println (err )
303+ }
304+ 
305+ conn , ok  :=  connections [loser .ID ]
306+ if  ! ok  {
307+ return 
308+ }
309+ 
310+ // TODO: loser is kicked instead of shown the result screen 
311+ conn .Send (AnyMessage  {
312+ Op : OpClose ,
313+ T : PlayerKickEvent ,
314+ Data : map [string ]interface {} {
315+ "type" : EliminationKick ,
316+ "result" : brGain ,
317+ "coinChange" : coinGain ,
318+ "noms" : loser .Noms ,
319+ "message" : "You were nommed by "  +  winner .Username ,
320+ },
321+ })
322+ 
323+ handleClose (conn )
324+ HandleEnd (r )
325+ }
326+ 
327+ func  HandleEnd (r  * room.Room ) {
328+ if  ! r .IsSingle () ||  r .State  !=  room .IngameState  {
329+ return 
330+ }
331+ 
332+ winner  :=  r .Players [0 ]
333+ brGain  :=  room .GetRewardForPlacement (room .BRRewardType , 0 )
334+ coinGain  :=  room .GetRewardForPlacement (room .CoinRewardType , 0 )
335+ winner .Update (brGain , coinGain , room .WinXPGain )
336+ 
337+ conn , ok  :=  connections [winner .ID ]
338+ if  ! ok  {
339+ return 
340+ }
341+ 
342+ // TODO: winner is kicked without a reason 
343+ // instead, don't use OpClose and send it as event instead 
344+ conn .Send (AnyMessage {
345+ Op : OpClose ,
346+ T : PlayerKickEvent ,
347+ Data : map [string ]interface {} {
348+ "type" : WinKick ,
349+ "result" : brGain ,
350+ "noms" : winner .Noms ,
351+ "message" : null .NewString ("" , false ),
352+ },
353+ })
354+ 
355+ handleClose (conn )
356+ 
357+ // TODO: delete room and create new room 
358+ // or alternatively, create a reset function on room struct 
359+ // that sets the state to waiting and other prep stuff 
360+ }
0 commit comments