Skip to content
Open

go #2

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2c407c4
update
cserli Dec 13, 2018
41ee360
update data
cserli Dec 27, 2018
fb9d077
Update README.md
cserli Dec 27, 2018
9b25528
Update README.md
cserli Dec 27, 2018
49df1da
Update README.md
cserli Dec 27, 2018
05e4f96
Create 第一节 游戏服务器开发中——消息的序列化.txt
cserli Jan 9, 2019
221ca09
update
cserli Jan 21, 2019
4288ccb
update data
cserli Jan 22, 2019
1da306d
Update main.go
cserli Feb 14, 2019
e849f96
Create WeiXin.zip
cserli Apr 11, 2019
299f65e
add Fle
cserli Apr 11, 2019
b8e6cd5
Create tmain.go
cserli Apr 20, 2019
27fd2bd
Create Proto.go
cserli Apr 20, 2019
e471c65
gophers
cserli Apr 21, 2019
97e402d
Update tmain.go
cserli Apr 24, 2019
d9af0a9
ful
cserli Apr 24, 2019
db9aeaa
Update tmain.go
cserli Apr 24, 2019
8a06c91
Create tmain.go
cserli Apr 24, 2019
fa7145a
Update tmain.go
cserli Apr 25, 2019
9490dab
Update tmain.go
cserli Apr 25, 2019
d015b21
Update tmain.go
cserli Apr 26, 2019
b0071d3
update classs
cserli May 4, 2019
6dd6379
Create end.zip
cserli May 5, 2019
6492d12
update files
cserli May 5, 2019
6b132c7
Update 课件PPT.txt
cserli May 5, 2019
6321564
dsq
cserli May 5, 2019
9a2d5d1
updat
cserli May 9, 2019
f3833af
v2020
cserli Mar 1, 2020
3ff6f86
Update README.md
cserli Mar 1, 2020
fae6adf
文档
cserli Mar 1, 2020
e8f2952
Create 下载.txt
cserli Mar 1, 2020
51bb6da
Create 下载地址.txt
cserli Mar 1, 2020
32cb787
server
cserli Mar 2, 2020
32493d2
Create 血战到底目录.png
cserli Mar 29, 2020
dc3cad3
1
cserli Apr 6, 2020
2c36fd1
1
cserli Apr 7, 2020
ede64ed
1
cserli Apr 9, 2020
a9ae84b
课件上传
cserli Apr 11, 2020
22cb71f
第十一节
cserli Apr 13, 2020
a740b87
课程斗兽棋sever
cserli Apr 14, 2020
f76907f
血战代码
cserli Apr 16, 2020
b3ad5cf
Create 血战到底设计.xmind
cserli Apr 16, 2020
b76569e
update
cserli Apr 24, 2020
cb92340
update
cserli Apr 26, 2020
9dd4450
update
cserli Apr 26, 2020
d84e5b6
class2
cserli Apr 26, 2020
3e9a48f
update
cserli Apr 27, 2020
77a5583
Update README.md
cserli Jun 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file added ByteEdu.Com/Gophers/bin/Gopher.exe
Binary file not shown.
Binary file added ByteEdu.Com/Gophers/bin/linux_amd64/Gopher
Binary file not shown.
Binary file added ByteEdu.Com/Gophers/src/Gopher/Gopher.exe
Binary file not shown.
181 changes: 181 additions & 0 deletions ByteEdu.Com/Gophers/src/Gopher/tmain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package main

import (
"Proto_Go"
"fmt"
"net/http"
"sort"
"strconv"
)

// http://127.0.0.1:8891/ByteEdu_Gophers?Protocol=1&Protocol2=1&Itype=1&LoginName=ByteEdu.Com&LoginPW=ByteEdu.Com

//------------------------------------------------------------------------------
// 保存玩家登陆、注册的协议
type PlayerData struct {
OpenID string
LoginName string
LoginPW string
Lev string // 职位
Score int
}

type ByteEduList []PlayerData

func (this ByteEduList) Len() int {
return len(this)
}

func (this ByteEduList) Less(i, j int) bool {
return this[i].Score < this[j].Score
}

func (this ByteEduList) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}

//------------------------------------------------------------------------------

var (
strport string = "8891"
PlayerDataG map[string]*PlayerData
)

func init() {
// 初始化 map
PlayerDataG = make(map[string]*PlayerData)
return
}

func main() {

http.HandleFunc("/ByteEdu_Gophers", IndexHandler)
http.ListenAndServe(":"+strport, nil)
}

func IndexHandler(w http.ResponseWriter, req *http.Request) {

if req.Method == "GET" {
w.Header().Set("Access-Control-Allow-Origin", "*")
req.ParseForm()
defer func() {
if err := recover(); err != nil {
fmt.Println("%s", err)
req.Body.Close()
return
}
}()

Protocol, bProtocol := req.Form["Protocol"]
Protocol2, bProtocol2 := req.Form["Protocol2"]
Itype, bItype := req.Form["Itype"]

if bProtocol && bProtocol2 && bItype {
if Protocol[0] == strconv.Itoa(Proto.ProtoGopher) {
switch Protocol2[0] {
case strconv.Itoa(Proto.C2S_PlayerLoginProto2):

LoginName, _ := req.Form["LoginName"]
LoginPW, _ := req.Form["LoginPW"]
strItype := Itype[0]
if strItype == "1" { // 注册
//1. 用户名要脏字过滤
//2. 判断用户是否注册过
_, ok := PlayerDataG[LoginName[0]]
if ok {
fmt.Fprintln(w, "已经注册过!")
return
} else {
data := &PlayerData{
OpenID: LoginName[0],
LoginName: LoginName[0],
LoginPW: LoginPW[0],
Lev: "军师", // 职位
}
// 保存
PlayerDataG[LoginName[0]] = data
fmt.Fprintln(w, "注册成功")
return
}
return
} else if strItype == "2" { // 登陆
_, ok := PlayerDataG[LoginName[0]]
if ok {
fmt.Fprintln(w, "登陆成功!")

return
} else {
fmt.Fprintln(w, "用户名或密码错误!")
return
}
}

fmt.Fprintln(w, "类型错误")
//------------------------------------------------------
return
case strconv.Itoa(Proto.C2S_GetRankProto2):
// 排行协议
LoginName, bLoginName := req.Form["LoginName"]
Score, bScore := req.Form["Score"]
if bLoginName && bScore {
v, ok := PlayerDataG[LoginName[0]]
iscore, _ := strconv.Atoi(Score[0])
// 一:数据的保存
if ok {
// 1. 保存的数据是否小于当前数据
if v.Score < iscore {
data := &PlayerData{
OpenID: LoginName[0],
LoginName: LoginName[0],
LoginPW: v.LoginPW,
Lev: v.Lev, // 职位
Score: iscore,
}
// 保存
PlayerDataG[LoginName[0]] = data
}
} else {
// 内存不存在的情况下保存
data := &PlayerData{
OpenID: LoginName[0],
LoginName: LoginName[0],
Lev: "军师",
Score: iscore,
}
// 保存
PlayerDataG[LoginName[0]] = data
}
// 二:排行
// 1. map ---> slice
playerdata := make([]PlayerData, len(PlayerDataG)+1)
i := 0
for _, v := range PlayerDataG {
i++
var data PlayerData
data.OpenID = v.OpenID
data.LoginName = v.LoginName
data.LoginPW = v.LoginPW
data.Lev = v.Lev
data.Score = v.Score
playerdata[i] = data
}
//2. Sort
sort.Sort(ByteEduList(playerdata))

// 三:返回数据给客户端
fmt.Fprintln(w, playerdata)
return
}
return
default:
fmt.Fprintln(w, "88902")
return
}
}
fmt.Fprintln(w, "88904")
return
}
fmt.Fprintln(w, "88901")
return
}
}
3 changes: 3 additions & 0 deletions ByteEdu.Com/Gophers/src/Proto_Go/Error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package Proto

// 错误码
66 changes: 66 additions & 0 deletions ByteEdu.Com/Gophers/src/Proto_Go/Proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package Proto

/*
主协议
*/
const (
ProtoINIT = iota
ProtoGopher // ProtoGopher == 1 地鼠的协议

)

// 排行的结构
type STRank struct {
Rank int
LoginName string
}

//------------------------------------------------------------------------------

/*
子协议
*/
const (
Proto2INIT = iota // 初始化
C2S_PlayerLoginProto2 // C2S_PlayerLoginProto2 == 1 用户登录协议
S2C_PlayerLoginProto2 // S2C_PlayerLoginProto2 == 2

C2S_GetRankProto2 // C2S_GetRankProto2 == 3 主动拉取排行
S2C_GetRankProto2 // S2C_GetRankProto2 == 4

)

//------------------------------------------------------------------------------
// 主动拉取排行
type C2S_GetRank struct {
Protocol int
Protocol2 int
LoginName string
Score int
Token string
}

type S2C_GetRank struct {
Protocol int
Protocol2 int
MapRank map[int]*STRank // 取前10名
}

//------------------------------------------------------------------------------
// 用户登录或者注册
type C2S_PlayerLogin struct {
Protocol int
Protocol2 int
Itype int // 1:表示注册,2:表示登录
LoginName string
LoginPW string
}

type S2C_PlayerLogin struct {
Protocol int
Protocol2 int
Token string
OpenID string
}

//------------------------------------------------------------------------------
36 changes: 36 additions & 0 deletions ByteEdu.Com/Go语言抽奖/src/binge/tmain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 初始化牌型
func InitDSQ(data1 []int) [4][4]int {

data, erdata, j, k := data1, [4][4]int{}, 0, 0

for i := 0; i < Proto2.Mouse*2; i++ {
icount := util.RandInterval_LollipopGo(0, int32(len(data))-1)
//fmt.Println("随机数:", icount)
if len(data) == 1 {
erdata[3][3] = data[0]
} else {
//------------------------------------------------------------------
if int(icount) < len(data) {
erdata[j][k] = data[icount]
k++
if k%4 == 0 {
j++
k = 0
}
data = append(data[:icount], data[icount+1:]...)
} else {
erdata[j][k] = data[icount]
k++
if k%4 == 0 {
j++
k = 0
}
data = data[:icount-1]
}
//------------------------------------------------------------------
}
//fmt.Println("生成的数据", erdata)
}

return erdata
}
4 changes: 4 additions & 0 deletions ByteEdu.Com/Go语言抽奖/src/draw/ByteEdu.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ByteEdu教育平台抽奖 结果
恭喜:Sixah,获得一等奖!
恭喜:右哼哼,获得一等奖!
恭喜:baobaoxiaoxuan,获得一等奖!恭喜:baobaoxiaoxuan,获得一等奖!
96 changes: 96 additions & 0 deletions ByteEdu.Com/Go语言抽奖/src/draw/Rand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"math/rand"
"time"
)

/*
作者:彬哥
来自:LollipopGo游戏服务器架构
*/

func init() {
rand.Seed(time.Now().UnixNano())
}

func RandGroup_LollipopGo(p ...uint32) int {
if p == nil {
panic("args not found")
}

r := make([]uint32, len(p))
for i := 0; i < len(p); i++ {
if i == 0 {
r[0] = p[0]
} else {
r[i] = r[i-1] + p[i]
}
}

rl := r[len(r)-1]
if rl == 0 {
return 0
}

rn := uint32(rand.Int63n(int64(rl)))
for i := 0; i < len(r); i++ {
if rn < r[i] {
return i
}
}

panic("bug")
}

func RandInterval_LollipopGo(b1, b2 int32) int32 {
if b1 == b2 {
return b1
}

min, max := int64(b1), int64(b2)
if min > max {
min, max = max, min
}
return int32(rand.Int63n(max-min+1) + min)
}

func RandIntervalN_LollipopGo(b1, b2 int32, n uint32) []int32 {
if b1 == b2 {
return []int32{b1}
}

min, max := int64(b1), int64(b2)
if min > max {
min, max = max, min
}
l := max - min + 1
if int64(n) > l {
n = uint32(l)
}

r := make([]int32, n)
m := make(map[int32]int32)
for i := uint32(0); i < n; i++ {
v := int32(rand.Int63n(l) + min)

if mv, ok := m[v]; ok {
r[i] = mv
} else {
r[i] = v
}

lv := int32(l - 1 + min)
if v != lv {
if mv, ok := m[lv]; ok {
m[v] = mv
} else {
m[v] = lv
}
}

l--
}

return r
}
Loading